ntdll/unix: Use Pc instead of Rip in signal_arm64.
[wine.git] / server / queue.c
blobc3925dd6646ebe301df5f4ec3edfa25e0cd2defd
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 abstime_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 static int update_desktop_cursor_pos( struct desktop *desktop, int x, int y )
371 int updated;
373 x = max( min( x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
374 y = max( min( y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
375 updated = (desktop->cursor.x != x || desktop->cursor.y != y);
376 desktop->cursor.x = x;
377 desktop->cursor.y = y;
378 desktop->cursor.last_change = get_tick_count();
380 return updated;
383 /* set the cursor position and queue the corresponding mouse message */
384 static void set_cursor_pos( struct desktop *desktop, int x, int y )
386 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
387 const struct rawinput_device *device;
388 struct message *msg;
390 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
392 update_desktop_cursor_pos( desktop, x, y );
393 return;
396 if (!(msg = alloc_hardware_message( 0, source, get_tick_count() ))) return;
398 msg->msg = WM_MOUSEMOVE;
399 msg->x = x;
400 msg->y = y;
401 queue_hardware_message( desktop, msg, 1 );
404 /* retrieve default position and time for synthesized messages */
405 static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
407 struct desktop *desktop = queue->input->desktop;
409 *x = desktop->cursor.x;
410 *y = desktop->cursor.y;
411 *time = get_tick_count();
414 /* set the cursor clip rectangle */
415 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect, int send_clip_msg )
417 rectangle_t top_rect;
418 int x, y;
420 get_top_window_rectangle( desktop, &top_rect );
421 if (rect)
423 rectangle_t new_rect = *rect;
424 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
425 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
426 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
427 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
428 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
429 desktop->cursor.clip = new_rect;
431 else desktop->cursor.clip = top_rect;
433 if (desktop->cursor.clip_msg && send_clip_msg)
434 post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
436 /* warp the mouse to be inside the clip rect */
437 x = max( min( desktop->cursor.x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
438 y = max( min( desktop->cursor.y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
439 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
442 /* change the foreground input and reset the cursor clip rect */
443 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
445 if (desktop->foreground_input == input) return;
446 set_clip_rectangle( desktop, NULL, 1 );
447 desktop->foreground_input = input;
450 /* get the hook table for a given thread */
451 struct hook_table *get_queue_hooks( struct thread *thread )
453 if (!thread->queue) return NULL;
454 return thread->queue->hooks;
457 /* set the hook table for a given thread, allocating the queue if needed */
458 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
460 struct msg_queue *queue = thread->queue;
461 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
462 if (queue->hooks) release_object( queue->hooks );
463 queue->hooks = hooks;
466 /* check the queue status */
467 static inline int is_signaled( struct msg_queue *queue )
469 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
472 /* set some queue bits */
473 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
475 queue->wake_bits |= bits;
476 queue->changed_bits |= bits;
477 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
480 /* clear some queue bits */
481 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
483 queue->wake_bits &= ~bits;
484 queue->changed_bits &= ~bits;
487 /* check whether msg is a keyboard message */
488 static inline int is_keyboard_msg( struct message *msg )
490 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
493 /* check if message is matched by the filter */
494 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
496 return (msg >= first && msg <= last);
499 /* check whether a message filter contains at least one potential hardware message */
500 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
502 /* hardware message ranges are (in numerical order):
503 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
504 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
505 * WM_MOUSEFIRST .. WM_MOUSELAST
507 if (last < WM_NCMOUSEFIRST) return 0;
508 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
509 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
510 if (first > WM_MOUSELAST) return 0;
511 return 1;
514 /* get the QS_* bit corresponding to a given hardware message */
515 static inline int get_hardware_msg_bit( struct message *msg )
517 if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
518 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
519 if (is_keyboard_msg( msg )) return QS_KEY;
520 return QS_MOUSEBUTTON;
523 /* get the current thread queue, creating it if needed */
524 static inline struct msg_queue *get_current_queue(void)
526 struct msg_queue *queue = current->queue;
527 if (!queue) queue = create_msg_queue( current, NULL );
528 return queue;
531 /* get a (pseudo-)unique id to tag hardware messages */
532 static inline unsigned int get_unique_id(void)
534 static unsigned int id;
535 if (!++id) id = 1; /* avoid an id of 0 */
536 return id;
539 /* try to merge a message with the last in the list; return 1 if successful */
540 static int merge_message( struct thread_input *input, const struct message *msg )
542 struct message *prev;
543 struct list *ptr;
545 if (msg->msg != WM_MOUSEMOVE) return 0;
546 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
548 prev = LIST_ENTRY( ptr, struct message, entry );
549 if (prev->msg != WM_INPUT) break;
551 if (!ptr) return 0;
552 if (prev->result) return 0;
553 if (prev->win && msg->win && prev->win != msg->win) return 0;
554 if (prev->msg != msg->msg) return 0;
555 if (prev->type != msg->type) return 0;
556 /* now we can merge it */
557 prev->wparam = msg->wparam;
558 prev->lparam = msg->lparam;
559 prev->x = msg->x;
560 prev->y = msg->y;
561 prev->time = msg->time;
562 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
564 struct hardware_msg_data *prev_data = prev->data;
565 struct hardware_msg_data *msg_data = msg->data;
566 prev_data->info = msg_data->info;
568 list_remove( ptr );
569 list_add_tail( &input->msg_list, ptr );
570 return 1;
573 /* free a result structure */
574 static void free_result( struct message_result *result )
576 if (result->timeout) remove_timeout_user( result->timeout );
577 free( result->data );
578 if (result->callback_msg) free_message( result->callback_msg );
579 if (result->hardware_msg) free_message( result->hardware_msg );
580 if (result->desktop) release_object( result->desktop );
581 free( result );
584 /* remove the result from the sender list it is on */
585 static inline void remove_result_from_sender( struct message_result *result )
587 assert( result->sender );
589 list_remove( &result->sender_entry );
590 result->sender = NULL;
591 if (!result->receiver) free_result( result );
594 /* store the message result in the appropriate structure */
595 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
597 res->result = result;
598 res->error = error;
599 res->replied = 1;
600 if (res->timeout)
602 remove_timeout_user( res->timeout );
603 res->timeout = NULL;
606 if (res->hardware_msg)
608 if (!error && result) /* rejected by the hook */
609 free_message( res->hardware_msg );
610 else
611 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
613 res->hardware_msg = NULL;
616 if (res->sender)
618 if (res->callback_msg)
620 /* queue the callback message in the sender queue */
621 struct callback_msg_data *data = res->callback_msg->data;
622 data->result = result;
623 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
624 set_queue_bits( res->sender, QS_SENDMESSAGE );
625 res->callback_msg = NULL;
626 remove_result_from_sender( res );
628 else
630 /* wake sender queue if waiting on this result */
631 if (list_head(&res->sender->send_result) == &res->sender_entry)
632 set_queue_bits( res->sender, QS_SMRESULT );
635 else if (!res->receiver) free_result( res );
638 /* free a message when deleting a queue or window */
639 static void free_message( struct message *msg )
641 struct message_result *result = msg->result;
642 if (result)
644 result->msg = NULL;
645 result->receiver = NULL;
646 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
648 free( msg->data );
649 free( msg );
652 /* remove (and free) a message from a message list */
653 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
654 enum message_kind kind )
656 list_remove( &msg->entry );
657 switch(kind)
659 case SEND_MESSAGE:
660 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
661 break;
662 case POST_MESSAGE:
663 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
664 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
665 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
666 clear_queue_bits( queue, QS_HOTKEY );
667 break;
669 free_message( msg );
672 /* message timed out without getting a reply */
673 static void result_timeout( void *private )
675 struct message_result *result = private;
677 assert( !result->replied );
679 result->timeout = NULL;
681 if (result->msg) /* not received yet */
683 struct message *msg = result->msg;
685 result->msg = NULL;
686 msg->result = NULL;
687 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
688 result->receiver = NULL;
690 store_message_result( result, 0, STATUS_TIMEOUT );
693 /* allocate and fill a message result structure */
694 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
695 struct msg_queue *recv_queue,
696 struct message *msg, timeout_t timeout )
698 struct message_result *result = mem_alloc( sizeof(*result) );
699 if (result)
701 result->msg = msg;
702 result->sender = send_queue;
703 result->receiver = recv_queue;
704 result->replied = 0;
705 result->data = NULL;
706 result->data_size = 0;
707 result->timeout = NULL;
708 result->hardware_msg = NULL;
709 result->desktop = NULL;
710 result->callback_msg = NULL;
712 if (msg->type == MSG_CALLBACK)
714 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
716 if (!callback_msg)
718 free( result );
719 return NULL;
721 callback_msg->type = MSG_CALLBACK_RESULT;
722 callback_msg->win = msg->win;
723 callback_msg->msg = msg->msg;
724 callback_msg->wparam = 0;
725 callback_msg->lparam = 0;
726 callback_msg->time = get_tick_count();
727 callback_msg->result = NULL;
728 /* steal the data from the original message */
729 callback_msg->data = msg->data;
730 callback_msg->data_size = msg->data_size;
731 msg->data = NULL;
732 msg->data_size = 0;
734 result->callback_msg = callback_msg;
735 list_add_head( &send_queue->callback_result, &result->sender_entry );
737 else if (send_queue)
739 list_add_head( &send_queue->send_result, &result->sender_entry );
740 clear_queue_bits( send_queue, QS_SMRESULT );
743 if (timeout != TIMEOUT_INFINITE)
744 result->timeout = add_timeout_user( timeout, result_timeout, result );
746 return result;
749 /* receive a message, removing it from the sent queue */
750 static void receive_message( struct msg_queue *queue, struct message *msg,
751 struct get_message_reply *reply )
753 struct message_result *result = msg->result;
755 reply->total = msg->data_size;
756 if (msg->data_size > get_reply_max_size())
758 set_error( STATUS_BUFFER_OVERFLOW );
759 return;
761 reply->type = msg->type;
762 reply->win = msg->win;
763 reply->msg = msg->msg;
764 reply->wparam = msg->wparam;
765 reply->lparam = msg->lparam;
766 reply->x = msg->x;
767 reply->y = msg->y;
768 reply->time = msg->time;
770 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
772 list_remove( &msg->entry );
773 /* put the result on the receiver result stack */
774 if (result)
776 result->msg = NULL;
777 result->recv_next = queue->recv_result;
778 queue->recv_result = result;
780 free( msg );
781 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
784 /* set the result of the current received message */
785 static void reply_message( struct msg_queue *queue, lparam_t result,
786 unsigned int error, int remove, const void *data, data_size_t len )
788 struct message_result *res = queue->recv_result;
790 if (remove)
792 queue->recv_result = res->recv_next;
793 res->receiver = NULL;
794 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
796 free_result( res );
797 return;
800 if (!res->replied)
802 if (len && (res->data = memdup( data, len ))) res->data_size = len;
803 store_message_result( res, result, error );
807 static int match_window( user_handle_t win, user_handle_t msg_win )
809 if (!win) return 1;
810 if (win == -1 || win == 1) return !msg_win;
811 if (msg_win == win) return 1;
812 return is_child_window( win, msg_win );
815 /* retrieve a posted message */
816 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
817 unsigned int first, unsigned int last, unsigned int flags,
818 struct get_message_reply *reply )
820 struct message *msg;
822 /* check against the filters */
823 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
825 if (!match_window( win, msg->win )) continue;
826 if (!check_msg_filter( msg->msg, first, last )) continue;
827 goto found; /* found one */
829 return 0;
831 /* return it to the app */
832 found:
833 reply->total = msg->data_size;
834 if (msg->data_size > get_reply_max_size())
836 set_error( STATUS_BUFFER_OVERFLOW );
837 return 1;
839 reply->type = msg->type;
840 reply->win = msg->win;
841 reply->msg = msg->msg;
842 reply->wparam = msg->wparam;
843 reply->lparam = msg->lparam;
844 reply->x = msg->x;
845 reply->y = msg->y;
846 reply->time = msg->time;
848 if (flags & PM_REMOVE)
850 if (msg->data)
852 set_reply_data_ptr( msg->data, msg->data_size );
853 msg->data = NULL;
854 msg->data_size = 0;
856 remove_queue_message( queue, msg, POST_MESSAGE );
858 else if (msg->data) set_reply_data( msg->data, msg->data_size );
860 return 1;
863 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
864 struct get_message_reply *reply )
866 if (queue->quit_message)
868 reply->total = 0;
869 reply->type = MSG_POSTED;
870 reply->win = 0;
871 reply->msg = WM_QUIT;
872 reply->wparam = queue->exit_code;
873 reply->lparam = 0;
875 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
877 if (flags & PM_REMOVE)
879 queue->quit_message = 0;
880 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
881 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
883 return 1;
885 else
886 return 0;
889 /* empty a message list and free all the messages */
890 static void empty_msg_list( struct list *list )
892 struct list *ptr;
894 while ((ptr = list_head( list )) != NULL)
896 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
897 list_remove( &msg->entry );
898 free_message( msg );
902 /* cleanup all pending results when deleting a queue */
903 static void cleanup_results( struct msg_queue *queue )
905 struct list *entry;
907 while ((entry = list_head( &queue->send_result )) != NULL)
909 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
912 while ((entry = list_head( &queue->callback_result )) != NULL)
914 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
917 while (queue->recv_result)
918 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
921 /* check if the thread owning the queue is hung (not checking for messages) */
922 static int is_queue_hung( struct msg_queue *queue )
924 struct wait_queue_entry *entry;
926 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
927 return 0; /* less than 5 seconds since last get message -> not hung */
929 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
931 if (get_wait_queue_thread(entry)->queue == queue)
932 return 0; /* thread is waiting on queue -> not hung */
934 return 1;
937 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
939 struct msg_queue *queue = (struct msg_queue *)obj;
940 struct process *process = get_wait_queue_thread(entry)->process;
942 /* a thread can only wait on its own queue */
943 if (get_wait_queue_thread(entry)->queue != queue)
945 set_error( STATUS_ACCESS_DENIED );
946 return 0;
948 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
950 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
951 set_fd_events( queue->fd, POLLIN );
952 add_queue( obj, entry );
953 return 1;
956 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
958 struct msg_queue *queue = (struct msg_queue *)obj;
960 remove_queue( obj, entry );
961 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
962 set_fd_events( queue->fd, 0 );
965 static void msg_queue_dump( struct object *obj, int verbose )
967 struct msg_queue *queue = (struct msg_queue *)obj;
968 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
969 queue->wake_bits, queue->wake_mask );
972 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
974 struct msg_queue *queue = (struct msg_queue *)obj;
975 int ret = 0;
977 if (queue->fd)
979 if ((ret = check_fd_events( queue->fd, POLLIN )))
980 /* stop waiting on select() if we are signaled */
981 set_fd_events( queue->fd, 0 );
982 else if (!list_empty( &obj->wait_queue ))
983 /* restart waiting on poll() if we are no longer signaled */
984 set_fd_events( queue->fd, POLLIN );
986 return ret || is_signaled( queue );
989 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
991 struct msg_queue *queue = (struct msg_queue *)obj;
992 queue->wake_mask = 0;
993 queue->changed_mask = 0;
996 static void msg_queue_destroy( struct object *obj )
998 struct msg_queue *queue = (struct msg_queue *)obj;
999 struct list *ptr;
1000 struct hotkey *hotkey, *hotkey2;
1001 int i;
1003 cleanup_results( queue );
1004 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
1006 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
1008 if (hotkey->queue == queue)
1010 list_remove( &hotkey->entry );
1011 free( hotkey );
1015 while ((ptr = list_head( &queue->pending_timers )))
1017 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1018 list_remove( &timer->entry );
1019 free( timer );
1021 while ((ptr = list_head( &queue->expired_timers )))
1023 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1024 list_remove( &timer->entry );
1025 free( timer );
1027 if (queue->timeout) remove_timeout_user( queue->timeout );
1028 queue->input->cursor_count -= queue->cursor_count;
1029 release_object( queue->input );
1030 if (queue->hooks) release_object( queue->hooks );
1031 if (queue->fd) release_object( queue->fd );
1034 static void msg_queue_poll_event( struct fd *fd, int event )
1036 struct msg_queue *queue = get_fd_user( fd );
1037 assert( queue->obj.ops == &msg_queue_ops );
1039 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1040 else set_fd_events( queue->fd, 0 );
1041 wake_up( &queue->obj, 0 );
1044 static void thread_input_dump( struct object *obj, int verbose )
1046 struct thread_input *input = (struct thread_input *)obj;
1047 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
1048 input->focus, input->capture, input->active );
1051 static void thread_input_destroy( struct object *obj )
1053 struct thread_input *input = (struct thread_input *)obj;
1055 empty_msg_list( &input->msg_list );
1056 if (input->desktop)
1058 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
1059 release_object( input->desktop );
1063 /* fix the thread input data when a window is destroyed */
1064 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1066 struct thread_input *input = queue->input;
1068 if (window == input->focus) input->focus = 0;
1069 if (window == input->capture) input->capture = 0;
1070 if (window == input->active) input->active = 0;
1071 if (window == input->menu_owner) input->menu_owner = 0;
1072 if (window == input->move_size) input->move_size = 0;
1073 if (window == input->caret) set_caret_window( input, 0 );
1076 /* check if the specified window can be set in the input data of a given queue */
1077 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1079 struct thread *thread;
1080 int ret = 0;
1082 if (!window) return 1; /* we can always clear the data */
1084 if ((thread = get_window_thread( window )))
1086 ret = (queue->input == thread->queue->input);
1087 if (!ret) set_error( STATUS_ACCESS_DENIED );
1088 release_object( thread );
1090 else set_error( STATUS_INVALID_HANDLE );
1092 return ret;
1095 /* make sure the specified thread has a queue */
1096 int init_thread_queue( struct thread *thread )
1098 if (thread->queue) return 1;
1099 return (create_msg_queue( thread, NULL ) != NULL);
1102 /* attach two thread input data structures */
1103 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1105 struct desktop *desktop;
1106 struct thread_input *input;
1107 int ret;
1109 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1110 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1111 input = (struct thread_input *)grab_object( thread_to->queue->input );
1112 if (input->desktop != desktop)
1114 set_error( STATUS_ACCESS_DENIED );
1115 release_object( input );
1116 release_object( desktop );
1117 return 0;
1119 release_object( desktop );
1121 if (thread_from->queue)
1123 if (!input->focus) input->focus = thread_from->queue->input->focus;
1124 if (!input->active) input->active = thread_from->queue->input->active;
1127 ret = assign_thread_input( thread_from, input );
1128 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1129 release_object( input );
1130 return ret;
1133 /* detach two thread input data structures */
1134 void detach_thread_input( struct thread *thread_from )
1136 struct thread *thread;
1137 struct thread_input *input, *old_input = thread_from->queue->input;
1139 if ((input = create_thread_input( thread_from )))
1141 if (old_input->focus && (thread = get_window_thread( old_input->focus )))
1143 if (thread == thread_from)
1145 input->focus = old_input->focus;
1146 old_input->focus = 0;
1148 release_object( thread );
1150 if (old_input->active && (thread = get_window_thread( old_input->active )))
1152 if (thread == thread_from)
1154 input->active = old_input->active;
1155 old_input->active = 0;
1157 release_object( thread );
1159 assign_thread_input( thread_from, input );
1160 release_object( input );
1165 /* set the next timer to expire */
1166 static void set_next_timer( struct msg_queue *queue )
1168 struct list *ptr;
1170 if (queue->timeout)
1172 remove_timeout_user( queue->timeout );
1173 queue->timeout = NULL;
1175 if ((ptr = list_head( &queue->pending_timers )))
1177 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1178 queue->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, queue );
1180 /* set/clear QS_TIMER bit */
1181 if (list_empty( &queue->expired_timers ))
1182 clear_queue_bits( queue, QS_TIMER );
1183 else
1184 set_queue_bits( queue, QS_TIMER );
1187 /* find a timer from its window and id */
1188 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1189 unsigned int msg, lparam_t id )
1191 struct list *ptr;
1193 /* we need to search both lists */
1195 LIST_FOR_EACH( ptr, &queue->pending_timers )
1197 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1198 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1200 LIST_FOR_EACH( ptr, &queue->expired_timers )
1202 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1203 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1205 return NULL;
1208 /* callback for the next timer expiration */
1209 static void timer_callback( void *private )
1211 struct msg_queue *queue = private;
1212 struct list *ptr;
1214 queue->timeout = NULL;
1215 /* move on to the next timer */
1216 ptr = list_head( &queue->pending_timers );
1217 list_remove( ptr );
1218 list_add_tail( &queue->expired_timers, ptr );
1219 set_next_timer( queue );
1222 /* link a timer at its rightful place in the queue list */
1223 static void link_timer( struct msg_queue *queue, struct timer *timer )
1225 struct list *ptr;
1227 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1229 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1230 if (t->when <= timer->when) break;
1232 list_add_before( ptr, &timer->entry );
1235 /* remove a timer from the queue timer list and free it */
1236 static void free_timer( struct msg_queue *queue, struct timer *timer )
1238 list_remove( &timer->entry );
1239 free( timer );
1240 set_next_timer( queue );
1243 /* restart an expired timer */
1244 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1246 list_remove( &timer->entry );
1247 while (-timer->when <= monotonic_time) timer->when -= (timeout_t)timer->rate * 10000;
1248 link_timer( queue, timer );
1249 set_next_timer( queue );
1252 /* find an expired timer matching the filtering parameters */
1253 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1254 unsigned int get_first, unsigned int get_last,
1255 int remove )
1257 struct list *ptr;
1259 LIST_FOR_EACH( ptr, &queue->expired_timers )
1261 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1262 if (win && timer->win != win) continue;
1263 if (check_msg_filter( timer->msg, get_first, get_last ))
1265 if (remove) restart_timer( queue, timer );
1266 return timer;
1269 return NULL;
1272 /* add a timer */
1273 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1275 struct timer *timer = mem_alloc( sizeof(*timer) );
1276 if (timer)
1278 timer->rate = max( rate, 1 );
1279 timer->when = -monotonic_time - (timeout_t)timer->rate * 10000;
1280 link_timer( queue, timer );
1281 /* check if we replaced the next timer */
1282 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1284 return timer;
1287 /* change the input key state for a given key */
1288 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1290 if (down)
1292 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1293 keystate[key] |= down;
1295 else keystate[key] &= ~0x80;
1298 /* update the input key state for a keyboard message */
1299 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1300 unsigned int msg, lparam_t wparam )
1302 unsigned char key;
1303 int down = 0;
1305 switch (msg)
1307 case WM_LBUTTONDOWN:
1308 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1309 /* fall through */
1310 case WM_LBUTTONUP:
1311 set_input_key_state( keystate, VK_LBUTTON, down );
1312 break;
1313 case WM_MBUTTONDOWN:
1314 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1315 /* fall through */
1316 case WM_MBUTTONUP:
1317 set_input_key_state( keystate, VK_MBUTTON, down );
1318 break;
1319 case WM_RBUTTONDOWN:
1320 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1321 /* fall through */
1322 case WM_RBUTTONUP:
1323 set_input_key_state( keystate, VK_RBUTTON, down );
1324 break;
1325 case WM_XBUTTONDOWN:
1326 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1327 /* fall through */
1328 case WM_XBUTTONUP:
1329 if (wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1330 else if (wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1331 break;
1332 case WM_KEYDOWN:
1333 case WM_SYSKEYDOWN:
1334 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1335 /* fall through */
1336 case WM_KEYUP:
1337 case WM_SYSKEYUP:
1338 key = (unsigned char)wparam;
1339 set_input_key_state( keystate, key, down );
1340 switch(key)
1342 case VK_LCONTROL:
1343 case VK_RCONTROL:
1344 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1345 set_input_key_state( keystate, VK_CONTROL, down );
1346 break;
1347 case VK_LMENU:
1348 case VK_RMENU:
1349 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1350 set_input_key_state( keystate, VK_MENU, down );
1351 break;
1352 case VK_LSHIFT:
1353 case VK_RSHIFT:
1354 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1355 set_input_key_state( keystate, VK_SHIFT, down );
1356 break;
1358 break;
1362 /* update the desktop key state according to a mouse message flags */
1363 static void update_desktop_mouse_state( struct desktop *desktop, unsigned int flags,
1364 int x, int y, lparam_t wparam )
1366 if (flags & MOUSEEVENTF_MOVE)
1367 update_desktop_cursor_pos( desktop, x, y );
1368 if (flags & MOUSEEVENTF_LEFTDOWN)
1369 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONDOWN, wparam );
1370 if (flags & MOUSEEVENTF_LEFTUP)
1371 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONUP, wparam );
1372 if (flags & MOUSEEVENTF_RIGHTDOWN)
1373 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONDOWN, wparam );
1374 if (flags & MOUSEEVENTF_RIGHTUP)
1375 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONUP, wparam );
1376 if (flags & MOUSEEVENTF_MIDDLEDOWN)
1377 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONDOWN, wparam );
1378 if (flags & MOUSEEVENTF_MIDDLEUP)
1379 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONUP, wparam );
1380 if (flags & MOUSEEVENTF_XDOWN)
1381 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONDOWN, wparam );
1382 if (flags & MOUSEEVENTF_XUP)
1383 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONUP, wparam );
1386 /* release the hardware message currently being processed by the given thread */
1387 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1388 int remove )
1390 struct thread_input *input = queue->input;
1391 struct message *msg;
1393 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1395 if (msg->unique_id == hw_id) break;
1397 if (&msg->entry == &input->msg_list) return; /* not found */
1399 /* clear the queue bit for that message */
1400 if (remove)
1402 struct message *other;
1403 int clr_bit;
1405 clr_bit = get_hardware_msg_bit( msg );
1406 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1408 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1410 clr_bit = 0;
1411 break;
1414 if (clr_bit) clear_queue_bits( queue, clr_bit );
1416 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1417 list_remove( &msg->entry );
1418 free_message( msg );
1422 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1424 struct hotkey *hotkey;
1425 unsigned int modifiers = 0;
1427 if (msg->msg != WM_KEYDOWN) return 0;
1429 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1430 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1431 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1432 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1434 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1436 if (hotkey->vkey != msg->wparam) continue;
1437 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1440 return 0;
1442 found:
1443 msg->type = MSG_POSTED;
1444 msg->win = hotkey->win;
1445 msg->msg = WM_HOTKEY;
1446 msg->wparam = hotkey->id;
1447 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1449 free( msg->data );
1450 msg->data = NULL;
1451 msg->data_size = 0;
1453 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1454 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1455 hotkey->queue->hotkey_count++;
1456 return 1;
1459 /* find the window that should receive a given hardware message */
1460 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1461 struct message *msg, unsigned int *msg_code,
1462 struct thread **thread )
1464 user_handle_t win = 0;
1466 *thread = NULL;
1467 *msg_code = msg->msg;
1468 if (msg->msg == WM_INPUT)
1470 if (!(win = msg->win) && input) win = input->focus;
1472 else if (is_keyboard_msg( msg ))
1474 if (input && !(win = input->focus))
1476 win = input->active;
1477 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1480 else if (!input || !(win = input->capture)) /* mouse message */
1482 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1483 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1485 *thread = window_thread_from_point( win, msg->x, msg->y );
1488 if (!*thread)
1489 *thread = get_window_thread( win );
1490 return win;
1493 static struct rawinput_device_entry *find_rawinput_device( unsigned short usage_page, unsigned short usage )
1495 struct rawinput_device_entry *e;
1497 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
1499 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1500 return e;
1503 return NULL;
1506 static void update_rawinput_device(const struct rawinput_device *device)
1508 struct rawinput_device_entry *e;
1510 if (!(e = find_rawinput_device( device->usage_page, device->usage )))
1512 if (!(e = mem_alloc( sizeof(*e) ))) return;
1513 list_add_tail( &current->process->rawinput_devices, &e->entry );
1516 if (device->flags & RIDEV_REMOVE)
1518 list_remove( &e->entry );
1519 free( e );
1520 return;
1523 e->device = *device;
1524 e->device.target = get_user_full_handle( e->device.target );
1527 /* queue a hardware message into a given thread input */
1528 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1530 user_handle_t win;
1531 struct thread *thread;
1532 struct thread_input *input;
1533 unsigned int msg_code;
1535 update_input_key_state( desktop, desktop->keystate, msg->msg, msg->wparam );
1536 last_input_time = get_tick_count();
1537 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1539 if (is_keyboard_msg( msg ))
1541 if (queue_hotkey_message( desktop, msg )) return;
1542 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1543 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1544 msg->lparam &= ~(KF_EXTENDED << 16);
1546 else if (msg->msg != WM_INPUT)
1548 if (msg->msg == WM_MOUSEMOVE && update_desktop_cursor_pos( desktop, msg->x, msg->y )) always_queue = 1;
1549 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1550 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1551 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1552 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1553 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1554 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1555 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1557 msg->x = desktop->cursor.x;
1558 msg->y = desktop->cursor.y;
1560 if (msg->win && (thread = get_window_thread( msg->win )))
1562 input = thread->queue->input;
1563 release_object( thread );
1565 else input = desktop->foreground_input;
1567 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1568 if (!win || !thread)
1570 if (input) update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1571 free_message( msg );
1572 return;
1574 input = thread->queue->input;
1576 if (win != desktop->cursor.win) always_queue = 1;
1577 desktop->cursor.win = win;
1579 if (!always_queue || merge_message( input, msg )) free_message( msg );
1580 else
1582 msg->unique_id = 0; /* will be set once we return it to the app */
1583 list_add_tail( &input->msg_list, &msg->entry );
1584 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1586 release_object( thread );
1589 /* send the low-level hook message for a given hardware message */
1590 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1591 const hw_input_t *input, struct msg_queue *sender )
1593 struct thread *hook_thread;
1594 struct msg_queue *queue;
1595 struct message *msg;
1596 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1597 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1599 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1600 if (!(queue = hook_thread->queue)) return 0;
1601 if (is_queue_hung( queue )) return 0;
1603 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1605 msg->type = MSG_HOOK_LL;
1606 msg->win = 0;
1607 msg->msg = id;
1608 msg->wparam = hardware_msg->msg;
1609 msg->x = hardware_msg->x;
1610 msg->y = hardware_msg->y;
1611 msg->time = hardware_msg->time;
1612 msg->data_size = hardware_msg->data_size;
1613 msg->result = NULL;
1615 if (input->type == INPUT_KEYBOARD)
1617 unsigned short vkey = input->kbd.vkey;
1618 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1619 msg->lparam = (input->kbd.scan << 16) | vkey;
1621 else msg->lparam = input->mouse.data << 16;
1623 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1624 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1626 free_message( msg );
1627 return 0;
1629 msg->result->hardware_msg = hardware_msg;
1630 msg->result->desktop = (struct desktop *)grab_object( desktop );
1631 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1632 set_queue_bits( queue, QS_SENDMESSAGE );
1633 return 1;
1636 /* get the foreground thread for a desktop and a window receiving input */
1637 static struct thread *get_foreground_thread( struct desktop *desktop, user_handle_t window )
1639 /* if desktop has no foreground process, assume the receiving window is */
1640 if (desktop->foreground_input) return get_window_thread( desktop->foreground_input->focus );
1641 if (window) return get_window_thread( window );
1642 return NULL;
1645 struct rawinput_message
1647 struct thread *foreground;
1648 struct desktop *desktop;
1649 struct hw_msg_source source;
1650 unsigned int time;
1651 struct hardware_msg_data data;
1654 /* check if process is supposed to receive a WM_INPUT message and eventually queue it */
1655 static int queue_rawinput_message( struct process* process, void *arg )
1657 const struct rawinput_message* raw_msg = arg;
1658 const struct rawinput_device *device = NULL;
1659 struct desktop *target_desktop = NULL;
1660 struct thread *target_thread = NULL;
1661 struct message *msg;
1662 int wparam = RIM_INPUT;
1664 if (raw_msg->data.rawinput.type == RIM_TYPEMOUSE)
1665 device = process->rawinput_mouse;
1666 else if (raw_msg->data.rawinput.type == RIM_TYPEKEYBOARD)
1667 device = process->rawinput_kbd;
1668 if (!device) return 0;
1670 if (process != raw_msg->foreground->process)
1672 if (!(device->flags & RIDEV_INPUTSINK)) goto done;
1673 if (!(target_thread = get_window_thread( device->target ))) goto done;
1674 if (!(target_desktop = get_thread_desktop( target_thread, 0 ))) goto done;
1675 if (target_desktop != raw_msg->desktop) goto done;
1676 wparam = RIM_INPUTSINK;
1679 if (!(msg = alloc_hardware_message( raw_msg->data.info, raw_msg->source, raw_msg->time )))
1680 goto done;
1682 msg->win = device->target;
1683 msg->msg = WM_INPUT;
1684 msg->wparam = wparam;
1685 msg->lparam = 0;
1686 memcpy( msg->data, &raw_msg->data, sizeof(raw_msg->data) );
1688 queue_hardware_message( raw_msg->desktop, msg, 1 );
1690 done:
1691 if (target_thread) release_object( target_thread );
1692 if (target_desktop) release_object( target_desktop );
1693 return 0;
1696 /* queue a hardware message for a mouse event */
1697 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1698 unsigned int origin, struct msg_queue *sender )
1700 const struct rawinput_device *device;
1701 struct hardware_msg_data *msg_data;
1702 struct rawinput_message raw_msg;
1703 struct message *msg;
1704 struct thread *foreground;
1705 unsigned int i, time, flags;
1706 struct hw_msg_source source = { IMDT_MOUSE, origin };
1707 int wait = 0, x, y;
1709 static const unsigned int messages[] =
1711 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1712 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1713 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1714 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1715 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1716 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1717 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1718 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1719 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1720 0, /* 0x0200 = unused */
1721 0, /* 0x0400 = unused */
1722 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1723 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1726 desktop->cursor.last_change = get_tick_count();
1727 flags = input->mouse.flags;
1728 time = input->mouse.time;
1729 if (!time) time = desktop->cursor.last_change;
1731 if (flags & MOUSEEVENTF_MOVE)
1733 if (flags & MOUSEEVENTF_ABSOLUTE)
1735 x = input->mouse.x;
1736 y = input->mouse.y;
1737 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1738 x == desktop->cursor.x && y == desktop->cursor.y)
1739 flags &= ~MOUSEEVENTF_MOVE;
1741 else
1743 x = desktop->cursor.x + input->mouse.x;
1744 y = desktop->cursor.y + input->mouse.y;
1747 else
1749 x = desktop->cursor.x;
1750 y = desktop->cursor.y;
1753 if ((foreground = get_foreground_thread( desktop, win )))
1755 raw_msg.foreground = foreground;
1756 raw_msg.desktop = desktop;
1757 raw_msg.source = source;
1758 raw_msg.time = time;
1760 msg_data = &raw_msg.data;
1761 msg_data->info = input->mouse.info;
1762 msg_data->flags = flags;
1763 msg_data->rawinput.type = RIM_TYPEMOUSE;
1764 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1765 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1766 msg_data->rawinput.mouse.data = input->mouse.data;
1768 enum_processes( queue_rawinput_message, &raw_msg );
1769 release_object( foreground );
1772 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
1774 update_desktop_mouse_state( desktop, flags, x, y, input->mouse.data << 16 );
1775 return 0;
1778 for (i = 0; i < ARRAY_SIZE( messages ); i++)
1780 if (!messages[i]) continue;
1781 if (!(flags & (1 << i))) continue;
1782 flags &= ~(1 << i);
1784 if (!(msg = alloc_hardware_message( input->mouse.info, source, time ))) return 0;
1785 msg_data = msg->data;
1787 msg->win = get_user_full_handle( win );
1788 msg->msg = messages[i];
1789 msg->wparam = input->mouse.data << 16;
1790 msg->lparam = 0;
1791 msg->x = x;
1792 msg->y = y;
1793 if (origin == IMO_INJECTED) msg_data->flags = LLMHF_INJECTED;
1795 /* specify a sender only when sending the last message */
1796 if (!(flags & ((1 << ARRAY_SIZE( messages )) - 1)))
1798 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1799 queue_hardware_message( desktop, msg, 0 );
1801 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1802 queue_hardware_message( desktop, msg, 0 );
1804 return wait;
1807 /* queue a hardware message for a keyboard event */
1808 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1809 unsigned int origin, struct msg_queue *sender )
1811 struct hw_msg_source source = { IMDT_KEYBOARD, origin };
1812 const struct rawinput_device *device;
1813 struct hardware_msg_data *msg_data;
1814 struct rawinput_message raw_msg;
1815 struct message *msg;
1816 struct thread *foreground;
1817 unsigned char vkey = input->kbd.vkey;
1818 unsigned int message_code, time;
1819 int wait;
1821 if (!(time = input->kbd.time)) time = get_tick_count();
1823 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1825 switch (vkey)
1827 case VK_MENU:
1828 case VK_LMENU:
1829 case VK_RMENU:
1830 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1831 break;
1832 case VK_CONTROL:
1833 case VK_LCONTROL:
1834 case VK_RCONTROL:
1835 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1836 break;
1837 case VK_SHIFT:
1838 case VK_LSHIFT:
1839 case VK_RSHIFT:
1840 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1841 break;
1845 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1846 switch (vkey)
1848 case VK_LMENU:
1849 case VK_RMENU:
1850 if (input->kbd.flags & KEYEVENTF_KEYUP)
1852 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1853 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1854 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1855 message_code = WM_SYSKEYUP;
1856 desktop->keystate[VK_MENU] &= ~0x02;
1858 else
1860 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1861 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1862 message_code = WM_SYSKEYDOWN;
1863 desktop->keystate[VK_MENU] |= 0x02;
1865 break;
1867 case VK_LCONTROL:
1868 case VK_RCONTROL:
1869 /* send WM_SYSKEYUP on release if Alt still pressed */
1870 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1871 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1872 message_code = WM_SYSKEYUP;
1873 desktop->keystate[VK_MENU] &= ~0x02;
1874 break;
1876 default:
1877 /* send WM_SYSKEY for Alt-anykey and for F10 */
1878 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1879 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1880 /* fall through */
1881 case VK_F10:
1882 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1883 desktop->keystate[VK_MENU] &= ~0x02;
1884 break;
1887 if ((foreground = get_foreground_thread( desktop, win )))
1889 raw_msg.foreground = foreground;
1890 raw_msg.desktop = desktop;
1891 raw_msg.source = source;
1892 raw_msg.time = time;
1894 msg_data = &raw_msg.data;
1895 msg_data->info = input->kbd.info;
1896 msg_data->flags = input->kbd.flags;
1897 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1898 msg_data->rawinput.kbd.message = message_code;
1899 msg_data->rawinput.kbd.vkey = vkey;
1900 msg_data->rawinput.kbd.scan = input->kbd.scan;
1902 enum_processes( queue_rawinput_message, &raw_msg );
1903 release_object( foreground );
1906 if ((device = current->process->rawinput_kbd) && (device->flags & RIDEV_NOLEGACY))
1908 update_input_key_state( desktop, desktop->keystate, message_code, vkey );
1909 return 0;
1912 if (!(msg = alloc_hardware_message( input->kbd.info, source, time ))) return 0;
1913 msg_data = msg->data;
1915 msg->win = get_user_full_handle( win );
1916 msg->msg = message_code;
1917 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1918 if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
1920 if (input->kbd.flags & KEYEVENTF_UNICODE && !vkey)
1922 msg->wparam = VK_PACKET;
1924 else
1926 unsigned int flags = 0;
1927 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1928 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1929 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1930 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1932 msg->wparam = vkey;
1933 msg->lparam |= flags << 16;
1934 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1937 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1938 queue_hardware_message( desktop, msg, 1 );
1940 return wait;
1943 /* queue a hardware message for a custom type of event */
1944 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1945 unsigned int origin, const hw_input_t *input )
1947 struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
1948 struct message *msg;
1950 if (!(msg = alloc_hardware_message( 0, source, get_tick_count() ))) return;
1952 msg->win = get_user_full_handle( win );
1953 msg->msg = input->hw.msg;
1954 msg->wparam = 0;
1955 msg->lparam = input->hw.lparam;
1956 msg->x = desktop->cursor.x;
1957 msg->y = desktop->cursor.y;
1959 queue_hardware_message( desktop, msg, 1 );
1962 /* check message filter for a hardware message */
1963 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1964 user_handle_t filter_win, unsigned int first, unsigned int last )
1966 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1968 /* we can only test the window for a keyboard message since the
1969 * dest window for a mouse message depends on hittest */
1970 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1971 return 0;
1972 /* the message code is final for a keyboard message, we can simply check it */
1973 return check_msg_filter( msg_code, first, last );
1975 else /* mouse message */
1977 /* we need to check all possible values that the message can have in the end */
1979 if (check_msg_filter( msg_code, first, last )) return 1;
1980 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1982 /* all other messages can become non-client messages */
1983 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1985 /* clicks can become double-clicks or non-client double-clicks */
1986 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1987 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1989 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1990 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1992 return 0;
1997 /* find a hardware message for the given queue */
1998 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1999 unsigned int first, unsigned int last, unsigned int flags,
2000 struct get_message_reply *reply )
2002 struct thread_input *input = thread->queue->input;
2003 struct thread *win_thread;
2004 struct list *ptr;
2005 user_handle_t win;
2006 int clear_bits, got_one = 0;
2007 unsigned int msg_code;
2009 ptr = list_head( &input->msg_list );
2010 if (hw_id)
2012 while (ptr)
2014 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2015 if (msg->unique_id == hw_id) break;
2016 ptr = list_next( &input->msg_list, ptr );
2018 if (!ptr) ptr = list_head( &input->msg_list );
2019 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
2022 if (ptr == list_head( &input->msg_list ))
2023 clear_bits = QS_INPUT;
2024 else
2025 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
2027 while (ptr)
2029 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2030 struct hardware_msg_data *data = msg->data;
2032 ptr = list_next( &input->msg_list, ptr );
2033 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
2034 if (!win || !win_thread)
2036 /* no window at all, remove it */
2037 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2038 list_remove( &msg->entry );
2039 free_message( msg );
2040 continue;
2042 if (win_thread != thread)
2044 if (win_thread->queue->input == input)
2046 /* wake the other thread */
2047 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
2048 got_one = 1;
2050 else
2052 /* for another thread input, drop it */
2053 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2054 list_remove( &msg->entry );
2055 free_message( msg );
2057 release_object( win_thread );
2058 continue;
2060 release_object( win_thread );
2062 /* if we already got a message for another thread, or if it doesn't
2063 * match the filter we skip it */
2064 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
2066 clear_bits &= ~get_hardware_msg_bit( msg );
2067 continue;
2069 /* now we can return it */
2070 if (!msg->unique_id) msg->unique_id = get_unique_id();
2071 reply->type = MSG_HARDWARE;
2072 reply->win = win;
2073 reply->msg = msg_code;
2074 reply->wparam = msg->wparam;
2075 reply->lparam = msg->lparam;
2076 reply->x = msg->x;
2077 reply->y = msg->y;
2078 reply->time = msg->time;
2080 data->hw_id = msg->unique_id;
2081 set_reply_data( msg->data, msg->data_size );
2082 if (msg->msg == WM_INPUT && (flags & PM_REMOVE))
2083 release_hardware_message( current->queue, data->hw_id, 1 );
2084 return 1;
2086 /* nothing found, clear the hardware queue bits */
2087 clear_queue_bits( thread->queue, clear_bits );
2088 return 0;
2091 /* increment (or decrement if 'incr' is negative) the queue paint count */
2092 void inc_queue_paint_count( struct thread *thread, int incr )
2094 struct msg_queue *queue = thread->queue;
2096 assert( queue );
2098 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2100 if (queue->paint_count)
2101 set_queue_bits( queue, QS_PAINT );
2102 else
2103 clear_queue_bits( queue, QS_PAINT );
2107 /* remove all messages and timers belonging to a certain window */
2108 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2110 struct msg_queue *queue = thread->queue;
2111 struct list *ptr;
2112 int i;
2114 if (!queue) return;
2116 /* remove timers */
2118 ptr = list_head( &queue->pending_timers );
2119 while (ptr)
2121 struct list *next = list_next( &queue->pending_timers, ptr );
2122 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2123 if (timer->win == win) free_timer( queue, timer );
2124 ptr = next;
2126 ptr = list_head( &queue->expired_timers );
2127 while (ptr)
2129 struct list *next = list_next( &queue->expired_timers, ptr );
2130 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2131 if (timer->win == win) free_timer( queue, timer );
2132 ptr = next;
2135 /* remove messages */
2136 for (i = 0; i < NB_MSG_KINDS; i++)
2138 struct list *ptr, *next;
2140 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2142 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2143 if (msg->win == win)
2145 if (msg->msg == WM_QUIT && !queue->quit_message)
2147 queue->quit_message = 1;
2148 queue->exit_code = msg->wparam;
2150 remove_queue_message( queue, msg, i );
2155 thread_input_cleanup_window( queue, win );
2158 /* post a message to a window */
2159 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2161 struct message *msg;
2162 struct thread *thread = get_window_thread( win );
2164 if (!thread) return;
2166 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2168 msg->type = MSG_POSTED;
2169 msg->win = get_user_full_handle( win );
2170 msg->msg = message;
2171 msg->wparam = wparam;
2172 msg->lparam = lparam;
2173 msg->result = NULL;
2174 msg->data = NULL;
2175 msg->data_size = 0;
2177 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2179 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2180 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2181 if (message == WM_HOTKEY)
2183 set_queue_bits( thread->queue, QS_HOTKEY );
2184 thread->queue->hotkey_count++;
2187 release_object( thread );
2190 /* send a notify message to a window */
2191 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2193 struct message *msg;
2194 struct thread *thread = get_window_thread( win );
2196 if (!thread) return;
2198 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2200 msg->type = MSG_NOTIFY;
2201 msg->win = get_user_full_handle( win );
2202 msg->msg = message;
2203 msg->wparam = wparam;
2204 msg->lparam = lparam;
2205 msg->result = NULL;
2206 msg->data = NULL;
2207 msg->data_size = 0;
2209 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2211 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2212 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2214 release_object( thread );
2217 /* post a win event */
2218 void post_win_event( struct thread *thread, unsigned int event,
2219 user_handle_t win, unsigned int object_id,
2220 unsigned int child_id, client_ptr_t hook_proc,
2221 const WCHAR *module, data_size_t module_size,
2222 user_handle_t hook)
2224 struct message *msg;
2226 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2228 struct winevent_msg_data *data;
2230 msg->type = MSG_WINEVENT;
2231 msg->win = get_user_full_handle( win );
2232 msg->msg = event;
2233 msg->wparam = object_id;
2234 msg->lparam = child_id;
2235 msg->time = get_tick_count();
2236 msg->result = NULL;
2238 if ((data = malloc( sizeof(*data) + module_size )))
2240 data->hook = hook;
2241 data->tid = get_thread_id( current );
2242 data->hook_proc = hook_proc;
2243 memcpy( data + 1, module, module_size );
2245 msg->data = data;
2246 msg->data_size = sizeof(*data) + module_size;
2248 if (debug_level > 1)
2249 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2250 get_thread_id(thread), event, win, object_id, child_id );
2251 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2252 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2254 else
2255 free( msg );
2259 /* free all hotkeys on a desktop, optionally filtering by window */
2260 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2262 struct hotkey *hotkey, *hotkey2;
2264 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2266 if (!window || hotkey->win == window)
2268 list_remove( &hotkey->entry );
2269 free( hotkey );
2275 /* check if the thread owning the window is hung */
2276 DECL_HANDLER(is_window_hung)
2278 struct thread *thread;
2280 thread = get_window_thread( req->win );
2281 if (thread)
2283 reply->is_hung = is_queue_hung( thread->queue );
2284 release_object( thread );
2286 else reply->is_hung = 0;
2290 /* get the message queue of the current thread */
2291 DECL_HANDLER(get_msg_queue)
2293 struct msg_queue *queue = get_current_queue();
2295 reply->handle = 0;
2296 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2300 /* set the file descriptor associated to the current thread queue */
2301 DECL_HANDLER(set_queue_fd)
2303 struct msg_queue *queue = get_current_queue();
2304 struct file *file;
2305 int unix_fd;
2307 if (queue->fd) /* fd can only be set once */
2309 set_error( STATUS_ACCESS_DENIED );
2310 return;
2312 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2314 if ((unix_fd = get_file_unix_fd( file )) != -1)
2316 if ((unix_fd = dup( unix_fd )) != -1)
2317 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2318 else
2319 file_set_error();
2321 release_object( file );
2325 /* set the current message queue wakeup mask */
2326 DECL_HANDLER(set_queue_mask)
2328 struct msg_queue *queue = get_current_queue();
2330 if (queue)
2332 queue->wake_mask = req->wake_mask;
2333 queue->changed_mask = req->changed_mask;
2334 reply->wake_bits = queue->wake_bits;
2335 reply->changed_bits = queue->changed_bits;
2336 if (is_signaled( queue ))
2338 /* if skip wait is set, do what would have been done in the subsequent wait */
2339 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2340 else wake_up( &queue->obj, 0 );
2346 /* get the current message queue status */
2347 DECL_HANDLER(get_queue_status)
2349 struct msg_queue *queue = current->queue;
2350 if (queue)
2352 reply->wake_bits = queue->wake_bits;
2353 reply->changed_bits = queue->changed_bits;
2354 queue->changed_bits &= ~req->clear_bits;
2356 else reply->wake_bits = reply->changed_bits = 0;
2360 /* send a message to a thread queue */
2361 DECL_HANDLER(send_message)
2363 struct message *msg;
2364 struct msg_queue *send_queue = get_current_queue();
2365 struct msg_queue *recv_queue = NULL;
2366 struct thread *thread = NULL;
2368 if (!(thread = get_thread_from_id( req->id ))) return;
2370 if (!(recv_queue = thread->queue))
2372 set_error( STATUS_INVALID_PARAMETER );
2373 release_object( thread );
2374 return;
2376 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2378 set_error( STATUS_TIMEOUT );
2379 release_object( thread );
2380 return;
2383 if ((msg = mem_alloc( sizeof(*msg) )))
2385 msg->type = req->type;
2386 msg->win = get_user_full_handle( req->win );
2387 msg->msg = req->msg;
2388 msg->wparam = req->wparam;
2389 msg->lparam = req->lparam;
2390 msg->result = NULL;
2391 msg->data = NULL;
2392 msg->data_size = get_req_data_size();
2394 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2396 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2398 free( msg );
2399 release_object( thread );
2400 return;
2403 switch(msg->type)
2405 case MSG_OTHER_PROCESS:
2406 case MSG_ASCII:
2407 case MSG_UNICODE:
2408 case MSG_CALLBACK:
2409 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2411 free_message( msg );
2412 break;
2414 /* fall through */
2415 case MSG_NOTIFY:
2416 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2417 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2418 break;
2419 case MSG_POSTED:
2420 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2421 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2422 if (msg->msg == WM_HOTKEY)
2424 set_queue_bits( recv_queue, QS_HOTKEY );
2425 recv_queue->hotkey_count++;
2427 break;
2428 case MSG_HARDWARE: /* should use send_hardware_message instead */
2429 case MSG_CALLBACK_RESULT: /* cannot send this one */
2430 case MSG_HOOK_LL: /* generated internally */
2431 default:
2432 set_error( STATUS_INVALID_PARAMETER );
2433 free( msg );
2434 break;
2437 release_object( thread );
2440 /* send a hardware message to a thread queue */
2441 DECL_HANDLER(send_hardware_message)
2443 struct thread *thread = NULL;
2444 struct desktop *desktop;
2445 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2446 struct msg_queue *sender = get_current_queue();
2447 data_size_t size = min( 256, get_reply_max_size() );
2449 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2451 if (req->win)
2453 if (!(thread = get_window_thread( req->win ))) return;
2454 if (desktop != thread->queue->input->desktop)
2456 /* don't allow queuing events to a different desktop */
2457 release_object( desktop );
2458 return;
2462 reply->prev_x = desktop->cursor.x;
2463 reply->prev_y = desktop->cursor.y;
2465 switch (req->input.type)
2467 case INPUT_MOUSE:
2468 reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2469 break;
2470 case INPUT_KEYBOARD:
2471 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2472 break;
2473 case INPUT_HARDWARE:
2474 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2475 break;
2476 default:
2477 set_error( STATUS_INVALID_PARAMETER );
2479 if (thread) release_object( thread );
2481 reply->new_x = desktop->cursor.x;
2482 reply->new_y = desktop->cursor.y;
2483 set_reply_data( desktop->keystate, size );
2484 release_object( desktop );
2487 /* post a quit message to the current queue */
2488 DECL_HANDLER(post_quit_message)
2490 struct msg_queue *queue = get_current_queue();
2492 if (!queue)
2493 return;
2495 queue->quit_message = 1;
2496 queue->exit_code = req->exit_code;
2497 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2500 /* get a message from the current queue */
2501 DECL_HANDLER(get_message)
2503 struct timer *timer;
2504 struct list *ptr;
2505 struct msg_queue *queue = get_current_queue();
2506 user_handle_t get_win = get_user_full_handle( req->get_win );
2507 unsigned int filter = req->flags >> 16;
2509 reply->active_hooks = get_active_hooks();
2511 if (get_win && get_win != 1 && get_win != -1 && !get_user_object( get_win, USER_WINDOW ))
2513 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2514 return;
2517 if (!queue) return;
2518 queue->last_get_msg = current_time;
2519 if (!filter) filter = QS_ALLINPUT;
2521 /* first check for sent messages */
2522 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2524 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2525 receive_message( queue, msg, reply );
2526 return;
2529 /* clear changed bits so we can wait on them if we don't find a message */
2530 if (filter & QS_POSTMESSAGE)
2532 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2533 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2535 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2536 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2538 /* then check for posted messages */
2539 if ((filter & QS_POSTMESSAGE) &&
2540 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2541 return;
2543 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2544 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2545 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2546 return;
2548 /* only check for quit messages if not posted messages pending */
2549 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2550 return;
2552 /* then check for any raw hardware message */
2553 if ((filter & QS_INPUT) &&
2554 filter_contains_hw_range( req->get_first, req->get_last ) &&
2555 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2556 return;
2558 /* now check for WM_PAINT */
2559 if ((filter & QS_PAINT) &&
2560 queue->paint_count &&
2561 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2562 (reply->win = find_window_to_repaint( get_win, current )))
2564 reply->type = MSG_POSTED;
2565 reply->msg = WM_PAINT;
2566 reply->wparam = 0;
2567 reply->lparam = 0;
2568 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2569 return;
2572 /* now check for timer */
2573 if ((filter & QS_TIMER) &&
2574 (timer = find_expired_timer( queue, get_win, req->get_first,
2575 req->get_last, (req->flags & PM_REMOVE) )))
2577 reply->type = MSG_POSTED;
2578 reply->win = timer->win;
2579 reply->msg = timer->msg;
2580 reply->wparam = timer->id;
2581 reply->lparam = timer->lparam;
2582 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2583 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2584 set_event( current->process->idle_event );
2585 return;
2588 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2589 queue->wake_mask = req->wake_mask;
2590 queue->changed_mask = req->changed_mask;
2591 set_error( STATUS_PENDING ); /* FIXME */
2595 /* reply to a sent message */
2596 DECL_HANDLER(reply_message)
2598 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2599 else if (current->queue->recv_result)
2600 reply_message( current->queue, req->result, 0, req->remove,
2601 get_req_data(), get_req_data_size() );
2605 /* accept the current hardware message */
2606 DECL_HANDLER(accept_hardware_message)
2608 if (current->queue)
2609 release_hardware_message( current->queue, req->hw_id, req->remove );
2610 else
2611 set_error( STATUS_ACCESS_DENIED );
2615 /* retrieve the reply for the last message sent */
2616 DECL_HANDLER(get_message_reply)
2618 struct message_result *result;
2619 struct list *entry;
2620 struct msg_queue *queue = current->queue;
2622 if (queue)
2624 set_error( STATUS_PENDING );
2625 reply->result = 0;
2627 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2629 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2630 if (result->replied || req->cancel)
2632 if (result->replied)
2634 reply->result = result->result;
2635 set_error( result->error );
2636 if (result->data)
2638 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2639 set_reply_data_ptr( result->data, data_len );
2640 result->data = NULL;
2641 result->data_size = 0;
2644 remove_result_from_sender( result );
2646 entry = list_head( &queue->send_result );
2647 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2648 else
2650 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2651 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2652 else clear_queue_bits( queue, QS_SMRESULT );
2656 else set_error( STATUS_ACCESS_DENIED );
2660 /* set a window timer */
2661 DECL_HANDLER(set_win_timer)
2663 struct timer *timer;
2664 struct msg_queue *queue;
2665 struct thread *thread = NULL;
2666 user_handle_t win = 0;
2667 lparam_t id = req->id;
2669 if (req->win)
2671 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2673 set_error( STATUS_INVALID_HANDLE );
2674 return;
2676 if (thread->process != current->process)
2678 release_object( thread );
2679 set_error( STATUS_ACCESS_DENIED );
2680 return;
2682 queue = thread->queue;
2683 /* remove it if it existed already */
2684 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2686 else
2688 queue = get_current_queue();
2689 /* look for a timer with this id */
2690 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2692 /* free and reuse id */
2693 free_timer( queue, timer );
2695 else
2697 lparam_t end_id = queue->next_timer_id;
2699 /* find a free id for it */
2700 while (1)
2702 id = queue->next_timer_id;
2703 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2705 if (!find_timer( queue, 0, req->msg, id )) break;
2706 if (queue->next_timer_id == end_id)
2708 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2709 return;
2715 if ((timer = set_timer( queue, req->rate )))
2717 timer->win = win;
2718 timer->msg = req->msg;
2719 timer->id = id;
2720 timer->lparam = req->lparam;
2721 reply->id = id;
2723 if (thread) release_object( thread );
2726 /* kill a window timer */
2727 DECL_HANDLER(kill_win_timer)
2729 struct timer *timer;
2730 struct thread *thread;
2731 user_handle_t win = 0;
2733 if (req->win)
2735 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2737 set_error( STATUS_INVALID_HANDLE );
2738 return;
2740 if (thread->process != current->process)
2742 release_object( thread );
2743 set_error( STATUS_ACCESS_DENIED );
2744 return;
2747 else thread = (struct thread *)grab_object( current );
2749 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2750 free_timer( thread->queue, timer );
2751 else
2752 set_error( STATUS_INVALID_PARAMETER );
2754 release_object( thread );
2757 DECL_HANDLER(register_hotkey)
2759 struct desktop *desktop;
2760 user_handle_t win_handle = req->window;
2761 struct hotkey *hotkey;
2762 struct hotkey *new_hotkey = NULL;
2763 struct thread *thread;
2764 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2766 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2768 if (win_handle)
2770 if (!(win_handle = get_valid_window_handle( win_handle )))
2772 release_object( desktop );
2773 return;
2776 thread = get_window_thread( win_handle );
2777 if (thread) release_object( thread );
2779 if (thread != current)
2781 release_object( desktop );
2782 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2783 return;
2787 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2789 if (req->vkey == hotkey->vkey &&
2790 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2792 release_object( desktop );
2793 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2794 return;
2796 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2797 new_hotkey = hotkey;
2800 if (new_hotkey)
2802 reply->replaced = 1;
2803 reply->flags = new_hotkey->flags;
2804 reply->vkey = new_hotkey->vkey;
2806 else
2808 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2809 if (new_hotkey)
2811 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2812 new_hotkey->queue = current->queue;
2813 new_hotkey->win = win_handle;
2814 new_hotkey->id = req->id;
2818 if (new_hotkey)
2820 new_hotkey->flags = req->flags;
2821 new_hotkey->vkey = req->vkey;
2824 release_object( desktop );
2827 DECL_HANDLER(unregister_hotkey)
2829 struct desktop *desktop;
2830 user_handle_t win_handle = req->window;
2831 struct hotkey *hotkey;
2832 struct thread *thread;
2834 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2836 if (win_handle)
2838 if (!(win_handle = get_valid_window_handle( win_handle )))
2840 release_object( desktop );
2841 return;
2844 thread = get_window_thread( win_handle );
2845 if (thread) release_object( thread );
2847 if (thread != current)
2849 release_object( desktop );
2850 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2851 return;
2855 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2857 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2858 goto found;
2861 release_object( desktop );
2862 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2863 return;
2865 found:
2866 reply->flags = hotkey->flags;
2867 reply->vkey = hotkey->vkey;
2868 list_remove( &hotkey->entry );
2869 free( hotkey );
2870 release_object( desktop );
2873 /* attach (or detach) thread inputs */
2874 DECL_HANDLER(attach_thread_input)
2876 struct thread *thread_from = get_thread_from_id( req->tid_from );
2877 struct thread *thread_to = get_thread_from_id( req->tid_to );
2879 if (!thread_from || !thread_to)
2881 if (thread_from) release_object( thread_from );
2882 if (thread_to) release_object( thread_to );
2883 return;
2885 if (thread_from != thread_to)
2887 if (req->attach)
2889 if ((thread_to->queue || thread_to == current) &&
2890 (thread_from->queue || thread_from == current))
2891 attach_thread_input( thread_from, thread_to );
2892 else
2893 set_error( STATUS_INVALID_PARAMETER );
2895 else
2897 if (thread_from->queue && thread_to->queue &&
2898 thread_from->queue->input == thread_to->queue->input)
2899 detach_thread_input( thread_from );
2900 else
2901 set_error( STATUS_ACCESS_DENIED );
2904 else set_error( STATUS_ACCESS_DENIED );
2905 release_object( thread_from );
2906 release_object( thread_to );
2910 /* get thread input data */
2911 DECL_HANDLER(get_thread_input)
2913 struct thread *thread = NULL;
2914 struct desktop *desktop;
2915 struct thread_input *input;
2917 if (req->tid)
2919 if (!(thread = get_thread_from_id( req->tid ))) return;
2920 if (!(desktop = get_thread_desktop( thread, 0 )))
2922 release_object( thread );
2923 return;
2925 input = thread->queue ? thread->queue->input : NULL;
2927 else
2929 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2930 input = desktop->foreground_input; /* get the foreground thread info */
2933 if (input)
2935 reply->focus = input->focus;
2936 reply->capture = input->capture;
2937 reply->active = input->active;
2938 reply->menu_owner = input->menu_owner;
2939 reply->move_size = input->move_size;
2940 reply->caret = input->caret;
2941 reply->cursor = input->cursor;
2942 reply->show_count = input->cursor_count;
2943 reply->rect = input->caret_rect;
2946 /* foreground window is active window of foreground thread */
2947 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2948 if (thread) release_object( thread );
2949 release_object( desktop );
2953 /* retrieve queue keyboard state for a given thread */
2954 DECL_HANDLER(get_key_state)
2956 struct thread *thread;
2957 struct desktop *desktop;
2958 data_size_t size = min( 256, get_reply_max_size() );
2960 if (!req->tid) /* get global async key state */
2962 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2963 if (req->key >= 0)
2965 reply->state = desktop->keystate[req->key & 0xff];
2966 desktop->keystate[req->key & 0xff] &= ~0x40;
2968 set_reply_data( desktop->keystate, size );
2969 release_object( desktop );
2971 else
2973 unsigned char *keystate;
2974 if (!(thread = get_thread_from_id( req->tid ))) return;
2975 if (thread->queue)
2977 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2978 set_reply_data( thread->queue->input->keystate, size );
2979 release_object( thread );
2980 return;
2982 release_object( thread );
2984 /* fallback to desktop keystate */
2985 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2986 if (req->key >= 0) reply->state = desktop->keystate[req->key & 0xff] & ~0x40;
2987 if ((keystate = set_reply_data_size( size )))
2989 unsigned int i;
2990 for (i = 0; i < size; i++) keystate[i] = desktop->keystate[i] & ~0x40;
2992 release_object( desktop );
2997 /* set queue keyboard state for a given thread */
2998 DECL_HANDLER(set_key_state)
3000 struct thread *thread;
3001 struct desktop *desktop;
3002 data_size_t size = min( 256, get_req_data_size() );
3004 if (!req->tid) /* set global async key state */
3006 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3007 memcpy( desktop->keystate, get_req_data(), size );
3008 release_object( desktop );
3010 else
3012 if (!(thread = get_thread_from_id( req->tid ))) return;
3013 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
3014 if (req->async && (desktop = get_thread_desktop( thread, 0 )))
3016 memcpy( desktop->keystate, get_req_data(), size );
3017 release_object( desktop );
3019 release_object( thread );
3024 /* set the system foreground window */
3025 DECL_HANDLER(set_foreground_window)
3027 struct thread *thread = NULL;
3028 struct desktop *desktop;
3029 struct msg_queue *queue = get_current_queue();
3031 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3032 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
3033 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
3034 reply->send_msg_new = FALSE;
3036 if (is_valid_foreground_window( req->handle ) &&
3037 (thread = get_window_thread( req->handle )) &&
3038 thread->queue->input->desktop == desktop)
3040 set_foreground_input( desktop, thread->queue->input );
3041 reply->send_msg_new = (desktop->foreground_input != queue->input);
3043 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
3045 if (thread) release_object( thread );
3046 release_object( desktop );
3050 /* set the current thread focus window */
3051 DECL_HANDLER(set_focus_window)
3053 struct msg_queue *queue = get_current_queue();
3055 reply->previous = 0;
3056 if (queue && check_queue_input_window( queue, req->handle ))
3058 reply->previous = queue->input->focus;
3059 queue->input->focus = get_user_full_handle( req->handle );
3064 /* set the current thread active window */
3065 DECL_HANDLER(set_active_window)
3067 struct msg_queue *queue = get_current_queue();
3069 reply->previous = 0;
3070 if (queue && check_queue_input_window( queue, req->handle ))
3072 if (!req->handle || make_window_active( req->handle ))
3074 reply->previous = queue->input->active;
3075 queue->input->active = get_user_full_handle( req->handle );
3077 else set_error( STATUS_INVALID_HANDLE );
3082 /* set the current thread capture window */
3083 DECL_HANDLER(set_capture_window)
3085 struct msg_queue *queue = get_current_queue();
3087 reply->previous = reply->full_handle = 0;
3088 if (queue && check_queue_input_window( queue, req->handle ))
3090 struct thread_input *input = queue->input;
3092 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
3093 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
3095 set_error(STATUS_ACCESS_DENIED);
3096 return;
3098 reply->previous = input->capture;
3099 input->capture = get_user_full_handle( req->handle );
3100 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
3101 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
3102 reply->full_handle = input->capture;
3107 /* Set the current thread caret window */
3108 DECL_HANDLER(set_caret_window)
3110 struct msg_queue *queue = get_current_queue();
3112 reply->previous = 0;
3113 if (queue && check_queue_input_window( queue, req->handle ))
3115 struct thread_input *input = queue->input;
3117 reply->previous = input->caret;
3118 reply->old_rect = input->caret_rect;
3119 reply->old_hide = input->caret_hide;
3120 reply->old_state = input->caret_state;
3122 set_caret_window( input, get_user_full_handle(req->handle) );
3123 input->caret_rect.right = input->caret_rect.left + req->width;
3124 input->caret_rect.bottom = input->caret_rect.top + req->height;
3129 /* Set the current thread caret information */
3130 DECL_HANDLER(set_caret_info)
3132 struct msg_queue *queue = get_current_queue();
3133 struct thread_input *input;
3135 if (!queue) return;
3136 input = queue->input;
3137 reply->full_handle = input->caret;
3138 reply->old_rect = input->caret_rect;
3139 reply->old_hide = input->caret_hide;
3140 reply->old_state = input->caret_state;
3142 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3144 set_error( STATUS_ACCESS_DENIED );
3145 return;
3147 if (req->flags & SET_CARET_POS)
3149 input->caret_rect.right += req->x - input->caret_rect.left;
3150 input->caret_rect.bottom += req->y - input->caret_rect.top;
3151 input->caret_rect.left = req->x;
3152 input->caret_rect.top = req->y;
3154 if (req->flags & SET_CARET_HIDE)
3156 input->caret_hide += req->hide;
3157 if (input->caret_hide < 0) input->caret_hide = 0;
3159 if (req->flags & SET_CARET_STATE)
3161 switch (req->state)
3163 case CARET_STATE_OFF: input->caret_state = 0; break;
3164 case CARET_STATE_ON: input->caret_state = 1; break;
3165 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3166 case CARET_STATE_ON_IF_MOVED:
3167 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3168 break;
3174 /* get the time of the last input event */
3175 DECL_HANDLER(get_last_input_time)
3177 reply->time = last_input_time;
3180 /* set/get the current cursor */
3181 DECL_HANDLER(set_cursor)
3183 struct msg_queue *queue = get_current_queue();
3184 struct thread_input *input;
3186 if (!queue) return;
3187 input = queue->input;
3189 reply->prev_handle = input->cursor;
3190 reply->prev_count = input->cursor_count;
3191 reply->prev_x = input->desktop->cursor.x;
3192 reply->prev_y = input->desktop->cursor.y;
3194 if (req->flags & SET_CURSOR_HANDLE)
3196 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3198 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3199 return;
3201 input->cursor = req->handle;
3203 if (req->flags & SET_CURSOR_COUNT)
3205 queue->cursor_count += req->show_count;
3206 input->cursor_count += req->show_count;
3208 if (req->flags & SET_CURSOR_POS)
3210 set_cursor_pos( input->desktop, req->x, req->y );
3212 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3214 struct desktop *desktop = input->desktop;
3216 /* only the desktop owner can set the message */
3217 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3218 desktop->cursor.clip_msg = req->clip_msg;
3220 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip, 0 );
3223 reply->new_x = input->desktop->cursor.x;
3224 reply->new_y = input->desktop->cursor.y;
3225 reply->new_clip = input->desktop->cursor.clip;
3226 reply->last_change = input->desktop->cursor.last_change;
3229 DECL_HANDLER(update_rawinput_devices)
3231 const struct rawinput_device *devices = get_req_data();
3232 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3233 const struct rawinput_device_entry *e;
3234 unsigned int i;
3236 for (i = 0; i < device_count; ++i)
3238 update_rawinput_device(&devices[i]);
3241 e = find_rawinput_device( 1, 2 );
3242 current->process->rawinput_mouse = e ? &e->device : NULL;
3243 e = find_rawinput_device( 1, 6 );
3244 current->process->rawinput_kbd = e ? &e->device : NULL;