crypt32/tests: Avoid "misleading indentation" warnings.
[wine.git] / server / queue.c
blob4f69a082b74ed97869d3bdb6be4c0696f36c6a30
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"
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <poll.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winternl.h"
38 #include "handle.h"
39 #include "file.h"
40 #include "thread.h"
41 #include "process.h"
42 #include "request.h"
43 #include "user.h"
45 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
46 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
48 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
49 #define NB_MSG_KINDS (POST_MESSAGE+1)
52 struct message_result
54 struct list sender_entry; /* entry in sender list */
55 struct message *msg; /* message the result is for */
56 struct message_result *recv_next; /* next in receiver list */
57 struct msg_queue *sender; /* sender queue */
58 struct msg_queue *receiver; /* receiver queue */
59 int replied; /* has it been replied to? */
60 unsigned int error; /* error code to pass back to sender */
61 lparam_t result; /* reply result */
62 struct message *hardware_msg; /* hardware message if low-level hook result */
63 struct desktop *desktop; /* desktop for hardware message */
64 struct message *callback_msg; /* message to queue for callback */
65 void *data; /* message reply data */
66 unsigned int data_size; /* size of message reply data */
67 struct timeout_user *timeout; /* result timeout */
70 struct message
72 struct list entry; /* entry in message list */
73 enum message_type type; /* message type */
74 user_handle_t win; /* window handle */
75 unsigned int msg; /* message code */
76 lparam_t wparam; /* parameters */
77 lparam_t lparam; /* parameters */
78 int x; /* message position */
79 int y;
80 unsigned int time; /* message time */
81 void *data; /* message data for sent messages */
82 unsigned int data_size; /* size of message data */
83 unsigned int unique_id; /* unique id for nested hw message waits */
84 struct message_result *result; /* result in sender queue */
87 struct timer
89 struct list entry; /* entry in timer list */
90 abstime_t when; /* next expiration */
91 unsigned int rate; /* timer rate in ms */
92 user_handle_t win; /* window handle */
93 unsigned int msg; /* message to post */
94 lparam_t id; /* timer id */
95 lparam_t lparam; /* lparam for message */
98 struct thread_input
100 struct object obj; /* object header */
101 struct desktop *desktop; /* desktop that this thread input belongs to */
102 user_handle_t focus; /* focus window */
103 user_handle_t capture; /* capture window */
104 user_handle_t active; /* active window */
105 user_handle_t menu_owner; /* current menu owner window */
106 user_handle_t move_size; /* current moving/resizing window */
107 user_handle_t caret; /* caret window */
108 rectangle_t caret_rect; /* caret rectangle */
109 int caret_hide; /* caret hide count */
110 int caret_state; /* caret on/off state */
111 user_handle_t cursor; /* current cursor */
112 int cursor_count; /* cursor show count */
113 struct list msg_list; /* list of hardware messages */
114 unsigned char keystate[256]; /* state of each key */
117 struct msg_queue
119 struct object obj; /* object header */
120 struct fd *fd; /* optional file descriptor to poll */
121 unsigned int wake_bits; /* wakeup bits */
122 unsigned int wake_mask; /* wakeup mask */
123 unsigned int changed_bits; /* changed wakeup bits */
124 unsigned int changed_mask; /* changed wakeup mask */
125 int paint_count; /* pending paint messages count */
126 int hotkey_count; /* pending hotkey messages count */
127 int quit_message; /* is there a pending quit message? */
128 int exit_code; /* exit code of pending quit message */
129 int cursor_count; /* per-queue cursor show count */
130 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
131 struct list send_result; /* stack of sent messages waiting for result */
132 struct list callback_result; /* list of callback messages waiting for result */
133 struct message_result *recv_result; /* stack of received messages waiting for result */
134 struct list pending_timers; /* list of pending timers */
135 struct list expired_timers; /* list of expired timers */
136 lparam_t next_timer_id; /* id for the next timer with a 0 window */
137 struct timeout_user *timeout; /* timeout for next timer to expire */
138 struct thread_input *input; /* thread input descriptor */
139 struct hook_table *hooks; /* hook table */
140 timeout_t last_get_msg; /* time of last get message call */
143 struct hotkey
145 struct list entry; /* entry in desktop hotkey list */
146 struct msg_queue *queue; /* queue owning this hotkey */
147 user_handle_t win; /* window handle */
148 int id; /* hotkey id */
149 unsigned int vkey; /* virtual key code */
150 unsigned int flags; /* key modifiers */
153 static void msg_queue_dump( struct object *obj, int verbose );
154 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
155 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
156 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
157 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
158 static void msg_queue_destroy( struct object *obj );
159 static void msg_queue_poll_event( struct fd *fd, int event );
160 static void thread_input_dump( struct object *obj, int verbose );
161 static void thread_input_destroy( struct object *obj );
162 static void timer_callback( void *private );
164 static const struct object_ops msg_queue_ops =
166 sizeof(struct msg_queue), /* size */
167 &no_type, /* type */
168 msg_queue_dump, /* dump */
169 msg_queue_add_queue, /* add_queue */
170 msg_queue_remove_queue, /* remove_queue */
171 msg_queue_signaled, /* signaled */
172 msg_queue_satisfied, /* satisfied */
173 no_signal, /* signal */
174 no_get_fd, /* get_fd */
175 default_map_access, /* map_access */
176 default_get_sd, /* get_sd */
177 default_set_sd, /* set_sd */
178 no_get_full_name, /* get_full_name */
179 no_lookup_name, /* lookup_name */
180 no_link_name, /* link_name */
181 NULL, /* unlink_name */
182 no_open_file, /* open_file */
183 no_kernel_obj_list, /* get_kernel_obj_list */
184 no_close_handle, /* close_handle */
185 msg_queue_destroy /* destroy */
188 static const struct fd_ops msg_queue_fd_ops =
190 NULL, /* get_poll_events */
191 msg_queue_poll_event, /* poll_event */
192 NULL, /* flush */
193 NULL, /* get_fd_type */
194 NULL, /* ioctl */
195 NULL, /* queue_async */
196 NULL, /* reselect_async */
197 NULL /* cancel async */
201 static const struct object_ops thread_input_ops =
203 sizeof(struct thread_input), /* size */
204 &no_type, /* type */
205 thread_input_dump, /* dump */
206 no_add_queue, /* add_queue */
207 NULL, /* remove_queue */
208 NULL, /* signaled */
209 NULL, /* satisfied */
210 no_signal, /* signal */
211 no_get_fd, /* get_fd */
212 default_map_access, /* map_access */
213 default_get_sd, /* get_sd */
214 default_set_sd, /* set_sd */
215 no_get_full_name, /* get_full_name */
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 cursor_pos_t cursor_history[64];
229 static unsigned int cursor_history_latest;
231 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
232 static void free_message( struct message *msg );
234 /* set the caret window in a given thread input */
235 static void set_caret_window( struct thread_input *input, user_handle_t win )
237 if (!win || win != input->caret)
239 input->caret_rect.left = 0;
240 input->caret_rect.top = 0;
241 input->caret_rect.right = 0;
242 input->caret_rect.bottom = 0;
244 input->caret = win;
245 input->caret_hide = 1;
246 input->caret_state = 0;
249 /* create a thread input object */
250 static struct thread_input *create_thread_input( struct thread *thread )
252 struct thread_input *input;
254 if ((input = alloc_object( &thread_input_ops )))
256 input->focus = 0;
257 input->capture = 0;
258 input->active = 0;
259 input->menu_owner = 0;
260 input->move_size = 0;
261 input->cursor = 0;
262 input->cursor_count = 0;
263 list_init( &input->msg_list );
264 set_caret_window( input, 0 );
265 memset( input->keystate, 0, sizeof(input->keystate) );
267 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
269 release_object( input );
270 return NULL;
273 return input;
276 /* create a message queue object */
277 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
279 struct thread_input *new_input = NULL;
280 struct msg_queue *queue;
281 int i;
283 if (!input)
285 if (!(new_input = create_thread_input( thread ))) return NULL;
286 input = new_input;
289 if ((queue = alloc_object( &msg_queue_ops )))
291 queue->fd = NULL;
292 queue->wake_bits = 0;
293 queue->wake_mask = 0;
294 queue->changed_bits = 0;
295 queue->changed_mask = 0;
296 queue->paint_count = 0;
297 queue->hotkey_count = 0;
298 queue->quit_message = 0;
299 queue->cursor_count = 0;
300 queue->recv_result = NULL;
301 queue->next_timer_id = 0x7fff;
302 queue->timeout = NULL;
303 queue->input = (struct thread_input *)grab_object( input );
304 queue->hooks = NULL;
305 queue->last_get_msg = current_time;
306 list_init( &queue->send_result );
307 list_init( &queue->callback_result );
308 list_init( &queue->pending_timers );
309 list_init( &queue->expired_timers );
310 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
312 thread->queue = queue;
314 if (new_input) release_object( new_input );
315 return queue;
318 /* free the message queue of a thread at thread exit */
319 void free_msg_queue( struct thread *thread )
321 remove_thread_hooks( thread );
322 if (!thread->queue) return;
323 release_object( thread->queue );
324 thread->queue = NULL;
327 /* change the thread input data of a given thread */
328 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
330 struct msg_queue *queue = thread->queue;
332 if (!queue)
334 thread->queue = create_msg_queue( thread, new_input );
335 return thread->queue != NULL;
337 if (queue->input)
339 queue->input->cursor_count -= queue->cursor_count;
340 release_object( queue->input );
342 queue->input = (struct thread_input *)grab_object( new_input );
343 new_input->cursor_count += queue->cursor_count;
344 return 1;
347 /* allocate a hardware message and its data */
348 static struct message *alloc_hardware_message( lparam_t info, struct hw_msg_source source,
349 unsigned int time, data_size_t extra_size )
351 struct hardware_msg_data *msg_data;
352 struct message *msg;
354 if (!(msg = mem_alloc( sizeof(*msg) ))) return NULL;
355 if (!(msg_data = mem_alloc( sizeof(*msg_data) + extra_size )))
357 free( msg );
358 return NULL;
360 memset( msg, 0, sizeof(*msg) );
361 msg->type = MSG_HARDWARE;
362 msg->time = time;
363 msg->data = msg_data;
364 msg->data_size = sizeof(*msg_data) + extra_size;
366 memset( msg_data, 0, sizeof(*msg_data) + extra_size );
367 msg_data->info = info;
368 msg_data->size = msg->data_size;
369 msg_data->source = source;
370 return msg;
373 static int update_desktop_cursor_pos( struct desktop *desktop, int x, int y )
375 int updated;
377 x = max( min( x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
378 y = max( min( y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
379 updated = (desktop->cursor.x != x || desktop->cursor.y != y);
380 desktop->cursor.x = x;
381 desktop->cursor.y = y;
382 desktop->cursor.last_change = get_tick_count();
384 return updated;
387 /* set the cursor position and queue the corresponding mouse message */
388 static void set_cursor_pos( struct desktop *desktop, int x, int y )
390 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
391 const struct rawinput_device *device;
392 struct message *msg;
394 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
396 update_desktop_cursor_pos( desktop, x, y );
397 return;
400 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
402 msg->msg = WM_MOUSEMOVE;
403 msg->x = x;
404 msg->y = y;
405 queue_hardware_message( desktop, msg, 1 );
408 /* retrieve default position and time for synthesized messages */
409 static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
411 struct desktop *desktop = queue->input->desktop;
413 *x = desktop->cursor.x;
414 *y = desktop->cursor.y;
415 *time = get_tick_count();
418 /* set the cursor clip rectangle */
419 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect, int send_clip_msg )
421 rectangle_t top_rect;
422 int x, y;
424 get_top_window_rectangle( desktop, &top_rect );
425 if (rect)
427 rectangle_t new_rect = *rect;
428 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
429 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
430 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
431 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
432 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
433 desktop->cursor.clip = new_rect;
435 else desktop->cursor.clip = top_rect;
437 if (desktop->cursor.clip_msg && send_clip_msg)
438 post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
440 /* warp the mouse to be inside the clip rect */
441 x = max( min( desktop->cursor.x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
442 y = max( min( desktop->cursor.y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
443 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
446 /* change the foreground input and reset the cursor clip rect */
447 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
449 if (desktop->foreground_input == input) return;
450 set_clip_rectangle( desktop, NULL, 1 );
451 desktop->foreground_input = input;
454 /* get the hook table for a given thread */
455 struct hook_table *get_queue_hooks( struct thread *thread )
457 if (!thread->queue) return NULL;
458 return thread->queue->hooks;
461 /* set the hook table for a given thread, allocating the queue if needed */
462 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
464 struct msg_queue *queue = thread->queue;
465 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
466 if (queue->hooks) release_object( queue->hooks );
467 queue->hooks = hooks;
470 /* check the queue status */
471 static inline int is_signaled( struct msg_queue *queue )
473 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
476 /* set some queue bits */
477 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
479 queue->wake_bits |= bits;
480 queue->changed_bits |= bits;
481 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
484 /* clear some queue bits */
485 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
487 queue->wake_bits &= ~bits;
488 queue->changed_bits &= ~bits;
491 /* check whether msg is a keyboard message */
492 static inline int is_keyboard_msg( struct message *msg )
494 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
497 /* check if message is matched by the filter */
498 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
500 return (msg >= first && msg <= last);
503 /* check whether a message filter contains at least one potential hardware message */
504 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
506 /* hardware message ranges are (in numerical order):
507 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
508 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
509 * WM_MOUSEFIRST .. WM_MOUSELAST
511 if (last < WM_NCMOUSEFIRST) return 0;
512 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
513 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
514 if (first > WM_MOUSELAST) return 0;
515 return 1;
518 /* get the QS_* bit corresponding to a given hardware message */
519 static inline int get_hardware_msg_bit( struct message *msg )
521 if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
522 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
523 if (is_keyboard_msg( msg )) return QS_KEY;
524 return QS_MOUSEBUTTON;
527 /* get the current thread queue, creating it if needed */
528 static inline struct msg_queue *get_current_queue(void)
530 struct msg_queue *queue = current->queue;
531 if (!queue) queue = create_msg_queue( current, NULL );
532 return queue;
535 /* get a (pseudo-)unique id to tag hardware messages */
536 static inline unsigned int get_unique_id(void)
538 static unsigned int id;
539 if (!++id) id = 1; /* avoid an id of 0 */
540 return id;
543 /* try to merge a message with the last in the list; return 1 if successful */
544 static int merge_message( struct thread_input *input, const struct message *msg )
546 struct message *prev;
547 struct list *ptr;
549 if (msg->msg != WM_MOUSEMOVE) return 0;
550 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
552 prev = LIST_ENTRY( ptr, struct message, entry );
553 if (prev->msg != WM_INPUT) break;
555 if (!ptr) return 0;
556 if (prev->result) return 0;
557 if (prev->win && msg->win && prev->win != msg->win) return 0;
558 if (prev->msg != msg->msg) return 0;
559 if (prev->type != msg->type) return 0;
560 /* now we can merge it */
561 prev->wparam = msg->wparam;
562 prev->lparam = msg->lparam;
563 prev->x = msg->x;
564 prev->y = msg->y;
565 prev->time = msg->time;
566 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
568 struct hardware_msg_data *prev_data = prev->data;
569 struct hardware_msg_data *msg_data = msg->data;
570 prev_data->info = msg_data->info;
572 list_remove( ptr );
573 list_add_tail( &input->msg_list, ptr );
574 return 1;
577 /* free a result structure */
578 static void free_result( struct message_result *result )
580 if (result->timeout) remove_timeout_user( result->timeout );
581 free( result->data );
582 if (result->callback_msg) free_message( result->callback_msg );
583 if (result->hardware_msg) free_message( result->hardware_msg );
584 if (result->desktop) release_object( result->desktop );
585 free( result );
588 /* remove the result from the sender list it is on */
589 static inline void remove_result_from_sender( struct message_result *result )
591 assert( result->sender );
593 list_remove( &result->sender_entry );
594 result->sender = NULL;
595 if (!result->receiver) free_result( result );
598 /* store the message result in the appropriate structure */
599 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
601 res->result = result;
602 res->error = error;
603 res->replied = 1;
604 if (res->timeout)
606 remove_timeout_user( res->timeout );
607 res->timeout = NULL;
610 if (res->hardware_msg)
612 if (!error && result) /* rejected by the hook */
613 free_message( res->hardware_msg );
614 else
615 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
617 res->hardware_msg = NULL;
620 if (res->sender)
622 if (res->callback_msg)
624 /* queue the callback message in the sender queue */
625 struct callback_msg_data *data = res->callback_msg->data;
626 data->result = result;
627 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
628 set_queue_bits( res->sender, QS_SENDMESSAGE );
629 res->callback_msg = NULL;
630 remove_result_from_sender( res );
632 else
634 /* wake sender queue if waiting on this result */
635 if (list_head(&res->sender->send_result) == &res->sender_entry)
636 set_queue_bits( res->sender, QS_SMRESULT );
639 else if (!res->receiver) free_result( res );
642 /* free a message when deleting a queue or window */
643 static void free_message( struct message *msg )
645 struct message_result *result = msg->result;
646 if (result)
648 result->msg = NULL;
649 result->receiver = NULL;
650 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
652 free( msg->data );
653 free( msg );
656 /* remove (and free) a message from a message list */
657 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
658 enum message_kind kind )
660 list_remove( &msg->entry );
661 switch(kind)
663 case SEND_MESSAGE:
664 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
665 break;
666 case POST_MESSAGE:
667 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
668 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
669 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
670 clear_queue_bits( queue, QS_HOTKEY );
671 break;
673 free_message( msg );
676 /* message timed out without getting a reply */
677 static void result_timeout( void *private )
679 struct message_result *result = private;
681 assert( !result->replied );
683 result->timeout = NULL;
685 if (result->msg) /* not received yet */
687 struct message *msg = result->msg;
689 result->msg = NULL;
690 msg->result = NULL;
691 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
692 result->receiver = NULL;
694 store_message_result( result, 0, STATUS_TIMEOUT );
697 /* allocate and fill a message result structure */
698 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
699 struct msg_queue *recv_queue,
700 struct message *msg, timeout_t timeout )
702 struct message_result *result = mem_alloc( sizeof(*result) );
703 if (result)
705 result->msg = msg;
706 result->sender = send_queue;
707 result->receiver = recv_queue;
708 result->replied = 0;
709 result->data = NULL;
710 result->data_size = 0;
711 result->timeout = NULL;
712 result->hardware_msg = NULL;
713 result->desktop = NULL;
714 result->callback_msg = NULL;
716 if (msg->type == MSG_CALLBACK)
718 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
720 if (!callback_msg)
722 free( result );
723 return NULL;
725 callback_msg->type = MSG_CALLBACK_RESULT;
726 callback_msg->win = msg->win;
727 callback_msg->msg = msg->msg;
728 callback_msg->wparam = 0;
729 callback_msg->lparam = 0;
730 callback_msg->time = get_tick_count();
731 callback_msg->result = NULL;
732 /* steal the data from the original message */
733 callback_msg->data = msg->data;
734 callback_msg->data_size = msg->data_size;
735 msg->data = NULL;
736 msg->data_size = 0;
738 result->callback_msg = callback_msg;
739 list_add_head( &send_queue->callback_result, &result->sender_entry );
741 else if (send_queue)
743 list_add_head( &send_queue->send_result, &result->sender_entry );
744 clear_queue_bits( send_queue, QS_SMRESULT );
747 if (timeout != TIMEOUT_INFINITE)
748 result->timeout = add_timeout_user( timeout, result_timeout, result );
750 return result;
753 /* receive a message, removing it from the sent queue */
754 static void receive_message( struct msg_queue *queue, struct message *msg,
755 struct get_message_reply *reply )
757 struct message_result *result = msg->result;
759 reply->total = msg->data_size;
760 if (msg->data_size > get_reply_max_size())
762 set_error( STATUS_BUFFER_OVERFLOW );
763 return;
765 reply->type = msg->type;
766 reply->win = msg->win;
767 reply->msg = msg->msg;
768 reply->wparam = msg->wparam;
769 reply->lparam = msg->lparam;
770 reply->x = msg->x;
771 reply->y = msg->y;
772 reply->time = msg->time;
774 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
776 list_remove( &msg->entry );
777 /* put the result on the receiver result stack */
778 if (result)
780 result->msg = NULL;
781 result->recv_next = queue->recv_result;
782 queue->recv_result = result;
784 free( msg );
785 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
788 /* set the result of the current received message */
789 static void reply_message( struct msg_queue *queue, lparam_t result,
790 unsigned int error, int remove, const void *data, data_size_t len )
792 struct message_result *res = queue->recv_result;
794 if (remove)
796 queue->recv_result = res->recv_next;
797 res->receiver = NULL;
798 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
800 free_result( res );
801 return;
804 if (!res->replied)
806 if (len && (res->data = memdup( data, len ))) res->data_size = len;
807 store_message_result( res, result, error );
811 static int match_window( user_handle_t win, user_handle_t msg_win )
813 if (!win) return 1;
814 if (win == -1 || win == 1) return !msg_win;
815 if (msg_win == win) return 1;
816 return is_child_window( win, msg_win );
819 /* retrieve a posted message */
820 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
821 unsigned int first, unsigned int last, unsigned int flags,
822 struct get_message_reply *reply )
824 struct message *msg;
826 /* check against the filters */
827 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
829 if (!match_window( win, msg->win )) continue;
830 if (!check_msg_filter( msg->msg, first, last )) continue;
831 goto found; /* found one */
833 return 0;
835 /* return it to the app */
836 found:
837 reply->total = msg->data_size;
838 if (msg->data_size > get_reply_max_size())
840 set_error( STATUS_BUFFER_OVERFLOW );
841 return 1;
843 reply->type = msg->type;
844 reply->win = msg->win;
845 reply->msg = msg->msg;
846 reply->wparam = msg->wparam;
847 reply->lparam = msg->lparam;
848 reply->x = msg->x;
849 reply->y = msg->y;
850 reply->time = msg->time;
852 if (flags & PM_REMOVE)
854 if (msg->data)
856 set_reply_data_ptr( msg->data, msg->data_size );
857 msg->data = NULL;
858 msg->data_size = 0;
860 remove_queue_message( queue, msg, POST_MESSAGE );
862 else if (msg->data) set_reply_data( msg->data, msg->data_size );
864 return 1;
867 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
868 struct get_message_reply *reply )
870 if (queue->quit_message)
872 reply->total = 0;
873 reply->type = MSG_POSTED;
874 reply->win = 0;
875 reply->msg = WM_QUIT;
876 reply->wparam = queue->exit_code;
877 reply->lparam = 0;
879 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
881 if (flags & PM_REMOVE)
883 queue->quit_message = 0;
884 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
885 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
887 return 1;
889 else
890 return 0;
893 /* empty a message list and free all the messages */
894 static void empty_msg_list( struct list *list )
896 struct list *ptr;
898 while ((ptr = list_head( list )) != NULL)
900 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
901 list_remove( &msg->entry );
902 free_message( msg );
906 /* cleanup all pending results when deleting a queue */
907 static void cleanup_results( struct msg_queue *queue )
909 struct list *entry;
911 while ((entry = list_head( &queue->send_result )) != NULL)
913 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
916 while ((entry = list_head( &queue->callback_result )) != NULL)
918 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
921 while (queue->recv_result)
922 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
925 /* check if the thread owning the queue is hung (not checking for messages) */
926 static int is_queue_hung( struct msg_queue *queue )
928 struct wait_queue_entry *entry;
930 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
931 return 0; /* less than 5 seconds since last get message -> not hung */
933 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
935 if (get_wait_queue_thread(entry)->queue == queue)
936 return 0; /* thread is waiting on queue -> not hung */
938 return 1;
941 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
943 struct msg_queue *queue = (struct msg_queue *)obj;
944 struct process *process = get_wait_queue_thread(entry)->process;
946 /* a thread can only wait on its own queue */
947 if (get_wait_queue_thread(entry)->queue != queue)
949 set_error( STATUS_ACCESS_DENIED );
950 return 0;
952 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
954 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
955 set_fd_events( queue->fd, POLLIN );
956 add_queue( obj, entry );
957 return 1;
960 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
962 struct msg_queue *queue = (struct msg_queue *)obj;
964 remove_queue( obj, entry );
965 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
966 set_fd_events( queue->fd, 0 );
969 static void msg_queue_dump( struct object *obj, int verbose )
971 struct msg_queue *queue = (struct msg_queue *)obj;
972 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
973 queue->wake_bits, queue->wake_mask );
976 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
978 struct msg_queue *queue = (struct msg_queue *)obj;
979 int ret = 0;
981 if (queue->fd)
983 if ((ret = check_fd_events( queue->fd, POLLIN )))
984 /* stop waiting on select() if we are signaled */
985 set_fd_events( queue->fd, 0 );
986 else if (!list_empty( &obj->wait_queue ))
987 /* restart waiting on poll() if we are no longer signaled */
988 set_fd_events( queue->fd, POLLIN );
990 return ret || is_signaled( queue );
993 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
995 struct msg_queue *queue = (struct msg_queue *)obj;
996 queue->wake_mask = 0;
997 queue->changed_mask = 0;
1000 static void msg_queue_destroy( struct object *obj )
1002 struct msg_queue *queue = (struct msg_queue *)obj;
1003 struct list *ptr;
1004 struct hotkey *hotkey, *hotkey2;
1005 int i;
1007 cleanup_results( queue );
1008 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
1010 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
1012 if (hotkey->queue == queue)
1014 list_remove( &hotkey->entry );
1015 free( hotkey );
1019 while ((ptr = list_head( &queue->pending_timers )))
1021 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1022 list_remove( &timer->entry );
1023 free( timer );
1025 while ((ptr = list_head( &queue->expired_timers )))
1027 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1028 list_remove( &timer->entry );
1029 free( timer );
1031 if (queue->timeout) remove_timeout_user( queue->timeout );
1032 queue->input->cursor_count -= queue->cursor_count;
1033 release_object( queue->input );
1034 if (queue->hooks) release_object( queue->hooks );
1035 if (queue->fd) release_object( queue->fd );
1038 static void msg_queue_poll_event( struct fd *fd, int event )
1040 struct msg_queue *queue = get_fd_user( fd );
1041 assert( queue->obj.ops == &msg_queue_ops );
1043 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1044 else set_fd_events( queue->fd, 0 );
1045 wake_up( &queue->obj, 0 );
1048 static void thread_input_dump( struct object *obj, int verbose )
1050 struct thread_input *input = (struct thread_input *)obj;
1051 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
1052 input->focus, input->capture, input->active );
1055 static void thread_input_destroy( struct object *obj )
1057 struct thread_input *input = (struct thread_input *)obj;
1059 empty_msg_list( &input->msg_list );
1060 if (input->desktop)
1062 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
1063 release_object( input->desktop );
1067 /* fix the thread input data when a window is destroyed */
1068 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1070 struct thread_input *input = queue->input;
1072 if (window == input->focus) input->focus = 0;
1073 if (window == input->capture) input->capture = 0;
1074 if (window == input->active) input->active = 0;
1075 if (window == input->menu_owner) input->menu_owner = 0;
1076 if (window == input->move_size) input->move_size = 0;
1077 if (window == input->caret) set_caret_window( input, 0 );
1080 /* check if the specified window can be set in the input data of a given queue */
1081 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1083 struct thread *thread;
1084 int ret = 0;
1086 if (!window) return 1; /* we can always clear the data */
1088 if ((thread = get_window_thread( window )))
1090 ret = (queue->input == thread->queue->input);
1091 if (!ret) set_error( STATUS_ACCESS_DENIED );
1092 release_object( thread );
1094 else set_error( STATUS_INVALID_HANDLE );
1096 return ret;
1099 /* make sure the specified thread has a queue */
1100 int init_thread_queue( struct thread *thread )
1102 if (thread->queue) return 1;
1103 return (create_msg_queue( thread, NULL ) != NULL);
1106 /* attach two thread input data structures */
1107 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1109 struct desktop *desktop;
1110 struct thread_input *input;
1111 int ret;
1113 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1114 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1115 input = (struct thread_input *)grab_object( thread_to->queue->input );
1116 if (input->desktop != desktop)
1118 set_error( STATUS_ACCESS_DENIED );
1119 release_object( input );
1120 release_object( desktop );
1121 return 0;
1123 release_object( desktop );
1125 if (thread_from->queue)
1127 if (!input->focus) input->focus = thread_from->queue->input->focus;
1128 if (!input->active) input->active = thread_from->queue->input->active;
1131 ret = assign_thread_input( thread_from, input );
1132 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1133 release_object( input );
1134 return ret;
1137 /* detach two thread input data structures */
1138 void detach_thread_input( struct thread *thread_from )
1140 struct thread *thread;
1141 struct thread_input *input, *old_input = thread_from->queue->input;
1143 if ((input = create_thread_input( thread_from )))
1145 if (old_input->focus && (thread = get_window_thread( old_input->focus )))
1147 if (thread == thread_from)
1149 input->focus = old_input->focus;
1150 old_input->focus = 0;
1152 release_object( thread );
1154 if (old_input->active && (thread = get_window_thread( old_input->active )))
1156 if (thread == thread_from)
1158 input->active = old_input->active;
1159 old_input->active = 0;
1161 release_object( thread );
1163 assign_thread_input( thread_from, input );
1164 release_object( input );
1169 /* set the next timer to expire */
1170 static void set_next_timer( struct msg_queue *queue )
1172 struct list *ptr;
1174 if (queue->timeout)
1176 remove_timeout_user( queue->timeout );
1177 queue->timeout = NULL;
1179 if ((ptr = list_head( &queue->pending_timers )))
1181 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1182 queue->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, queue );
1184 /* set/clear QS_TIMER bit */
1185 if (list_empty( &queue->expired_timers ))
1186 clear_queue_bits( queue, QS_TIMER );
1187 else
1188 set_queue_bits( queue, QS_TIMER );
1191 /* find a timer from its window and id */
1192 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1193 unsigned int msg, lparam_t id )
1195 struct list *ptr;
1197 /* we need to search both lists */
1199 LIST_FOR_EACH( ptr, &queue->pending_timers )
1201 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1202 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1204 LIST_FOR_EACH( ptr, &queue->expired_timers )
1206 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1207 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1209 return NULL;
1212 /* callback for the next timer expiration */
1213 static void timer_callback( void *private )
1215 struct msg_queue *queue = private;
1216 struct list *ptr;
1218 queue->timeout = NULL;
1219 /* move on to the next timer */
1220 ptr = list_head( &queue->pending_timers );
1221 list_remove( ptr );
1222 list_add_tail( &queue->expired_timers, ptr );
1223 set_next_timer( queue );
1226 /* link a timer at its rightful place in the queue list */
1227 static void link_timer( struct msg_queue *queue, struct timer *timer )
1229 struct list *ptr;
1231 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1233 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1234 if (t->when <= timer->when) break;
1236 list_add_before( ptr, &timer->entry );
1239 /* remove a timer from the queue timer list and free it */
1240 static void free_timer( struct msg_queue *queue, struct timer *timer )
1242 list_remove( &timer->entry );
1243 free( timer );
1244 set_next_timer( queue );
1247 /* restart an expired timer */
1248 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1250 list_remove( &timer->entry );
1251 while (-timer->when <= monotonic_time) timer->when -= (timeout_t)timer->rate * 10000;
1252 link_timer( queue, timer );
1253 set_next_timer( queue );
1256 /* find an expired timer matching the filtering parameters */
1257 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1258 unsigned int get_first, unsigned int get_last,
1259 int remove )
1261 struct list *ptr;
1263 LIST_FOR_EACH( ptr, &queue->expired_timers )
1265 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1266 if (win && timer->win != win) continue;
1267 if (check_msg_filter( timer->msg, get_first, get_last ))
1269 if (remove) restart_timer( queue, timer );
1270 return timer;
1273 return NULL;
1276 /* add a timer */
1277 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1279 struct timer *timer = mem_alloc( sizeof(*timer) );
1280 if (timer)
1282 timer->rate = max( rate, 1 );
1283 timer->when = -monotonic_time - (timeout_t)timer->rate * 10000;
1284 link_timer( queue, timer );
1285 /* check if we replaced the next timer */
1286 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1288 return timer;
1291 /* change the input key state for a given key */
1292 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1294 if (down)
1296 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1297 keystate[key] |= down;
1299 else keystate[key] &= ~0x80;
1302 /* update the input key state for a keyboard message */
1303 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1304 unsigned int msg, lparam_t wparam )
1306 unsigned char key;
1307 int down = 0;
1309 switch (msg)
1311 case WM_LBUTTONDOWN:
1312 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1313 /* fall through */
1314 case WM_LBUTTONUP:
1315 set_input_key_state( keystate, VK_LBUTTON, down );
1316 break;
1317 case WM_MBUTTONDOWN:
1318 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1319 /* fall through */
1320 case WM_MBUTTONUP:
1321 set_input_key_state( keystate, VK_MBUTTON, down );
1322 break;
1323 case WM_RBUTTONDOWN:
1324 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1325 /* fall through */
1326 case WM_RBUTTONUP:
1327 set_input_key_state( keystate, VK_RBUTTON, down );
1328 break;
1329 case WM_XBUTTONDOWN:
1330 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1331 /* fall through */
1332 case WM_XBUTTONUP:
1333 if (wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1334 else if (wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1335 break;
1336 case WM_KEYDOWN:
1337 case WM_SYSKEYDOWN:
1338 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1339 /* fall through */
1340 case WM_KEYUP:
1341 case WM_SYSKEYUP:
1342 key = (unsigned char)wparam;
1343 set_input_key_state( keystate, key, down );
1344 switch(key)
1346 case VK_LCONTROL:
1347 case VK_RCONTROL:
1348 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1349 set_input_key_state( keystate, VK_CONTROL, down );
1350 break;
1351 case VK_LMENU:
1352 case VK_RMENU:
1353 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1354 set_input_key_state( keystate, VK_MENU, down );
1355 break;
1356 case VK_LSHIFT:
1357 case VK_RSHIFT:
1358 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1359 set_input_key_state( keystate, VK_SHIFT, down );
1360 break;
1362 break;
1366 /* update the desktop key state according to a mouse message flags */
1367 static void update_desktop_mouse_state( struct desktop *desktop, unsigned int flags,
1368 int x, int y, lparam_t wparam )
1370 if (flags & MOUSEEVENTF_MOVE)
1371 update_desktop_cursor_pos( desktop, x, y );
1372 if (flags & MOUSEEVENTF_LEFTDOWN)
1373 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONDOWN, wparam );
1374 if (flags & MOUSEEVENTF_LEFTUP)
1375 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONUP, wparam );
1376 if (flags & MOUSEEVENTF_RIGHTDOWN)
1377 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONDOWN, wparam );
1378 if (flags & MOUSEEVENTF_RIGHTUP)
1379 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONUP, wparam );
1380 if (flags & MOUSEEVENTF_MIDDLEDOWN)
1381 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONDOWN, wparam );
1382 if (flags & MOUSEEVENTF_MIDDLEUP)
1383 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONUP, wparam );
1384 if (flags & MOUSEEVENTF_XDOWN)
1385 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONDOWN, wparam );
1386 if (flags & MOUSEEVENTF_XUP)
1387 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONUP, wparam );
1390 /* release the hardware message currently being processed by the given thread */
1391 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id )
1393 struct thread_input *input = queue->input;
1394 struct message *msg, *other;
1395 int clr_bit;
1397 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1399 if (msg->unique_id == hw_id) break;
1401 if (&msg->entry == &input->msg_list) return; /* not found */
1403 /* clear the queue bit for that message */
1404 clr_bit = get_hardware_msg_bit( msg );
1405 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1407 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1409 clr_bit = 0;
1410 break;
1413 if (clr_bit) clear_queue_bits( queue, clr_bit );
1415 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1416 list_remove( &msg->entry );
1417 free_message( msg );
1420 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1422 struct hotkey *hotkey;
1423 unsigned int modifiers = 0;
1425 if (msg->msg != WM_KEYDOWN && msg->msg != WM_SYSKEYDOWN) return 0;
1427 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1428 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1429 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1430 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1432 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1434 if (hotkey->vkey != msg->wparam) continue;
1435 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1438 return 0;
1440 found:
1441 msg->type = MSG_POSTED;
1442 msg->win = hotkey->win;
1443 msg->msg = WM_HOTKEY;
1444 msg->wparam = hotkey->id;
1445 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1447 free( msg->data );
1448 msg->data = NULL;
1449 msg->data_size = 0;
1451 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1452 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1453 hotkey->queue->hotkey_count++;
1454 return 1;
1457 /* find the window that should receive a given hardware message */
1458 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1459 struct message *msg, unsigned int *msg_code,
1460 struct thread **thread )
1462 user_handle_t win = 0;
1464 *thread = NULL;
1465 *msg_code = msg->msg;
1466 if (msg->msg == WM_INPUT || msg->msg == WM_INPUT_DEVICE_CHANGE)
1468 if (!(win = msg->win) && input) win = input->focus;
1470 else if (is_keyboard_msg( msg ))
1472 if (input && !(win = input->focus))
1474 win = input->active;
1475 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1478 else if (!input || !(win = input->capture)) /* mouse message */
1480 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1481 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1483 *thread = window_thread_from_point( win, msg->x, msg->y );
1486 if (!*thread)
1487 *thread = get_window_thread( win );
1488 return win;
1491 static struct rawinput_device_entry *find_rawinput_device( struct process *process, unsigned short usage_page, unsigned short usage )
1493 struct rawinput_device_entry *e;
1495 LIST_FOR_EACH_ENTRY( e, &process->rawinput_devices, struct rawinput_device_entry, entry )
1497 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1498 return e;
1501 return NULL;
1504 static void update_rawinput_device(const struct rawinput_device *device)
1506 struct rawinput_device_entry *e;
1508 if (!(e = find_rawinput_device( current->process, device->usage_page, device->usage )))
1510 if (!(e = mem_alloc( sizeof(*e) ))) return;
1511 list_add_tail( &current->process->rawinput_devices, &e->entry );
1514 if (device->flags & RIDEV_REMOVE)
1516 list_remove( &e->entry );
1517 free( e );
1518 return;
1521 e->device = *device;
1522 e->device.target = get_user_full_handle( e->device.target );
1525 static void prepend_cursor_history( int x, int y, unsigned int time, lparam_t info )
1527 cursor_pos_t *pos = &cursor_history[--cursor_history_latest % ARRAY_SIZE(cursor_history)];
1529 pos->x = x;
1530 pos->y = y;
1531 pos->time = time;
1532 pos->info = info;
1535 /* queue a hardware message into a given thread input */
1536 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1538 user_handle_t win;
1539 struct thread *thread;
1540 struct thread_input *input;
1541 struct hardware_msg_data *msg_data = msg->data;
1542 unsigned int msg_code;
1544 update_input_key_state( desktop, desktop->keystate, msg->msg, msg->wparam );
1545 last_input_time = get_tick_count();
1546 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1548 if (is_keyboard_msg( msg ))
1550 if (queue_hotkey_message( desktop, msg )) return;
1551 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1552 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1553 msg->lparam &= ~(KF_EXTENDED << 16);
1555 else if (msg->msg != WM_INPUT && msg->msg != WM_INPUT_DEVICE_CHANGE)
1557 if (msg->msg == WM_MOUSEMOVE)
1559 prepend_cursor_history( msg->x, msg->y, msg->time, msg_data->info );
1560 if (update_desktop_cursor_pos( desktop, msg->x, msg->y )) always_queue = 1;
1562 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1563 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1564 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1565 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1566 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1567 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1568 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1570 msg->x = desktop->cursor.x;
1571 msg->y = desktop->cursor.y;
1573 if (msg->win && (thread = get_window_thread( msg->win )))
1575 input = thread->queue->input;
1576 release_object( thread );
1578 else input = desktop->foreground_input;
1580 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1581 if (!win || !thread)
1583 if (input) update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1584 free_message( msg );
1585 return;
1587 input = thread->queue->input;
1589 if (win != desktop->cursor.win) always_queue = 1;
1590 desktop->cursor.win = win;
1592 if (!always_queue || merge_message( input, msg )) free_message( msg );
1593 else
1595 msg->unique_id = 0; /* will be set once we return it to the app */
1596 list_add_tail( &input->msg_list, &msg->entry );
1597 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1599 release_object( thread );
1602 /* send the low-level hook message for a given hardware message */
1603 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1604 const hw_input_t *input, struct msg_queue *sender )
1606 struct thread *hook_thread;
1607 struct msg_queue *queue;
1608 struct message *msg;
1609 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1610 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1612 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1613 if (!(queue = hook_thread->queue)) return 0;
1614 if (is_queue_hung( queue )) return 0;
1616 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1618 msg->type = MSG_HOOK_LL;
1619 msg->win = 0;
1620 msg->msg = id;
1621 msg->wparam = hardware_msg->msg;
1622 msg->x = hardware_msg->x;
1623 msg->y = hardware_msg->y;
1624 msg->time = hardware_msg->time;
1625 msg->data_size = hardware_msg->data_size;
1626 msg->result = NULL;
1628 if (input->type == INPUT_KEYBOARD)
1630 unsigned short vkey = input->kbd.vkey;
1631 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1632 msg->lparam = (input->kbd.scan << 16) | vkey;
1634 else msg->lparam = input->mouse.data << 16;
1636 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1637 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1639 free_message( msg );
1640 return 0;
1642 msg->result->hardware_msg = hardware_msg;
1643 msg->result->desktop = (struct desktop *)grab_object( desktop );
1644 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1645 set_queue_bits( queue, QS_SENDMESSAGE );
1646 return 1;
1649 /* get the foreground thread for a desktop and a window receiving input */
1650 static struct thread *get_foreground_thread( struct desktop *desktop, user_handle_t window )
1652 /* if desktop has no foreground process, assume the receiving window is */
1653 if (desktop->foreground_input) return get_window_thread( desktop->foreground_input->focus );
1654 if (window) return get_window_thread( window );
1655 return NULL;
1658 struct rawinput_message
1660 struct thread *foreground;
1661 struct desktop *desktop;
1662 struct hw_msg_source source;
1663 unsigned int time;
1664 unsigned int message;
1665 struct hardware_msg_data data;
1666 const void *hid_report;
1669 /* check if process is supposed to receive a WM_INPUT message and eventually queue it */
1670 static int queue_rawinput_message( struct process* process, void *arg )
1672 const struct rawinput_message* raw_msg = arg;
1673 const struct rawinput_device_entry *entry;
1674 const struct rawinput_device *device = NULL;
1675 struct desktop *target_desktop = NULL, *desktop = NULL;
1676 struct thread *target_thread = NULL, *foreground = NULL;
1677 struct message *msg;
1678 data_size_t report_size;
1679 int wparam = RIM_INPUT;
1681 if (raw_msg->data.rawinput.type == RIM_TYPEMOUSE)
1682 device = process->rawinput_mouse;
1683 else if (raw_msg->data.rawinput.type == RIM_TYPEKEYBOARD)
1684 device = process->rawinput_kbd;
1685 else if ((entry = find_rawinput_device( process, raw_msg->data.rawinput.hid.usage_page, raw_msg->data.rawinput.hid.usage )))
1686 device = &entry->device;
1687 if (!device) return 0;
1689 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && !(device->flags & RIDEV_DEVNOTIFY)) return 0;
1691 if (raw_msg->desktop) desktop = (struct desktop *)grab_object( raw_msg->desktop );
1692 else if (!(desktop = get_desktop_obj( process, process->desktop, 0 ))) goto done;
1694 if (raw_msg->foreground) foreground = (struct thread *)grab_object( raw_msg->foreground );
1695 else if (!(foreground = get_foreground_thread( desktop, 0 ))) goto done;
1697 if (process != foreground->process)
1699 if (raw_msg->message == WM_INPUT && !(device->flags & RIDEV_INPUTSINK)) goto done;
1700 if (!(target_thread = get_window_thread( device->target ))) goto done;
1701 if (!(target_desktop = get_thread_desktop( target_thread, 0 ))) goto done;
1702 if (target_desktop != desktop) goto done;
1703 wparam = RIM_INPUTSINK;
1706 if (raw_msg->data.rawinput.type != RIM_TYPEHID || !raw_msg->hid_report) report_size = 0;
1707 else report_size = raw_msg->data.size - sizeof(raw_msg->data);
1709 if (!(msg = alloc_hardware_message( raw_msg->data.info, raw_msg->source, raw_msg->time, report_size )))
1710 goto done;
1712 msg->win = device->target;
1713 msg->msg = raw_msg->message;
1714 msg->wparam = wparam;
1715 msg->lparam = 0;
1716 memcpy( msg->data, &raw_msg->data, sizeof(raw_msg->data) );
1717 if (report_size) memcpy( (struct hardware_msg_data *)msg->data + 1, raw_msg->hid_report, report_size );
1719 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && raw_msg->data.rawinput.type == RIM_TYPEHID)
1721 msg->wparam = raw_msg->data.rawinput.hid.param;
1722 msg->lparam = raw_msg->data.rawinput.hid.device;
1725 queue_hardware_message( desktop, msg, 1 );
1727 done:
1728 if (target_thread) release_object( target_thread );
1729 if (target_desktop) release_object( target_desktop );
1730 if (foreground) release_object( foreground );
1731 if (desktop) release_object( desktop );
1732 return 0;
1735 /* queue a hardware message for a mouse event */
1736 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1737 unsigned int origin, struct msg_queue *sender )
1739 const struct rawinput_device *device;
1740 struct hardware_msg_data *msg_data;
1741 struct rawinput_message raw_msg;
1742 struct message *msg;
1743 struct thread *foreground;
1744 unsigned int i, time, flags;
1745 struct hw_msg_source source = { IMDT_MOUSE, origin };
1746 int wait = 0, x, y;
1748 static const unsigned int messages[] =
1750 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1751 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1752 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1753 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1754 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1755 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1756 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1757 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1758 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1759 0, /* 0x0200 = unused */
1760 0, /* 0x0400 = unused */
1761 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1762 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1765 desktop->cursor.last_change = get_tick_count();
1766 flags = input->mouse.flags;
1767 time = input->mouse.time;
1768 if (!time) time = desktop->cursor.last_change;
1770 if (flags & MOUSEEVENTF_MOVE)
1772 if (flags & MOUSEEVENTF_ABSOLUTE)
1774 x = input->mouse.x;
1775 y = input->mouse.y;
1776 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1777 x == desktop->cursor.x && y == desktop->cursor.y)
1778 flags &= ~MOUSEEVENTF_MOVE;
1780 else
1782 x = desktop->cursor.x + input->mouse.x;
1783 y = desktop->cursor.y + input->mouse.y;
1786 else
1788 x = desktop->cursor.x;
1789 y = desktop->cursor.y;
1792 if ((foreground = get_foreground_thread( desktop, win )))
1794 memset( &raw_msg, 0, sizeof(raw_msg) );
1795 raw_msg.foreground = foreground;
1796 raw_msg.desktop = desktop;
1797 raw_msg.source = source;
1798 raw_msg.time = time;
1799 raw_msg.message = WM_INPUT;
1801 msg_data = &raw_msg.data;
1802 msg_data->info = input->mouse.info;
1803 msg_data->size = sizeof(*msg_data);
1804 msg_data->flags = flags;
1805 msg_data->rawinput.type = RIM_TYPEMOUSE;
1806 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1807 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1808 msg_data->rawinput.mouse.data = input->mouse.data;
1810 enum_processes( queue_rawinput_message, &raw_msg );
1811 release_object( foreground );
1814 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
1816 update_desktop_mouse_state( desktop, flags, x, y, input->mouse.data << 16 );
1817 return 0;
1820 for (i = 0; i < ARRAY_SIZE( messages ); i++)
1822 if (!messages[i]) continue;
1823 if (!(flags & (1 << i))) continue;
1824 flags &= ~(1 << i);
1826 if (!(msg = alloc_hardware_message( input->mouse.info, source, time, 0 ))) return 0;
1827 msg_data = msg->data;
1829 msg->win = get_user_full_handle( win );
1830 msg->msg = messages[i];
1831 msg->wparam = input->mouse.data << 16;
1832 msg->lparam = 0;
1833 msg->x = x;
1834 msg->y = y;
1835 if (origin == IMO_INJECTED) msg_data->flags = LLMHF_INJECTED;
1837 /* specify a sender only when sending the last message */
1838 if (!(flags & ((1 << ARRAY_SIZE( messages )) - 1)))
1840 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1841 queue_hardware_message( desktop, msg, 0 );
1843 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1844 queue_hardware_message( desktop, msg, 0 );
1846 return wait;
1849 /* queue a hardware message for a keyboard event */
1850 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1851 unsigned int origin, struct msg_queue *sender )
1853 struct hw_msg_source source = { IMDT_KEYBOARD, origin };
1854 const struct rawinput_device *device;
1855 struct hardware_msg_data *msg_data;
1856 struct rawinput_message raw_msg;
1857 struct message *msg;
1858 struct thread *foreground;
1859 unsigned char vkey = input->kbd.vkey;
1860 unsigned int message_code, time;
1861 int wait;
1863 if (!(time = input->kbd.time)) time = get_tick_count();
1865 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1867 switch (vkey)
1869 case VK_MENU:
1870 case VK_LMENU:
1871 case VK_RMENU:
1872 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1873 break;
1874 case VK_CONTROL:
1875 case VK_LCONTROL:
1876 case VK_RCONTROL:
1877 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1878 break;
1879 case VK_SHIFT:
1880 case VK_LSHIFT:
1881 case VK_RSHIFT:
1882 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1883 break;
1887 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1888 switch (vkey)
1890 case VK_LMENU:
1891 case VK_RMENU:
1892 if (input->kbd.flags & KEYEVENTF_KEYUP)
1894 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1895 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1896 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1897 message_code = WM_SYSKEYUP;
1898 desktop->keystate[VK_MENU] &= ~0x02;
1900 else
1902 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1903 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1904 message_code = WM_SYSKEYDOWN;
1905 desktop->keystate[VK_MENU] |= 0x02;
1907 break;
1909 case VK_LCONTROL:
1910 case VK_RCONTROL:
1911 /* send WM_SYSKEYUP on release if Alt still pressed */
1912 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1913 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1914 message_code = WM_SYSKEYUP;
1915 desktop->keystate[VK_MENU] &= ~0x02;
1916 break;
1918 default:
1919 /* send WM_SYSKEY for Alt-anykey and for F10 */
1920 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1921 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1922 /* fall through */
1923 case VK_F10:
1924 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1925 desktop->keystate[VK_MENU] &= ~0x02;
1926 break;
1929 if ((foreground = get_foreground_thread( desktop, win )))
1931 memset( &raw_msg, 0, sizeof(raw_msg) );
1932 raw_msg.foreground = foreground;
1933 raw_msg.desktop = desktop;
1934 raw_msg.source = source;
1935 raw_msg.time = time;
1936 raw_msg.message = WM_INPUT;
1938 msg_data = &raw_msg.data;
1939 msg_data->info = input->kbd.info;
1940 msg_data->size = sizeof(*msg_data);
1941 msg_data->flags = input->kbd.flags;
1942 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1943 msg_data->rawinput.kbd.message = message_code;
1944 msg_data->rawinput.kbd.vkey = vkey;
1945 msg_data->rawinput.kbd.scan = input->kbd.scan;
1947 enum_processes( queue_rawinput_message, &raw_msg );
1948 release_object( foreground );
1951 if ((device = current->process->rawinput_kbd) && (device->flags & RIDEV_NOLEGACY))
1953 update_input_key_state( desktop, desktop->keystate, message_code, vkey );
1954 return 0;
1957 if (!(msg = alloc_hardware_message( input->kbd.info, source, time, 0 ))) return 0;
1958 msg_data = msg->data;
1960 msg->win = get_user_full_handle( win );
1961 msg->msg = message_code;
1962 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1963 if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
1965 if (input->kbd.flags & KEYEVENTF_UNICODE && !vkey)
1967 msg->wparam = VK_PACKET;
1969 else
1971 unsigned int flags = 0;
1972 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1973 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1974 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1975 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1977 msg->wparam = vkey;
1978 msg->lparam |= flags << 16;
1979 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1982 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1983 queue_hardware_message( desktop, msg, 1 );
1985 return wait;
1988 /* queue a hardware message for a custom type of event */
1989 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1990 unsigned int origin, const hw_input_t *input )
1992 struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
1993 struct hardware_msg_data *msg_data;
1994 struct rawinput_message raw_msg;
1995 struct message *msg;
1996 data_size_t report_size = 0;
1998 switch (input->hw.msg)
2000 case WM_INPUT:
2001 case WM_INPUT_DEVICE_CHANGE:
2002 memset( &raw_msg, 0, sizeof(raw_msg) );
2003 raw_msg.source = source;
2004 raw_msg.time = get_tick_count();
2005 raw_msg.message = input->hw.msg;
2007 if (input->hw.rawinput.type == RIM_TYPEHID)
2009 raw_msg.hid_report = get_req_data();
2010 report_size = input->hw.rawinput.hid.length * input->hw.rawinput.hid.count;
2013 if (report_size != get_req_data_size())
2015 set_error( STATUS_INVALID_PARAMETER );
2016 return;
2019 msg_data = &raw_msg.data;
2020 msg_data->size = sizeof(*msg_data) + report_size;
2021 msg_data->rawinput = input->hw.rawinput;
2023 enum_processes( queue_rawinput_message, &raw_msg );
2025 if (raw_msg.foreground) release_object( raw_msg.foreground );
2026 return;
2029 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
2031 msg->win = get_user_full_handle( win );
2032 msg->msg = input->hw.msg;
2033 msg->wparam = 0;
2034 msg->lparam = input->hw.lparam;
2035 msg->x = desktop->cursor.x;
2036 msg->y = desktop->cursor.y;
2038 queue_hardware_message( desktop, msg, 1 );
2041 /* check message filter for a hardware message */
2042 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
2043 user_handle_t filter_win, unsigned int first, unsigned int last )
2045 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
2047 /* we can only test the window for a keyboard message since the
2048 * dest window for a mouse message depends on hittest */
2049 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
2050 return 0;
2051 /* the message code is final for a keyboard message, we can simply check it */
2052 return check_msg_filter( msg_code, first, last );
2054 else /* mouse message */
2056 /* we need to check all possible values that the message can have in the end */
2058 if (check_msg_filter( msg_code, first, last )) return 1;
2059 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
2061 /* all other messages can become non-client messages */
2062 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
2064 /* clicks can become double-clicks or non-client double-clicks */
2065 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
2066 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
2068 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2069 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2071 return 0;
2076 /* find a hardware message for the given queue */
2077 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
2078 unsigned int first, unsigned int last, unsigned int flags,
2079 struct get_message_reply *reply )
2081 struct thread_input *input = thread->queue->input;
2082 struct thread *win_thread;
2083 struct list *ptr;
2084 user_handle_t win;
2085 int clear_bits, got_one = 0;
2086 unsigned int msg_code;
2088 ptr = list_head( &input->msg_list );
2089 if (hw_id)
2091 while (ptr)
2093 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2094 if (msg->unique_id == hw_id) break;
2095 ptr = list_next( &input->msg_list, ptr );
2097 if (!ptr) ptr = list_head( &input->msg_list );
2098 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
2101 if (ptr == list_head( &input->msg_list ))
2102 clear_bits = QS_INPUT;
2103 else
2104 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
2106 while (ptr)
2108 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2109 struct hardware_msg_data *data = msg->data;
2111 ptr = list_next( &input->msg_list, ptr );
2112 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
2113 if (!win || !win_thread)
2115 /* no window at all, remove it */
2116 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2117 list_remove( &msg->entry );
2118 free_message( msg );
2119 continue;
2121 if (win_thread != thread)
2123 if (win_thread->queue->input == input)
2125 /* wake the other thread */
2126 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
2127 got_one = 1;
2129 else
2131 /* for another thread input, drop it */
2132 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2133 list_remove( &msg->entry );
2134 free_message( msg );
2136 release_object( win_thread );
2137 continue;
2139 release_object( win_thread );
2141 /* if we already got a message for another thread, or if it doesn't
2142 * match the filter we skip it */
2143 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
2145 clear_bits &= ~get_hardware_msg_bit( msg );
2146 continue;
2149 reply->total = msg->data_size;
2150 if (msg->data_size > get_reply_max_size())
2152 set_error( STATUS_BUFFER_OVERFLOW );
2153 return 1;
2156 /* now we can return it */
2157 if (!msg->unique_id) msg->unique_id = get_unique_id();
2158 reply->type = MSG_HARDWARE;
2159 reply->win = win;
2160 reply->msg = msg_code;
2161 reply->wparam = msg->wparam;
2162 reply->lparam = msg->lparam;
2163 reply->x = msg->x;
2164 reply->y = msg->y;
2165 reply->time = msg->time;
2167 data->hw_id = msg->unique_id;
2168 set_reply_data( msg->data, msg->data_size );
2169 if ((msg->msg == WM_INPUT || msg->msg == WM_INPUT_DEVICE_CHANGE) && (flags & PM_REMOVE))
2170 release_hardware_message( current->queue, data->hw_id );
2171 return 1;
2173 /* nothing found, clear the hardware queue bits */
2174 clear_queue_bits( thread->queue, clear_bits );
2175 return 0;
2178 /* increment (or decrement if 'incr' is negative) the queue paint count */
2179 void inc_queue_paint_count( struct thread *thread, int incr )
2181 struct msg_queue *queue = thread->queue;
2183 assert( queue );
2185 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2187 if (queue->paint_count)
2188 set_queue_bits( queue, QS_PAINT );
2189 else
2190 clear_queue_bits( queue, QS_PAINT );
2194 /* remove all messages and timers belonging to a certain window */
2195 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2197 struct msg_queue *queue = thread->queue;
2198 struct list *ptr;
2199 int i;
2201 if (!queue) return;
2203 /* remove timers */
2205 ptr = list_head( &queue->pending_timers );
2206 while (ptr)
2208 struct list *next = list_next( &queue->pending_timers, ptr );
2209 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2210 if (timer->win == win) free_timer( queue, timer );
2211 ptr = next;
2213 ptr = list_head( &queue->expired_timers );
2214 while (ptr)
2216 struct list *next = list_next( &queue->expired_timers, ptr );
2217 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2218 if (timer->win == win) free_timer( queue, timer );
2219 ptr = next;
2222 /* remove messages */
2223 for (i = 0; i < NB_MSG_KINDS; i++)
2225 struct list *ptr, *next;
2227 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2229 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2230 if (msg->win == win)
2232 if (msg->msg == WM_QUIT && !queue->quit_message)
2234 queue->quit_message = 1;
2235 queue->exit_code = msg->wparam;
2237 remove_queue_message( queue, msg, i );
2242 thread_input_cleanup_window( queue, win );
2245 /* post a message to a window */
2246 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2248 struct message *msg;
2249 struct thread *thread = get_window_thread( win );
2251 if (!thread) return;
2253 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2255 msg->type = MSG_POSTED;
2256 msg->win = get_user_full_handle( win );
2257 msg->msg = message;
2258 msg->wparam = wparam;
2259 msg->lparam = lparam;
2260 msg->result = NULL;
2261 msg->data = NULL;
2262 msg->data_size = 0;
2264 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2266 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2267 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2268 if (message == WM_HOTKEY)
2270 set_queue_bits( thread->queue, QS_HOTKEY );
2271 thread->queue->hotkey_count++;
2274 release_object( thread );
2277 /* send a notify message to a window */
2278 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2280 struct message *msg;
2281 struct thread *thread = get_window_thread( win );
2283 if (!thread) return;
2285 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2287 msg->type = MSG_NOTIFY;
2288 msg->win = get_user_full_handle( win );
2289 msg->msg = message;
2290 msg->wparam = wparam;
2291 msg->lparam = lparam;
2292 msg->result = NULL;
2293 msg->data = NULL;
2294 msg->data_size = 0;
2296 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2298 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2299 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2301 release_object( thread );
2304 /* post a win event */
2305 void post_win_event( struct thread *thread, unsigned int event,
2306 user_handle_t win, unsigned int object_id,
2307 unsigned int child_id, client_ptr_t hook_proc,
2308 const WCHAR *module, data_size_t module_size,
2309 user_handle_t hook)
2311 struct message *msg;
2313 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2315 struct winevent_msg_data *data;
2317 msg->type = MSG_WINEVENT;
2318 msg->win = get_user_full_handle( win );
2319 msg->msg = event;
2320 msg->wparam = object_id;
2321 msg->lparam = child_id;
2322 msg->time = get_tick_count();
2323 msg->result = NULL;
2325 if ((data = malloc( sizeof(*data) + module_size )))
2327 data->hook = hook;
2328 data->tid = get_thread_id( current );
2329 data->hook_proc = hook_proc;
2330 memcpy( data + 1, module, module_size );
2332 msg->data = data;
2333 msg->data_size = sizeof(*data) + module_size;
2335 if (debug_level > 1)
2336 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2337 get_thread_id(thread), event, win, object_id, child_id );
2338 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2339 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2341 else
2342 free( msg );
2346 /* free all hotkeys on a desktop, optionally filtering by window */
2347 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2349 struct hotkey *hotkey, *hotkey2;
2351 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2353 if (!window || hotkey->win == window)
2355 list_remove( &hotkey->entry );
2356 free( hotkey );
2362 /* check if the thread owning the window is hung */
2363 DECL_HANDLER(is_window_hung)
2365 struct thread *thread;
2367 thread = get_window_thread( req->win );
2368 if (thread)
2370 reply->is_hung = is_queue_hung( thread->queue );
2371 release_object( thread );
2373 else reply->is_hung = 0;
2377 /* get the message queue of the current thread */
2378 DECL_HANDLER(get_msg_queue)
2380 struct msg_queue *queue = get_current_queue();
2382 reply->handle = 0;
2383 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2387 /* set the file descriptor associated to the current thread queue */
2388 DECL_HANDLER(set_queue_fd)
2390 struct msg_queue *queue = get_current_queue();
2391 struct file *file;
2392 int unix_fd;
2394 if (queue->fd) /* fd can only be set once */
2396 set_error( STATUS_ACCESS_DENIED );
2397 return;
2399 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2401 if ((unix_fd = get_file_unix_fd( file )) != -1)
2403 if ((unix_fd = dup( unix_fd )) != -1)
2404 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2405 else
2406 file_set_error();
2408 release_object( file );
2412 /* set the current message queue wakeup mask */
2413 DECL_HANDLER(set_queue_mask)
2415 struct msg_queue *queue = get_current_queue();
2417 if (queue)
2419 queue->wake_mask = req->wake_mask;
2420 queue->changed_mask = req->changed_mask;
2421 reply->wake_bits = queue->wake_bits;
2422 reply->changed_bits = queue->changed_bits;
2423 if (is_signaled( queue ))
2425 /* if skip wait is set, do what would have been done in the subsequent wait */
2426 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2427 else wake_up( &queue->obj, 0 );
2433 /* get the current message queue status */
2434 DECL_HANDLER(get_queue_status)
2436 struct msg_queue *queue = current->queue;
2437 if (queue)
2439 reply->wake_bits = queue->wake_bits;
2440 reply->changed_bits = queue->changed_bits;
2441 queue->changed_bits &= ~req->clear_bits;
2443 else reply->wake_bits = reply->changed_bits = 0;
2447 /* send a message to a thread queue */
2448 DECL_HANDLER(send_message)
2450 struct message *msg;
2451 struct msg_queue *send_queue = get_current_queue();
2452 struct msg_queue *recv_queue = NULL;
2453 struct thread *thread = NULL;
2455 if (!(thread = get_thread_from_id( req->id ))) return;
2457 if (!(recv_queue = thread->queue))
2459 set_error( STATUS_INVALID_PARAMETER );
2460 release_object( thread );
2461 return;
2463 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2465 set_error( STATUS_TIMEOUT );
2466 release_object( thread );
2467 return;
2470 if ((msg = mem_alloc( sizeof(*msg) )))
2472 msg->type = req->type;
2473 msg->win = get_user_full_handle( req->win );
2474 msg->msg = req->msg;
2475 msg->wparam = req->wparam;
2476 msg->lparam = req->lparam;
2477 msg->result = NULL;
2478 msg->data = NULL;
2479 msg->data_size = get_req_data_size();
2481 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2483 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2485 free( msg );
2486 release_object( thread );
2487 return;
2490 switch(msg->type)
2492 case MSG_OTHER_PROCESS:
2493 case MSG_ASCII:
2494 case MSG_UNICODE:
2495 case MSG_CALLBACK:
2496 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2498 free_message( msg );
2499 break;
2501 /* fall through */
2502 case MSG_NOTIFY:
2503 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2504 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2505 break;
2506 case MSG_POSTED:
2507 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2508 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2509 if (msg->msg == WM_HOTKEY)
2511 set_queue_bits( recv_queue, QS_HOTKEY );
2512 recv_queue->hotkey_count++;
2514 break;
2515 case MSG_HARDWARE: /* should use send_hardware_message instead */
2516 case MSG_CALLBACK_RESULT: /* cannot send this one */
2517 case MSG_HOOK_LL: /* generated internally */
2518 default:
2519 set_error( STATUS_INVALID_PARAMETER );
2520 free( msg );
2521 break;
2524 release_object( thread );
2527 /* send a hardware message to a thread queue */
2528 DECL_HANDLER(send_hardware_message)
2530 struct thread *thread = NULL;
2531 struct desktop *desktop;
2532 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2533 struct msg_queue *sender = get_current_queue();
2534 data_size_t size = min( 256, get_reply_max_size() );
2536 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2538 if (req->win)
2540 if (!(thread = get_window_thread( req->win ))) return;
2541 if (desktop != thread->queue->input->desktop)
2543 /* don't allow queuing events to a different desktop */
2544 release_object( desktop );
2545 return;
2549 reply->prev_x = desktop->cursor.x;
2550 reply->prev_y = desktop->cursor.y;
2552 switch (req->input.type)
2554 case INPUT_MOUSE:
2555 reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2556 break;
2557 case INPUT_KEYBOARD:
2558 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2559 break;
2560 case INPUT_HARDWARE:
2561 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2562 break;
2563 default:
2564 set_error( STATUS_INVALID_PARAMETER );
2566 if (thread) release_object( thread );
2568 reply->new_x = desktop->cursor.x;
2569 reply->new_y = desktop->cursor.y;
2570 set_reply_data( desktop->keystate, size );
2571 release_object( desktop );
2574 /* post a quit message to the current queue */
2575 DECL_HANDLER(post_quit_message)
2577 struct msg_queue *queue = get_current_queue();
2579 if (!queue)
2580 return;
2582 queue->quit_message = 1;
2583 queue->exit_code = req->exit_code;
2584 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2587 /* get a message from the current queue */
2588 DECL_HANDLER(get_message)
2590 struct timer *timer;
2591 struct list *ptr;
2592 struct msg_queue *queue = get_current_queue();
2593 user_handle_t get_win = get_user_full_handle( req->get_win );
2594 unsigned int filter = req->flags >> 16;
2596 reply->active_hooks = get_active_hooks();
2598 if (get_win && get_win != 1 && get_win != -1 && !get_user_object( get_win, USER_WINDOW ))
2600 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2601 return;
2604 if (!queue) return;
2605 queue->last_get_msg = current_time;
2606 if (!filter) filter = QS_ALLINPUT;
2608 /* first check for sent messages */
2609 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2611 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2612 receive_message( queue, msg, reply );
2613 return;
2616 /* clear changed bits so we can wait on them if we don't find a message */
2617 if (filter & QS_POSTMESSAGE)
2619 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2620 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2622 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2623 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2625 /* then check for posted messages */
2626 if ((filter & QS_POSTMESSAGE) &&
2627 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2628 return;
2630 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2631 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2632 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2633 return;
2635 /* only check for quit messages if not posted messages pending */
2636 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2637 return;
2639 /* then check for any raw hardware message */
2640 if ((filter & QS_INPUT) &&
2641 filter_contains_hw_range( req->get_first, req->get_last ) &&
2642 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2643 return;
2645 /* now check for WM_PAINT */
2646 if ((filter & QS_PAINT) &&
2647 queue->paint_count &&
2648 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2649 (reply->win = find_window_to_repaint( get_win, current )))
2651 reply->type = MSG_POSTED;
2652 reply->msg = WM_PAINT;
2653 reply->wparam = 0;
2654 reply->lparam = 0;
2655 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2656 return;
2659 /* now check for timer */
2660 if ((filter & QS_TIMER) &&
2661 (timer = find_expired_timer( queue, get_win, req->get_first,
2662 req->get_last, (req->flags & PM_REMOVE) )))
2664 reply->type = MSG_POSTED;
2665 reply->win = timer->win;
2666 reply->msg = timer->msg;
2667 reply->wparam = timer->id;
2668 reply->lparam = timer->lparam;
2669 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2670 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2671 set_event( current->process->idle_event );
2672 return;
2675 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2676 queue->wake_mask = req->wake_mask;
2677 queue->changed_mask = req->changed_mask;
2678 set_error( STATUS_PENDING ); /* FIXME */
2682 /* reply to a sent message */
2683 DECL_HANDLER(reply_message)
2685 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2686 else if (current->queue->recv_result)
2687 reply_message( current->queue, req->result, 0, req->remove,
2688 get_req_data(), get_req_data_size() );
2692 /* accept the current hardware message */
2693 DECL_HANDLER(accept_hardware_message)
2695 if (current->queue)
2696 release_hardware_message( current->queue, req->hw_id );
2697 else
2698 set_error( STATUS_ACCESS_DENIED );
2702 /* retrieve the reply for the last message sent */
2703 DECL_HANDLER(get_message_reply)
2705 struct message_result *result;
2706 struct list *entry;
2707 struct msg_queue *queue = current->queue;
2709 if (queue)
2711 set_error( STATUS_PENDING );
2712 reply->result = 0;
2714 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2716 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2717 if (result->replied || req->cancel)
2719 if (result->replied)
2721 reply->result = result->result;
2722 set_error( result->error );
2723 if (result->data)
2725 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2726 set_reply_data_ptr( result->data, data_len );
2727 result->data = NULL;
2728 result->data_size = 0;
2731 remove_result_from_sender( result );
2733 entry = list_head( &queue->send_result );
2734 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2735 else
2737 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2738 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2739 else clear_queue_bits( queue, QS_SMRESULT );
2743 else set_error( STATUS_ACCESS_DENIED );
2747 /* set a window timer */
2748 DECL_HANDLER(set_win_timer)
2750 struct timer *timer;
2751 struct msg_queue *queue;
2752 struct thread *thread = NULL;
2753 user_handle_t win = 0;
2754 lparam_t id = req->id;
2756 if (req->win)
2758 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2760 set_error( STATUS_INVALID_HANDLE );
2761 return;
2763 if (thread->process != current->process)
2765 release_object( thread );
2766 set_error( STATUS_ACCESS_DENIED );
2767 return;
2769 queue = thread->queue;
2770 /* remove it if it existed already */
2771 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2773 else
2775 queue = get_current_queue();
2776 /* look for a timer with this id */
2777 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2779 /* free and reuse id */
2780 free_timer( queue, timer );
2782 else
2784 lparam_t end_id = queue->next_timer_id;
2786 /* find a free id for it */
2787 while (1)
2789 id = queue->next_timer_id;
2790 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2792 if (!find_timer( queue, 0, req->msg, id )) break;
2793 if (queue->next_timer_id == end_id)
2795 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2796 return;
2802 if ((timer = set_timer( queue, req->rate )))
2804 timer->win = win;
2805 timer->msg = req->msg;
2806 timer->id = id;
2807 timer->lparam = req->lparam;
2808 reply->id = id;
2810 if (thread) release_object( thread );
2813 /* kill a window timer */
2814 DECL_HANDLER(kill_win_timer)
2816 struct timer *timer;
2817 struct thread *thread;
2818 user_handle_t win = 0;
2820 if (req->win)
2822 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2824 set_error( STATUS_INVALID_HANDLE );
2825 return;
2827 if (thread->process != current->process)
2829 release_object( thread );
2830 set_error( STATUS_ACCESS_DENIED );
2831 return;
2834 else thread = (struct thread *)grab_object( current );
2836 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2837 free_timer( thread->queue, timer );
2838 else
2839 set_error( STATUS_INVALID_PARAMETER );
2841 release_object( thread );
2844 DECL_HANDLER(register_hotkey)
2846 struct desktop *desktop;
2847 user_handle_t win_handle = req->window;
2848 struct hotkey *hotkey;
2849 struct hotkey *new_hotkey = NULL;
2850 struct thread *thread;
2851 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2853 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2855 if (win_handle)
2857 if (!(win_handle = get_valid_window_handle( win_handle )))
2859 release_object( desktop );
2860 return;
2863 thread = get_window_thread( win_handle );
2864 if (thread) release_object( thread );
2866 if (thread != current)
2868 release_object( desktop );
2869 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2870 return;
2874 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2876 if (req->vkey == hotkey->vkey &&
2877 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2879 release_object( desktop );
2880 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2881 return;
2883 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2884 new_hotkey = hotkey;
2887 if (new_hotkey)
2889 reply->replaced = 1;
2890 reply->flags = new_hotkey->flags;
2891 reply->vkey = new_hotkey->vkey;
2893 else
2895 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2896 if (new_hotkey)
2898 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2899 new_hotkey->queue = current->queue;
2900 new_hotkey->win = win_handle;
2901 new_hotkey->id = req->id;
2905 if (new_hotkey)
2907 new_hotkey->flags = req->flags;
2908 new_hotkey->vkey = req->vkey;
2911 release_object( desktop );
2914 DECL_HANDLER(unregister_hotkey)
2916 struct desktop *desktop;
2917 user_handle_t win_handle = req->window;
2918 struct hotkey *hotkey;
2919 struct thread *thread;
2921 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2923 if (win_handle)
2925 if (!(win_handle = get_valid_window_handle( win_handle )))
2927 release_object( desktop );
2928 return;
2931 thread = get_window_thread( win_handle );
2932 if (thread) release_object( thread );
2934 if (thread != current)
2936 release_object( desktop );
2937 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2938 return;
2942 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2944 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2945 goto found;
2948 release_object( desktop );
2949 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2950 return;
2952 found:
2953 reply->flags = hotkey->flags;
2954 reply->vkey = hotkey->vkey;
2955 list_remove( &hotkey->entry );
2956 free( hotkey );
2957 release_object( desktop );
2960 /* attach (or detach) thread inputs */
2961 DECL_HANDLER(attach_thread_input)
2963 struct thread *thread_from = get_thread_from_id( req->tid_from );
2964 struct thread *thread_to = get_thread_from_id( req->tid_to );
2966 if (!thread_from || !thread_to)
2968 if (thread_from) release_object( thread_from );
2969 if (thread_to) release_object( thread_to );
2970 return;
2972 if (thread_from != thread_to)
2974 if (req->attach)
2976 if ((thread_to->queue || thread_to == current) &&
2977 (thread_from->queue || thread_from == current))
2978 attach_thread_input( thread_from, thread_to );
2979 else
2980 set_error( STATUS_INVALID_PARAMETER );
2982 else
2984 if (thread_from->queue && thread_to->queue &&
2985 thread_from->queue->input == thread_to->queue->input)
2986 detach_thread_input( thread_from );
2987 else
2988 set_error( STATUS_ACCESS_DENIED );
2991 else set_error( STATUS_ACCESS_DENIED );
2992 release_object( thread_from );
2993 release_object( thread_to );
2997 /* get thread input data */
2998 DECL_HANDLER(get_thread_input)
3000 struct thread *thread = NULL;
3001 struct desktop *desktop;
3002 struct thread_input *input;
3004 if (req->tid)
3006 if (!(thread = get_thread_from_id( req->tid ))) return;
3007 if (!(desktop = get_thread_desktop( thread, 0 )))
3009 release_object( thread );
3010 return;
3012 input = thread->queue ? thread->queue->input : NULL;
3014 else
3016 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3017 input = desktop->foreground_input; /* get the foreground thread info */
3020 if (input)
3022 reply->focus = input->focus;
3023 reply->capture = input->capture;
3024 reply->active = input->active;
3025 reply->menu_owner = input->menu_owner;
3026 reply->move_size = input->move_size;
3027 reply->caret = input->caret;
3028 reply->cursor = input->cursor;
3029 reply->show_count = input->cursor_count;
3030 reply->rect = input->caret_rect;
3033 /* foreground window is active window of foreground thread */
3034 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
3035 if (thread) release_object( thread );
3036 release_object( desktop );
3040 /* retrieve queue keyboard state for current thread or global async state */
3041 DECL_HANDLER(get_key_state)
3043 struct desktop *desktop;
3044 data_size_t size = min( 256, get_reply_max_size() );
3046 if (req->async) /* get global async key state */
3048 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3049 if (req->key >= 0)
3051 reply->state = desktop->keystate[req->key & 0xff];
3052 desktop->keystate[req->key & 0xff] &= ~0x40;
3054 set_reply_data( desktop->keystate, size );
3055 release_object( desktop );
3057 else if (!current->queue)
3059 unsigned char *keystate;
3060 /* fallback to desktop keystate */
3061 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3062 if (req->key >= 0) reply->state = desktop->keystate[req->key & 0xff] & ~0x40;
3063 if ((keystate = set_reply_data_size( size )))
3065 unsigned int i;
3066 for (i = 0; i < size; i++) keystate[i] = desktop->keystate[i] & ~0x40;
3068 release_object( desktop );
3070 else
3072 unsigned char *keystate = current->queue->input->keystate;
3073 if (req->key >= 0) reply->state = keystate[req->key & 0xff];
3074 set_reply_data( keystate, size );
3079 /* set queue keyboard state for current thread */
3080 DECL_HANDLER(set_key_state)
3082 struct desktop *desktop;
3083 data_size_t size = min( 256, get_req_data_size() );
3085 if (current->queue) memcpy( current->queue->input->keystate, get_req_data(), size );
3086 if (req->async && (desktop = get_thread_desktop( current, 0 )))
3088 memcpy( desktop->keystate, get_req_data(), size );
3089 release_object( desktop );
3094 /* set the system foreground window */
3095 DECL_HANDLER(set_foreground_window)
3097 struct thread *thread = NULL;
3098 struct desktop *desktop;
3099 struct msg_queue *queue = get_current_queue();
3101 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3102 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
3103 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
3104 reply->send_msg_new = FALSE;
3106 if (is_valid_foreground_window( req->handle ) &&
3107 (thread = get_window_thread( req->handle )) &&
3108 thread->queue->input->desktop == desktop)
3110 set_foreground_input( desktop, thread->queue->input );
3111 reply->send_msg_new = (desktop->foreground_input != queue->input);
3113 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
3115 if (thread) release_object( thread );
3116 release_object( desktop );
3120 /* set the current thread focus window */
3121 DECL_HANDLER(set_focus_window)
3123 struct msg_queue *queue = get_current_queue();
3125 reply->previous = 0;
3126 if (queue && check_queue_input_window( queue, req->handle ))
3128 reply->previous = queue->input->focus;
3129 queue->input->focus = get_user_full_handle( req->handle );
3134 /* set the current thread active window */
3135 DECL_HANDLER(set_active_window)
3137 struct msg_queue *queue = get_current_queue();
3139 reply->previous = 0;
3140 if (queue && check_queue_input_window( queue, req->handle ))
3142 if (!req->handle || make_window_active( req->handle ))
3144 reply->previous = queue->input->active;
3145 queue->input->active = get_user_full_handle( req->handle );
3147 else set_error( STATUS_INVALID_HANDLE );
3152 /* set the current thread capture window */
3153 DECL_HANDLER(set_capture_window)
3155 struct msg_queue *queue = get_current_queue();
3157 reply->previous = reply->full_handle = 0;
3158 if (queue && check_queue_input_window( queue, req->handle ))
3160 struct thread_input *input = queue->input;
3162 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
3163 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
3165 set_error(STATUS_ACCESS_DENIED);
3166 return;
3168 reply->previous = input->capture;
3169 input->capture = get_user_full_handle( req->handle );
3170 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
3171 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
3172 reply->full_handle = input->capture;
3177 /* Set the current thread caret window */
3178 DECL_HANDLER(set_caret_window)
3180 struct msg_queue *queue = get_current_queue();
3182 reply->previous = 0;
3183 if (queue && check_queue_input_window( queue, req->handle ))
3185 struct thread_input *input = queue->input;
3187 reply->previous = input->caret;
3188 reply->old_rect = input->caret_rect;
3189 reply->old_hide = input->caret_hide;
3190 reply->old_state = input->caret_state;
3192 set_caret_window( input, get_user_full_handle(req->handle) );
3193 input->caret_rect.right = input->caret_rect.left + req->width;
3194 input->caret_rect.bottom = input->caret_rect.top + req->height;
3199 /* Set the current thread caret information */
3200 DECL_HANDLER(set_caret_info)
3202 struct msg_queue *queue = get_current_queue();
3203 struct thread_input *input;
3205 if (!queue) return;
3206 input = queue->input;
3207 reply->full_handle = input->caret;
3208 reply->old_rect = input->caret_rect;
3209 reply->old_hide = input->caret_hide;
3210 reply->old_state = input->caret_state;
3212 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3214 set_error( STATUS_ACCESS_DENIED );
3215 return;
3217 if (req->flags & SET_CARET_POS)
3219 input->caret_rect.right += req->x - input->caret_rect.left;
3220 input->caret_rect.bottom += req->y - input->caret_rect.top;
3221 input->caret_rect.left = req->x;
3222 input->caret_rect.top = req->y;
3224 if (req->flags & SET_CARET_HIDE)
3226 input->caret_hide += req->hide;
3227 if (input->caret_hide < 0) input->caret_hide = 0;
3229 if (req->flags & SET_CARET_STATE)
3231 switch (req->state)
3233 case CARET_STATE_OFF: input->caret_state = 0; break;
3234 case CARET_STATE_ON: input->caret_state = 1; break;
3235 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3236 case CARET_STATE_ON_IF_MOVED:
3237 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3238 break;
3244 /* get the time of the last input event */
3245 DECL_HANDLER(get_last_input_time)
3247 reply->time = last_input_time;
3250 /* set/get the current cursor */
3251 DECL_HANDLER(set_cursor)
3253 struct msg_queue *queue = get_current_queue();
3254 struct thread_input *input;
3256 if (!queue) return;
3257 input = queue->input;
3259 reply->prev_handle = input->cursor;
3260 reply->prev_count = input->cursor_count;
3261 reply->prev_x = input->desktop->cursor.x;
3262 reply->prev_y = input->desktop->cursor.y;
3264 if (req->flags & SET_CURSOR_HANDLE)
3266 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3268 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3269 return;
3271 input->cursor = req->handle;
3273 if (req->flags & SET_CURSOR_COUNT)
3275 queue->cursor_count += req->show_count;
3276 input->cursor_count += req->show_count;
3278 if (req->flags & SET_CURSOR_POS)
3280 set_cursor_pos( input->desktop, req->x, req->y );
3282 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3284 struct desktop *desktop = input->desktop;
3286 /* only the desktop owner can set the message */
3287 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3288 desktop->cursor.clip_msg = req->clip_msg;
3290 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip, 0 );
3293 reply->new_x = input->desktop->cursor.x;
3294 reply->new_y = input->desktop->cursor.y;
3295 reply->new_clip = input->desktop->cursor.clip;
3296 reply->last_change = input->desktop->cursor.last_change;
3299 /* Get the history of the 64 last cursor positions */
3300 DECL_HANDLER(get_cursor_history)
3302 cursor_pos_t *pos;
3303 unsigned int i, count = min( 64, get_reply_max_size() / sizeof(*pos) );
3305 if ((pos = set_reply_data_size( count * sizeof(*pos) )))
3306 for (i = 0; i < count; i++)
3307 pos[i] = cursor_history[(i + cursor_history_latest) % ARRAY_SIZE(cursor_history)];
3310 DECL_HANDLER(get_rawinput_buffer)
3312 struct thread_input *input = current->queue->input;
3313 data_size_t size = 0, next_size = 0;
3314 struct list *ptr;
3315 char *buf, *cur, *tmp;
3316 int count = 0, buf_size = 16 * sizeof(struct hardware_msg_data);
3318 if (!req->buffer_size) buf = NULL;
3319 else if (!(buf = mem_alloc( buf_size ))) return;
3321 cur = buf;
3322 ptr = list_head( &input->msg_list );
3323 while (ptr)
3325 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
3326 struct hardware_msg_data *data = msg->data;
3327 data_size_t extra_size = data->size - sizeof(*data);
3329 ptr = list_next( &input->msg_list, ptr );
3330 if (msg->msg != WM_INPUT) continue;
3332 next_size = req->rawinput_size + extra_size;
3333 if (size + next_size > req->buffer_size) break;
3334 if (cur + data->size > buf + get_reply_max_size()) break;
3335 if (cur + data->size > buf + buf_size)
3337 buf_size += buf_size / 2 + extra_size;
3338 if (!(tmp = realloc( buf, buf_size )))
3340 set_error( STATUS_NO_MEMORY );
3341 return;
3343 cur = tmp + (cur - buf);
3344 buf = tmp;
3347 memcpy( cur, data, data->size );
3348 list_remove( &msg->entry );
3349 free_message( msg );
3351 size += next_size;
3352 cur += sizeof(*data);
3353 count++;
3356 reply->next_size = next_size;
3357 reply->count = count;
3358 set_reply_data_ptr( buf, cur - buf );
3361 DECL_HANDLER(update_rawinput_devices)
3363 const struct rawinput_device *devices = get_req_data();
3364 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3365 const struct rawinput_device_entry *e;
3366 unsigned int i;
3368 for (i = 0; i < device_count; ++i)
3370 update_rawinput_device(&devices[i]);
3373 e = find_rawinput_device( current->process, 1, 2 );
3374 current->process->rawinput_mouse = e ? &e->device : NULL;
3375 e = find_rawinput_device( current->process, 1, 6 );
3376 current->process->rawinput_kbd = e ? &e->device : NULL;
3379 DECL_HANDLER(get_rawinput_devices)
3381 struct rawinput_device_entry *e;
3382 struct rawinput_device *devices;
3383 unsigned int i = 0, device_count = list_count( &current->process->rawinput_devices );
3384 unsigned int size = device_count * sizeof(*devices);
3386 reply->device_count = device_count;
3388 /* no buffer provided, nothing else to do */
3389 if (!get_reply_max_size()) return;
3391 if (size > get_reply_max_size())
3392 set_error( STATUS_BUFFER_TOO_SMALL );
3393 else if ((devices = set_reply_data_size( size )))
3395 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
3396 devices[i++] = e->device;