server: Use get_hardware_msg_bit consistently to classify messages.
[wine.git] / server / queue.c
blob746316a4286422e22b76ae8dfa3f0299a834477c
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"
37 #include "ntuser.h"
39 #include "handle.h"
40 #include "file.h"
41 #include "thread.h"
42 #include "process.h"
43 #include "request.h"
44 #include "user.h"
46 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
47 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
49 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
50 #define NB_MSG_KINDS (POST_MESSAGE+1)
53 struct message_result
55 struct list sender_entry; /* entry in sender list */
56 struct message *msg; /* message the result is for */
57 struct message_result *recv_next; /* next in receiver list */
58 struct msg_queue *sender; /* sender queue */
59 struct msg_queue *receiver; /* receiver queue */
60 int replied; /* has it been replied to? */
61 unsigned int error; /* error code to pass back to sender */
62 lparam_t result; /* reply result */
63 struct message *hardware_msg; /* hardware message if low-level hook result */
64 struct desktop *desktop; /* desktop for hardware message */
65 struct message *callback_msg; /* message to queue for callback */
66 void *data; /* message reply data */
67 unsigned int data_size; /* size of message reply data */
68 struct timeout_user *timeout; /* result timeout */
71 struct message
73 struct list entry; /* entry in message list */
74 enum message_type type; /* message type */
75 user_handle_t win; /* window handle */
76 unsigned int msg; /* message code */
77 lparam_t wparam; /* parameters */
78 lparam_t lparam; /* parameters */
79 int x; /* message position */
80 int y;
81 unsigned int time; /* message time */
82 void *data; /* message data for sent messages */
83 unsigned int data_size; /* size of message data */
84 unsigned int unique_id; /* unique id for nested hw message waits */
85 struct message_result *result; /* result in sender queue */
88 struct timer
90 struct list entry; /* entry in timer list */
91 abstime_t when; /* next expiration */
92 unsigned int rate; /* timer rate in ms */
93 user_handle_t win; /* window handle */
94 unsigned int msg; /* message to post */
95 lparam_t id; /* timer id */
96 lparam_t lparam; /* lparam for message */
99 struct thread_input
101 struct object obj; /* object header */
102 struct desktop *desktop; /* desktop that this thread input belongs to */
103 user_handle_t focus; /* focus window */
104 user_handle_t capture; /* capture window */
105 user_handle_t active; /* active window */
106 user_handle_t menu_owner; /* current menu owner window */
107 user_handle_t move_size; /* current moving/resizing window */
108 user_handle_t caret; /* caret window */
109 rectangle_t caret_rect; /* caret rectangle */
110 int caret_hide; /* caret hide count */
111 int caret_state; /* caret on/off state */
112 user_handle_t cursor; /* current cursor */
113 int cursor_count; /* cursor show count */
114 struct list msg_list; /* list of hardware messages */
115 unsigned char keystate[256]; /* state of each key */
116 unsigned char desktop_keystate[256]; /* desktop keystate when keystate was synced */
117 int keystate_lock; /* keystate is locked */
120 struct msg_queue
122 struct object obj; /* object header */
123 struct fd *fd; /* optional file descriptor to poll */
124 unsigned int wake_bits; /* wakeup bits */
125 unsigned int wake_mask; /* wakeup mask */
126 unsigned int changed_bits; /* changed wakeup bits */
127 unsigned int changed_mask; /* changed wakeup mask */
128 int paint_count; /* pending paint messages count */
129 int hotkey_count; /* pending hotkey messages count */
130 int quit_message; /* is there a pending quit message? */
131 int exit_code; /* exit code of pending quit message */
132 int cursor_count; /* per-queue cursor show count */
133 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
134 struct list send_result; /* stack of sent messages waiting for result */
135 struct list callback_result; /* list of callback messages waiting for result */
136 struct message_result *recv_result; /* stack of received messages waiting for result */
137 struct list pending_timers; /* list of pending timers */
138 struct list expired_timers; /* list of expired timers */
139 lparam_t next_timer_id; /* id for the next timer with a 0 window */
140 struct timeout_user *timeout; /* timeout for next timer to expire */
141 struct thread_input *input; /* thread input descriptor */
142 struct hook_table *hooks; /* hook table */
143 timeout_t last_get_msg; /* time of last get message call */
144 int keystate_lock; /* owns an input keystate lock */
147 struct hotkey
149 struct list entry; /* entry in desktop hotkey list */
150 struct msg_queue *queue; /* queue owning this hotkey */
151 user_handle_t win; /* window handle */
152 int id; /* hotkey id */
153 unsigned int vkey; /* virtual key code */
154 unsigned int flags; /* key modifiers */
157 static void msg_queue_dump( struct object *obj, int verbose );
158 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
159 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
160 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
161 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
162 static void msg_queue_destroy( struct object *obj );
163 static void msg_queue_poll_event( struct fd *fd, int event );
164 static void thread_input_dump( struct object *obj, int verbose );
165 static void thread_input_destroy( struct object *obj );
166 static void timer_callback( void *private );
168 static const struct object_ops msg_queue_ops =
170 sizeof(struct msg_queue), /* size */
171 &no_type, /* type */
172 msg_queue_dump, /* dump */
173 msg_queue_add_queue, /* add_queue */
174 msg_queue_remove_queue, /* remove_queue */
175 msg_queue_signaled, /* signaled */
176 msg_queue_satisfied, /* satisfied */
177 no_signal, /* signal */
178 no_get_fd, /* get_fd */
179 default_map_access, /* map_access */
180 default_get_sd, /* get_sd */
181 default_set_sd, /* set_sd */
182 no_get_full_name, /* get_full_name */
183 no_lookup_name, /* lookup_name */
184 no_link_name, /* link_name */
185 NULL, /* unlink_name */
186 no_open_file, /* open_file */
187 no_kernel_obj_list, /* get_kernel_obj_list */
188 no_close_handle, /* close_handle */
189 msg_queue_destroy /* destroy */
192 static const struct fd_ops msg_queue_fd_ops =
194 NULL, /* get_poll_events */
195 msg_queue_poll_event, /* poll_event */
196 NULL, /* flush */
197 NULL, /* get_fd_type */
198 NULL, /* ioctl */
199 NULL, /* queue_async */
200 NULL, /* reselect_async */
201 NULL /* cancel async */
205 static const struct object_ops thread_input_ops =
207 sizeof(struct thread_input), /* size */
208 &no_type, /* type */
209 thread_input_dump, /* dump */
210 no_add_queue, /* add_queue */
211 NULL, /* remove_queue */
212 NULL, /* signaled */
213 NULL, /* satisfied */
214 no_signal, /* signal */
215 no_get_fd, /* get_fd */
216 default_map_access, /* map_access */
217 default_get_sd, /* get_sd */
218 default_set_sd, /* set_sd */
219 no_get_full_name, /* get_full_name */
220 no_lookup_name, /* lookup_name */
221 no_link_name, /* link_name */
222 NULL, /* unlink_name */
223 no_open_file, /* open_file */
224 no_kernel_obj_list, /* get_kernel_obj_list */
225 no_close_handle, /* close_handle */
226 thread_input_destroy /* destroy */
229 /* pointer to input structure of foreground thread */
230 static unsigned int last_input_time;
232 static cursor_pos_t cursor_history[64];
233 static unsigned int cursor_history_latest;
235 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
236 static void free_message( struct message *msg );
238 /* set the caret window in a given thread input */
239 static void set_caret_window( struct thread_input *input, user_handle_t win )
241 if (!win || win != input->caret)
243 input->caret_rect.left = 0;
244 input->caret_rect.top = 0;
245 input->caret_rect.right = 0;
246 input->caret_rect.bottom = 0;
248 input->caret = win;
249 input->caret_hide = 1;
250 input->caret_state = 0;
253 /* create a thread input object */
254 static struct thread_input *create_thread_input( struct thread *thread )
256 struct thread_input *input;
258 if ((input = alloc_object( &thread_input_ops )))
260 input->focus = 0;
261 input->capture = 0;
262 input->active = 0;
263 input->menu_owner = 0;
264 input->move_size = 0;
265 input->cursor = 0;
266 input->cursor_count = 0;
267 list_init( &input->msg_list );
268 set_caret_window( input, 0 );
269 memset( input->keystate, 0, sizeof(input->keystate) );
270 input->keystate_lock = 0;
272 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
274 release_object( input );
275 return NULL;
277 memcpy( input->desktop_keystate, input->desktop->keystate, sizeof(input->desktop_keystate) );
279 return input;
282 /* create a message queue object */
283 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
285 struct thread_input *new_input = NULL;
286 struct msg_queue *queue;
287 int i;
289 if (!input)
291 if (!(new_input = create_thread_input( thread ))) return NULL;
292 input = new_input;
295 if ((queue = alloc_object( &msg_queue_ops )))
297 queue->fd = NULL;
298 queue->wake_bits = 0;
299 queue->wake_mask = 0;
300 queue->changed_bits = 0;
301 queue->changed_mask = 0;
302 queue->paint_count = 0;
303 queue->hotkey_count = 0;
304 queue->quit_message = 0;
305 queue->cursor_count = 0;
306 queue->recv_result = NULL;
307 queue->next_timer_id = 0x7fff;
308 queue->timeout = NULL;
309 queue->input = (struct thread_input *)grab_object( input );
310 queue->hooks = NULL;
311 queue->last_get_msg = current_time;
312 queue->keystate_lock = 0;
313 list_init( &queue->send_result );
314 list_init( &queue->callback_result );
315 list_init( &queue->pending_timers );
316 list_init( &queue->expired_timers );
317 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
319 thread->queue = queue;
321 if (new_input) release_object( new_input );
322 return queue;
325 /* free the message queue of a thread at thread exit */
326 void free_msg_queue( struct thread *thread )
328 remove_thread_hooks( thread );
329 if (!thread->queue) return;
330 release_object( thread->queue );
331 thread->queue = NULL;
334 /* synchronize thread input keystate with the desktop */
335 static void sync_input_keystate( struct thread_input *input )
337 int i;
338 if (!input->desktop || input->keystate_lock) return;
339 for (i = 0; i < sizeof(input->keystate); ++i)
341 if (input->desktop_keystate[i] == input->desktop->keystate[i]) continue;
342 input->keystate[i] = input->desktop_keystate[i] = input->desktop->keystate[i];
346 /* locks thread input keystate to prevent synchronization */
347 static void lock_input_keystate( struct thread_input *input )
349 input->keystate_lock++;
352 /* unlock the thread input keystate and synchronize it again */
353 static void unlock_input_keystate( struct thread_input *input )
355 input->keystate_lock--;
356 if (!input->keystate_lock) sync_input_keystate( input );
359 /* change the thread input data of a given thread */
360 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
362 struct msg_queue *queue = thread->queue;
364 if (!queue)
366 thread->queue = create_msg_queue( thread, new_input );
367 return thread->queue != NULL;
369 if (queue->input)
371 queue->input->cursor_count -= queue->cursor_count;
372 if (queue->keystate_lock) unlock_input_keystate( queue->input );
373 release_object( queue->input );
375 queue->input = (struct thread_input *)grab_object( new_input );
376 if (queue->keystate_lock) lock_input_keystate( queue->input );
377 new_input->cursor_count += queue->cursor_count;
378 return 1;
381 /* allocate a hardware message and its data */
382 static struct message *alloc_hardware_message( lparam_t info, struct hw_msg_source source,
383 unsigned int time, data_size_t extra_size )
385 struct hardware_msg_data *msg_data;
386 struct message *msg;
388 if (!(msg = mem_alloc( sizeof(*msg) ))) return NULL;
389 if (!(msg_data = mem_alloc( sizeof(*msg_data) + extra_size )))
391 free( msg );
392 return NULL;
394 memset( msg, 0, sizeof(*msg) );
395 msg->type = MSG_HARDWARE;
396 msg->time = time;
397 msg->data = msg_data;
398 msg->data_size = sizeof(*msg_data) + extra_size;
400 memset( msg_data, 0, sizeof(*msg_data) + extra_size );
401 msg_data->info = info;
402 msg_data->size = msg->data_size;
403 msg_data->source = source;
404 return msg;
407 static int update_desktop_cursor_pos( struct desktop *desktop, int x, int y )
409 int updated;
411 x = max( min( x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
412 y = max( min( y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
413 updated = (desktop->cursor.x != x || desktop->cursor.y != y);
414 desktop->cursor.x = x;
415 desktop->cursor.y = y;
416 desktop->cursor.last_change = get_tick_count();
418 return updated;
421 /* set the cursor position and queue the corresponding mouse message */
422 static void set_cursor_pos( struct desktop *desktop, int x, int y )
424 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
425 const struct rawinput_device *device;
426 struct message *msg;
428 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
430 update_desktop_cursor_pos( desktop, x, y );
431 return;
434 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
436 msg->msg = WM_MOUSEMOVE;
437 msg->x = x;
438 msg->y = y;
439 queue_hardware_message( desktop, msg, 1 );
442 /* retrieve default position and time for synthesized messages */
443 static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
445 struct desktop *desktop = queue->input->desktop;
447 *x = desktop->cursor.x;
448 *y = desktop->cursor.y;
449 *time = get_tick_count();
452 /* set the cursor clip rectangle */
453 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect, int send_clip_msg )
455 rectangle_t top_rect;
456 int x, y;
458 get_top_window_rectangle( desktop, &top_rect );
459 if (rect)
461 rectangle_t new_rect = *rect;
462 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
463 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
464 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
465 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
466 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
467 desktop->cursor.clip = new_rect;
469 else desktop->cursor.clip = top_rect;
471 if (send_clip_msg) post_desktop_message( desktop, WM_WINE_CLIPCURSOR, rect != NULL, 0 );
473 /* warp the mouse to be inside the clip rect */
474 x = max( min( desktop->cursor.x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
475 y = max( min( desktop->cursor.y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
476 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
479 /* change the foreground input and reset the cursor clip rect */
480 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
482 if (desktop->foreground_input == input) return;
483 set_clip_rectangle( desktop, NULL, 1 );
484 desktop->foreground_input = input;
487 /* get the hook table for a given thread */
488 struct hook_table *get_queue_hooks( struct thread *thread )
490 if (!thread->queue) return NULL;
491 return thread->queue->hooks;
494 /* set the hook table for a given thread, allocating the queue if needed */
495 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
497 struct msg_queue *queue = thread->queue;
498 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
499 if (queue->hooks) release_object( queue->hooks );
500 queue->hooks = hooks;
503 /* check the queue status */
504 static inline int is_signaled( struct msg_queue *queue )
506 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
509 /* set some queue bits */
510 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
512 if (bits & (QS_KEY | QS_MOUSEBUTTON))
514 if (!queue->keystate_lock) lock_input_keystate( queue->input );
515 queue->keystate_lock = 1;
517 queue->wake_bits |= bits;
518 queue->changed_bits |= bits;
519 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
522 /* clear some queue bits */
523 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
525 queue->wake_bits &= ~bits;
526 queue->changed_bits &= ~bits;
527 if (!(queue->wake_bits & (QS_KEY | QS_MOUSEBUTTON)))
529 if (queue->keystate_lock) unlock_input_keystate( queue->input );
530 queue->keystate_lock = 0;
534 /* check if message is matched by the filter */
535 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
537 return (msg >= first && msg <= last);
540 /* check whether a message filter contains at least one potential hardware message */
541 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
543 /* hardware message ranges are (in numerical order):
544 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
545 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
546 * WM_MOUSEFIRST .. WM_MOUSELAST
548 if (last < WM_NCMOUSEFIRST) return 0;
549 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
550 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
551 if (first > WM_MOUSELAST) return 0;
552 return 1;
555 /* get the QS_* bit corresponding to a given hardware message */
556 static inline int get_hardware_msg_bit( struct message *msg )
558 if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
559 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
560 if (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST) return QS_KEY;
561 return QS_MOUSEBUTTON;
564 /* get the current thread queue, creating it if needed */
565 static inline struct msg_queue *get_current_queue(void)
567 struct msg_queue *queue = current->queue;
568 if (!queue) queue = create_msg_queue( current, NULL );
569 return queue;
572 /* get a (pseudo-)unique id to tag hardware messages */
573 static inline unsigned int get_unique_id(void)
575 static unsigned int id;
576 if (!++id) id = 1; /* avoid an id of 0 */
577 return id;
580 /* try to merge a message with the last in the list; return 1 if successful */
581 static int merge_message( struct thread_input *input, const struct message *msg )
583 struct message *prev;
584 struct list *ptr;
586 if (msg->msg != WM_MOUSEMOVE) return 0;
587 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
589 prev = LIST_ENTRY( ptr, struct message, entry );
590 if (prev->msg != WM_INPUT) break;
592 if (!ptr) return 0;
593 if (prev->result) return 0;
594 if (prev->win && msg->win && prev->win != msg->win) return 0;
595 if (prev->msg != msg->msg) return 0;
596 if (prev->type != msg->type) return 0;
597 /* now we can merge it */
598 prev->wparam = msg->wparam;
599 prev->lparam = msg->lparam;
600 prev->x = msg->x;
601 prev->y = msg->y;
602 prev->time = msg->time;
603 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
605 struct hardware_msg_data *prev_data = prev->data;
606 struct hardware_msg_data *msg_data = msg->data;
607 prev_data->info = msg_data->info;
609 list_remove( ptr );
610 list_add_tail( &input->msg_list, ptr );
611 return 1;
614 /* free a result structure */
615 static void free_result( struct message_result *result )
617 if (result->timeout) remove_timeout_user( result->timeout );
618 free( result->data );
619 if (result->callback_msg) free_message( result->callback_msg );
620 if (result->hardware_msg) free_message( result->hardware_msg );
621 if (result->desktop) release_object( result->desktop );
622 free( result );
625 /* remove the result from the sender list it is on */
626 static inline void remove_result_from_sender( struct message_result *result )
628 assert( result->sender );
630 list_remove( &result->sender_entry );
631 result->sender = NULL;
632 if (!result->receiver) free_result( result );
635 /* store the message result in the appropriate structure */
636 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
638 res->result = result;
639 res->error = error;
640 res->replied = 1;
641 if (res->timeout)
643 remove_timeout_user( res->timeout );
644 res->timeout = NULL;
647 if (res->hardware_msg)
649 if (!error && result) /* rejected by the hook */
650 free_message( res->hardware_msg );
651 else
652 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
654 res->hardware_msg = NULL;
657 if (res->sender)
659 if (res->callback_msg)
661 /* queue the callback message in the sender queue */
662 struct callback_msg_data *data = res->callback_msg->data;
663 data->result = result;
664 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
665 set_queue_bits( res->sender, QS_SENDMESSAGE );
666 res->callback_msg = NULL;
667 remove_result_from_sender( res );
669 else
671 /* wake sender queue if waiting on this result */
672 if (list_head(&res->sender->send_result) == &res->sender_entry)
673 set_queue_bits( res->sender, QS_SMRESULT );
676 else if (!res->receiver) free_result( res );
679 /* free a message when deleting a queue or window */
680 static void free_message( struct message *msg )
682 struct message_result *result = msg->result;
683 if (result)
685 result->msg = NULL;
686 result->receiver = NULL;
687 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
689 free( msg->data );
690 free( msg );
693 /* remove (and free) a message from a message list */
694 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
695 enum message_kind kind )
697 list_remove( &msg->entry );
698 switch(kind)
700 case SEND_MESSAGE:
701 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
702 break;
703 case POST_MESSAGE:
704 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
705 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
706 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
707 clear_queue_bits( queue, QS_HOTKEY );
708 break;
710 free_message( msg );
713 /* message timed out without getting a reply */
714 static void result_timeout( void *private )
716 struct message_result *result = private;
718 assert( !result->replied );
720 result->timeout = NULL;
722 if (result->msg) /* not received yet */
724 struct message *msg = result->msg;
726 result->msg = NULL;
727 msg->result = NULL;
728 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
729 result->receiver = NULL;
731 store_message_result( result, 0, STATUS_TIMEOUT );
734 /* allocate and fill a message result structure */
735 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
736 struct msg_queue *recv_queue,
737 struct message *msg, timeout_t timeout )
739 struct message_result *result = mem_alloc( sizeof(*result) );
740 if (result)
742 result->msg = msg;
743 result->sender = send_queue;
744 result->receiver = recv_queue;
745 result->replied = 0;
746 result->data = NULL;
747 result->data_size = 0;
748 result->timeout = NULL;
749 result->hardware_msg = NULL;
750 result->desktop = NULL;
751 result->callback_msg = NULL;
753 if (msg->type == MSG_CALLBACK)
755 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
757 if (!callback_msg)
759 free( result );
760 return NULL;
762 callback_msg->type = MSG_CALLBACK_RESULT;
763 callback_msg->win = msg->win;
764 callback_msg->msg = msg->msg;
765 callback_msg->wparam = 0;
766 callback_msg->lparam = 0;
767 callback_msg->time = get_tick_count();
768 callback_msg->result = NULL;
769 /* steal the data from the original message */
770 callback_msg->data = msg->data;
771 callback_msg->data_size = msg->data_size;
772 msg->data = NULL;
773 msg->data_size = 0;
775 result->callback_msg = callback_msg;
776 list_add_head( &send_queue->callback_result, &result->sender_entry );
778 else if (send_queue)
780 list_add_head( &send_queue->send_result, &result->sender_entry );
781 clear_queue_bits( send_queue, QS_SMRESULT );
784 if (timeout != TIMEOUT_INFINITE)
785 result->timeout = add_timeout_user( timeout, result_timeout, result );
787 return result;
790 /* receive a message, removing it from the sent queue */
791 static void receive_message( struct msg_queue *queue, struct message *msg,
792 struct get_message_reply *reply )
794 struct message_result *result = msg->result;
796 reply->total = msg->data_size;
797 if (msg->data_size > get_reply_max_size())
799 set_error( STATUS_BUFFER_OVERFLOW );
800 return;
802 reply->type = msg->type;
803 reply->win = msg->win;
804 reply->msg = msg->msg;
805 reply->wparam = msg->wparam;
806 reply->lparam = msg->lparam;
807 reply->x = msg->x;
808 reply->y = msg->y;
809 reply->time = msg->time;
811 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
813 list_remove( &msg->entry );
814 /* put the result on the receiver result stack */
815 if (result)
817 result->msg = NULL;
818 result->recv_next = queue->recv_result;
819 queue->recv_result = result;
821 free( msg );
822 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
825 /* set the result of the current received message */
826 static void reply_message( struct msg_queue *queue, lparam_t result,
827 unsigned int error, int remove, const void *data, data_size_t len )
829 struct message_result *res = queue->recv_result;
831 if (remove)
833 queue->recv_result = res->recv_next;
834 res->receiver = NULL;
835 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
837 free_result( res );
838 return;
841 if (!res->replied)
843 if (len && (res->data = memdup( data, len ))) res->data_size = len;
844 store_message_result( res, result, error );
848 static int match_window( user_handle_t win, user_handle_t msg_win )
850 if (!win) return 1;
851 if (win == -1 || win == 1) return !msg_win;
852 if (msg_win == win) return 1;
853 return is_child_window( win, msg_win );
856 /* retrieve a posted message */
857 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
858 unsigned int first, unsigned int last, unsigned int flags,
859 struct get_message_reply *reply )
861 struct message *msg;
863 /* check against the filters */
864 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
866 if (!match_window( win, msg->win )) continue;
867 if (!check_msg_filter( msg->msg, first, last )) continue;
868 goto found; /* found one */
870 return 0;
872 /* return it to the app */
873 found:
874 reply->total = msg->data_size;
875 if (msg->data_size > get_reply_max_size())
877 set_error( STATUS_BUFFER_OVERFLOW );
878 return 1;
880 reply->type = msg->type;
881 reply->win = msg->win;
882 reply->msg = msg->msg;
883 reply->wparam = msg->wparam;
884 reply->lparam = msg->lparam;
885 reply->x = msg->x;
886 reply->y = msg->y;
887 reply->time = msg->time;
889 if (flags & PM_REMOVE)
891 if (msg->data)
893 set_reply_data_ptr( msg->data, msg->data_size );
894 msg->data = NULL;
895 msg->data_size = 0;
897 remove_queue_message( queue, msg, POST_MESSAGE );
899 else if (msg->data) set_reply_data( msg->data, msg->data_size );
901 return 1;
904 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
905 struct get_message_reply *reply )
907 if (queue->quit_message)
909 reply->total = 0;
910 reply->type = MSG_POSTED;
911 reply->win = 0;
912 reply->msg = WM_QUIT;
913 reply->wparam = queue->exit_code;
914 reply->lparam = 0;
916 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
918 if (flags & PM_REMOVE)
920 queue->quit_message = 0;
921 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
922 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
924 return 1;
926 else
927 return 0;
930 /* empty a message list and free all the messages */
931 static void empty_msg_list( struct list *list )
933 struct list *ptr;
935 while ((ptr = list_head( list )) != NULL)
937 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
938 list_remove( &msg->entry );
939 free_message( msg );
943 /* cleanup all pending results when deleting a queue */
944 static void cleanup_results( struct msg_queue *queue )
946 struct list *entry;
948 while ((entry = list_head( &queue->send_result )) != NULL)
950 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
953 while ((entry = list_head( &queue->callback_result )) != NULL)
955 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
958 while (queue->recv_result)
959 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
962 /* check if the thread owning the queue is hung (not checking for messages) */
963 static int is_queue_hung( struct msg_queue *queue )
965 struct wait_queue_entry *entry;
967 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
968 return 0; /* less than 5 seconds since last get message -> not hung */
970 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
972 if (get_wait_queue_thread(entry)->queue == queue)
973 return 0; /* thread is waiting on queue -> not hung */
975 return 1;
978 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
980 struct msg_queue *queue = (struct msg_queue *)obj;
981 struct process *process = get_wait_queue_thread(entry)->process;
983 /* a thread can only wait on its own queue */
984 if (get_wait_queue_thread(entry)->queue != queue)
986 set_error( STATUS_ACCESS_DENIED );
987 return 0;
989 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
991 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
992 set_fd_events( queue->fd, POLLIN );
993 add_queue( obj, entry );
994 return 1;
997 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
999 struct msg_queue *queue = (struct msg_queue *)obj;
1001 remove_queue( obj, entry );
1002 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
1003 set_fd_events( queue->fd, 0 );
1006 static void msg_queue_dump( struct object *obj, int verbose )
1008 struct msg_queue *queue = (struct msg_queue *)obj;
1009 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
1010 queue->wake_bits, queue->wake_mask );
1013 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
1015 struct msg_queue *queue = (struct msg_queue *)obj;
1016 int ret = 0;
1018 if (queue->fd)
1020 if ((ret = check_fd_events( queue->fd, POLLIN )))
1021 /* stop waiting on select() if we are signaled */
1022 set_fd_events( queue->fd, 0 );
1023 else if (!list_empty( &obj->wait_queue ))
1024 /* restart waiting on poll() if we are no longer signaled */
1025 set_fd_events( queue->fd, POLLIN );
1027 return ret || is_signaled( queue );
1030 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
1032 struct msg_queue *queue = (struct msg_queue *)obj;
1033 queue->wake_mask = 0;
1034 queue->changed_mask = 0;
1037 static void msg_queue_destroy( struct object *obj )
1039 struct msg_queue *queue = (struct msg_queue *)obj;
1040 struct list *ptr;
1041 struct hotkey *hotkey, *hotkey2;
1042 int i;
1044 cleanup_results( queue );
1045 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
1047 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
1049 if (hotkey->queue == queue)
1051 list_remove( &hotkey->entry );
1052 free( hotkey );
1056 while ((ptr = list_head( &queue->pending_timers )))
1058 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1059 list_remove( &timer->entry );
1060 free( timer );
1062 while ((ptr = list_head( &queue->expired_timers )))
1064 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1065 list_remove( &timer->entry );
1066 free( timer );
1068 if (queue->timeout) remove_timeout_user( queue->timeout );
1069 queue->input->cursor_count -= queue->cursor_count;
1070 if (queue->keystate_lock) unlock_input_keystate( queue->input );
1071 release_object( queue->input );
1072 if (queue->hooks) release_object( queue->hooks );
1073 if (queue->fd) release_object( queue->fd );
1076 static void msg_queue_poll_event( struct fd *fd, int event )
1078 struct msg_queue *queue = get_fd_user( fd );
1079 assert( queue->obj.ops == &msg_queue_ops );
1081 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1082 else set_fd_events( queue->fd, 0 );
1083 wake_up( &queue->obj, 0 );
1086 static void thread_input_dump( struct object *obj, int verbose )
1088 struct thread_input *input = (struct thread_input *)obj;
1089 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
1090 input->focus, input->capture, input->active );
1093 static void thread_input_destroy( struct object *obj )
1095 struct thread_input *input = (struct thread_input *)obj;
1097 empty_msg_list( &input->msg_list );
1098 if (input->desktop)
1100 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
1101 release_object( input->desktop );
1105 /* fix the thread input data when a window is destroyed */
1106 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1108 struct thread_input *input = queue->input;
1110 if (window == input->focus) input->focus = 0;
1111 if (window == input->capture) input->capture = 0;
1112 if (window == input->active) input->active = 0;
1113 if (window == input->menu_owner) input->menu_owner = 0;
1114 if (window == input->move_size) input->move_size = 0;
1115 if (window == input->caret) set_caret_window( input, 0 );
1118 /* check if the specified window can be set in the input data of a given queue */
1119 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1121 struct thread *thread;
1122 int ret = 0;
1124 if (!window) return 1; /* we can always clear the data */
1126 if ((thread = get_window_thread( window )))
1128 ret = (queue->input == thread->queue->input);
1129 if (!ret) set_error( STATUS_ACCESS_DENIED );
1130 release_object( thread );
1132 else set_error( STATUS_INVALID_HANDLE );
1134 return ret;
1137 /* make sure the specified thread has a queue */
1138 int init_thread_queue( struct thread *thread )
1140 if (thread->queue) return 1;
1141 return (create_msg_queue( thread, NULL ) != NULL);
1144 /* attach two thread input data structures */
1145 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1147 struct desktop *desktop;
1148 struct thread_input *input;
1149 int ret;
1151 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1152 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1153 input = (struct thread_input *)grab_object( thread_to->queue->input );
1154 if (input->desktop != desktop)
1156 set_error( STATUS_ACCESS_DENIED );
1157 release_object( input );
1158 release_object( desktop );
1159 return 0;
1161 release_object( desktop );
1163 if (thread_from->queue)
1165 if (!input->focus) input->focus = thread_from->queue->input->focus;
1166 if (!input->active) input->active = thread_from->queue->input->active;
1169 ret = assign_thread_input( thread_from, input );
1170 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1171 release_object( input );
1172 return ret;
1175 /* detach two thread input data structures */
1176 void detach_thread_input( struct thread *thread_from )
1178 struct thread *thread;
1179 struct thread_input *input, *old_input = thread_from->queue->input;
1181 if ((input = create_thread_input( thread_from )))
1183 if (old_input->focus && (thread = get_window_thread( old_input->focus )))
1185 if (thread == thread_from)
1187 input->focus = old_input->focus;
1188 old_input->focus = 0;
1190 release_object( thread );
1192 if (old_input->active && (thread = get_window_thread( old_input->active )))
1194 if (thread == thread_from)
1196 input->active = old_input->active;
1197 old_input->active = 0;
1199 release_object( thread );
1201 assign_thread_input( thread_from, input );
1202 release_object( input );
1207 /* set the next timer to expire */
1208 static void set_next_timer( struct msg_queue *queue )
1210 struct list *ptr;
1212 if (queue->timeout)
1214 remove_timeout_user( queue->timeout );
1215 queue->timeout = NULL;
1217 if ((ptr = list_head( &queue->pending_timers )))
1219 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1220 queue->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, queue );
1222 /* set/clear QS_TIMER bit */
1223 if (list_empty( &queue->expired_timers ))
1224 clear_queue_bits( queue, QS_TIMER );
1225 else
1226 set_queue_bits( queue, QS_TIMER );
1229 /* find a timer from its window and id */
1230 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1231 unsigned int msg, lparam_t id )
1233 struct list *ptr;
1235 /* we need to search both lists */
1237 LIST_FOR_EACH( ptr, &queue->pending_timers )
1239 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1240 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1242 LIST_FOR_EACH( ptr, &queue->expired_timers )
1244 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1245 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1247 return NULL;
1250 /* callback for the next timer expiration */
1251 static void timer_callback( void *private )
1253 struct msg_queue *queue = private;
1254 struct list *ptr;
1256 queue->timeout = NULL;
1257 /* move on to the next timer */
1258 ptr = list_head( &queue->pending_timers );
1259 list_remove( ptr );
1260 list_add_tail( &queue->expired_timers, ptr );
1261 set_next_timer( queue );
1264 /* link a timer at its rightful place in the queue list */
1265 static void link_timer( struct msg_queue *queue, struct timer *timer )
1267 struct list *ptr;
1269 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1271 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1272 if (t->when <= timer->when) break;
1274 list_add_before( ptr, &timer->entry );
1277 /* remove a timer from the queue timer list and free it */
1278 static void free_timer( struct msg_queue *queue, struct timer *timer )
1280 list_remove( &timer->entry );
1281 free( timer );
1282 set_next_timer( queue );
1285 /* restart an expired timer */
1286 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1288 list_remove( &timer->entry );
1289 while (-timer->when <= monotonic_time) timer->when -= (timeout_t)timer->rate * 10000;
1290 link_timer( queue, timer );
1291 set_next_timer( queue );
1294 /* find an expired timer matching the filtering parameters */
1295 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1296 unsigned int get_first, unsigned int get_last,
1297 int remove )
1299 struct list *ptr;
1301 LIST_FOR_EACH( ptr, &queue->expired_timers )
1303 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1304 if (win && timer->win != win) continue;
1305 if (check_msg_filter( timer->msg, get_first, get_last ))
1307 if (remove) restart_timer( queue, timer );
1308 return timer;
1311 return NULL;
1314 /* add a timer */
1315 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1317 struct timer *timer = mem_alloc( sizeof(*timer) );
1318 if (timer)
1320 timer->rate = max( rate, 1 );
1321 timer->when = -monotonic_time - (timeout_t)timer->rate * 10000;
1322 link_timer( queue, timer );
1323 /* check if we replaced the next timer */
1324 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1326 return timer;
1329 /* change the input key state for a given key */
1330 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1332 if (down)
1334 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1335 keystate[key] |= down;
1337 else keystate[key] &= ~0x80;
1340 /* update the input key state for a keyboard message */
1341 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1342 unsigned int msg, lparam_t wparam )
1344 unsigned char key;
1345 int down = 0;
1347 switch (msg)
1349 case WM_LBUTTONDOWN:
1350 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1351 /* fall through */
1352 case WM_LBUTTONUP:
1353 set_input_key_state( keystate, VK_LBUTTON, down );
1354 break;
1355 case WM_MBUTTONDOWN:
1356 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1357 /* fall through */
1358 case WM_MBUTTONUP:
1359 set_input_key_state( keystate, VK_MBUTTON, down );
1360 break;
1361 case WM_RBUTTONDOWN:
1362 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1363 /* fall through */
1364 case WM_RBUTTONUP:
1365 set_input_key_state( keystate, VK_RBUTTON, down );
1366 break;
1367 case WM_XBUTTONDOWN:
1368 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1369 /* fall through */
1370 case WM_XBUTTONUP:
1371 if (wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1372 else if (wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1373 break;
1374 case WM_KEYDOWN:
1375 case WM_SYSKEYDOWN:
1376 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1377 /* fall through */
1378 case WM_KEYUP:
1379 case WM_SYSKEYUP:
1380 key = (unsigned char)wparam;
1381 set_input_key_state( keystate, key, down );
1382 switch(key)
1384 case VK_LCONTROL:
1385 case VK_RCONTROL:
1386 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1387 set_input_key_state( keystate, VK_CONTROL, down );
1388 break;
1389 case VK_LMENU:
1390 case VK_RMENU:
1391 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1392 set_input_key_state( keystate, VK_MENU, down );
1393 break;
1394 case VK_LSHIFT:
1395 case VK_RSHIFT:
1396 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1397 set_input_key_state( keystate, VK_SHIFT, down );
1398 break;
1400 break;
1404 /* update the desktop key state according to a mouse message flags */
1405 static void update_desktop_mouse_state( struct desktop *desktop, unsigned int flags,
1406 int x, int y, lparam_t wparam )
1408 if (flags & MOUSEEVENTF_MOVE)
1409 update_desktop_cursor_pos( desktop, x, y );
1410 if (flags & MOUSEEVENTF_LEFTDOWN)
1411 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONDOWN, wparam );
1412 if (flags & MOUSEEVENTF_LEFTUP)
1413 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONUP, wparam );
1414 if (flags & MOUSEEVENTF_RIGHTDOWN)
1415 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONDOWN, wparam );
1416 if (flags & MOUSEEVENTF_RIGHTUP)
1417 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONUP, wparam );
1418 if (flags & MOUSEEVENTF_MIDDLEDOWN)
1419 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONDOWN, wparam );
1420 if (flags & MOUSEEVENTF_MIDDLEUP)
1421 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONUP, wparam );
1422 if (flags & MOUSEEVENTF_XDOWN)
1423 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONDOWN, wparam );
1424 if (flags & MOUSEEVENTF_XUP)
1425 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONUP, wparam );
1428 /* release the hardware message currently being processed by the given thread */
1429 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id )
1431 struct thread_input *input = queue->input;
1432 struct message *msg, *other;
1433 int clr_bit;
1435 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1437 if (msg->unique_id == hw_id) break;
1439 if (&msg->entry == &input->msg_list) return; /* not found */
1441 /* clear the queue bit for that message */
1442 clr_bit = get_hardware_msg_bit( msg );
1443 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1445 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1447 clr_bit = 0;
1448 break;
1451 if (clr_bit) clear_queue_bits( queue, clr_bit );
1453 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1454 list_remove( &msg->entry );
1455 free_message( msg );
1458 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1460 struct hotkey *hotkey;
1461 unsigned int modifiers = 0;
1463 if (msg->msg != WM_KEYDOWN && msg->msg != WM_SYSKEYDOWN) return 0;
1465 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1466 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1467 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1468 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1470 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1472 if (hotkey->vkey != msg->wparam) continue;
1473 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1476 return 0;
1478 found:
1479 msg->type = MSG_POSTED;
1480 msg->win = hotkey->win;
1481 msg->msg = WM_HOTKEY;
1482 msg->wparam = hotkey->id;
1483 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1485 free( msg->data );
1486 msg->data = NULL;
1487 msg->data_size = 0;
1489 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1490 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1491 hotkey->queue->hotkey_count++;
1492 return 1;
1495 /* find the window that should receive a given hardware message */
1496 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1497 struct message *msg, unsigned int *msg_code,
1498 struct thread **thread )
1500 user_handle_t win = 0;
1502 *thread = NULL;
1503 *msg_code = msg->msg;
1504 switch (get_hardware_msg_bit( msg ))
1506 case QS_RAWINPUT:
1507 if (!(win = msg->win) && input) win = input->focus;
1508 break;
1509 case QS_KEY:
1510 if (input && !(win = input->focus))
1512 win = input->active;
1513 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1515 break;
1516 case QS_MOUSEMOVE:
1517 case QS_MOUSEBUTTON:
1518 if (!input || !(win = input->capture))
1520 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1521 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1522 *thread = window_thread_from_point( win, msg->x, msg->y );
1524 break;
1527 if (!*thread)
1528 *thread = get_window_thread( win );
1529 return win;
1532 static struct rawinput_device *find_rawinput_device( struct process *process, unsigned short usage_page, unsigned short usage )
1534 struct rawinput_device *device, *end;
1536 for (device = process->rawinput_devices, end = device + process->rawinput_device_count; device != end; device++)
1538 if (device->usage_page != usage_page || device->usage != usage) continue;
1539 return device;
1542 return NULL;
1545 static void prepend_cursor_history( int x, int y, unsigned int time, lparam_t info )
1547 cursor_pos_t *pos = &cursor_history[--cursor_history_latest % ARRAY_SIZE(cursor_history)];
1549 pos->x = x;
1550 pos->y = y;
1551 pos->time = time;
1552 pos->info = info;
1555 /* queue a hardware message into a given thread input */
1556 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1558 user_handle_t win;
1559 struct thread *thread;
1560 struct thread_input *input;
1561 struct hardware_msg_data *msg_data = msg->data;
1562 unsigned int msg_code;
1564 update_input_key_state( desktop, desktop->keystate, msg->msg, msg->wparam );
1565 last_input_time = get_tick_count();
1566 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1568 switch (get_hardware_msg_bit( msg ))
1570 case QS_KEY:
1571 if (queue_hotkey_message( desktop, msg )) return;
1572 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1573 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1574 msg->lparam &= ~(KF_EXTENDED << 16);
1575 break;
1576 case QS_MOUSEMOVE:
1577 prepend_cursor_history( msg->x, msg->y, msg->time, msg_data->info );
1578 if (update_desktop_cursor_pos( desktop, msg->x, msg->y )) always_queue = 1;
1579 /* fallthrough */
1580 case QS_MOUSEBUTTON:
1581 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1582 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1583 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1584 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1585 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1586 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1587 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1588 break;
1590 msg->x = desktop->cursor.x;
1591 msg->y = desktop->cursor.y;
1593 if (msg->win && (thread = get_window_thread( msg->win )))
1595 input = thread->queue->input;
1596 release_object( thread );
1598 else input = desktop->foreground_input;
1600 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1601 if (!win || !thread)
1603 if (input) update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1604 free_message( msg );
1605 return;
1607 input = thread->queue->input;
1609 if (win != desktop->cursor.win) always_queue = 1;
1610 desktop->cursor.win = win;
1612 if (!always_queue || merge_message( input, msg )) free_message( msg );
1613 else
1615 msg->unique_id = 0; /* will be set once we return it to the app */
1616 list_add_tail( &input->msg_list, &msg->entry );
1617 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1619 release_object( thread );
1622 /* send the low-level hook message for a given hardware message */
1623 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1624 const hw_input_t *input, struct msg_queue *sender )
1626 struct thread *hook_thread;
1627 struct msg_queue *queue;
1628 struct message *msg;
1629 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1630 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1632 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1633 if (!(queue = hook_thread->queue)) return 0;
1634 if (is_queue_hung( queue )) return 0;
1636 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1638 msg->type = MSG_HOOK_LL;
1639 msg->win = 0;
1640 msg->msg = id;
1641 msg->wparam = hardware_msg->msg;
1642 msg->x = hardware_msg->x;
1643 msg->y = hardware_msg->y;
1644 msg->time = hardware_msg->time;
1645 msg->data_size = hardware_msg->data_size;
1646 msg->result = NULL;
1648 if (input->type == INPUT_KEYBOARD)
1650 unsigned short vkey = input->kbd.vkey;
1651 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1652 msg->lparam = (input->kbd.scan << 16) | vkey;
1654 else msg->lparam = input->mouse.data << 16;
1656 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1657 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1659 free_message( msg );
1660 return 0;
1662 msg->result->hardware_msg = hardware_msg;
1663 msg->result->desktop = (struct desktop *)grab_object( desktop );
1664 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1665 set_queue_bits( queue, QS_SENDMESSAGE );
1666 return 1;
1669 /* get the foreground thread for a desktop and a window receiving input */
1670 static struct thread *get_foreground_thread( struct desktop *desktop, user_handle_t window )
1672 /* if desktop has no foreground process, assume the receiving window is */
1673 if (desktop->foreground_input) return get_window_thread( desktop->foreground_input->focus );
1674 if (window) return get_window_thread( window );
1675 return NULL;
1678 struct rawinput_message
1680 struct thread *foreground;
1681 struct desktop *desktop;
1682 struct hw_msg_source source;
1683 unsigned int time;
1684 unsigned int message;
1685 struct hardware_msg_data data;
1686 const void *hid_report;
1689 /* check if process is supposed to receive a WM_INPUT message and eventually queue it */
1690 static int queue_rawinput_message( struct process* process, void *arg )
1692 const struct rawinput_message* raw_msg = arg;
1693 const struct rawinput_device *device = NULL;
1694 struct desktop *target_desktop = NULL, *desktop = NULL;
1695 struct thread *target_thread = NULL, *foreground = NULL;
1696 struct message *msg;
1697 data_size_t report_size;
1698 int wparam = RIM_INPUT;
1700 if (raw_msg->data.rawinput.type == RIM_TYPEMOUSE)
1701 device = process->rawinput_mouse;
1702 else if (raw_msg->data.rawinput.type == RIM_TYPEKEYBOARD)
1703 device = process->rawinput_kbd;
1704 else
1705 device = find_rawinput_device( process, raw_msg->data.rawinput.hid.usage_page, raw_msg->data.rawinput.hid.usage );
1706 if (!device) return 0;
1708 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && !(device->flags & RIDEV_DEVNOTIFY)) return 0;
1710 if (raw_msg->desktop) desktop = (struct desktop *)grab_object( raw_msg->desktop );
1711 else if (!(desktop = get_desktop_obj( process, process->desktop, 0 ))) goto done;
1713 if (raw_msg->foreground) foreground = (struct thread *)grab_object( raw_msg->foreground );
1714 else if (!(foreground = get_foreground_thread( desktop, 0 ))) goto done;
1716 if (process != foreground->process)
1718 if (raw_msg->message == WM_INPUT && !(device->flags & RIDEV_INPUTSINK)) goto done;
1719 if (!(target_thread = get_window_thread( device->target ))) goto done;
1720 if (!(target_desktop = get_thread_desktop( target_thread, 0 ))) goto done;
1721 if (target_desktop != desktop) goto done;
1722 wparam = RIM_INPUTSINK;
1725 if (raw_msg->data.rawinput.type != RIM_TYPEHID || !raw_msg->hid_report) report_size = 0;
1726 else report_size = raw_msg->data.size - sizeof(raw_msg->data);
1728 if (!(msg = alloc_hardware_message( raw_msg->data.info, raw_msg->source, raw_msg->time, report_size )))
1729 goto done;
1731 msg->win = device->target;
1732 msg->msg = raw_msg->message;
1733 msg->wparam = wparam;
1734 msg->lparam = 0;
1735 memcpy( msg->data, &raw_msg->data, sizeof(raw_msg->data) );
1736 if (report_size) memcpy( (struct hardware_msg_data *)msg->data + 1, raw_msg->hid_report, report_size );
1738 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && raw_msg->data.rawinput.type == RIM_TYPEHID)
1740 msg->wparam = raw_msg->data.rawinput.hid.param;
1741 msg->lparam = raw_msg->data.rawinput.hid.device;
1744 queue_hardware_message( desktop, msg, 1 );
1746 done:
1747 if (target_thread) release_object( target_thread );
1748 if (target_desktop) release_object( target_desktop );
1749 if (foreground) release_object( foreground );
1750 if (desktop) release_object( desktop );
1751 return 0;
1754 /* queue a hardware message for a mouse event */
1755 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1756 unsigned int origin, struct msg_queue *sender )
1758 const struct rawinput_device *device;
1759 struct hardware_msg_data *msg_data;
1760 struct rawinput_message raw_msg;
1761 struct message *msg;
1762 struct thread *foreground;
1763 unsigned int i, time, flags;
1764 struct hw_msg_source source = { IMDT_MOUSE, origin };
1765 int wait = 0, x, y;
1767 static const unsigned int messages[] =
1769 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1770 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1771 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1772 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1773 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1774 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1775 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1776 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1777 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1778 0, /* 0x0200 = unused */
1779 0, /* 0x0400 = unused */
1780 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1781 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1784 desktop->cursor.last_change = get_tick_count();
1785 flags = input->mouse.flags;
1786 time = input->mouse.time;
1787 if (!time) time = desktop->cursor.last_change;
1789 if (flags & MOUSEEVENTF_MOVE)
1791 if (flags & MOUSEEVENTF_ABSOLUTE)
1793 x = input->mouse.x;
1794 y = input->mouse.y;
1795 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1796 x == desktop->cursor.x && y == desktop->cursor.y)
1797 flags &= ~MOUSEEVENTF_MOVE;
1799 else
1801 x = desktop->cursor.x + input->mouse.x;
1802 y = desktop->cursor.y + input->mouse.y;
1805 else
1807 x = desktop->cursor.x;
1808 y = desktop->cursor.y;
1811 if ((foreground = get_foreground_thread( desktop, win )))
1813 memset( &raw_msg, 0, sizeof(raw_msg) );
1814 raw_msg.foreground = foreground;
1815 raw_msg.desktop = desktop;
1816 raw_msg.source = source;
1817 raw_msg.time = time;
1818 raw_msg.message = WM_INPUT;
1820 msg_data = &raw_msg.data;
1821 msg_data->info = input->mouse.info;
1822 msg_data->size = sizeof(*msg_data);
1823 msg_data->flags = flags;
1824 msg_data->rawinput.type = RIM_TYPEMOUSE;
1825 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1826 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1827 msg_data->rawinput.mouse.data = input->mouse.data;
1829 enum_processes( queue_rawinput_message, &raw_msg );
1830 release_object( foreground );
1833 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
1835 update_desktop_mouse_state( desktop, flags, x, y, input->mouse.data << 16 );
1836 return 0;
1839 for (i = 0; i < ARRAY_SIZE( messages ); i++)
1841 if (!messages[i]) continue;
1842 if (!(flags & (1 << i))) continue;
1843 flags &= ~(1 << i);
1845 if (!(msg = alloc_hardware_message( input->mouse.info, source, time, 0 ))) return 0;
1846 msg_data = msg->data;
1848 msg->win = get_user_full_handle( win );
1849 msg->msg = messages[i];
1850 msg->wparam = input->mouse.data << 16;
1851 msg->lparam = 0;
1852 msg->x = x;
1853 msg->y = y;
1854 if (origin == IMO_INJECTED) msg_data->flags = LLMHF_INJECTED;
1856 /* specify a sender only when sending the last message */
1857 if (!(flags & ((1 << ARRAY_SIZE( messages )) - 1)))
1859 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1860 queue_hardware_message( desktop, msg, 0 );
1862 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1863 queue_hardware_message( desktop, msg, 0 );
1865 return wait;
1868 /* queue a hardware message for a keyboard event */
1869 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1870 unsigned int origin, struct msg_queue *sender )
1872 struct hw_msg_source source = { IMDT_KEYBOARD, origin };
1873 const struct rawinput_device *device;
1874 struct hardware_msg_data *msg_data;
1875 struct rawinput_message raw_msg;
1876 struct message *msg;
1877 struct thread *foreground;
1878 unsigned char vkey = input->kbd.vkey;
1879 unsigned int message_code, time;
1880 int wait;
1882 if (!(time = input->kbd.time)) time = get_tick_count();
1884 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1886 switch (vkey)
1888 case VK_MENU:
1889 case VK_LMENU:
1890 case VK_RMENU:
1891 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1892 break;
1893 case VK_CONTROL:
1894 case VK_LCONTROL:
1895 case VK_RCONTROL:
1896 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1897 break;
1898 case VK_SHIFT:
1899 case VK_LSHIFT:
1900 case VK_RSHIFT:
1901 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1902 break;
1906 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1907 switch (vkey)
1909 case VK_LMENU:
1910 case VK_RMENU:
1911 if (input->kbd.flags & KEYEVENTF_KEYUP)
1913 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1914 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1915 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1916 message_code = WM_SYSKEYUP;
1917 desktop->keystate[VK_MENU] &= ~0x02;
1919 else
1921 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1922 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1923 message_code = WM_SYSKEYDOWN;
1924 desktop->keystate[VK_MENU] |= 0x02;
1926 break;
1928 case VK_LCONTROL:
1929 case VK_RCONTROL:
1930 /* send WM_SYSKEYUP on release if Alt still pressed */
1931 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1932 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1933 message_code = WM_SYSKEYUP;
1934 desktop->keystate[VK_MENU] &= ~0x02;
1935 break;
1937 default:
1938 /* send WM_SYSKEY for Alt-anykey and for F10 */
1939 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1940 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1941 /* fall through */
1942 case VK_F10:
1943 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1944 desktop->keystate[VK_MENU] &= ~0x02;
1945 break;
1948 if ((foreground = get_foreground_thread( desktop, win )))
1950 memset( &raw_msg, 0, sizeof(raw_msg) );
1951 raw_msg.foreground = foreground;
1952 raw_msg.desktop = desktop;
1953 raw_msg.source = source;
1954 raw_msg.time = time;
1955 raw_msg.message = WM_INPUT;
1957 msg_data = &raw_msg.data;
1958 msg_data->info = input->kbd.info;
1959 msg_data->size = sizeof(*msg_data);
1960 msg_data->flags = input->kbd.flags;
1961 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1962 msg_data->rawinput.kbd.message = message_code;
1963 msg_data->rawinput.kbd.vkey = vkey;
1964 msg_data->rawinput.kbd.scan = input->kbd.scan;
1966 enum_processes( queue_rawinput_message, &raw_msg );
1967 release_object( foreground );
1970 if ((device = current->process->rawinput_kbd) && (device->flags & RIDEV_NOLEGACY))
1972 update_input_key_state( desktop, desktop->keystate, message_code, vkey );
1973 return 0;
1976 if (!(msg = alloc_hardware_message( input->kbd.info, source, time, 0 ))) return 0;
1977 msg_data = msg->data;
1979 msg->win = get_user_full_handle( win );
1980 msg->msg = message_code;
1981 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1982 if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
1984 if (input->kbd.flags & KEYEVENTF_UNICODE && !vkey)
1986 msg->wparam = VK_PACKET;
1988 else
1990 unsigned int flags = 0;
1991 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1992 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1993 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1994 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1996 msg->wparam = vkey;
1997 msg->lparam |= flags << 16;
1998 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
2001 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
2002 queue_hardware_message( desktop, msg, 1 );
2004 return wait;
2007 /* queue a hardware message for a custom type of event */
2008 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
2009 unsigned int origin, const hw_input_t *input )
2011 struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
2012 struct hardware_msg_data *msg_data;
2013 struct rawinput_message raw_msg;
2014 struct message *msg;
2015 data_size_t report_size = 0;
2017 switch (input->hw.msg)
2019 case WM_INPUT:
2020 case WM_INPUT_DEVICE_CHANGE:
2021 memset( &raw_msg, 0, sizeof(raw_msg) );
2022 raw_msg.source = source;
2023 raw_msg.time = get_tick_count();
2024 raw_msg.message = input->hw.msg;
2026 if (input->hw.rawinput.type == RIM_TYPEHID)
2028 raw_msg.hid_report = get_req_data();
2029 report_size = input->hw.rawinput.hid.length * input->hw.rawinput.hid.count;
2032 if (report_size != get_req_data_size())
2034 set_error( STATUS_INVALID_PARAMETER );
2035 return;
2038 msg_data = &raw_msg.data;
2039 msg_data->size = sizeof(*msg_data) + report_size;
2040 msg_data->rawinput = input->hw.rawinput;
2042 enum_processes( queue_rawinput_message, &raw_msg );
2043 return;
2046 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
2048 msg->win = get_user_full_handle( win );
2049 msg->msg = input->hw.msg;
2050 msg->wparam = 0;
2051 msg->lparam = input->hw.lparam;
2052 msg->x = desktop->cursor.x;
2053 msg->y = desktop->cursor.y;
2055 queue_hardware_message( desktop, msg, 1 );
2058 /* check message filter for a hardware message */
2059 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
2060 user_handle_t filter_win, unsigned int first, unsigned int last )
2062 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
2064 /* we can only test the window for a keyboard message since the
2065 * dest window for a mouse message depends on hittest */
2066 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
2067 return 0;
2068 /* the message code is final for a keyboard message, we can simply check it */
2069 return check_msg_filter( msg_code, first, last );
2071 else /* mouse message */
2073 /* we need to check all possible values that the message can have in the end */
2075 if (check_msg_filter( msg_code, first, last )) return 1;
2076 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
2078 /* all other messages can become non-client messages */
2079 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
2081 /* clicks can become double-clicks or non-client double-clicks */
2082 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
2083 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
2085 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2086 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2088 return 0;
2093 /* find a hardware message for the given queue */
2094 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
2095 unsigned int first, unsigned int last, unsigned int flags,
2096 struct get_message_reply *reply )
2098 struct thread_input *input = thread->queue->input;
2099 struct thread *win_thread;
2100 struct list *ptr;
2101 user_handle_t win;
2102 int clear_bits, got_one = 0;
2103 unsigned int msg_code;
2105 ptr = list_head( &input->msg_list );
2106 if (hw_id)
2108 while (ptr)
2110 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2111 if (msg->unique_id == hw_id) break;
2112 ptr = list_next( &input->msg_list, ptr );
2114 if (!ptr) ptr = list_head( &input->msg_list );
2115 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
2118 if (ptr == list_head( &input->msg_list ))
2119 clear_bits = QS_INPUT;
2120 else
2121 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
2123 while (ptr)
2125 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2126 struct hardware_msg_data *data = msg->data;
2128 ptr = list_next( &input->msg_list, ptr );
2129 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
2130 if (!win || !win_thread)
2132 /* no window at all, remove it */
2133 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2134 list_remove( &msg->entry );
2135 free_message( msg );
2136 continue;
2138 if (win_thread != thread)
2140 if (win_thread->queue->input == input)
2142 /* wake the other thread */
2143 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
2144 got_one = 1;
2146 else
2148 /* for another thread input, drop it */
2149 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2150 list_remove( &msg->entry );
2151 free_message( msg );
2153 release_object( win_thread );
2154 continue;
2156 release_object( win_thread );
2158 /* if we already got a message for another thread, or if it doesn't
2159 * match the filter we skip it */
2160 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
2162 clear_bits &= ~get_hardware_msg_bit( msg );
2163 continue;
2166 reply->total = msg->data_size;
2167 if (msg->data_size > get_reply_max_size())
2169 set_error( STATUS_BUFFER_OVERFLOW );
2170 return 1;
2173 /* now we can return it */
2174 if (!msg->unique_id) msg->unique_id = get_unique_id();
2175 reply->type = MSG_HARDWARE;
2176 reply->win = win;
2177 reply->msg = msg_code;
2178 reply->wparam = msg->wparam;
2179 reply->lparam = msg->lparam;
2180 reply->x = msg->x;
2181 reply->y = msg->y;
2182 reply->time = msg->time;
2184 data->hw_id = msg->unique_id;
2185 set_reply_data( msg->data, msg->data_size );
2186 if (get_hardware_msg_bit( msg ) == QS_RAWINPUT && (flags & PM_REMOVE))
2187 release_hardware_message( current->queue, data->hw_id );
2188 return 1;
2190 /* nothing found, clear the hardware queue bits */
2191 clear_queue_bits( thread->queue, clear_bits );
2192 return 0;
2195 /* increment (or decrement if 'incr' is negative) the queue paint count */
2196 void inc_queue_paint_count( struct thread *thread, int incr )
2198 struct msg_queue *queue = thread->queue;
2200 assert( queue );
2202 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2204 if (queue->paint_count)
2205 set_queue_bits( queue, QS_PAINT );
2206 else
2207 clear_queue_bits( queue, QS_PAINT );
2211 /* remove all messages and timers belonging to a certain window */
2212 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2214 struct msg_queue *queue = thread->queue;
2215 struct list *ptr;
2216 int i;
2218 if (!queue) return;
2220 /* remove timers */
2222 ptr = list_head( &queue->pending_timers );
2223 while (ptr)
2225 struct list *next = list_next( &queue->pending_timers, ptr );
2226 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2227 if (timer->win == win) free_timer( queue, timer );
2228 ptr = next;
2230 ptr = list_head( &queue->expired_timers );
2231 while (ptr)
2233 struct list *next = list_next( &queue->expired_timers, ptr );
2234 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2235 if (timer->win == win) free_timer( queue, timer );
2236 ptr = next;
2239 /* remove messages */
2240 for (i = 0; i < NB_MSG_KINDS; i++)
2242 struct list *ptr, *next;
2244 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2246 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2247 if (msg->win == win)
2249 if (msg->msg == WM_QUIT && !queue->quit_message)
2251 queue->quit_message = 1;
2252 queue->exit_code = msg->wparam;
2254 remove_queue_message( queue, msg, i );
2259 thread_input_cleanup_window( queue, win );
2262 /* post a message to a window */
2263 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2265 struct message *msg;
2266 struct thread *thread = get_window_thread( win );
2268 if (!thread) return;
2270 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2272 msg->type = MSG_POSTED;
2273 msg->win = get_user_full_handle( win );
2274 msg->msg = message;
2275 msg->wparam = wparam;
2276 msg->lparam = lparam;
2277 msg->result = NULL;
2278 msg->data = NULL;
2279 msg->data_size = 0;
2281 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2283 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2284 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2285 if (message == WM_HOTKEY)
2287 set_queue_bits( thread->queue, QS_HOTKEY );
2288 thread->queue->hotkey_count++;
2291 release_object( thread );
2294 /* send a notify message to a window */
2295 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2297 struct message *msg;
2298 struct thread *thread = get_window_thread( win );
2300 if (!thread) return;
2302 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2304 msg->type = MSG_NOTIFY;
2305 msg->win = get_user_full_handle( win );
2306 msg->msg = message;
2307 msg->wparam = wparam;
2308 msg->lparam = lparam;
2309 msg->result = NULL;
2310 msg->data = NULL;
2311 msg->data_size = 0;
2313 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2315 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2316 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2318 release_object( thread );
2321 /* post a win event */
2322 void post_win_event( struct thread *thread, unsigned int event,
2323 user_handle_t win, unsigned int object_id,
2324 unsigned int child_id, client_ptr_t hook_proc,
2325 const WCHAR *module, data_size_t module_size,
2326 user_handle_t hook)
2328 struct message *msg;
2330 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2332 struct winevent_msg_data *data;
2334 msg->type = MSG_WINEVENT;
2335 msg->win = get_user_full_handle( win );
2336 msg->msg = event;
2337 msg->wparam = object_id;
2338 msg->lparam = child_id;
2339 msg->time = get_tick_count();
2340 msg->result = NULL;
2342 if ((data = malloc( sizeof(*data) + module_size )))
2344 data->hook = hook;
2345 data->tid = get_thread_id( current );
2346 data->hook_proc = hook_proc;
2347 memcpy( data + 1, module, module_size );
2349 msg->data = data;
2350 msg->data_size = sizeof(*data) + module_size;
2352 if (debug_level > 1)
2353 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2354 get_thread_id(thread), event, win, object_id, child_id );
2355 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2356 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2358 else
2359 free( msg );
2363 /* free all hotkeys on a desktop, optionally filtering by window */
2364 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2366 struct hotkey *hotkey, *hotkey2;
2368 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2370 if (!window || hotkey->win == window)
2372 list_remove( &hotkey->entry );
2373 free( hotkey );
2379 /* check if the thread owning the window is hung */
2380 DECL_HANDLER(is_window_hung)
2382 struct thread *thread;
2384 thread = get_window_thread( req->win );
2385 if (thread)
2387 reply->is_hung = is_queue_hung( thread->queue );
2388 release_object( thread );
2390 else reply->is_hung = 0;
2394 /* get the message queue of the current thread */
2395 DECL_HANDLER(get_msg_queue)
2397 struct msg_queue *queue = get_current_queue();
2399 reply->handle = 0;
2400 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2404 /* set the file descriptor associated to the current thread queue */
2405 DECL_HANDLER(set_queue_fd)
2407 struct msg_queue *queue = get_current_queue();
2408 struct file *file;
2409 int unix_fd;
2411 if (queue->fd) /* fd can only be set once */
2413 set_error( STATUS_ACCESS_DENIED );
2414 return;
2416 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2418 if ((unix_fd = get_file_unix_fd( file )) != -1)
2420 if ((unix_fd = dup( unix_fd )) != -1)
2421 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2422 else
2423 file_set_error();
2425 release_object( file );
2429 /* set the current message queue wakeup mask */
2430 DECL_HANDLER(set_queue_mask)
2432 struct msg_queue *queue = get_current_queue();
2434 if (queue)
2436 queue->wake_mask = req->wake_mask;
2437 queue->changed_mask = req->changed_mask;
2438 reply->wake_bits = queue->wake_bits;
2439 reply->changed_bits = queue->changed_bits;
2440 if (is_signaled( queue ))
2442 /* if skip wait is set, do what would have been done in the subsequent wait */
2443 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2444 else wake_up( &queue->obj, 0 );
2450 /* get the current message queue status */
2451 DECL_HANDLER(get_queue_status)
2453 struct msg_queue *queue = current->queue;
2454 if (queue)
2456 reply->wake_bits = queue->wake_bits;
2457 reply->changed_bits = queue->changed_bits;
2458 queue->changed_bits &= ~req->clear_bits;
2460 else reply->wake_bits = reply->changed_bits = 0;
2464 /* send a message to a thread queue */
2465 DECL_HANDLER(send_message)
2467 struct message *msg;
2468 struct msg_queue *send_queue = get_current_queue();
2469 struct msg_queue *recv_queue = NULL;
2470 struct thread *thread = NULL;
2472 if (!(thread = get_thread_from_id( req->id ))) return;
2474 if (!(recv_queue = thread->queue))
2476 set_error( STATUS_INVALID_PARAMETER );
2477 release_object( thread );
2478 return;
2480 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2482 set_error( STATUS_TIMEOUT );
2483 release_object( thread );
2484 return;
2487 if ((msg = mem_alloc( sizeof(*msg) )))
2489 msg->type = req->type;
2490 msg->win = get_user_full_handle( req->win );
2491 msg->msg = req->msg;
2492 msg->wparam = req->wparam;
2493 msg->lparam = req->lparam;
2494 msg->result = NULL;
2495 msg->data = NULL;
2496 msg->data_size = get_req_data_size();
2498 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2500 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2502 free( msg );
2503 release_object( thread );
2504 return;
2507 switch(msg->type)
2509 case MSG_OTHER_PROCESS:
2510 case MSG_ASCII:
2511 case MSG_UNICODE:
2512 case MSG_CALLBACK:
2513 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2515 free_message( msg );
2516 break;
2518 /* fall through */
2519 case MSG_NOTIFY:
2520 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2521 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2522 break;
2523 case MSG_POSTED:
2524 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2525 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2526 if (msg->msg == WM_HOTKEY)
2528 set_queue_bits( recv_queue, QS_HOTKEY );
2529 recv_queue->hotkey_count++;
2531 break;
2532 case MSG_HARDWARE: /* should use send_hardware_message instead */
2533 case MSG_CALLBACK_RESULT: /* cannot send this one */
2534 case MSG_HOOK_LL: /* generated internally */
2535 default:
2536 set_error( STATUS_INVALID_PARAMETER );
2537 free( msg );
2538 break;
2541 release_object( thread );
2544 /* send a hardware message to a thread queue */
2545 DECL_HANDLER(send_hardware_message)
2547 struct thread *thread = NULL;
2548 struct desktop *desktop;
2549 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2550 struct msg_queue *sender = get_current_queue();
2552 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2554 if (req->win)
2556 if (!(thread = get_window_thread( req->win ))) return;
2557 if (desktop != thread->queue->input->desktop)
2559 /* don't allow queuing events to a different desktop */
2560 release_object( desktop );
2561 return;
2565 reply->prev_x = desktop->cursor.x;
2566 reply->prev_y = desktop->cursor.y;
2568 switch (req->input.type)
2570 case INPUT_MOUSE:
2571 reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2572 break;
2573 case INPUT_KEYBOARD:
2574 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2575 break;
2576 case INPUT_HARDWARE:
2577 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2578 break;
2579 default:
2580 set_error( STATUS_INVALID_PARAMETER );
2582 if (thread) release_object( thread );
2584 reply->new_x = desktop->cursor.x;
2585 reply->new_y = desktop->cursor.y;
2586 release_object( desktop );
2589 /* post a quit message to the current queue */
2590 DECL_HANDLER(post_quit_message)
2592 struct msg_queue *queue = get_current_queue();
2594 if (!queue)
2595 return;
2597 queue->quit_message = 1;
2598 queue->exit_code = req->exit_code;
2599 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2602 /* get a message from the current queue */
2603 DECL_HANDLER(get_message)
2605 struct timer *timer;
2606 struct list *ptr;
2607 struct msg_queue *queue = get_current_queue();
2608 user_handle_t get_win = get_user_full_handle( req->get_win );
2609 unsigned int filter = req->flags >> 16;
2611 reply->active_hooks = get_active_hooks();
2613 if (get_win && get_win != 1 && get_win != -1 && !get_user_object( get_win, USER_WINDOW ))
2615 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2616 return;
2619 if (!queue) return;
2620 queue->last_get_msg = current_time;
2621 if (!filter) filter = QS_ALLINPUT;
2623 /* first check for sent messages */
2624 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2626 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2627 receive_message( queue, msg, reply );
2628 return;
2631 /* clear changed bits so we can wait on them if we don't find a message */
2632 if (filter & QS_POSTMESSAGE)
2634 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2635 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2637 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2638 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2640 /* then check for posted messages */
2641 if ((filter & QS_POSTMESSAGE) &&
2642 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2643 return;
2645 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2646 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2647 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2648 return;
2650 /* only check for quit messages if not posted messages pending */
2651 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2652 return;
2654 /* then check for any raw hardware message */
2655 if ((filter & QS_INPUT) &&
2656 filter_contains_hw_range( req->get_first, req->get_last ) &&
2657 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2658 return;
2660 /* now check for WM_PAINT */
2661 if ((filter & QS_PAINT) &&
2662 queue->paint_count &&
2663 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2664 (reply->win = find_window_to_repaint( get_win, current )))
2666 reply->type = MSG_POSTED;
2667 reply->msg = WM_PAINT;
2668 reply->wparam = 0;
2669 reply->lparam = 0;
2670 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2671 return;
2674 /* now check for timer */
2675 if ((filter & QS_TIMER) &&
2676 (timer = find_expired_timer( queue, get_win, req->get_first,
2677 req->get_last, (req->flags & PM_REMOVE) )))
2679 reply->type = MSG_POSTED;
2680 reply->win = timer->win;
2681 reply->msg = timer->msg;
2682 reply->wparam = timer->id;
2683 reply->lparam = timer->lparam;
2684 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2685 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2686 set_event( current->process->idle_event );
2687 return;
2690 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2691 queue->wake_mask = req->wake_mask;
2692 queue->changed_mask = req->changed_mask;
2693 set_error( STATUS_PENDING ); /* FIXME */
2697 /* reply to a sent message */
2698 DECL_HANDLER(reply_message)
2700 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2701 else if (current->queue->recv_result)
2702 reply_message( current->queue, req->result, 0, req->remove,
2703 get_req_data(), get_req_data_size() );
2707 /* accept the current hardware message */
2708 DECL_HANDLER(accept_hardware_message)
2710 if (current->queue)
2711 release_hardware_message( current->queue, req->hw_id );
2712 else
2713 set_error( STATUS_ACCESS_DENIED );
2717 /* retrieve the reply for the last message sent */
2718 DECL_HANDLER(get_message_reply)
2720 struct message_result *result;
2721 struct list *entry;
2722 struct msg_queue *queue = current->queue;
2724 if (queue)
2726 set_error( STATUS_PENDING );
2727 reply->result = 0;
2729 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2731 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2732 if (result->replied || req->cancel)
2734 if (result->replied)
2736 reply->result = result->result;
2737 set_error( result->error );
2738 if (result->data)
2740 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2741 set_reply_data_ptr( result->data, data_len );
2742 result->data = NULL;
2743 result->data_size = 0;
2746 remove_result_from_sender( result );
2748 entry = list_head( &queue->send_result );
2749 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2750 else
2752 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2753 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2754 else clear_queue_bits( queue, QS_SMRESULT );
2758 else set_error( STATUS_ACCESS_DENIED );
2762 /* set a window timer */
2763 DECL_HANDLER(set_win_timer)
2765 struct timer *timer;
2766 struct msg_queue *queue;
2767 struct thread *thread = NULL;
2768 user_handle_t win = 0;
2769 lparam_t id = req->id;
2771 if (req->win)
2773 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2775 set_error( STATUS_INVALID_HANDLE );
2776 return;
2778 if (thread->process != current->process)
2780 release_object( thread );
2781 set_error( STATUS_ACCESS_DENIED );
2782 return;
2784 queue = thread->queue;
2785 /* remove it if it existed already */
2786 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2788 else
2790 queue = get_current_queue();
2791 /* look for a timer with this id */
2792 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2794 /* free and reuse id */
2795 free_timer( queue, timer );
2797 else
2799 lparam_t end_id = queue->next_timer_id;
2801 /* find a free id for it */
2802 while (1)
2804 id = queue->next_timer_id;
2805 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2807 if (!find_timer( queue, 0, req->msg, id )) break;
2808 if (queue->next_timer_id == end_id)
2810 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2811 return;
2817 if ((timer = set_timer( queue, req->rate )))
2819 timer->win = win;
2820 timer->msg = req->msg;
2821 timer->id = id;
2822 timer->lparam = req->lparam;
2823 reply->id = id;
2825 if (thread) release_object( thread );
2828 /* kill a window timer */
2829 DECL_HANDLER(kill_win_timer)
2831 struct timer *timer;
2832 struct thread *thread;
2833 user_handle_t win = 0;
2835 if (req->win)
2837 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2839 set_error( STATUS_INVALID_HANDLE );
2840 return;
2842 if (thread->process != current->process)
2844 release_object( thread );
2845 set_error( STATUS_ACCESS_DENIED );
2846 return;
2849 else thread = (struct thread *)grab_object( current );
2851 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2852 free_timer( thread->queue, timer );
2853 else
2854 set_error( STATUS_INVALID_PARAMETER );
2856 release_object( thread );
2859 DECL_HANDLER(register_hotkey)
2861 struct desktop *desktop;
2862 user_handle_t win_handle = req->window;
2863 struct hotkey *hotkey;
2864 struct hotkey *new_hotkey = NULL;
2865 struct thread *thread;
2866 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2868 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2870 if (win_handle)
2872 if (!(win_handle = get_valid_window_handle( win_handle )))
2874 release_object( desktop );
2875 return;
2878 thread = get_window_thread( win_handle );
2879 if (thread) release_object( thread );
2881 if (thread != current)
2883 release_object( desktop );
2884 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2885 return;
2889 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2891 if (req->vkey == hotkey->vkey &&
2892 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2894 release_object( desktop );
2895 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2896 return;
2898 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2899 new_hotkey = hotkey;
2902 if (new_hotkey)
2904 reply->replaced = 1;
2905 reply->flags = new_hotkey->flags;
2906 reply->vkey = new_hotkey->vkey;
2908 else
2910 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2911 if (new_hotkey)
2913 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2914 new_hotkey->queue = current->queue;
2915 new_hotkey->win = win_handle;
2916 new_hotkey->id = req->id;
2920 if (new_hotkey)
2922 new_hotkey->flags = req->flags;
2923 new_hotkey->vkey = req->vkey;
2926 release_object( desktop );
2929 DECL_HANDLER(unregister_hotkey)
2931 struct desktop *desktop;
2932 user_handle_t win_handle = req->window;
2933 struct hotkey *hotkey;
2934 struct thread *thread;
2936 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2938 if (win_handle)
2940 if (!(win_handle = get_valid_window_handle( win_handle )))
2942 release_object( desktop );
2943 return;
2946 thread = get_window_thread( win_handle );
2947 if (thread) release_object( thread );
2949 if (thread != current)
2951 release_object( desktop );
2952 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2953 return;
2957 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2959 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2960 goto found;
2963 release_object( desktop );
2964 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2965 return;
2967 found:
2968 reply->flags = hotkey->flags;
2969 reply->vkey = hotkey->vkey;
2970 list_remove( &hotkey->entry );
2971 free( hotkey );
2972 release_object( desktop );
2975 /* attach (or detach) thread inputs */
2976 DECL_HANDLER(attach_thread_input)
2978 struct thread *thread_from = get_thread_from_id( req->tid_from );
2979 struct thread *thread_to = get_thread_from_id( req->tid_to );
2981 if (!thread_from || !thread_to)
2983 if (thread_from) release_object( thread_from );
2984 if (thread_to) release_object( thread_to );
2985 return;
2987 if (thread_from != thread_to)
2989 if (req->attach)
2991 if ((thread_to->queue || thread_to == current) &&
2992 (thread_from->queue || thread_from == current))
2993 attach_thread_input( thread_from, thread_to );
2994 else
2995 set_error( STATUS_INVALID_PARAMETER );
2997 else
2999 if (thread_from->queue && thread_to->queue &&
3000 thread_from->queue->input == thread_to->queue->input)
3001 detach_thread_input( thread_from );
3002 else
3003 set_error( STATUS_ACCESS_DENIED );
3006 else set_error( STATUS_ACCESS_DENIED );
3007 release_object( thread_from );
3008 release_object( thread_to );
3012 /* get thread input data */
3013 DECL_HANDLER(get_thread_input)
3015 struct thread *thread = NULL;
3016 struct desktop *desktop;
3017 struct thread_input *input;
3019 if (req->tid)
3021 if (!(thread = get_thread_from_id( req->tid ))) return;
3022 if (!(desktop = get_thread_desktop( thread, 0 )))
3024 release_object( thread );
3025 return;
3027 input = thread->queue ? thread->queue->input : NULL;
3029 else
3031 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3032 input = desktop->foreground_input; /* get the foreground thread info */
3035 if (input)
3037 reply->focus = input->focus;
3038 reply->capture = input->capture;
3039 reply->active = input->active;
3040 reply->menu_owner = input->menu_owner;
3041 reply->move_size = input->move_size;
3042 reply->caret = input->caret;
3043 reply->cursor = input->cursor;
3044 reply->show_count = input->cursor_count;
3045 reply->rect = input->caret_rect;
3048 /* foreground window is active window of foreground thread */
3049 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
3050 if (thread) release_object( thread );
3051 release_object( desktop );
3055 /* retrieve queue keyboard state for current thread or global async state */
3056 DECL_HANDLER(get_key_state)
3058 struct desktop *desktop;
3059 data_size_t size = min( 256, get_reply_max_size() );
3061 if (req->async) /* get global async key state */
3063 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3064 if (req->key >= 0)
3066 reply->state = desktop->keystate[req->key & 0xff];
3067 desktop->keystate[req->key & 0xff] &= ~0x40;
3069 set_reply_data( desktop->keystate, size );
3070 release_object( desktop );
3072 else
3074 struct msg_queue *queue = get_current_queue();
3075 unsigned char *keystate = queue->input->keystate;
3076 if (req->key >= 0)
3078 sync_input_keystate( queue->input );
3079 reply->state = keystate[req->key & 0xff];
3081 set_reply_data( keystate, size );
3086 /* set queue keyboard state for current thread */
3087 DECL_HANDLER(set_key_state)
3089 struct desktop *desktop;
3090 struct msg_queue *queue = get_current_queue();
3091 data_size_t size = min( 256, get_req_data_size() );
3093 memcpy( queue->input->keystate, get_req_data(), size );
3094 memcpy( queue->input->desktop_keystate, queue->input->desktop->keystate, 256 );
3095 if (req->async && (desktop = get_thread_desktop( current, 0 )))
3097 memcpy( desktop->keystate, get_req_data(), size );
3098 release_object( desktop );
3103 /* set the system foreground window */
3104 DECL_HANDLER(set_foreground_window)
3106 struct thread *thread = NULL;
3107 struct desktop *desktop;
3108 struct msg_queue *queue = get_current_queue();
3110 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3111 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
3112 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
3113 reply->send_msg_new = FALSE;
3115 if (is_valid_foreground_window( req->handle ) &&
3116 (thread = get_window_thread( req->handle )) &&
3117 thread->queue->input->desktop == desktop)
3119 set_foreground_input( desktop, thread->queue->input );
3120 reply->send_msg_new = (desktop->foreground_input != queue->input);
3122 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
3124 if (thread) release_object( thread );
3125 release_object( desktop );
3129 /* set the current thread focus window */
3130 DECL_HANDLER(set_focus_window)
3132 struct msg_queue *queue = get_current_queue();
3134 reply->previous = 0;
3135 if (queue && check_queue_input_window( queue, req->handle ))
3137 reply->previous = queue->input->focus;
3138 queue->input->focus = get_user_full_handle( req->handle );
3143 /* set the current thread active window */
3144 DECL_HANDLER(set_active_window)
3146 struct msg_queue *queue = get_current_queue();
3148 reply->previous = 0;
3149 if (queue && check_queue_input_window( queue, req->handle ))
3151 if (!req->handle || make_window_active( req->handle ))
3153 reply->previous = queue->input->active;
3154 queue->input->active = get_user_full_handle( req->handle );
3156 else set_error( STATUS_INVALID_HANDLE );
3161 /* set the current thread capture window */
3162 DECL_HANDLER(set_capture_window)
3164 struct msg_queue *queue = get_current_queue();
3166 reply->previous = reply->full_handle = 0;
3167 if (queue && check_queue_input_window( queue, req->handle ))
3169 struct thread_input *input = queue->input;
3171 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
3172 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
3174 set_error(STATUS_ACCESS_DENIED);
3175 return;
3177 reply->previous = input->capture;
3178 input->capture = get_user_full_handle( req->handle );
3179 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
3180 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
3181 reply->full_handle = input->capture;
3186 /* Set the current thread caret window */
3187 DECL_HANDLER(set_caret_window)
3189 struct msg_queue *queue = get_current_queue();
3191 reply->previous = 0;
3192 if (queue && check_queue_input_window( queue, req->handle ))
3194 struct thread_input *input = queue->input;
3196 reply->previous = input->caret;
3197 reply->old_rect = input->caret_rect;
3198 reply->old_hide = input->caret_hide;
3199 reply->old_state = input->caret_state;
3201 set_caret_window( input, get_user_full_handle(req->handle) );
3202 input->caret_rect.right = input->caret_rect.left + req->width;
3203 input->caret_rect.bottom = input->caret_rect.top + req->height;
3208 /* Set the current thread caret information */
3209 DECL_HANDLER(set_caret_info)
3211 struct msg_queue *queue = get_current_queue();
3212 struct thread_input *input;
3214 if (!queue) return;
3215 input = queue->input;
3216 reply->full_handle = input->caret;
3217 reply->old_rect = input->caret_rect;
3218 reply->old_hide = input->caret_hide;
3219 reply->old_state = input->caret_state;
3221 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3223 set_error( STATUS_ACCESS_DENIED );
3224 return;
3226 if (req->flags & SET_CARET_POS)
3228 input->caret_rect.right += req->x - input->caret_rect.left;
3229 input->caret_rect.bottom += req->y - input->caret_rect.top;
3230 input->caret_rect.left = req->x;
3231 input->caret_rect.top = req->y;
3233 if (req->flags & SET_CARET_HIDE)
3235 input->caret_hide += req->hide;
3236 if (input->caret_hide < 0) input->caret_hide = 0;
3238 if (req->flags & SET_CARET_STATE)
3240 switch (req->state)
3242 case CARET_STATE_OFF: input->caret_state = 0; break;
3243 case CARET_STATE_ON: input->caret_state = 1; break;
3244 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3245 case CARET_STATE_ON_IF_MOVED:
3246 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3247 break;
3253 /* get the time of the last input event */
3254 DECL_HANDLER(get_last_input_time)
3256 reply->time = last_input_time;
3259 /* set/get the current cursor */
3260 DECL_HANDLER(set_cursor)
3262 struct msg_queue *queue = get_current_queue();
3263 struct thread_input *input;
3264 struct desktop *desktop;
3266 if (!queue) return;
3267 input = queue->input;
3268 desktop = input->desktop;
3270 reply->prev_handle = input->cursor;
3271 reply->prev_count = input->cursor_count;
3272 reply->prev_x = desktop->cursor.x;
3273 reply->prev_y = desktop->cursor.y;
3275 if (req->flags & SET_CURSOR_HANDLE)
3277 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3279 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3280 return;
3282 input->cursor = req->handle;
3284 if (req->flags & SET_CURSOR_COUNT)
3286 queue->cursor_count += req->show_count;
3287 input->cursor_count += req->show_count;
3289 if (req->flags & SET_CURSOR_POS) set_cursor_pos( desktop, req->x, req->y );
3290 if (req->flags & SET_CURSOR_CLIP) set_clip_rectangle( desktop, &req->clip, 0 );
3291 if (req->flags & SET_CURSOR_NOCLIP) set_clip_rectangle( desktop, NULL, 0 );
3293 reply->new_x = desktop->cursor.x;
3294 reply->new_y = desktop->cursor.y;
3295 reply->new_clip = desktop->cursor.clip;
3296 reply->last_change = 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, pos = 0;
3314 struct list *ptr;
3315 char *buf, *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 ptr = list_head( &input->msg_list );
3322 while (ptr)
3324 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
3325 struct hardware_msg_data *data = msg->data;
3326 data_size_t extra_size = data->size - sizeof(*data);
3328 ptr = list_next( &input->msg_list, ptr );
3329 if (msg->msg != WM_INPUT) continue;
3331 next_size = req->rawinput_size + extra_size;
3332 if (size + next_size > req->buffer_size) break;
3333 if (pos + data->size > get_reply_max_size()) break;
3334 if (pos + data->size > buf_size)
3336 buf_size += buf_size / 2 + extra_size;
3337 if (!(tmp = realloc( buf, buf_size )))
3339 free( buf );
3340 set_error( STATUS_NO_MEMORY );
3341 return;
3343 buf = tmp;
3346 memcpy( buf + pos, data, data->size );
3347 list_remove( &msg->entry );
3348 free_message( msg );
3350 size += next_size;
3351 pos += sizeof(*data) + extra_size;
3352 count++;
3355 reply->next_size = next_size;
3356 reply->count = count;
3357 set_reply_data_ptr( buf, pos );
3360 DECL_HANDLER(update_rawinput_devices)
3362 const struct rawinput_device *tmp, *devices = get_req_data();
3363 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3364 size_t size = device_count * sizeof(*devices);
3365 struct process *process = current->process;
3367 if (!size)
3369 process->rawinput_device_count = 0;
3370 process->rawinput_mouse = NULL;
3371 process->rawinput_kbd = NULL;
3372 return;
3375 if (!(tmp = realloc( process->rawinput_devices, size )))
3377 set_error( STATUS_NO_MEMORY );
3378 return;
3380 process->rawinput_devices = (struct rawinput_device *)tmp;
3381 process->rawinput_device_count = device_count;
3382 memcpy( process->rawinput_devices, devices, size );
3384 process->rawinput_mouse = find_rawinput_device( process, 1, 2 );
3385 process->rawinput_kbd = find_rawinput_device( process, 1, 6 );