ntdll: Fix the Mac build with SDKs older than 10.14.
[wine.git] / server / queue.c
blob96587d11d1e9b63107efecdd79e686465621cf51
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_kernel_obj_list, /* get_kernel_obj_list */
185 no_close_handle, /* close_handle */
186 msg_queue_destroy /* destroy */
189 static const struct fd_ops msg_queue_fd_ops =
191 NULL, /* get_poll_events */
192 msg_queue_poll_event, /* poll_event */
193 NULL, /* flush */
194 NULL, /* get_fd_type */
195 NULL, /* ioctl */
196 NULL, /* queue_async */
197 NULL, /* reselect_async */
198 NULL /* cancel async */
202 static const struct object_ops thread_input_ops =
204 sizeof(struct thread_input), /* size */
205 thread_input_dump, /* dump */
206 no_get_type, /* get_type */
207 no_add_queue, /* add_queue */
208 NULL, /* remove_queue */
209 NULL, /* signaled */
210 NULL, /* satisfied */
211 no_signal, /* signal */
212 no_get_fd, /* get_fd */
213 no_map_access, /* map_access */
214 default_get_sd, /* get_sd */
215 default_set_sd, /* set_sd */
216 no_lookup_name, /* lookup_name */
217 no_link_name, /* link_name */
218 NULL, /* unlink_name */
219 no_open_file, /* open_file */
220 no_kernel_obj_list, /* get_kernel_obj_list */
221 no_close_handle, /* close_handle */
222 thread_input_destroy /* destroy */
225 /* pointer to input structure of foreground thread */
226 static unsigned int last_input_time;
228 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
229 static void free_message( struct message *msg );
231 /* set the caret window in a given thread input */
232 static void set_caret_window( struct thread_input *input, user_handle_t win )
234 if (!win || win != input->caret)
236 input->caret_rect.left = 0;
237 input->caret_rect.top = 0;
238 input->caret_rect.right = 0;
239 input->caret_rect.bottom = 0;
241 input->caret = win;
242 input->caret_hide = 1;
243 input->caret_state = 0;
246 /* create a thread input object */
247 static struct thread_input *create_thread_input( struct thread *thread )
249 struct thread_input *input;
251 if ((input = alloc_object( &thread_input_ops )))
253 input->focus = 0;
254 input->capture = 0;
255 input->active = 0;
256 input->menu_owner = 0;
257 input->move_size = 0;
258 input->cursor = 0;
259 input->cursor_count = 0;
260 list_init( &input->msg_list );
261 set_caret_window( input, 0 );
262 memset( input->keystate, 0, sizeof(input->keystate) );
264 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
266 release_object( input );
267 return NULL;
270 return input;
273 /* create a message queue object */
274 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
276 struct thread_input *new_input = NULL;
277 struct msg_queue *queue;
278 int i;
280 if (!input)
282 if (!(new_input = create_thread_input( thread ))) return NULL;
283 input = new_input;
286 if ((queue = alloc_object( &msg_queue_ops )))
288 queue->fd = NULL;
289 queue->wake_bits = 0;
290 queue->wake_mask = 0;
291 queue->changed_bits = 0;
292 queue->changed_mask = 0;
293 queue->paint_count = 0;
294 queue->hotkey_count = 0;
295 queue->quit_message = 0;
296 queue->cursor_count = 0;
297 queue->recv_result = NULL;
298 queue->next_timer_id = 0x7fff;
299 queue->timeout = NULL;
300 queue->input = (struct thread_input *)grab_object( input );
301 queue->hooks = NULL;
302 queue->last_get_msg = current_time;
303 list_init( &queue->send_result );
304 list_init( &queue->callback_result );
305 list_init( &queue->pending_timers );
306 list_init( &queue->expired_timers );
307 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
309 thread->queue = queue;
311 if (new_input) release_object( new_input );
312 return queue;
315 /* free the message queue of a thread at thread exit */
316 void free_msg_queue( struct thread *thread )
318 remove_thread_hooks( thread );
319 if (!thread->queue) return;
320 release_object( thread->queue );
321 thread->queue = NULL;
324 /* change the thread input data of a given thread */
325 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
327 struct msg_queue *queue = thread->queue;
329 if (!queue)
331 thread->queue = create_msg_queue( thread, new_input );
332 return thread->queue != NULL;
334 if (queue->input)
336 queue->input->cursor_count -= queue->cursor_count;
337 release_object( queue->input );
339 queue->input = (struct thread_input *)grab_object( new_input );
340 new_input->cursor_count += queue->cursor_count;
341 return 1;
344 /* allocate a hardware message and its data */
345 static struct message *alloc_hardware_message( lparam_t info, struct hw_msg_source source,
346 unsigned int time )
348 struct hardware_msg_data *msg_data;
349 struct message *msg;
351 if (!(msg = mem_alloc( sizeof(*msg) ))) return NULL;
352 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
354 free( msg );
355 return NULL;
357 memset( msg, 0, sizeof(*msg) );
358 msg->type = MSG_HARDWARE;
359 msg->time = time;
360 msg->data = msg_data;
361 msg->data_size = sizeof(*msg_data);
363 memset( msg_data, 0, sizeof(*msg_data) );
364 msg_data->info = info;
365 msg_data->source = source;
366 return msg;
369 /* set the cursor position and queue the corresponding mouse message */
370 static void set_cursor_pos( struct desktop *desktop, int x, int y )
372 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
373 struct message *msg;
375 if (!(msg = alloc_hardware_message( 0, source, get_tick_count() ))) return;
377 msg->msg = WM_MOUSEMOVE;
378 msg->x = x;
379 msg->y = y;
380 queue_hardware_message( desktop, msg, 1 );
383 /* retrieve default position and time for synthesized messages */
384 static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
386 struct desktop *desktop = queue->input->desktop;
388 *x = desktop->cursor.x;
389 *y = desktop->cursor.y;
390 *time = get_tick_count();
393 /* set the cursor clip rectangle */
394 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect, int send_clip_msg )
396 rectangle_t top_rect;
397 int x, y;
399 get_top_window_rectangle( desktop, &top_rect );
400 if (rect)
402 rectangle_t new_rect = *rect;
403 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
404 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
405 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
406 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
407 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
408 desktop->cursor.clip = new_rect;
410 else desktop->cursor.clip = top_rect;
412 if (desktop->cursor.clip_msg && send_clip_msg)
413 post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
415 /* warp the mouse to be inside the clip rect */
416 x = max( min( desktop->cursor.x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
417 y = max( min( desktop->cursor.y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
418 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
421 /* change the foreground input and reset the cursor clip rect */
422 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
424 if (desktop->foreground_input == input) return;
425 set_clip_rectangle( desktop, NULL, 1 );
426 desktop->foreground_input = input;
429 /* get the hook table for a given thread */
430 struct hook_table *get_queue_hooks( struct thread *thread )
432 if (!thread->queue) return NULL;
433 return thread->queue->hooks;
436 /* set the hook table for a given thread, allocating the queue if needed */
437 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
439 struct msg_queue *queue = thread->queue;
440 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
441 if (queue->hooks) release_object( queue->hooks );
442 queue->hooks = hooks;
445 /* check the queue status */
446 static inline int is_signaled( struct msg_queue *queue )
448 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
451 /* set some queue bits */
452 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
454 queue->wake_bits |= bits;
455 queue->changed_bits |= bits;
456 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
459 /* clear some queue bits */
460 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
462 queue->wake_bits &= ~bits;
463 queue->changed_bits &= ~bits;
466 /* check whether msg is a keyboard message */
467 static inline int is_keyboard_msg( struct message *msg )
469 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
472 /* check if message is matched by the filter */
473 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
475 return (msg >= first && msg <= last);
478 /* check whether a message filter contains at least one potential hardware message */
479 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
481 /* hardware message ranges are (in numerical order):
482 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
483 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
484 * WM_MOUSEFIRST .. WM_MOUSELAST
486 if (last < WM_NCMOUSEFIRST) return 0;
487 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
488 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
489 if (first > WM_MOUSELAST) return 0;
490 return 1;
493 /* get the QS_* bit corresponding to a given hardware message */
494 static inline int get_hardware_msg_bit( struct message *msg )
496 if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
497 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
498 if (is_keyboard_msg( msg )) return QS_KEY;
499 return QS_MOUSEBUTTON;
502 /* get the current thread queue, creating it if needed */
503 static inline struct msg_queue *get_current_queue(void)
505 struct msg_queue *queue = current->queue;
506 if (!queue) queue = create_msg_queue( current, NULL );
507 return queue;
510 /* get a (pseudo-)unique id to tag hardware messages */
511 static inline unsigned int get_unique_id(void)
513 static unsigned int id;
514 if (!++id) id = 1; /* avoid an id of 0 */
515 return id;
518 /* try to merge a message with the last in the list; return 1 if successful */
519 static int merge_message( struct thread_input *input, const struct message *msg )
521 struct message *prev;
522 struct list *ptr;
524 if (msg->msg != WM_MOUSEMOVE) return 0;
525 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
527 prev = LIST_ENTRY( ptr, struct message, entry );
528 if (prev->msg != WM_INPUT) break;
530 if (!ptr) return 0;
531 if (prev->result) return 0;
532 if (prev->win && msg->win && prev->win != msg->win) return 0;
533 if (prev->msg != msg->msg) return 0;
534 if (prev->type != msg->type) return 0;
535 /* now we can merge it */
536 prev->wparam = msg->wparam;
537 prev->lparam = msg->lparam;
538 prev->x = msg->x;
539 prev->y = msg->y;
540 prev->time = msg->time;
541 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
543 struct hardware_msg_data *prev_data = prev->data;
544 struct hardware_msg_data *msg_data = msg->data;
545 prev_data->info = msg_data->info;
547 list_remove( ptr );
548 list_add_tail( &input->msg_list, ptr );
549 return 1;
552 /* free a result structure */
553 static void free_result( struct message_result *result )
555 if (result->timeout) remove_timeout_user( result->timeout );
556 free( result->data );
557 if (result->callback_msg) free_message( result->callback_msg );
558 if (result->hardware_msg) free_message( result->hardware_msg );
559 if (result->desktop) release_object( result->desktop );
560 free( result );
563 /* remove the result from the sender list it is on */
564 static inline void remove_result_from_sender( struct message_result *result )
566 assert( result->sender );
568 list_remove( &result->sender_entry );
569 result->sender = NULL;
570 if (!result->receiver) free_result( result );
573 /* store the message result in the appropriate structure */
574 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
576 res->result = result;
577 res->error = error;
578 res->replied = 1;
579 if (res->timeout)
581 remove_timeout_user( res->timeout );
582 res->timeout = NULL;
585 if (res->hardware_msg)
587 if (!error && result) /* rejected by the hook */
588 free_message( res->hardware_msg );
589 else
590 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
592 res->hardware_msg = NULL;
595 if (res->sender)
597 if (res->callback_msg)
599 /* queue the callback message in the sender queue */
600 struct callback_msg_data *data = res->callback_msg->data;
601 data->result = result;
602 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
603 set_queue_bits( res->sender, QS_SENDMESSAGE );
604 res->callback_msg = NULL;
605 remove_result_from_sender( res );
607 else
609 /* wake sender queue if waiting on this result */
610 if (list_head(&res->sender->send_result) == &res->sender_entry)
611 set_queue_bits( res->sender, QS_SMRESULT );
614 else if (!res->receiver) free_result( res );
617 /* free a message when deleting a queue or window */
618 static void free_message( struct message *msg )
620 struct message_result *result = msg->result;
621 if (result)
623 result->msg = NULL;
624 result->receiver = NULL;
625 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
627 free( msg->data );
628 free( msg );
631 /* remove (and free) a message from a message list */
632 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
633 enum message_kind kind )
635 list_remove( &msg->entry );
636 switch(kind)
638 case SEND_MESSAGE:
639 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
640 break;
641 case POST_MESSAGE:
642 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
643 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
644 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
645 clear_queue_bits( queue, QS_HOTKEY );
646 break;
648 free_message( msg );
651 /* message timed out without getting a reply */
652 static void result_timeout( void *private )
654 struct message_result *result = private;
656 assert( !result->replied );
658 result->timeout = NULL;
660 if (result->msg) /* not received yet */
662 struct message *msg = result->msg;
664 result->msg = NULL;
665 msg->result = NULL;
666 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
667 result->receiver = NULL;
669 store_message_result( result, 0, STATUS_TIMEOUT );
672 /* allocate and fill a message result structure */
673 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
674 struct msg_queue *recv_queue,
675 struct message *msg, timeout_t timeout )
677 struct message_result *result = mem_alloc( sizeof(*result) );
678 if (result)
680 result->msg = msg;
681 result->sender = send_queue;
682 result->receiver = recv_queue;
683 result->replied = 0;
684 result->data = NULL;
685 result->data_size = 0;
686 result->timeout = NULL;
687 result->hardware_msg = NULL;
688 result->desktop = NULL;
689 result->callback_msg = NULL;
691 if (msg->type == MSG_CALLBACK)
693 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
695 if (!callback_msg)
697 free( result );
698 return NULL;
700 callback_msg->type = MSG_CALLBACK_RESULT;
701 callback_msg->win = msg->win;
702 callback_msg->msg = msg->msg;
703 callback_msg->wparam = 0;
704 callback_msg->lparam = 0;
705 callback_msg->time = get_tick_count();
706 callback_msg->result = NULL;
707 /* steal the data from the original message */
708 callback_msg->data = msg->data;
709 callback_msg->data_size = msg->data_size;
710 msg->data = NULL;
711 msg->data_size = 0;
713 result->callback_msg = callback_msg;
714 list_add_head( &send_queue->callback_result, &result->sender_entry );
716 else if (send_queue)
718 list_add_head( &send_queue->send_result, &result->sender_entry );
719 clear_queue_bits( send_queue, QS_SMRESULT );
722 if (timeout != TIMEOUT_INFINITE)
723 result->timeout = add_timeout_user( timeout, result_timeout, result );
725 return result;
728 /* receive a message, removing it from the sent queue */
729 static void receive_message( struct msg_queue *queue, struct message *msg,
730 struct get_message_reply *reply )
732 struct message_result *result = msg->result;
734 reply->total = msg->data_size;
735 if (msg->data_size > get_reply_max_size())
737 set_error( STATUS_BUFFER_OVERFLOW );
738 return;
740 reply->type = msg->type;
741 reply->win = msg->win;
742 reply->msg = msg->msg;
743 reply->wparam = msg->wparam;
744 reply->lparam = msg->lparam;
745 reply->x = msg->x;
746 reply->y = msg->y;
747 reply->time = msg->time;
749 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
751 list_remove( &msg->entry );
752 /* put the result on the receiver result stack */
753 if (result)
755 result->msg = NULL;
756 result->recv_next = queue->recv_result;
757 queue->recv_result = result;
759 free( msg );
760 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
763 /* set the result of the current received message */
764 static void reply_message( struct msg_queue *queue, lparam_t result,
765 unsigned int error, int remove, const void *data, data_size_t len )
767 struct message_result *res = queue->recv_result;
769 if (remove)
771 queue->recv_result = res->recv_next;
772 res->receiver = NULL;
773 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
775 free_result( res );
776 return;
779 if (!res->replied)
781 if (len && (res->data = memdup( data, len ))) res->data_size = len;
782 store_message_result( res, result, error );
786 static int match_window( user_handle_t win, user_handle_t msg_win )
788 if (!win) return 1;
789 if (win == -1 || win == 1) return !msg_win;
790 if (msg_win == win) return 1;
791 return is_child_window( win, msg_win );
794 /* retrieve a posted message */
795 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
796 unsigned int first, unsigned int last, unsigned int flags,
797 struct get_message_reply *reply )
799 struct message *msg;
801 /* check against the filters */
802 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
804 if (!match_window( win, msg->win )) continue;
805 if (!check_msg_filter( msg->msg, first, last )) continue;
806 goto found; /* found one */
808 return 0;
810 /* return it to the app */
811 found:
812 reply->total = msg->data_size;
813 if (msg->data_size > get_reply_max_size())
815 set_error( STATUS_BUFFER_OVERFLOW );
816 return 1;
818 reply->type = msg->type;
819 reply->win = msg->win;
820 reply->msg = msg->msg;
821 reply->wparam = msg->wparam;
822 reply->lparam = msg->lparam;
823 reply->x = msg->x;
824 reply->y = msg->y;
825 reply->time = msg->time;
827 if (flags & PM_REMOVE)
829 if (msg->data)
831 set_reply_data_ptr( msg->data, msg->data_size );
832 msg->data = NULL;
833 msg->data_size = 0;
835 remove_queue_message( queue, msg, POST_MESSAGE );
837 else if (msg->data) set_reply_data( msg->data, msg->data_size );
839 return 1;
842 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
843 struct get_message_reply *reply )
845 if (queue->quit_message)
847 reply->total = 0;
848 reply->type = MSG_POSTED;
849 reply->win = 0;
850 reply->msg = WM_QUIT;
851 reply->wparam = queue->exit_code;
852 reply->lparam = 0;
854 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
856 if (flags & PM_REMOVE)
858 queue->quit_message = 0;
859 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
860 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
862 return 1;
864 else
865 return 0;
868 /* empty a message list and free all the messages */
869 static void empty_msg_list( struct list *list )
871 struct list *ptr;
873 while ((ptr = list_head( list )) != NULL)
875 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
876 list_remove( &msg->entry );
877 free_message( msg );
881 /* cleanup all pending results when deleting a queue */
882 static void cleanup_results( struct msg_queue *queue )
884 struct list *entry;
886 while ((entry = list_head( &queue->send_result )) != NULL)
888 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
891 while ((entry = list_head( &queue->callback_result )) != NULL)
893 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
896 while (queue->recv_result)
897 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
900 /* check if the thread owning the queue is hung (not checking for messages) */
901 static int is_queue_hung( struct msg_queue *queue )
903 struct wait_queue_entry *entry;
905 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
906 return 0; /* less than 5 seconds since last get message -> not hung */
908 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
910 if (get_wait_queue_thread(entry)->queue == queue)
911 return 0; /* thread is waiting on queue -> not hung */
913 return 1;
916 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
918 struct msg_queue *queue = (struct msg_queue *)obj;
919 struct process *process = get_wait_queue_thread(entry)->process;
921 /* a thread can only wait on its own queue */
922 if (get_wait_queue_thread(entry)->queue != queue)
924 set_error( STATUS_ACCESS_DENIED );
925 return 0;
927 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
929 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
930 set_fd_events( queue->fd, POLLIN );
931 add_queue( obj, entry );
932 return 1;
935 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
937 struct msg_queue *queue = (struct msg_queue *)obj;
939 remove_queue( obj, entry );
940 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
941 set_fd_events( queue->fd, 0 );
944 static void msg_queue_dump( struct object *obj, int verbose )
946 struct msg_queue *queue = (struct msg_queue *)obj;
947 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
948 queue->wake_bits, queue->wake_mask );
951 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
953 struct msg_queue *queue = (struct msg_queue *)obj;
954 int ret = 0;
956 if (queue->fd)
958 if ((ret = check_fd_events( queue->fd, POLLIN )))
959 /* stop waiting on select() if we are signaled */
960 set_fd_events( queue->fd, 0 );
961 else if (!list_empty( &obj->wait_queue ))
962 /* restart waiting on poll() if we are no longer signaled */
963 set_fd_events( queue->fd, POLLIN );
965 return ret || is_signaled( queue );
968 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
970 struct msg_queue *queue = (struct msg_queue *)obj;
971 queue->wake_mask = 0;
972 queue->changed_mask = 0;
975 static void msg_queue_destroy( struct object *obj )
977 struct msg_queue *queue = (struct msg_queue *)obj;
978 struct list *ptr;
979 struct hotkey *hotkey, *hotkey2;
980 int i;
982 cleanup_results( queue );
983 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
985 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
987 if (hotkey->queue == queue)
989 list_remove( &hotkey->entry );
990 free( hotkey );
994 while ((ptr = list_head( &queue->pending_timers )))
996 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
997 list_remove( &timer->entry );
998 free( timer );
1000 while ((ptr = list_head( &queue->expired_timers )))
1002 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1003 list_remove( &timer->entry );
1004 free( timer );
1006 if (queue->timeout) remove_timeout_user( queue->timeout );
1007 queue->input->cursor_count -= queue->cursor_count;
1008 release_object( queue->input );
1009 if (queue->hooks) release_object( queue->hooks );
1010 if (queue->fd) release_object( queue->fd );
1013 static void msg_queue_poll_event( struct fd *fd, int event )
1015 struct msg_queue *queue = get_fd_user( fd );
1016 assert( queue->obj.ops == &msg_queue_ops );
1018 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1019 else set_fd_events( queue->fd, 0 );
1020 wake_up( &queue->obj, 0 );
1023 static void thread_input_dump( struct object *obj, int verbose )
1025 struct thread_input *input = (struct thread_input *)obj;
1026 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
1027 input->focus, input->capture, input->active );
1030 static void thread_input_destroy( struct object *obj )
1032 struct thread_input *input = (struct thread_input *)obj;
1034 empty_msg_list( &input->msg_list );
1035 if (input->desktop)
1037 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
1038 release_object( input->desktop );
1042 /* fix the thread input data when a window is destroyed */
1043 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1045 struct thread_input *input = queue->input;
1047 if (window == input->focus) input->focus = 0;
1048 if (window == input->capture) input->capture = 0;
1049 if (window == input->active) input->active = 0;
1050 if (window == input->menu_owner) input->menu_owner = 0;
1051 if (window == input->move_size) input->move_size = 0;
1052 if (window == input->caret) set_caret_window( input, 0 );
1055 /* check if the specified window can be set in the input data of a given queue */
1056 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1058 struct thread *thread;
1059 int ret = 0;
1061 if (!window) return 1; /* we can always clear the data */
1063 if ((thread = get_window_thread( window )))
1065 ret = (queue->input == thread->queue->input);
1066 if (!ret) set_error( STATUS_ACCESS_DENIED );
1067 release_object( thread );
1069 else set_error( STATUS_INVALID_HANDLE );
1071 return ret;
1074 /* make sure the specified thread has a queue */
1075 int init_thread_queue( struct thread *thread )
1077 if (thread->queue) return 1;
1078 return (create_msg_queue( thread, NULL ) != NULL);
1081 /* attach two thread input data structures */
1082 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1084 struct desktop *desktop;
1085 struct thread_input *input;
1086 int ret;
1088 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1089 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1090 input = (struct thread_input *)grab_object( thread_to->queue->input );
1091 if (input->desktop != desktop)
1093 set_error( STATUS_ACCESS_DENIED );
1094 release_object( input );
1095 release_object( desktop );
1096 return 0;
1098 release_object( desktop );
1100 if (thread_from->queue)
1102 if (!input->focus) input->focus = thread_from->queue->input->focus;
1103 if (!input->active) input->active = thread_from->queue->input->active;
1106 ret = assign_thread_input( thread_from, input );
1107 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1108 release_object( input );
1109 return ret;
1112 /* detach two thread input data structures */
1113 void detach_thread_input( struct thread *thread_from )
1115 struct thread *thread;
1116 struct thread_input *input, *old_input = thread_from->queue->input;
1118 if ((input = create_thread_input( thread_from )))
1120 if (old_input->focus && (thread = get_window_thread( old_input->focus )))
1122 if (thread == thread_from)
1124 input->focus = old_input->focus;
1125 old_input->focus = 0;
1127 release_object( thread );
1129 if (old_input->active && (thread = get_window_thread( old_input->active )))
1131 if (thread == thread_from)
1133 input->active = old_input->active;
1134 old_input->active = 0;
1136 release_object( thread );
1138 assign_thread_input( thread_from, input );
1139 release_object( input );
1144 /* set the next timer to expire */
1145 static void set_next_timer( struct msg_queue *queue )
1147 struct list *ptr;
1149 if (queue->timeout)
1151 remove_timeout_user( queue->timeout );
1152 queue->timeout = NULL;
1154 if ((ptr = list_head( &queue->pending_timers )))
1156 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1157 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1159 /* set/clear QS_TIMER bit */
1160 if (list_empty( &queue->expired_timers ))
1161 clear_queue_bits( queue, QS_TIMER );
1162 else
1163 set_queue_bits( queue, QS_TIMER );
1166 /* find a timer from its window and id */
1167 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1168 unsigned int msg, lparam_t id )
1170 struct list *ptr;
1172 /* we need to search both lists */
1174 LIST_FOR_EACH( ptr, &queue->pending_timers )
1176 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1177 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1179 LIST_FOR_EACH( ptr, &queue->expired_timers )
1181 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1182 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1184 return NULL;
1187 /* callback for the next timer expiration */
1188 static void timer_callback( void *private )
1190 struct msg_queue *queue = private;
1191 struct list *ptr;
1193 queue->timeout = NULL;
1194 /* move on to the next timer */
1195 ptr = list_head( &queue->pending_timers );
1196 list_remove( ptr );
1197 list_add_tail( &queue->expired_timers, ptr );
1198 set_next_timer( queue );
1201 /* link a timer at its rightful place in the queue list */
1202 static void link_timer( struct msg_queue *queue, struct timer *timer )
1204 struct list *ptr;
1206 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1208 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1209 if (t->when >= timer->when) break;
1211 list_add_before( ptr, &timer->entry );
1214 /* remove a timer from the queue timer list and free it */
1215 static void free_timer( struct msg_queue *queue, struct timer *timer )
1217 list_remove( &timer->entry );
1218 free( timer );
1219 set_next_timer( queue );
1222 /* restart an expired timer */
1223 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1225 list_remove( &timer->entry );
1226 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1227 link_timer( queue, timer );
1228 set_next_timer( queue );
1231 /* find an expired timer matching the filtering parameters */
1232 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1233 unsigned int get_first, unsigned int get_last,
1234 int remove )
1236 struct list *ptr;
1238 LIST_FOR_EACH( ptr, &queue->expired_timers )
1240 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1241 if (win && timer->win != win) continue;
1242 if (check_msg_filter( timer->msg, get_first, get_last ))
1244 if (remove) restart_timer( queue, timer );
1245 return timer;
1248 return NULL;
1251 /* add a timer */
1252 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1254 struct timer *timer = mem_alloc( sizeof(*timer) );
1255 if (timer)
1257 timer->rate = max( rate, 1 );
1258 timer->when = current_time + (timeout_t)timer->rate * 10000;
1259 link_timer( queue, timer );
1260 /* check if we replaced the next timer */
1261 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1263 return timer;
1266 /* change the input key state for a given key */
1267 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1269 if (down)
1271 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1272 keystate[key] |= down;
1274 else keystate[key] &= ~0x80;
1277 /* update the input key state for a keyboard message */
1278 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1279 const struct message *msg )
1281 unsigned char key;
1282 int down = 0;
1284 switch (msg->msg)
1286 case WM_LBUTTONDOWN:
1287 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1288 /* fall through */
1289 case WM_LBUTTONUP:
1290 set_input_key_state( keystate, VK_LBUTTON, down );
1291 break;
1292 case WM_MBUTTONDOWN:
1293 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1294 /* fall through */
1295 case WM_MBUTTONUP:
1296 set_input_key_state( keystate, VK_MBUTTON, down );
1297 break;
1298 case WM_RBUTTONDOWN:
1299 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1300 /* fall through */
1301 case WM_RBUTTONUP:
1302 set_input_key_state( keystate, VK_RBUTTON, down );
1303 break;
1304 case WM_XBUTTONDOWN:
1305 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1306 /* fall through */
1307 case WM_XBUTTONUP:
1308 if (msg->wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1309 else if (msg->wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1310 break;
1311 case WM_KEYDOWN:
1312 case WM_SYSKEYDOWN:
1313 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1314 /* fall through */
1315 case WM_KEYUP:
1316 case WM_SYSKEYUP:
1317 key = (unsigned char)msg->wparam;
1318 set_input_key_state( keystate, key, down );
1319 switch(key)
1321 case VK_LCONTROL:
1322 case VK_RCONTROL:
1323 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1324 set_input_key_state( keystate, VK_CONTROL, down );
1325 break;
1326 case VK_LMENU:
1327 case VK_RMENU:
1328 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1329 set_input_key_state( keystate, VK_MENU, down );
1330 break;
1331 case VK_LSHIFT:
1332 case VK_RSHIFT:
1333 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1334 set_input_key_state( keystate, VK_SHIFT, down );
1335 break;
1337 break;
1341 /* release the hardware message currently being processed by the given thread */
1342 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1343 int remove )
1345 struct thread_input *input = queue->input;
1346 struct message *msg;
1348 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1350 if (msg->unique_id == hw_id) break;
1352 if (&msg->entry == &input->msg_list) return; /* not found */
1354 /* clear the queue bit for that message */
1355 if (remove)
1357 struct message *other;
1358 int clr_bit;
1360 clr_bit = get_hardware_msg_bit( msg );
1361 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1363 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1365 clr_bit = 0;
1366 break;
1369 if (clr_bit) clear_queue_bits( queue, clr_bit );
1371 update_input_key_state( input->desktop, input->keystate, msg );
1372 list_remove( &msg->entry );
1373 free_message( msg );
1377 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1379 struct hotkey *hotkey;
1380 unsigned int modifiers = 0;
1382 if (msg->msg != WM_KEYDOWN) return 0;
1384 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1385 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1386 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1387 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1389 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1391 if (hotkey->vkey != msg->wparam) continue;
1392 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1395 return 0;
1397 found:
1398 msg->type = MSG_POSTED;
1399 msg->win = hotkey->win;
1400 msg->msg = WM_HOTKEY;
1401 msg->wparam = hotkey->id;
1402 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1404 free( msg->data );
1405 msg->data = NULL;
1406 msg->data_size = 0;
1408 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1409 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1410 hotkey->queue->hotkey_count++;
1411 return 1;
1414 /* find the window that should receive a given hardware message */
1415 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1416 struct message *msg, unsigned int *msg_code,
1417 struct thread **thread )
1419 user_handle_t win = 0;
1421 *thread = NULL;
1422 *msg_code = msg->msg;
1423 if (msg->msg == WM_INPUT)
1425 if (!(win = msg->win) && input) win = input->focus;
1427 else if (is_keyboard_msg( msg ))
1429 if (input && !(win = input->focus))
1431 win = input->active;
1432 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1435 else if (!input || !(win = input->capture)) /* mouse message */
1437 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1438 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1440 *thread = window_thread_from_point( win, msg->x, msg->y );
1443 if (!*thread)
1444 *thread = get_window_thread( win );
1445 return win;
1448 static struct rawinput_device_entry *find_rawinput_device( unsigned short usage_page, unsigned short usage )
1450 struct rawinput_device_entry *e;
1452 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
1454 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1455 return e;
1458 return NULL;
1461 static void update_rawinput_device(const struct rawinput_device *device)
1463 struct rawinput_device_entry *e;
1465 if (!(e = find_rawinput_device( device->usage_page, device->usage )))
1467 if (!(e = mem_alloc( sizeof(*e) ))) return;
1468 list_add_tail( &current->process->rawinput_devices, &e->entry );
1471 if (device->flags & RIDEV_REMOVE)
1473 list_remove( &e->entry );
1474 free( e );
1475 return;
1478 e->device = *device;
1479 e->device.target = get_user_full_handle( e->device.target );
1482 /* queue a hardware message into a given thread input */
1483 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1485 user_handle_t win;
1486 struct thread *thread;
1487 struct thread_input *input;
1488 unsigned int msg_code;
1490 update_input_key_state( desktop, desktop->keystate, msg );
1491 last_input_time = get_tick_count();
1492 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1494 if (is_keyboard_msg( msg ))
1496 if (queue_hotkey_message( desktop, msg )) return;
1497 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1498 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1499 msg->lparam &= ~(KF_EXTENDED << 16);
1501 else if (msg->msg != WM_INPUT)
1503 if (msg->msg == WM_MOUSEMOVE)
1505 int x = max( min( msg->x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
1506 int y = max( min( msg->y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
1507 if (desktop->cursor.x != x || desktop->cursor.y != y) always_queue = 1;
1508 desktop->cursor.x = x;
1509 desktop->cursor.y = y;
1510 desktop->cursor.last_change = get_tick_count();
1512 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1513 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1514 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1515 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1516 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1517 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1518 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1520 msg->x = desktop->cursor.x;
1521 msg->y = desktop->cursor.y;
1523 if (msg->win && (thread = get_window_thread( msg->win )))
1525 input = thread->queue->input;
1526 release_object( thread );
1528 else input = desktop->foreground_input;
1530 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1531 if (!win || !thread)
1533 if (input) update_input_key_state( input->desktop, input->keystate, msg );
1534 free_message( msg );
1535 return;
1537 input = thread->queue->input;
1539 if (win != desktop->cursor.win) always_queue = 1;
1540 desktop->cursor.win = win;
1542 if (!always_queue || merge_message( input, msg )) free_message( msg );
1543 else
1545 msg->unique_id = 0; /* will be set once we return it to the app */
1546 list_add_tail( &input->msg_list, &msg->entry );
1547 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1549 release_object( thread );
1552 /* send the low-level hook message for a given hardware message */
1553 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1554 const hw_input_t *input, struct msg_queue *sender )
1556 struct thread *hook_thread;
1557 struct msg_queue *queue;
1558 struct message *msg;
1559 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1560 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1562 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1563 if (!(queue = hook_thread->queue)) return 0;
1564 if (is_queue_hung( queue )) return 0;
1566 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1568 msg->type = MSG_HOOK_LL;
1569 msg->win = 0;
1570 msg->msg = id;
1571 msg->wparam = hardware_msg->msg;
1572 msg->x = hardware_msg->x;
1573 msg->y = hardware_msg->y;
1574 msg->time = hardware_msg->time;
1575 msg->data_size = hardware_msg->data_size;
1576 msg->result = NULL;
1578 if (input->type == INPUT_KEYBOARD)
1580 unsigned short vkey = input->kbd.vkey;
1581 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1582 msg->lparam = (input->kbd.scan << 16) | vkey;
1584 else msg->lparam = input->mouse.data << 16;
1586 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1587 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1589 free_message( msg );
1590 return 0;
1592 msg->result->hardware_msg = hardware_msg;
1593 msg->result->desktop = (struct desktop *)grab_object( desktop );
1594 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1595 set_queue_bits( queue, QS_SENDMESSAGE );
1596 return 1;
1599 /* queue a hardware message for a mouse event */
1600 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1601 unsigned int origin, struct msg_queue *sender )
1603 const struct rawinput_device *device;
1604 struct hardware_msg_data *msg_data;
1605 struct message *msg;
1606 unsigned int i, time, flags;
1607 struct hw_msg_source source = { IMDT_MOUSE, origin };
1608 int wait = 0, x, y;
1610 static const unsigned int messages[] =
1612 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1613 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1614 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1615 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1616 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1617 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1618 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1619 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1620 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1621 0, /* 0x0200 = unused */
1622 0, /* 0x0400 = unused */
1623 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1624 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1627 desktop->cursor.last_change = get_tick_count();
1628 flags = input->mouse.flags;
1629 time = input->mouse.time;
1630 if (!time) time = desktop->cursor.last_change;
1632 if (flags & MOUSEEVENTF_MOVE)
1634 if (flags & MOUSEEVENTF_ABSOLUTE)
1636 x = input->mouse.x;
1637 y = input->mouse.y;
1638 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1639 x == desktop->cursor.x && y == desktop->cursor.y)
1640 flags &= ~MOUSEEVENTF_MOVE;
1642 else
1644 x = desktop->cursor.x + input->mouse.x;
1645 y = desktop->cursor.y + input->mouse.y;
1648 else
1650 x = desktop->cursor.x;
1651 y = desktop->cursor.y;
1654 if ((device = current->process->rawinput_mouse))
1656 if (!(msg = alloc_hardware_message( input->mouse.info, source, time ))) return 0;
1657 msg_data = msg->data;
1659 msg->win = device->target;
1660 msg->msg = WM_INPUT;
1661 msg->wparam = RIM_INPUT;
1662 msg->lparam = 0;
1664 msg_data->flags = flags;
1665 msg_data->rawinput.type = RIM_TYPEMOUSE;
1666 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1667 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1668 msg_data->rawinput.mouse.data = input->mouse.data;
1670 queue_hardware_message( desktop, msg, 0 );
1673 for (i = 0; i < ARRAY_SIZE( messages ); i++)
1675 if (!messages[i]) continue;
1676 if (!(flags & (1 << i))) continue;
1677 flags &= ~(1 << i);
1679 if (!(msg = alloc_hardware_message( input->mouse.info, source, time ))) return 0;
1680 msg_data = msg->data;
1682 msg->win = get_user_full_handle( win );
1683 msg->msg = messages[i];
1684 msg->wparam = input->mouse.data << 16;
1685 msg->lparam = 0;
1686 msg->x = x;
1687 msg->y = y;
1688 if (origin == IMO_INJECTED) msg_data->flags = LLMHF_INJECTED;
1690 /* specify a sender only when sending the last message */
1691 if (!(flags & ((1 << ARRAY_SIZE( messages )) - 1)))
1693 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1694 queue_hardware_message( desktop, msg, 0 );
1696 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1697 queue_hardware_message( desktop, msg, 0 );
1699 return wait;
1702 /* queue a hardware message for a keyboard event */
1703 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1704 unsigned int origin, struct msg_queue *sender )
1706 struct hw_msg_source source = { IMDT_KEYBOARD, origin };
1707 const struct rawinput_device *device;
1708 struct hardware_msg_data *msg_data;
1709 struct message *msg;
1710 unsigned char vkey = input->kbd.vkey;
1711 unsigned int message_code, time;
1712 int wait;
1714 if (!(time = input->kbd.time)) time = get_tick_count();
1716 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1718 switch (vkey)
1720 case VK_MENU:
1721 case VK_LMENU:
1722 case VK_RMENU:
1723 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1724 break;
1725 case VK_CONTROL:
1726 case VK_LCONTROL:
1727 case VK_RCONTROL:
1728 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1729 break;
1730 case VK_SHIFT:
1731 case VK_LSHIFT:
1732 case VK_RSHIFT:
1733 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1734 break;
1738 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1739 switch (vkey)
1741 case VK_LMENU:
1742 case VK_RMENU:
1743 if (input->kbd.flags & KEYEVENTF_KEYUP)
1745 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1746 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1747 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1748 message_code = WM_SYSKEYUP;
1749 desktop->keystate[VK_MENU] &= ~0x02;
1751 else
1753 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1754 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1755 message_code = WM_SYSKEYDOWN;
1756 desktop->keystate[VK_MENU] |= 0x02;
1758 break;
1760 case VK_LCONTROL:
1761 case VK_RCONTROL:
1762 /* send WM_SYSKEYUP on release if Alt still pressed */
1763 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1764 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1765 message_code = WM_SYSKEYUP;
1766 desktop->keystate[VK_MENU] &= ~0x02;
1767 break;
1769 default:
1770 /* send WM_SYSKEY for Alt-anykey and for F10 */
1771 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1772 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1773 /* fall through */
1774 case VK_F10:
1775 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1776 desktop->keystate[VK_MENU] &= ~0x02;
1777 break;
1780 if ((device = current->process->rawinput_kbd))
1782 if (!(msg = alloc_hardware_message( input->kbd.info, source, time ))) return 0;
1783 msg_data = msg->data;
1785 msg->win = device->target;
1786 msg->msg = WM_INPUT;
1787 msg->wparam = RIM_INPUT;
1789 msg_data->flags = input->kbd.flags;
1790 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1791 msg_data->rawinput.kbd.message = message_code;
1792 msg_data->rawinput.kbd.vkey = vkey;
1793 msg_data->rawinput.kbd.scan = input->kbd.scan;
1795 queue_hardware_message( desktop, msg, 0 );
1798 if (!(msg = alloc_hardware_message( input->kbd.info, source, time ))) return 0;
1799 msg_data = msg->data;
1801 msg->win = get_user_full_handle( win );
1802 msg->msg = message_code;
1803 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1804 if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
1806 if (input->kbd.flags & KEYEVENTF_UNICODE && !vkey)
1808 msg->wparam = VK_PACKET;
1810 else
1812 unsigned int flags = 0;
1813 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1814 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1815 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1816 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1818 msg->wparam = vkey;
1819 msg->lparam |= flags << 16;
1820 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1823 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1824 queue_hardware_message( desktop, msg, 1 );
1826 return wait;
1829 /* queue a hardware message for a custom type of event */
1830 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1831 unsigned int origin, const hw_input_t *input )
1833 struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
1834 struct message *msg;
1836 if (!(msg = alloc_hardware_message( 0, source, get_tick_count() ))) return;
1838 msg->win = get_user_full_handle( win );
1839 msg->msg = input->hw.msg;
1840 msg->wparam = 0;
1841 msg->lparam = input->hw.lparam;
1842 msg->x = desktop->cursor.x;
1843 msg->y = desktop->cursor.y;
1845 queue_hardware_message( desktop, msg, 1 );
1848 /* check message filter for a hardware message */
1849 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1850 user_handle_t filter_win, unsigned int first, unsigned int last )
1852 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1854 /* we can only test the window for a keyboard message since the
1855 * dest window for a mouse message depends on hittest */
1856 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1857 return 0;
1858 /* the message code is final for a keyboard message, we can simply check it */
1859 return check_msg_filter( msg_code, first, last );
1861 else /* mouse message */
1863 /* we need to check all possible values that the message can have in the end */
1865 if (check_msg_filter( msg_code, first, last )) return 1;
1866 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1868 /* all other messages can become non-client messages */
1869 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1871 /* clicks can become double-clicks or non-client double-clicks */
1872 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1873 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1875 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1876 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1878 return 0;
1883 /* find a hardware message for the given queue */
1884 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1885 unsigned int first, unsigned int last, unsigned int flags,
1886 struct get_message_reply *reply )
1888 struct thread_input *input = thread->queue->input;
1889 struct thread *win_thread;
1890 struct list *ptr;
1891 user_handle_t win;
1892 int clear_bits, got_one = 0;
1893 unsigned int msg_code;
1895 ptr = list_head( &input->msg_list );
1896 if (hw_id)
1898 while (ptr)
1900 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1901 if (msg->unique_id == hw_id) break;
1902 ptr = list_next( &input->msg_list, ptr );
1904 if (!ptr) ptr = list_head( &input->msg_list );
1905 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1908 if (ptr == list_head( &input->msg_list ))
1909 clear_bits = QS_INPUT;
1910 else
1911 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1913 while (ptr)
1915 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1916 struct hardware_msg_data *data = msg->data;
1918 ptr = list_next( &input->msg_list, ptr );
1919 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
1920 if (!win || !win_thread)
1922 /* no window at all, remove it */
1923 update_input_key_state( input->desktop, input->keystate, msg );
1924 list_remove( &msg->entry );
1925 free_message( msg );
1926 continue;
1928 if (win_thread != thread)
1930 if (win_thread->queue->input == input)
1932 /* wake the other thread */
1933 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1934 got_one = 1;
1936 else
1938 /* for another thread input, drop it */
1939 update_input_key_state( input->desktop, input->keystate, msg );
1940 list_remove( &msg->entry );
1941 free_message( msg );
1943 release_object( win_thread );
1944 continue;
1946 release_object( win_thread );
1948 /* if we already got a message for another thread, or if it doesn't
1949 * match the filter we skip it */
1950 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1952 clear_bits &= ~get_hardware_msg_bit( msg );
1953 continue;
1955 /* now we can return it */
1956 if (!msg->unique_id) msg->unique_id = get_unique_id();
1957 reply->type = MSG_HARDWARE;
1958 reply->win = win;
1959 reply->msg = msg_code;
1960 reply->wparam = msg->wparam;
1961 reply->lparam = msg->lparam;
1962 reply->x = msg->x;
1963 reply->y = msg->y;
1964 reply->time = msg->time;
1966 data->hw_id = msg->unique_id;
1967 set_reply_data( msg->data, msg->data_size );
1968 if (msg->msg == WM_INPUT && (flags & PM_REMOVE))
1969 release_hardware_message( current->queue, data->hw_id, 1 );
1970 return 1;
1972 /* nothing found, clear the hardware queue bits */
1973 clear_queue_bits( thread->queue, clear_bits );
1974 return 0;
1977 /* increment (or decrement if 'incr' is negative) the queue paint count */
1978 void inc_queue_paint_count( struct thread *thread, int incr )
1980 struct msg_queue *queue = thread->queue;
1982 assert( queue );
1984 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1986 if (queue->paint_count)
1987 set_queue_bits( queue, QS_PAINT );
1988 else
1989 clear_queue_bits( queue, QS_PAINT );
1993 /* remove all messages and timers belonging to a certain window */
1994 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1996 struct msg_queue *queue = thread->queue;
1997 struct list *ptr;
1998 int i;
2000 if (!queue) return;
2002 /* remove timers */
2004 ptr = list_head( &queue->pending_timers );
2005 while (ptr)
2007 struct list *next = list_next( &queue->pending_timers, ptr );
2008 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2009 if (timer->win == win) free_timer( queue, timer );
2010 ptr = next;
2012 ptr = list_head( &queue->expired_timers );
2013 while (ptr)
2015 struct list *next = list_next( &queue->expired_timers, ptr );
2016 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2017 if (timer->win == win) free_timer( queue, timer );
2018 ptr = next;
2021 /* remove messages */
2022 for (i = 0; i < NB_MSG_KINDS; i++)
2024 struct list *ptr, *next;
2026 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2028 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2029 if (msg->win == win)
2031 if (msg->msg == WM_QUIT && !queue->quit_message)
2033 queue->quit_message = 1;
2034 queue->exit_code = msg->wparam;
2036 remove_queue_message( queue, msg, i );
2041 thread_input_cleanup_window( queue, win );
2044 /* post a message to a window */
2045 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2047 struct message *msg;
2048 struct thread *thread = get_window_thread( win );
2050 if (!thread) return;
2052 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2054 msg->type = MSG_POSTED;
2055 msg->win = get_user_full_handle( win );
2056 msg->msg = message;
2057 msg->wparam = wparam;
2058 msg->lparam = lparam;
2059 msg->result = NULL;
2060 msg->data = NULL;
2061 msg->data_size = 0;
2063 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2065 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2066 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2067 if (message == WM_HOTKEY)
2069 set_queue_bits( thread->queue, QS_HOTKEY );
2070 thread->queue->hotkey_count++;
2073 release_object( thread );
2076 /* send a notify message to a window */
2077 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2079 struct message *msg;
2080 struct thread *thread = get_window_thread( win );
2082 if (!thread) return;
2084 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2086 msg->type = MSG_NOTIFY;
2087 msg->win = get_user_full_handle( win );
2088 msg->msg = message;
2089 msg->wparam = wparam;
2090 msg->lparam = lparam;
2091 msg->result = NULL;
2092 msg->data = NULL;
2093 msg->data_size = 0;
2095 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2097 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2098 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2100 release_object( thread );
2103 /* post a win event */
2104 void post_win_event( struct thread *thread, unsigned int event,
2105 user_handle_t win, unsigned int object_id,
2106 unsigned int child_id, client_ptr_t hook_proc,
2107 const WCHAR *module, data_size_t module_size,
2108 user_handle_t hook)
2110 struct message *msg;
2112 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2114 struct winevent_msg_data *data;
2116 msg->type = MSG_WINEVENT;
2117 msg->win = get_user_full_handle( win );
2118 msg->msg = event;
2119 msg->wparam = object_id;
2120 msg->lparam = child_id;
2121 msg->time = get_tick_count();
2122 msg->result = NULL;
2124 if ((data = malloc( sizeof(*data) + module_size )))
2126 data->hook = hook;
2127 data->tid = get_thread_id( current );
2128 data->hook_proc = hook_proc;
2129 memcpy( data + 1, module, module_size );
2131 msg->data = data;
2132 msg->data_size = sizeof(*data) + module_size;
2134 if (debug_level > 1)
2135 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2136 get_thread_id(thread), event, win, object_id, child_id );
2137 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2138 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2140 else
2141 free( msg );
2145 /* free all hotkeys on a desktop, optionally filtering by window */
2146 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2148 struct hotkey *hotkey, *hotkey2;
2150 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2152 if (!window || hotkey->win == window)
2154 list_remove( &hotkey->entry );
2155 free( hotkey );
2161 /* check if the thread owning the window is hung */
2162 DECL_HANDLER(is_window_hung)
2164 struct thread *thread;
2166 thread = get_window_thread( req->win );
2167 if (thread)
2169 reply->is_hung = is_queue_hung( thread->queue );
2170 release_object( thread );
2172 else reply->is_hung = 0;
2176 /* get the message queue of the current thread */
2177 DECL_HANDLER(get_msg_queue)
2179 struct msg_queue *queue = get_current_queue();
2181 reply->handle = 0;
2182 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2186 /* set the file descriptor associated to the current thread queue */
2187 DECL_HANDLER(set_queue_fd)
2189 struct msg_queue *queue = get_current_queue();
2190 struct file *file;
2191 int unix_fd;
2193 if (queue->fd) /* fd can only be set once */
2195 set_error( STATUS_ACCESS_DENIED );
2196 return;
2198 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2200 if ((unix_fd = get_file_unix_fd( file )) != -1)
2202 if ((unix_fd = dup( unix_fd )) != -1)
2203 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2204 else
2205 file_set_error();
2207 release_object( file );
2211 /* set the current message queue wakeup mask */
2212 DECL_HANDLER(set_queue_mask)
2214 struct msg_queue *queue = get_current_queue();
2216 if (queue)
2218 queue->wake_mask = req->wake_mask;
2219 queue->changed_mask = req->changed_mask;
2220 reply->wake_bits = queue->wake_bits;
2221 reply->changed_bits = queue->changed_bits;
2222 if (is_signaled( queue ))
2224 /* if skip wait is set, do what would have been done in the subsequent wait */
2225 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2226 else wake_up( &queue->obj, 0 );
2232 /* get the current message queue status */
2233 DECL_HANDLER(get_queue_status)
2235 struct msg_queue *queue = current->queue;
2236 if (queue)
2238 reply->wake_bits = queue->wake_bits;
2239 reply->changed_bits = queue->changed_bits;
2240 queue->changed_bits &= ~req->clear_bits;
2242 else reply->wake_bits = reply->changed_bits = 0;
2246 /* send a message to a thread queue */
2247 DECL_HANDLER(send_message)
2249 struct message *msg;
2250 struct msg_queue *send_queue = get_current_queue();
2251 struct msg_queue *recv_queue = NULL;
2252 struct thread *thread = NULL;
2254 if (!(thread = get_thread_from_id( req->id ))) return;
2256 if (!(recv_queue = thread->queue))
2258 set_error( STATUS_INVALID_PARAMETER );
2259 release_object( thread );
2260 return;
2262 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2264 set_error( STATUS_TIMEOUT );
2265 release_object( thread );
2266 return;
2269 if ((msg = mem_alloc( sizeof(*msg) )))
2271 msg->type = req->type;
2272 msg->win = get_user_full_handle( req->win );
2273 msg->msg = req->msg;
2274 msg->wparam = req->wparam;
2275 msg->lparam = req->lparam;
2276 msg->result = NULL;
2277 msg->data = NULL;
2278 msg->data_size = get_req_data_size();
2280 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2282 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2284 free( msg );
2285 release_object( thread );
2286 return;
2289 switch(msg->type)
2291 case MSG_OTHER_PROCESS:
2292 case MSG_ASCII:
2293 case MSG_UNICODE:
2294 case MSG_CALLBACK:
2295 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2297 free_message( msg );
2298 break;
2300 /* fall through */
2301 case MSG_NOTIFY:
2302 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2303 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2304 break;
2305 case MSG_POSTED:
2306 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2307 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2308 if (msg->msg == WM_HOTKEY)
2310 set_queue_bits( recv_queue, QS_HOTKEY );
2311 recv_queue->hotkey_count++;
2313 break;
2314 case MSG_HARDWARE: /* should use send_hardware_message instead */
2315 case MSG_CALLBACK_RESULT: /* cannot send this one */
2316 case MSG_HOOK_LL: /* generated internally */
2317 default:
2318 set_error( STATUS_INVALID_PARAMETER );
2319 free( msg );
2320 break;
2323 release_object( thread );
2326 /* send a hardware message to a thread queue */
2327 DECL_HANDLER(send_hardware_message)
2329 struct thread *thread = NULL;
2330 struct desktop *desktop;
2331 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2332 struct msg_queue *sender = get_current_queue();
2333 data_size_t size = min( 256, get_reply_max_size() );
2335 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2337 if (req->win)
2339 if (!(thread = get_window_thread( req->win ))) return;
2340 if (desktop != thread->queue->input->desktop)
2342 /* don't allow queuing events to a different desktop */
2343 release_object( desktop );
2344 return;
2348 reply->prev_x = desktop->cursor.x;
2349 reply->prev_y = desktop->cursor.y;
2351 switch (req->input.type)
2353 case INPUT_MOUSE:
2354 reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2355 break;
2356 case INPUT_KEYBOARD:
2357 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2358 break;
2359 case INPUT_HARDWARE:
2360 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2361 break;
2362 default:
2363 set_error( STATUS_INVALID_PARAMETER );
2365 if (thread) release_object( thread );
2367 reply->new_x = desktop->cursor.x;
2368 reply->new_y = desktop->cursor.y;
2369 set_reply_data( desktop->keystate, size );
2370 release_object( desktop );
2373 /* post a quit message to the current queue */
2374 DECL_HANDLER(post_quit_message)
2376 struct msg_queue *queue = get_current_queue();
2378 if (!queue)
2379 return;
2381 queue->quit_message = 1;
2382 queue->exit_code = req->exit_code;
2383 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2386 /* get a message from the current queue */
2387 DECL_HANDLER(get_message)
2389 struct timer *timer;
2390 struct list *ptr;
2391 struct msg_queue *queue = get_current_queue();
2392 user_handle_t get_win = get_user_full_handle( req->get_win );
2393 unsigned int filter = req->flags >> 16;
2395 reply->active_hooks = get_active_hooks();
2397 if (!queue) return;
2398 queue->last_get_msg = current_time;
2399 if (!filter) filter = QS_ALLINPUT;
2401 /* first check for sent messages */
2402 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2404 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2405 receive_message( queue, msg, reply );
2406 return;
2409 /* clear changed bits so we can wait on them if we don't find a message */
2410 if (filter & QS_POSTMESSAGE)
2412 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2413 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2415 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2416 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2418 /* then check for posted messages */
2419 if ((filter & QS_POSTMESSAGE) &&
2420 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2421 return;
2423 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2424 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2425 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2426 return;
2428 /* only check for quit messages if not posted messages pending */
2429 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2430 return;
2432 /* then check for any raw hardware message */
2433 if ((filter & QS_INPUT) &&
2434 filter_contains_hw_range( req->get_first, req->get_last ) &&
2435 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2436 return;
2438 /* now check for WM_PAINT */
2439 if ((filter & QS_PAINT) &&
2440 queue->paint_count &&
2441 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2442 (reply->win = find_window_to_repaint( get_win, current )))
2444 reply->type = MSG_POSTED;
2445 reply->msg = WM_PAINT;
2446 reply->wparam = 0;
2447 reply->lparam = 0;
2448 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2449 return;
2452 /* now check for timer */
2453 if ((filter & QS_TIMER) &&
2454 (timer = find_expired_timer( queue, get_win, req->get_first,
2455 req->get_last, (req->flags & PM_REMOVE) )))
2457 reply->type = MSG_POSTED;
2458 reply->win = timer->win;
2459 reply->msg = timer->msg;
2460 reply->wparam = timer->id;
2461 reply->lparam = timer->lparam;
2462 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2463 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2464 set_event( current->process->idle_event );
2465 return;
2468 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2469 queue->wake_mask = req->wake_mask;
2470 queue->changed_mask = req->changed_mask;
2471 set_error( STATUS_PENDING ); /* FIXME */
2475 /* reply to a sent message */
2476 DECL_HANDLER(reply_message)
2478 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2479 else if (current->queue->recv_result)
2480 reply_message( current->queue, req->result, 0, req->remove,
2481 get_req_data(), get_req_data_size() );
2485 /* accept the current hardware message */
2486 DECL_HANDLER(accept_hardware_message)
2488 if (current->queue)
2489 release_hardware_message( current->queue, req->hw_id, req->remove );
2490 else
2491 set_error( STATUS_ACCESS_DENIED );
2495 /* retrieve the reply for the last message sent */
2496 DECL_HANDLER(get_message_reply)
2498 struct message_result *result;
2499 struct list *entry;
2500 struct msg_queue *queue = current->queue;
2502 if (queue)
2504 set_error( STATUS_PENDING );
2505 reply->result = 0;
2507 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2509 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2510 if (result->replied || req->cancel)
2512 if (result->replied)
2514 reply->result = result->result;
2515 set_error( result->error );
2516 if (result->data)
2518 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2519 set_reply_data_ptr( result->data, data_len );
2520 result->data = NULL;
2521 result->data_size = 0;
2524 remove_result_from_sender( result );
2526 entry = list_head( &queue->send_result );
2527 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2528 else
2530 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2531 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2532 else clear_queue_bits( queue, QS_SMRESULT );
2536 else set_error( STATUS_ACCESS_DENIED );
2540 /* set a window timer */
2541 DECL_HANDLER(set_win_timer)
2543 struct timer *timer;
2544 struct msg_queue *queue;
2545 struct thread *thread = NULL;
2546 user_handle_t win = 0;
2547 lparam_t id = req->id;
2549 if (req->win)
2551 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2553 set_error( STATUS_INVALID_HANDLE );
2554 return;
2556 if (thread->process != current->process)
2558 release_object( thread );
2559 set_error( STATUS_ACCESS_DENIED );
2560 return;
2562 queue = thread->queue;
2563 /* remove it if it existed already */
2564 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2566 else
2568 queue = get_current_queue();
2569 /* look for a timer with this id */
2570 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2572 /* free and reuse id */
2573 free_timer( queue, timer );
2575 else
2577 lparam_t end_id = queue->next_timer_id;
2579 /* find a free id for it */
2580 while (1)
2582 id = queue->next_timer_id;
2583 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2585 if (!find_timer( queue, 0, req->msg, id )) break;
2586 if (queue->next_timer_id == end_id)
2588 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2589 return;
2595 if ((timer = set_timer( queue, req->rate )))
2597 timer->win = win;
2598 timer->msg = req->msg;
2599 timer->id = id;
2600 timer->lparam = req->lparam;
2601 reply->id = id;
2603 if (thread) release_object( thread );
2606 /* kill a window timer */
2607 DECL_HANDLER(kill_win_timer)
2609 struct timer *timer;
2610 struct thread *thread;
2611 user_handle_t win = 0;
2613 if (req->win)
2615 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2617 set_error( STATUS_INVALID_HANDLE );
2618 return;
2620 if (thread->process != current->process)
2622 release_object( thread );
2623 set_error( STATUS_ACCESS_DENIED );
2624 return;
2627 else thread = (struct thread *)grab_object( current );
2629 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2630 free_timer( thread->queue, timer );
2631 else
2632 set_error( STATUS_INVALID_PARAMETER );
2634 release_object( thread );
2637 DECL_HANDLER(register_hotkey)
2639 struct desktop *desktop;
2640 user_handle_t win_handle = req->window;
2641 struct hotkey *hotkey;
2642 struct hotkey *new_hotkey = NULL;
2643 struct thread *thread;
2644 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2646 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2648 if (win_handle)
2650 if (!(win_handle = get_valid_window_handle( win_handle )))
2652 release_object( desktop );
2653 return;
2656 thread = get_window_thread( win_handle );
2657 if (thread) release_object( thread );
2659 if (thread != current)
2661 release_object( desktop );
2662 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2663 return;
2667 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2669 if (req->vkey == hotkey->vkey &&
2670 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2672 release_object( desktop );
2673 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2674 return;
2676 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2677 new_hotkey = hotkey;
2680 if (new_hotkey)
2682 reply->replaced = 1;
2683 reply->flags = new_hotkey->flags;
2684 reply->vkey = new_hotkey->vkey;
2686 else
2688 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2689 if (new_hotkey)
2691 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2692 new_hotkey->queue = current->queue;
2693 new_hotkey->win = win_handle;
2694 new_hotkey->id = req->id;
2698 if (new_hotkey)
2700 new_hotkey->flags = req->flags;
2701 new_hotkey->vkey = req->vkey;
2704 release_object( desktop );
2707 DECL_HANDLER(unregister_hotkey)
2709 struct desktop *desktop;
2710 user_handle_t win_handle = req->window;
2711 struct hotkey *hotkey;
2712 struct thread *thread;
2714 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2716 if (win_handle)
2718 if (!(win_handle = get_valid_window_handle( win_handle )))
2720 release_object( desktop );
2721 return;
2724 thread = get_window_thread( win_handle );
2725 if (thread) release_object( thread );
2727 if (thread != current)
2729 release_object( desktop );
2730 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2731 return;
2735 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2737 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2738 goto found;
2741 release_object( desktop );
2742 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2743 return;
2745 found:
2746 reply->flags = hotkey->flags;
2747 reply->vkey = hotkey->vkey;
2748 list_remove( &hotkey->entry );
2749 free( hotkey );
2750 release_object( desktop );
2753 /* attach (or detach) thread inputs */
2754 DECL_HANDLER(attach_thread_input)
2756 struct thread *thread_from = get_thread_from_id( req->tid_from );
2757 struct thread *thread_to = get_thread_from_id( req->tid_to );
2759 if (!thread_from || !thread_to)
2761 if (thread_from) release_object( thread_from );
2762 if (thread_to) release_object( thread_to );
2763 return;
2765 if (thread_from != thread_to)
2767 if (req->attach)
2769 if ((thread_to->queue || thread_to == current) &&
2770 (thread_from->queue || thread_from == current))
2771 attach_thread_input( thread_from, thread_to );
2772 else
2773 set_error( STATUS_INVALID_PARAMETER );
2775 else
2777 if (thread_from->queue && thread_to->queue &&
2778 thread_from->queue->input == thread_to->queue->input)
2779 detach_thread_input( thread_from );
2780 else
2781 set_error( STATUS_ACCESS_DENIED );
2784 else set_error( STATUS_ACCESS_DENIED );
2785 release_object( thread_from );
2786 release_object( thread_to );
2790 /* get thread input data */
2791 DECL_HANDLER(get_thread_input)
2793 struct thread *thread = NULL;
2794 struct desktop *desktop;
2795 struct thread_input *input;
2797 if (req->tid)
2799 if (!(thread = get_thread_from_id( req->tid ))) return;
2800 if (!(desktop = get_thread_desktop( thread, 0 )))
2802 release_object( thread );
2803 return;
2805 input = thread->queue ? thread->queue->input : NULL;
2807 else
2809 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2810 input = desktop->foreground_input; /* get the foreground thread info */
2813 if (input)
2815 reply->focus = input->focus;
2816 reply->capture = input->capture;
2817 reply->active = input->active;
2818 reply->menu_owner = input->menu_owner;
2819 reply->move_size = input->move_size;
2820 reply->caret = input->caret;
2821 reply->cursor = input->cursor;
2822 reply->show_count = input->cursor_count;
2823 reply->rect = input->caret_rect;
2826 /* foreground window is active window of foreground thread */
2827 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2828 if (thread) release_object( thread );
2829 release_object( desktop );
2833 /* retrieve queue keyboard state for a given thread */
2834 DECL_HANDLER(get_key_state)
2836 struct thread *thread;
2837 struct desktop *desktop;
2838 data_size_t size = min( 256, get_reply_max_size() );
2840 if (!req->tid) /* get global async key state */
2842 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2843 if (req->key >= 0)
2845 reply->state = desktop->keystate[req->key & 0xff];
2846 desktop->keystate[req->key & 0xff] &= ~0x40;
2848 set_reply_data( desktop->keystate, size );
2849 release_object( desktop );
2851 else
2853 unsigned char *keystate;
2854 if (!(thread = get_thread_from_id( req->tid ))) return;
2855 if (thread->queue)
2857 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2858 set_reply_data( thread->queue->input->keystate, size );
2859 release_object( thread );
2860 return;
2862 release_object( thread );
2864 /* fallback to desktop keystate */
2865 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2866 if (req->key >= 0) reply->state = desktop->keystate[req->key & 0xff] & ~0x40;
2867 if ((keystate = set_reply_data_size( size )))
2869 unsigned int i;
2870 for (i = 0; i < size; i++) keystate[i] = desktop->keystate[i] & ~0x40;
2872 release_object( desktop );
2877 /* set queue keyboard state for a given thread */
2878 DECL_HANDLER(set_key_state)
2880 struct thread *thread;
2881 struct desktop *desktop;
2882 data_size_t size = min( 256, get_req_data_size() );
2884 if (!req->tid) /* set global async key state */
2886 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2887 memcpy( desktop->keystate, get_req_data(), size );
2888 release_object( desktop );
2890 else
2892 if (!(thread = get_thread_from_id( req->tid ))) return;
2893 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
2894 if (req->async && (desktop = get_thread_desktop( thread, 0 )))
2896 memcpy( desktop->keystate, get_req_data(), size );
2897 release_object( desktop );
2899 release_object( thread );
2904 /* set the system foreground window */
2905 DECL_HANDLER(set_foreground_window)
2907 struct thread *thread = NULL;
2908 struct desktop *desktop;
2909 struct msg_queue *queue = get_current_queue();
2911 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2912 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
2913 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
2914 reply->send_msg_new = FALSE;
2916 if (is_valid_foreground_window( req->handle ) &&
2917 (thread = get_window_thread( req->handle )) &&
2918 thread->queue->input->desktop == desktop)
2920 set_foreground_input( desktop, thread->queue->input );
2921 reply->send_msg_new = (desktop->foreground_input != queue->input);
2923 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2925 if (thread) release_object( thread );
2926 release_object( desktop );
2930 /* set the current thread focus window */
2931 DECL_HANDLER(set_focus_window)
2933 struct msg_queue *queue = get_current_queue();
2935 reply->previous = 0;
2936 if (queue && check_queue_input_window( queue, req->handle ))
2938 reply->previous = queue->input->focus;
2939 queue->input->focus = get_user_full_handle( req->handle );
2944 /* set the current thread active window */
2945 DECL_HANDLER(set_active_window)
2947 struct msg_queue *queue = get_current_queue();
2949 reply->previous = 0;
2950 if (queue && check_queue_input_window( queue, req->handle ))
2952 if (!req->handle || make_window_active( req->handle ))
2954 reply->previous = queue->input->active;
2955 queue->input->active = get_user_full_handle( req->handle );
2957 else set_error( STATUS_INVALID_HANDLE );
2962 /* set the current thread capture window */
2963 DECL_HANDLER(set_capture_window)
2965 struct msg_queue *queue = get_current_queue();
2967 reply->previous = reply->full_handle = 0;
2968 if (queue && check_queue_input_window( queue, req->handle ))
2970 struct thread_input *input = queue->input;
2972 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2973 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2975 set_error(STATUS_ACCESS_DENIED);
2976 return;
2978 reply->previous = input->capture;
2979 input->capture = get_user_full_handle( req->handle );
2980 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2981 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2982 reply->full_handle = input->capture;
2987 /* Set the current thread caret window */
2988 DECL_HANDLER(set_caret_window)
2990 struct msg_queue *queue = get_current_queue();
2992 reply->previous = 0;
2993 if (queue && check_queue_input_window( queue, req->handle ))
2995 struct thread_input *input = queue->input;
2997 reply->previous = input->caret;
2998 reply->old_rect = input->caret_rect;
2999 reply->old_hide = input->caret_hide;
3000 reply->old_state = input->caret_state;
3002 set_caret_window( input, get_user_full_handle(req->handle) );
3003 input->caret_rect.right = input->caret_rect.left + req->width;
3004 input->caret_rect.bottom = input->caret_rect.top + req->height;
3009 /* Set the current thread caret information */
3010 DECL_HANDLER(set_caret_info)
3012 struct msg_queue *queue = get_current_queue();
3013 struct thread_input *input;
3015 if (!queue) return;
3016 input = queue->input;
3017 reply->full_handle = input->caret;
3018 reply->old_rect = input->caret_rect;
3019 reply->old_hide = input->caret_hide;
3020 reply->old_state = input->caret_state;
3022 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3024 set_error( STATUS_ACCESS_DENIED );
3025 return;
3027 if (req->flags & SET_CARET_POS)
3029 input->caret_rect.right += req->x - input->caret_rect.left;
3030 input->caret_rect.bottom += req->y - input->caret_rect.top;
3031 input->caret_rect.left = req->x;
3032 input->caret_rect.top = req->y;
3034 if (req->flags & SET_CARET_HIDE)
3036 input->caret_hide += req->hide;
3037 if (input->caret_hide < 0) input->caret_hide = 0;
3039 if (req->flags & SET_CARET_STATE)
3041 switch (req->state)
3043 case CARET_STATE_OFF: input->caret_state = 0; break;
3044 case CARET_STATE_ON: input->caret_state = 1; break;
3045 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3046 case CARET_STATE_ON_IF_MOVED:
3047 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3048 break;
3054 /* get the time of the last input event */
3055 DECL_HANDLER(get_last_input_time)
3057 reply->time = last_input_time;
3060 /* set/get the current cursor */
3061 DECL_HANDLER(set_cursor)
3063 struct msg_queue *queue = get_current_queue();
3064 struct thread_input *input;
3066 if (!queue) return;
3067 input = queue->input;
3069 reply->prev_handle = input->cursor;
3070 reply->prev_count = input->cursor_count;
3071 reply->prev_x = input->desktop->cursor.x;
3072 reply->prev_y = input->desktop->cursor.y;
3074 if (req->flags & SET_CURSOR_HANDLE)
3076 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3078 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3079 return;
3081 input->cursor = req->handle;
3083 if (req->flags & SET_CURSOR_COUNT)
3085 queue->cursor_count += req->show_count;
3086 input->cursor_count += req->show_count;
3088 if (req->flags & SET_CURSOR_POS)
3090 set_cursor_pos( input->desktop, req->x, req->y );
3092 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3094 struct desktop *desktop = input->desktop;
3096 /* only the desktop owner can set the message */
3097 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3098 desktop->cursor.clip_msg = req->clip_msg;
3100 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip, 0 );
3103 reply->new_x = input->desktop->cursor.x;
3104 reply->new_y = input->desktop->cursor.y;
3105 reply->new_clip = input->desktop->cursor.clip;
3106 reply->last_change = input->desktop->cursor.last_change;
3109 DECL_HANDLER(update_rawinput_devices)
3111 const struct rawinput_device *devices = get_req_data();
3112 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3113 const struct rawinput_device_entry *e;
3114 unsigned int i;
3116 for (i = 0; i < device_count; ++i)
3118 update_rawinput_device(&devices[i]);
3121 e = find_rawinput_device( 1, 2 );
3122 current->process->rawinput_mouse = e ? &e->device : NULL;
3123 e = find_rawinput_device( 1, 6 );
3124 current->process->rawinput_kbd = e ? &e->device : NULL;