dxgi: Document some struct d3d12_swapchain fields.
[wine.git] / server / queue.c
blob0c7d2c50a493b040aecc103bd0e446d86c290662
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;
1096 struct desktop *desktop;
1098 empty_msg_list( &input->msg_list );
1099 if ((desktop = input->desktop))
1101 if (desktop->foreground_input == input) desktop->foreground_input = NULL;
1102 release_object( desktop );
1106 /* fix the thread input data when a window is destroyed */
1107 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1109 struct thread_input *input = queue->input;
1111 if (window == input->focus) input->focus = 0;
1112 if (window == input->capture) input->capture = 0;
1113 if (window == input->active) input->active = 0;
1114 if (window == input->menu_owner) input->menu_owner = 0;
1115 if (window == input->move_size) input->move_size = 0;
1116 if (window == input->caret) set_caret_window( input, 0 );
1119 /* check if the specified window can be set in the input data of a given queue */
1120 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1122 struct thread *thread;
1123 int ret = 0;
1125 if (!window) return 1; /* we can always clear the data */
1127 if ((thread = get_window_thread( window )))
1129 ret = (queue->input == thread->queue->input);
1130 if (!ret) set_error( STATUS_ACCESS_DENIED );
1131 release_object( thread );
1133 else set_error( STATUS_INVALID_HANDLE );
1135 return ret;
1138 /* make sure the specified thread has a queue */
1139 int init_thread_queue( struct thread *thread )
1141 if (thread->queue) return 1;
1142 return (create_msg_queue( thread, NULL ) != NULL);
1145 /* attach two thread input data structures */
1146 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1148 struct desktop *desktop;
1149 struct thread_input *input;
1150 int ret;
1152 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1153 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1154 input = (struct thread_input *)grab_object( thread_to->queue->input );
1155 if (input->desktop != desktop)
1157 set_error( STATUS_ACCESS_DENIED );
1158 release_object( input );
1159 release_object( desktop );
1160 return 0;
1162 release_object( desktop );
1164 if (thread_from->queue)
1166 if (!input->focus) input->focus = thread_from->queue->input->focus;
1167 if (!input->active) input->active = thread_from->queue->input->active;
1170 ret = assign_thread_input( thread_from, input );
1171 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1172 release_object( input );
1173 return ret;
1176 /* detach two thread input data structures */
1177 void detach_thread_input( struct thread *thread_from )
1179 struct thread *thread;
1180 struct thread_input *input, *old_input = thread_from->queue->input;
1182 if ((input = create_thread_input( thread_from )))
1184 if (old_input->focus && (thread = get_window_thread( old_input->focus )))
1186 if (thread == thread_from)
1188 input->focus = old_input->focus;
1189 old_input->focus = 0;
1191 release_object( thread );
1193 if (old_input->active && (thread = get_window_thread( old_input->active )))
1195 if (thread == thread_from)
1197 input->active = old_input->active;
1198 old_input->active = 0;
1200 release_object( thread );
1202 assign_thread_input( thread_from, input );
1203 release_object( input );
1208 /* set the next timer to expire */
1209 static void set_next_timer( struct msg_queue *queue )
1211 struct list *ptr;
1213 if (queue->timeout)
1215 remove_timeout_user( queue->timeout );
1216 queue->timeout = NULL;
1218 if ((ptr = list_head( &queue->pending_timers )))
1220 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1221 queue->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, queue );
1223 /* set/clear QS_TIMER bit */
1224 if (list_empty( &queue->expired_timers ))
1225 clear_queue_bits( queue, QS_TIMER );
1226 else
1227 set_queue_bits( queue, QS_TIMER );
1230 /* find a timer from its window and id */
1231 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1232 unsigned int msg, lparam_t id )
1234 struct list *ptr;
1236 /* we need to search both lists */
1238 LIST_FOR_EACH( ptr, &queue->pending_timers )
1240 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1241 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1243 LIST_FOR_EACH( ptr, &queue->expired_timers )
1245 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1246 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1248 return NULL;
1251 /* callback for the next timer expiration */
1252 static void timer_callback( void *private )
1254 struct msg_queue *queue = private;
1255 struct list *ptr;
1257 queue->timeout = NULL;
1258 /* move on to the next timer */
1259 ptr = list_head( &queue->pending_timers );
1260 list_remove( ptr );
1261 list_add_tail( &queue->expired_timers, ptr );
1262 set_next_timer( queue );
1265 /* link a timer at its rightful place in the queue list */
1266 static void link_timer( struct msg_queue *queue, struct timer *timer )
1268 struct list *ptr;
1270 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1272 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1273 if (t->when <= timer->when) break;
1275 list_add_before( ptr, &timer->entry );
1278 /* remove a timer from the queue timer list and free it */
1279 static void free_timer( struct msg_queue *queue, struct timer *timer )
1281 list_remove( &timer->entry );
1282 free( timer );
1283 set_next_timer( queue );
1286 /* restart an expired timer */
1287 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1289 list_remove( &timer->entry );
1290 while (-timer->when <= monotonic_time) timer->when -= (timeout_t)timer->rate * 10000;
1291 link_timer( queue, timer );
1292 set_next_timer( queue );
1295 /* find an expired timer matching the filtering parameters */
1296 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1297 unsigned int get_first, unsigned int get_last,
1298 int remove )
1300 struct list *ptr;
1302 LIST_FOR_EACH( ptr, &queue->expired_timers )
1304 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1305 if (win && timer->win != win) continue;
1306 if (check_msg_filter( timer->msg, get_first, get_last ))
1308 if (remove) restart_timer( queue, timer );
1309 return timer;
1312 return NULL;
1315 /* add a timer */
1316 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1318 struct timer *timer = mem_alloc( sizeof(*timer) );
1319 if (timer)
1321 timer->rate = max( rate, 1 );
1322 timer->when = -monotonic_time - (timeout_t)timer->rate * 10000;
1323 link_timer( queue, timer );
1324 /* check if we replaced the next timer */
1325 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1327 return timer;
1330 /* change the input key state for a given key */
1331 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1333 if (down)
1335 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1336 keystate[key] |= down;
1338 else keystate[key] &= ~0x80;
1341 /* update the input key state for a keyboard message */
1342 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1343 unsigned int msg, lparam_t wparam )
1345 unsigned char key;
1346 int down = 0;
1348 switch (msg)
1350 case WM_LBUTTONDOWN:
1351 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1352 /* fall through */
1353 case WM_LBUTTONUP:
1354 set_input_key_state( keystate, VK_LBUTTON, down );
1355 break;
1356 case WM_MBUTTONDOWN:
1357 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1358 /* fall through */
1359 case WM_MBUTTONUP:
1360 set_input_key_state( keystate, VK_MBUTTON, down );
1361 break;
1362 case WM_RBUTTONDOWN:
1363 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1364 /* fall through */
1365 case WM_RBUTTONUP:
1366 set_input_key_state( keystate, VK_RBUTTON, down );
1367 break;
1368 case WM_XBUTTONDOWN:
1369 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1370 /* fall through */
1371 case WM_XBUTTONUP:
1372 if (wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1373 else if (wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1374 break;
1375 case WM_KEYDOWN:
1376 case WM_SYSKEYDOWN:
1377 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1378 /* fall through */
1379 case WM_KEYUP:
1380 case WM_SYSKEYUP:
1381 key = (unsigned char)wparam;
1382 set_input_key_state( keystate, key, down );
1383 switch(key)
1385 case VK_LCONTROL:
1386 case VK_RCONTROL:
1387 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1388 set_input_key_state( keystate, VK_CONTROL, down );
1389 break;
1390 case VK_LMENU:
1391 case VK_RMENU:
1392 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1393 set_input_key_state( keystate, VK_MENU, down );
1394 break;
1395 case VK_LSHIFT:
1396 case VK_RSHIFT:
1397 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1398 set_input_key_state( keystate, VK_SHIFT, down );
1399 break;
1401 break;
1405 /* update the desktop key state according to a mouse message flags */
1406 static void update_desktop_mouse_state( struct desktop *desktop, unsigned int flags,
1407 int x, int y, lparam_t wparam )
1409 if (flags & MOUSEEVENTF_MOVE)
1410 update_desktop_cursor_pos( desktop, x, y );
1411 if (flags & MOUSEEVENTF_LEFTDOWN)
1412 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONDOWN, wparam );
1413 if (flags & MOUSEEVENTF_LEFTUP)
1414 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONUP, wparam );
1415 if (flags & MOUSEEVENTF_RIGHTDOWN)
1416 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONDOWN, wparam );
1417 if (flags & MOUSEEVENTF_RIGHTUP)
1418 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONUP, wparam );
1419 if (flags & MOUSEEVENTF_MIDDLEDOWN)
1420 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONDOWN, wparam );
1421 if (flags & MOUSEEVENTF_MIDDLEUP)
1422 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONUP, wparam );
1423 if (flags & MOUSEEVENTF_XDOWN)
1424 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONDOWN, wparam );
1425 if (flags & MOUSEEVENTF_XUP)
1426 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONUP, wparam );
1429 /* release the hardware message currently being processed by the given thread */
1430 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id )
1432 struct thread_input *input = queue->input;
1433 struct message *msg, *other;
1434 int clr_bit;
1436 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1438 if (msg->unique_id == hw_id) break;
1440 if (&msg->entry == &input->msg_list) return; /* not found */
1442 /* clear the queue bit for that message */
1443 clr_bit = get_hardware_msg_bit( msg );
1444 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1446 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1448 clr_bit = 0;
1449 break;
1452 if (clr_bit) clear_queue_bits( queue, clr_bit );
1454 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1455 list_remove( &msg->entry );
1456 free_message( msg );
1459 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1461 struct hotkey *hotkey;
1462 unsigned int modifiers = 0;
1464 if (msg->msg != WM_KEYDOWN && msg->msg != WM_SYSKEYDOWN) return 0;
1466 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1467 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1468 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1469 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1471 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1473 if (hotkey->vkey != msg->wparam) continue;
1474 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1477 return 0;
1479 found:
1480 msg->type = MSG_POSTED;
1481 msg->win = hotkey->win;
1482 msg->msg = WM_HOTKEY;
1483 msg->wparam = hotkey->id;
1484 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1486 free( msg->data );
1487 msg->data = NULL;
1488 msg->data_size = 0;
1490 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1491 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1492 hotkey->queue->hotkey_count++;
1493 return 1;
1496 /* find the window that should receive a given hardware message */
1497 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1498 struct message *msg, unsigned int *msg_code,
1499 struct thread **thread )
1501 user_handle_t win = 0;
1503 *thread = NULL;
1504 *msg_code = msg->msg;
1505 switch (get_hardware_msg_bit( msg ))
1507 case QS_RAWINPUT:
1508 if (!(win = msg->win) && input) win = input->focus;
1509 break;
1510 case QS_KEY:
1511 if (input && !(win = input->focus))
1513 win = input->active;
1514 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1516 break;
1517 case QS_MOUSEMOVE:
1518 case QS_MOUSEBUTTON:
1519 if (!input || !(win = input->capture))
1521 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1522 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1523 *thread = window_thread_from_point( win, msg->x, msg->y );
1525 break;
1528 if (!*thread)
1529 *thread = get_window_thread( win );
1530 return win;
1533 static struct rawinput_device *find_rawinput_device( struct process *process, unsigned short usage_page, unsigned short usage )
1535 struct rawinput_device *device, *end;
1537 for (device = process->rawinput_devices, end = device + process->rawinput_device_count; device != end; device++)
1539 if (device->usage_page != usage_page || device->usage != usage) continue;
1540 return device;
1543 return NULL;
1546 static void prepend_cursor_history( int x, int y, unsigned int time, lparam_t info )
1548 cursor_pos_t *pos = &cursor_history[--cursor_history_latest % ARRAY_SIZE(cursor_history)];
1550 pos->x = x;
1551 pos->y = y;
1552 pos->time = time;
1553 pos->info = info;
1556 /* queue a hardware message into a given thread input */
1557 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1559 user_handle_t win;
1560 struct thread *thread;
1561 struct thread_input *input;
1562 struct hardware_msg_data *msg_data = msg->data;
1563 unsigned int msg_code;
1565 update_input_key_state( desktop, desktop->keystate, msg->msg, msg->wparam );
1566 last_input_time = get_tick_count();
1567 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1569 switch (get_hardware_msg_bit( msg ))
1571 case QS_KEY:
1572 if (queue_hotkey_message( desktop, msg )) return;
1573 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1574 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1575 msg->lparam &= ~(KF_EXTENDED << 16);
1576 break;
1577 case QS_MOUSEMOVE:
1578 prepend_cursor_history( msg->x, msg->y, msg->time, msg_data->info );
1579 if (update_desktop_cursor_pos( desktop, msg->x, msg->y )) always_queue = 1;
1580 /* fallthrough */
1581 case QS_MOUSEBUTTON:
1582 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1583 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1584 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1585 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1586 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1587 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1588 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1589 break;
1591 msg->x = desktop->cursor.x;
1592 msg->y = desktop->cursor.y;
1594 if (msg->win && (thread = get_window_thread( msg->win )))
1596 input = thread->queue->input;
1597 release_object( thread );
1599 else input = desktop->foreground_input;
1601 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1602 if (!win || !thread)
1604 if (input) update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1605 free_message( msg );
1606 return;
1608 input = thread->queue->input;
1610 if (win != desktop->cursor.win) always_queue = 1;
1611 desktop->cursor.win = win;
1613 if (!always_queue || merge_message( input, msg )) free_message( msg );
1614 else
1616 msg->unique_id = 0; /* will be set once we return it to the app */
1617 list_add_tail( &input->msg_list, &msg->entry );
1618 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1620 release_object( thread );
1623 /* send the low-level hook message for a given hardware message */
1624 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1625 const hw_input_t *input, struct msg_queue *sender )
1627 struct thread *hook_thread;
1628 struct msg_queue *queue;
1629 struct message *msg;
1630 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1631 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1633 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1634 if (!(queue = hook_thread->queue)) return 0;
1635 if (is_queue_hung( queue )) return 0;
1637 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1639 msg->type = MSG_HOOK_LL;
1640 msg->win = 0;
1641 msg->msg = id;
1642 msg->wparam = hardware_msg->msg;
1643 msg->x = hardware_msg->x;
1644 msg->y = hardware_msg->y;
1645 msg->time = hardware_msg->time;
1646 msg->data_size = hardware_msg->data_size;
1647 msg->result = NULL;
1649 if (input->type == INPUT_KEYBOARD)
1651 unsigned short vkey = input->kbd.vkey;
1652 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1653 msg->lparam = (input->kbd.scan << 16) | vkey;
1655 else msg->lparam = input->mouse.data << 16;
1657 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1658 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1660 free_message( msg );
1661 return 0;
1663 msg->result->hardware_msg = hardware_msg;
1664 msg->result->desktop = (struct desktop *)grab_object( desktop );
1665 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1666 set_queue_bits( queue, QS_SENDMESSAGE );
1667 return 1;
1670 /* get the foreground thread for a desktop and a window receiving input */
1671 static struct thread *get_foreground_thread( struct desktop *desktop, user_handle_t window )
1673 /* if desktop has no foreground process, assume the receiving window is */
1674 if (desktop->foreground_input) return get_window_thread( desktop->foreground_input->focus );
1675 if (window) return get_window_thread( window );
1676 return NULL;
1679 struct rawinput_message
1681 struct thread *foreground;
1682 struct desktop *desktop;
1683 struct hw_msg_source source;
1684 unsigned int time;
1685 unsigned int message;
1686 struct hardware_msg_data data;
1687 const void *hid_report;
1690 /* check if process is supposed to receive a WM_INPUT message and eventually queue it */
1691 static int queue_rawinput_message( struct process* process, void *arg )
1693 const struct rawinput_message* raw_msg = arg;
1694 const struct rawinput_device *device = NULL;
1695 struct desktop *target_desktop = NULL, *desktop = NULL;
1696 struct thread *target_thread = NULL, *foreground = NULL;
1697 struct message *msg;
1698 data_size_t report_size;
1699 int wparam = RIM_INPUT;
1701 if (raw_msg->data.rawinput.type == RIM_TYPEMOUSE)
1702 device = process->rawinput_mouse;
1703 else if (raw_msg->data.rawinput.type == RIM_TYPEKEYBOARD)
1704 device = process->rawinput_kbd;
1705 else
1706 device = find_rawinput_device( process, raw_msg->data.rawinput.hid.usage_page, raw_msg->data.rawinput.hid.usage );
1707 if (!device) return 0;
1709 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && !(device->flags & RIDEV_DEVNOTIFY)) return 0;
1711 if (raw_msg->desktop) desktop = (struct desktop *)grab_object( raw_msg->desktop );
1712 else if (!(desktop = get_desktop_obj( process, process->desktop, 0 ))) goto done;
1714 if (raw_msg->foreground) foreground = (struct thread *)grab_object( raw_msg->foreground );
1715 else if (!(foreground = get_foreground_thread( desktop, 0 ))) goto done;
1717 if (process != foreground->process)
1719 if (raw_msg->message == WM_INPUT && !(device->flags & RIDEV_INPUTSINK)) goto done;
1720 if (!(target_thread = get_window_thread( device->target ))) goto done;
1721 if (!(target_desktop = get_thread_desktop( target_thread, 0 ))) goto done;
1722 if (target_desktop != desktop) goto done;
1723 wparam = RIM_INPUTSINK;
1726 if (raw_msg->data.rawinput.type != RIM_TYPEHID || !raw_msg->hid_report) report_size = 0;
1727 else report_size = raw_msg->data.size - sizeof(raw_msg->data);
1729 if (!(msg = alloc_hardware_message( raw_msg->data.info, raw_msg->source, raw_msg->time, report_size )))
1730 goto done;
1732 msg->win = device->target;
1733 msg->msg = raw_msg->message;
1734 msg->wparam = wparam;
1735 msg->lparam = 0;
1736 memcpy( msg->data, &raw_msg->data, sizeof(raw_msg->data) );
1737 if (report_size) memcpy( (struct hardware_msg_data *)msg->data + 1, raw_msg->hid_report, report_size );
1739 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && raw_msg->data.rawinput.type == RIM_TYPEHID)
1741 msg->wparam = raw_msg->data.rawinput.hid.param;
1742 msg->lparam = raw_msg->data.rawinput.hid.device;
1745 queue_hardware_message( desktop, msg, 1 );
1747 done:
1748 if (target_thread) release_object( target_thread );
1749 if (target_desktop) release_object( target_desktop );
1750 if (foreground) release_object( foreground );
1751 if (desktop) release_object( desktop );
1752 return 0;
1755 /* queue a hardware message for a mouse event */
1756 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1757 unsigned int origin, struct msg_queue *sender )
1759 const struct rawinput_device *device;
1760 struct hardware_msg_data *msg_data;
1761 struct rawinput_message raw_msg;
1762 struct message *msg;
1763 struct thread *foreground;
1764 unsigned int i, time, flags;
1765 struct hw_msg_source source = { IMDT_MOUSE, origin };
1766 int wait = 0, x, y;
1768 static const unsigned int messages[] =
1770 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1771 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1772 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1773 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1774 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1775 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1776 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1777 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1778 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1779 0, /* 0x0200 = unused */
1780 0, /* 0x0400 = unused */
1781 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1782 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1785 desktop->cursor.last_change = get_tick_count();
1786 flags = input->mouse.flags;
1787 time = input->mouse.time;
1788 if (!time) time = desktop->cursor.last_change;
1790 if (flags & MOUSEEVENTF_MOVE)
1792 if (flags & MOUSEEVENTF_ABSOLUTE)
1794 x = input->mouse.x;
1795 y = input->mouse.y;
1796 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1797 x == desktop->cursor.x && y == desktop->cursor.y)
1798 flags &= ~MOUSEEVENTF_MOVE;
1800 else
1802 x = desktop->cursor.x + input->mouse.x;
1803 y = desktop->cursor.y + input->mouse.y;
1806 else
1808 x = desktop->cursor.x;
1809 y = desktop->cursor.y;
1812 if ((foreground = get_foreground_thread( desktop, win )))
1814 memset( &raw_msg, 0, sizeof(raw_msg) );
1815 raw_msg.foreground = foreground;
1816 raw_msg.desktop = desktop;
1817 raw_msg.source = source;
1818 raw_msg.time = time;
1819 raw_msg.message = WM_INPUT;
1821 msg_data = &raw_msg.data;
1822 msg_data->info = input->mouse.info;
1823 msg_data->size = sizeof(*msg_data);
1824 msg_data->flags = flags;
1825 msg_data->rawinput.type = RIM_TYPEMOUSE;
1826 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1827 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1828 msg_data->rawinput.mouse.data = input->mouse.data;
1830 enum_processes( queue_rawinput_message, &raw_msg );
1831 release_object( foreground );
1834 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
1836 update_desktop_mouse_state( desktop, flags, x, y, input->mouse.data << 16 );
1837 return 0;
1840 for (i = 0; i < ARRAY_SIZE( messages ); i++)
1842 if (!messages[i]) continue;
1843 if (!(flags & (1 << i))) continue;
1844 flags &= ~(1 << i);
1846 if (!(msg = alloc_hardware_message( input->mouse.info, source, time, 0 ))) return 0;
1847 msg_data = msg->data;
1849 msg->win = get_user_full_handle( win );
1850 msg->msg = messages[i];
1851 msg->wparam = input->mouse.data << 16;
1852 msg->lparam = 0;
1853 msg->x = x;
1854 msg->y = y;
1855 if (origin == IMO_INJECTED) msg_data->flags = LLMHF_INJECTED;
1857 /* specify a sender only when sending the last message */
1858 if (!(flags & ((1 << ARRAY_SIZE( messages )) - 1)))
1860 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1861 queue_hardware_message( desktop, msg, 0 );
1863 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1864 queue_hardware_message( desktop, msg, 0 );
1866 return wait;
1869 /* queue a hardware message for a keyboard event */
1870 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1871 unsigned int origin, struct msg_queue *sender )
1873 struct hw_msg_source source = { IMDT_KEYBOARD, origin };
1874 const struct rawinput_device *device;
1875 struct hardware_msg_data *msg_data;
1876 struct rawinput_message raw_msg;
1877 struct message *msg;
1878 struct thread *foreground;
1879 unsigned char vkey = input->kbd.vkey;
1880 unsigned int message_code, time;
1881 int wait;
1883 if (!(time = input->kbd.time)) time = get_tick_count();
1885 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1887 switch (vkey)
1889 case VK_MENU:
1890 case VK_LMENU:
1891 case VK_RMENU:
1892 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1893 break;
1894 case VK_CONTROL:
1895 case VK_LCONTROL:
1896 case VK_RCONTROL:
1897 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1898 break;
1899 case VK_SHIFT:
1900 case VK_LSHIFT:
1901 case VK_RSHIFT:
1902 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1903 break;
1907 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1908 switch (vkey)
1910 case VK_LMENU:
1911 case VK_RMENU:
1912 if (input->kbd.flags & KEYEVENTF_KEYUP)
1914 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1915 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1916 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1917 message_code = WM_SYSKEYUP;
1918 desktop->keystate[VK_MENU] &= ~0x02;
1920 else
1922 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1923 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1924 message_code = WM_SYSKEYDOWN;
1925 desktop->keystate[VK_MENU] |= 0x02;
1927 break;
1929 case VK_LCONTROL:
1930 case VK_RCONTROL:
1931 /* send WM_SYSKEYUP on release if Alt still pressed */
1932 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1933 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1934 message_code = WM_SYSKEYUP;
1935 desktop->keystate[VK_MENU] &= ~0x02;
1936 break;
1938 default:
1939 /* send WM_SYSKEY for Alt-anykey and for F10 */
1940 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1941 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1942 /* fall through */
1943 case VK_F10:
1944 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1945 desktop->keystate[VK_MENU] &= ~0x02;
1946 break;
1949 if ((foreground = get_foreground_thread( desktop, win )))
1951 memset( &raw_msg, 0, sizeof(raw_msg) );
1952 raw_msg.foreground = foreground;
1953 raw_msg.desktop = desktop;
1954 raw_msg.source = source;
1955 raw_msg.time = time;
1956 raw_msg.message = WM_INPUT;
1958 msg_data = &raw_msg.data;
1959 msg_data->info = input->kbd.info;
1960 msg_data->size = sizeof(*msg_data);
1961 msg_data->flags = input->kbd.flags;
1962 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1963 msg_data->rawinput.kbd.message = message_code;
1964 msg_data->rawinput.kbd.vkey = vkey;
1965 msg_data->rawinput.kbd.scan = input->kbd.scan;
1967 enum_processes( queue_rawinput_message, &raw_msg );
1968 release_object( foreground );
1971 if ((device = current->process->rawinput_kbd) && (device->flags & RIDEV_NOLEGACY))
1973 update_input_key_state( desktop, desktop->keystate, message_code, vkey );
1974 return 0;
1977 if (!(msg = alloc_hardware_message( input->kbd.info, source, time, 0 ))) return 0;
1978 msg_data = msg->data;
1980 msg->win = get_user_full_handle( win );
1981 msg->msg = message_code;
1982 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1983 if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
1985 if (input->kbd.flags & KEYEVENTF_UNICODE && !vkey)
1987 msg->wparam = VK_PACKET;
1989 else
1991 unsigned int flags = 0;
1992 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1993 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1994 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1995 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1997 msg->wparam = vkey;
1998 msg->lparam |= flags << 16;
1999 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
2002 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
2003 queue_hardware_message( desktop, msg, 1 );
2005 return wait;
2008 /* queue a hardware message for a custom type of event */
2009 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
2010 unsigned int origin, const hw_input_t *input )
2012 struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
2013 struct hardware_msg_data *msg_data;
2014 struct rawinput_message raw_msg;
2015 struct message *msg;
2016 data_size_t report_size = 0;
2018 switch (input->hw.msg)
2020 case WM_INPUT:
2021 case WM_INPUT_DEVICE_CHANGE:
2022 memset( &raw_msg, 0, sizeof(raw_msg) );
2023 raw_msg.source = source;
2024 raw_msg.time = get_tick_count();
2025 raw_msg.message = input->hw.msg;
2027 if (input->hw.rawinput.type == RIM_TYPEHID)
2029 raw_msg.hid_report = get_req_data();
2030 report_size = input->hw.rawinput.hid.length * input->hw.rawinput.hid.count;
2033 if (report_size != get_req_data_size())
2035 set_error( STATUS_INVALID_PARAMETER );
2036 return;
2039 msg_data = &raw_msg.data;
2040 msg_data->size = sizeof(*msg_data) + report_size;
2041 msg_data->rawinput = input->hw.rawinput;
2043 enum_processes( queue_rawinput_message, &raw_msg );
2044 return;
2047 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
2049 msg->win = get_user_full_handle( win );
2050 msg->msg = input->hw.msg;
2051 msg->wparam = 0;
2052 msg->lparam = input->hw.lparam;
2053 msg->x = desktop->cursor.x;
2054 msg->y = desktop->cursor.y;
2056 queue_hardware_message( desktop, msg, 1 );
2059 /* check message filter for a hardware message */
2060 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
2061 user_handle_t filter_win, unsigned int first, unsigned int last )
2063 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
2065 /* we can only test the window for a keyboard message since the
2066 * dest window for a mouse message depends on hittest */
2067 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
2068 return 0;
2069 /* the message code is final for a keyboard message, we can simply check it */
2070 return check_msg_filter( msg_code, first, last );
2072 else /* mouse message */
2074 /* we need to check all possible values that the message can have in the end */
2076 if (check_msg_filter( msg_code, first, last )) return 1;
2077 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
2079 /* all other messages can become non-client messages */
2080 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
2082 /* clicks can become double-clicks or non-client double-clicks */
2083 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
2084 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
2086 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2087 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2089 return 0;
2094 /* find a hardware message for the given queue */
2095 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
2096 unsigned int first, unsigned int last, unsigned int flags,
2097 struct get_message_reply *reply )
2099 struct thread_input *input = thread->queue->input;
2100 struct thread *win_thread;
2101 struct list *ptr;
2102 user_handle_t win;
2103 int clear_bits, got_one = 0;
2104 unsigned int msg_code;
2106 ptr = list_head( &input->msg_list );
2107 if (hw_id)
2109 while (ptr)
2111 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2112 if (msg->unique_id == hw_id) break;
2113 ptr = list_next( &input->msg_list, ptr );
2115 if (!ptr) ptr = list_head( &input->msg_list );
2116 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
2119 if (ptr == list_head( &input->msg_list ))
2120 clear_bits = QS_INPUT;
2121 else
2122 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
2124 while (ptr)
2126 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2127 struct hardware_msg_data *data = msg->data;
2129 ptr = list_next( &input->msg_list, ptr );
2130 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
2131 if (!win || !win_thread)
2133 /* no window at all, remove it */
2134 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2135 list_remove( &msg->entry );
2136 free_message( msg );
2137 continue;
2139 if (win_thread != thread)
2141 if (win_thread->queue->input == input)
2143 /* wake the other thread */
2144 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
2145 got_one = 1;
2147 else
2149 /* for another thread input, drop it */
2150 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2151 list_remove( &msg->entry );
2152 free_message( msg );
2154 release_object( win_thread );
2155 continue;
2157 release_object( win_thread );
2159 /* if we already got a message for another thread, or if it doesn't
2160 * match the filter we skip it */
2161 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
2163 clear_bits &= ~get_hardware_msg_bit( msg );
2164 continue;
2167 reply->total = msg->data_size;
2168 if (msg->data_size > get_reply_max_size())
2170 set_error( STATUS_BUFFER_OVERFLOW );
2171 return 1;
2174 /* now we can return it */
2175 if (!msg->unique_id) msg->unique_id = get_unique_id();
2176 reply->type = MSG_HARDWARE;
2177 reply->win = win;
2178 reply->msg = msg_code;
2179 reply->wparam = msg->wparam;
2180 reply->lparam = msg->lparam;
2181 reply->x = msg->x;
2182 reply->y = msg->y;
2183 reply->time = msg->time;
2185 data->hw_id = msg->unique_id;
2186 set_reply_data( msg->data, msg->data_size );
2187 if (get_hardware_msg_bit( msg ) == QS_RAWINPUT && (flags & PM_REMOVE))
2188 release_hardware_message( current->queue, data->hw_id );
2189 return 1;
2191 /* nothing found, clear the hardware queue bits */
2192 clear_queue_bits( thread->queue, clear_bits );
2193 return 0;
2196 /* increment (or decrement if 'incr' is negative) the queue paint count */
2197 void inc_queue_paint_count( struct thread *thread, int incr )
2199 struct msg_queue *queue = thread->queue;
2201 assert( queue );
2203 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2205 if (queue->paint_count)
2206 set_queue_bits( queue, QS_PAINT );
2207 else
2208 clear_queue_bits( queue, QS_PAINT );
2212 /* remove all messages and timers belonging to a certain window */
2213 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2215 struct msg_queue *queue = thread->queue;
2216 struct list *ptr;
2217 int i;
2219 if (!queue) return;
2221 /* remove timers */
2223 ptr = list_head( &queue->pending_timers );
2224 while (ptr)
2226 struct list *next = list_next( &queue->pending_timers, ptr );
2227 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2228 if (timer->win == win) free_timer( queue, timer );
2229 ptr = next;
2231 ptr = list_head( &queue->expired_timers );
2232 while (ptr)
2234 struct list *next = list_next( &queue->expired_timers, ptr );
2235 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2236 if (timer->win == win) free_timer( queue, timer );
2237 ptr = next;
2240 /* remove messages */
2241 for (i = 0; i < NB_MSG_KINDS; i++)
2243 struct list *ptr, *next;
2245 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2247 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2248 if (msg->win == win)
2250 if (msg->msg == WM_QUIT && !queue->quit_message)
2252 queue->quit_message = 1;
2253 queue->exit_code = msg->wparam;
2255 remove_queue_message( queue, msg, i );
2260 thread_input_cleanup_window( queue, win );
2263 /* post a message to a window */
2264 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2266 struct message *msg;
2267 struct thread *thread = get_window_thread( win );
2269 if (!thread) return;
2271 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2273 msg->type = MSG_POSTED;
2274 msg->win = get_user_full_handle( win );
2275 msg->msg = message;
2276 msg->wparam = wparam;
2277 msg->lparam = lparam;
2278 msg->result = NULL;
2279 msg->data = NULL;
2280 msg->data_size = 0;
2282 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2284 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2285 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2286 if (message == WM_HOTKEY)
2288 set_queue_bits( thread->queue, QS_HOTKEY );
2289 thread->queue->hotkey_count++;
2292 release_object( thread );
2295 /* send a notify message to a window */
2296 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2298 struct message *msg;
2299 struct thread *thread = get_window_thread( win );
2301 if (!thread) return;
2303 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2305 msg->type = MSG_NOTIFY;
2306 msg->win = get_user_full_handle( win );
2307 msg->msg = message;
2308 msg->wparam = wparam;
2309 msg->lparam = lparam;
2310 msg->result = NULL;
2311 msg->data = NULL;
2312 msg->data_size = 0;
2314 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2316 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2317 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2319 release_object( thread );
2322 /* post a win event */
2323 void post_win_event( struct thread *thread, unsigned int event,
2324 user_handle_t win, unsigned int object_id,
2325 unsigned int child_id, client_ptr_t hook_proc,
2326 const WCHAR *module, data_size_t module_size,
2327 user_handle_t hook)
2329 struct message *msg;
2331 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2333 struct winevent_msg_data *data;
2335 msg->type = MSG_WINEVENT;
2336 msg->win = get_user_full_handle( win );
2337 msg->msg = event;
2338 msg->wparam = object_id;
2339 msg->lparam = child_id;
2340 msg->time = get_tick_count();
2341 msg->result = NULL;
2343 if ((data = malloc( sizeof(*data) + module_size )))
2345 data->hook = hook;
2346 data->tid = get_thread_id( current );
2347 data->hook_proc = hook_proc;
2348 memcpy( data + 1, module, module_size );
2350 msg->data = data;
2351 msg->data_size = sizeof(*data) + module_size;
2353 if (debug_level > 1)
2354 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2355 get_thread_id(thread), event, win, object_id, child_id );
2356 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2357 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2359 else
2360 free( msg );
2364 /* free all hotkeys on a desktop, optionally filtering by window */
2365 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2367 struct hotkey *hotkey, *hotkey2;
2369 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2371 if (!window || hotkey->win == window)
2373 list_remove( &hotkey->entry );
2374 free( hotkey );
2380 /* check if the thread owning the window is hung */
2381 DECL_HANDLER(is_window_hung)
2383 struct thread *thread;
2385 thread = get_window_thread( req->win );
2386 if (thread)
2388 reply->is_hung = is_queue_hung( thread->queue );
2389 release_object( thread );
2391 else reply->is_hung = 0;
2395 /* get the message queue of the current thread */
2396 DECL_HANDLER(get_msg_queue)
2398 struct msg_queue *queue = get_current_queue();
2400 reply->handle = 0;
2401 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2405 /* set the file descriptor associated to the current thread queue */
2406 DECL_HANDLER(set_queue_fd)
2408 struct msg_queue *queue = get_current_queue();
2409 struct file *file;
2410 int unix_fd;
2412 if (queue->fd) /* fd can only be set once */
2414 set_error( STATUS_ACCESS_DENIED );
2415 return;
2417 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2419 if ((unix_fd = get_file_unix_fd( file )) != -1)
2421 if ((unix_fd = dup( unix_fd )) != -1)
2422 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2423 else
2424 file_set_error();
2426 release_object( file );
2430 /* set the current message queue wakeup mask */
2431 DECL_HANDLER(set_queue_mask)
2433 struct msg_queue *queue = get_current_queue();
2435 if (queue)
2437 queue->wake_mask = req->wake_mask;
2438 queue->changed_mask = req->changed_mask;
2439 reply->wake_bits = queue->wake_bits;
2440 reply->changed_bits = queue->changed_bits;
2441 if (is_signaled( queue ))
2443 /* if skip wait is set, do what would have been done in the subsequent wait */
2444 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2445 else wake_up( &queue->obj, 0 );
2451 /* get the current message queue status */
2452 DECL_HANDLER(get_queue_status)
2454 struct msg_queue *queue = current->queue;
2455 if (queue)
2457 reply->wake_bits = queue->wake_bits;
2458 reply->changed_bits = queue->changed_bits;
2459 queue->changed_bits &= ~req->clear_bits;
2461 else reply->wake_bits = reply->changed_bits = 0;
2465 /* send a message to a thread queue */
2466 DECL_HANDLER(send_message)
2468 struct message *msg;
2469 struct msg_queue *send_queue = get_current_queue();
2470 struct msg_queue *recv_queue = NULL;
2471 struct thread *thread = NULL;
2473 if (!(thread = get_thread_from_id( req->id ))) return;
2475 if (!(recv_queue = thread->queue))
2477 set_error( STATUS_INVALID_PARAMETER );
2478 release_object( thread );
2479 return;
2481 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2483 set_error( STATUS_TIMEOUT );
2484 release_object( thread );
2485 return;
2488 if ((msg = mem_alloc( sizeof(*msg) )))
2490 msg->type = req->type;
2491 msg->win = get_user_full_handle( req->win );
2492 msg->msg = req->msg;
2493 msg->wparam = req->wparam;
2494 msg->lparam = req->lparam;
2495 msg->result = NULL;
2496 msg->data = NULL;
2497 msg->data_size = get_req_data_size();
2499 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2501 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2503 free( msg );
2504 release_object( thread );
2505 return;
2508 switch(msg->type)
2510 case MSG_OTHER_PROCESS:
2511 case MSG_ASCII:
2512 case MSG_UNICODE:
2513 case MSG_CALLBACK:
2514 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2516 free_message( msg );
2517 break;
2519 /* fall through */
2520 case MSG_NOTIFY:
2521 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2522 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2523 break;
2524 case MSG_POSTED:
2525 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2526 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2527 if (msg->msg == WM_HOTKEY)
2529 set_queue_bits( recv_queue, QS_HOTKEY );
2530 recv_queue->hotkey_count++;
2532 break;
2533 case MSG_HARDWARE: /* should use send_hardware_message instead */
2534 case MSG_CALLBACK_RESULT: /* cannot send this one */
2535 case MSG_HOOK_LL: /* generated internally */
2536 default:
2537 set_error( STATUS_INVALID_PARAMETER );
2538 free( msg );
2539 break;
2542 release_object( thread );
2545 /* send a hardware message to a thread queue */
2546 DECL_HANDLER(send_hardware_message)
2548 struct thread *thread = NULL;
2549 struct desktop *desktop;
2550 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2551 struct msg_queue *sender = get_current_queue();
2553 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2555 if (req->win)
2557 if (!(thread = get_window_thread( req->win ))) return;
2558 if (desktop != thread->queue->input->desktop)
2560 /* don't allow queuing events to a different desktop */
2561 release_object( desktop );
2562 return;
2566 reply->prev_x = desktop->cursor.x;
2567 reply->prev_y = desktop->cursor.y;
2569 switch (req->input.type)
2571 case INPUT_MOUSE:
2572 reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2573 break;
2574 case INPUT_KEYBOARD:
2575 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2576 break;
2577 case INPUT_HARDWARE:
2578 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2579 break;
2580 default:
2581 set_error( STATUS_INVALID_PARAMETER );
2583 if (thread) release_object( thread );
2585 reply->new_x = desktop->cursor.x;
2586 reply->new_y = desktop->cursor.y;
2587 release_object( desktop );
2590 /* post a quit message to the current queue */
2591 DECL_HANDLER(post_quit_message)
2593 struct msg_queue *queue = get_current_queue();
2595 if (!queue)
2596 return;
2598 queue->quit_message = 1;
2599 queue->exit_code = req->exit_code;
2600 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2603 /* get a message from the current queue */
2604 DECL_HANDLER(get_message)
2606 struct timer *timer;
2607 struct list *ptr;
2608 struct msg_queue *queue = get_current_queue();
2609 user_handle_t get_win = get_user_full_handle( req->get_win );
2610 unsigned int filter = req->flags >> 16;
2612 reply->active_hooks = get_active_hooks();
2614 if (get_win && get_win != 1 && get_win != -1 && !get_user_object( get_win, USER_WINDOW ))
2616 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2617 return;
2620 if (!queue) return;
2621 queue->last_get_msg = current_time;
2622 if (!filter) filter = QS_ALLINPUT;
2624 /* first check for sent messages */
2625 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2627 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2628 receive_message( queue, msg, reply );
2629 return;
2632 /* clear changed bits so we can wait on them if we don't find a message */
2633 if (filter & QS_POSTMESSAGE)
2635 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2636 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2638 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2639 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2641 /* then check for posted messages */
2642 if ((filter & QS_POSTMESSAGE) &&
2643 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2644 return;
2646 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2647 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2648 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2649 return;
2651 /* only check for quit messages if not posted messages pending */
2652 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2653 return;
2655 /* then check for any raw hardware message */
2656 if ((filter & QS_INPUT) &&
2657 filter_contains_hw_range( req->get_first, req->get_last ) &&
2658 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2659 return;
2661 /* now check for WM_PAINT */
2662 if ((filter & QS_PAINT) &&
2663 queue->paint_count &&
2664 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2665 (reply->win = find_window_to_repaint( get_win, current )))
2667 reply->type = MSG_POSTED;
2668 reply->msg = WM_PAINT;
2669 reply->wparam = 0;
2670 reply->lparam = 0;
2671 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2672 return;
2675 /* now check for timer */
2676 if ((filter & QS_TIMER) &&
2677 (timer = find_expired_timer( queue, get_win, req->get_first,
2678 req->get_last, (req->flags & PM_REMOVE) )))
2680 reply->type = MSG_POSTED;
2681 reply->win = timer->win;
2682 reply->msg = timer->msg;
2683 reply->wparam = timer->id;
2684 reply->lparam = timer->lparam;
2685 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2686 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2687 set_event( current->process->idle_event );
2688 return;
2691 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2692 queue->wake_mask = req->wake_mask;
2693 queue->changed_mask = req->changed_mask;
2694 set_error( STATUS_PENDING ); /* FIXME */
2698 /* reply to a sent message */
2699 DECL_HANDLER(reply_message)
2701 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2702 else if (current->queue->recv_result)
2703 reply_message( current->queue, req->result, 0, req->remove,
2704 get_req_data(), get_req_data_size() );
2708 /* accept the current hardware message */
2709 DECL_HANDLER(accept_hardware_message)
2711 if (current->queue)
2712 release_hardware_message( current->queue, req->hw_id );
2713 else
2714 set_error( STATUS_ACCESS_DENIED );
2718 /* retrieve the reply for the last message sent */
2719 DECL_HANDLER(get_message_reply)
2721 struct message_result *result;
2722 struct list *entry;
2723 struct msg_queue *queue = current->queue;
2725 if (queue)
2727 set_error( STATUS_PENDING );
2728 reply->result = 0;
2730 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2732 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2733 if (result->replied || req->cancel)
2735 if (result->replied)
2737 reply->result = result->result;
2738 set_error( result->error );
2739 if (result->data)
2741 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2742 set_reply_data_ptr( result->data, data_len );
2743 result->data = NULL;
2744 result->data_size = 0;
2747 remove_result_from_sender( result );
2749 entry = list_head( &queue->send_result );
2750 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2751 else
2753 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2754 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2755 else clear_queue_bits( queue, QS_SMRESULT );
2759 else set_error( STATUS_ACCESS_DENIED );
2763 /* set a window timer */
2764 DECL_HANDLER(set_win_timer)
2766 struct timer *timer;
2767 struct msg_queue *queue;
2768 struct thread *thread = NULL;
2769 user_handle_t win = 0;
2770 lparam_t id = req->id;
2772 if (req->win)
2774 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2776 set_error( STATUS_INVALID_HANDLE );
2777 return;
2779 if (thread->process != current->process)
2781 release_object( thread );
2782 set_error( STATUS_ACCESS_DENIED );
2783 return;
2785 queue = thread->queue;
2786 /* remove it if it existed already */
2787 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2789 else
2791 queue = get_current_queue();
2792 /* look for a timer with this id */
2793 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2795 /* free and reuse id */
2796 free_timer( queue, timer );
2798 else
2800 lparam_t end_id = queue->next_timer_id;
2802 /* find a free id for it */
2803 while (1)
2805 id = queue->next_timer_id;
2806 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2808 if (!find_timer( queue, 0, req->msg, id )) break;
2809 if (queue->next_timer_id == end_id)
2811 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2812 return;
2818 if ((timer = set_timer( queue, req->rate )))
2820 timer->win = win;
2821 timer->msg = req->msg;
2822 timer->id = id;
2823 timer->lparam = req->lparam;
2824 reply->id = id;
2826 if (thread) release_object( thread );
2829 /* kill a window timer */
2830 DECL_HANDLER(kill_win_timer)
2832 struct timer *timer;
2833 struct thread *thread;
2834 user_handle_t win = 0;
2836 if (req->win)
2838 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2840 set_error( STATUS_INVALID_HANDLE );
2841 return;
2843 if (thread->process != current->process)
2845 release_object( thread );
2846 set_error( STATUS_ACCESS_DENIED );
2847 return;
2850 else thread = (struct thread *)grab_object( current );
2852 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2853 free_timer( thread->queue, timer );
2854 else
2855 set_error( STATUS_INVALID_PARAMETER );
2857 release_object( thread );
2860 DECL_HANDLER(register_hotkey)
2862 struct desktop *desktop;
2863 user_handle_t win_handle = req->window;
2864 struct hotkey *hotkey;
2865 struct hotkey *new_hotkey = NULL;
2866 struct thread *thread;
2867 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2869 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2871 if (win_handle)
2873 if (!(win_handle = get_valid_window_handle( win_handle )))
2875 release_object( desktop );
2876 return;
2879 thread = get_window_thread( win_handle );
2880 if (thread) release_object( thread );
2882 if (thread != current)
2884 release_object( desktop );
2885 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2886 return;
2890 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2892 if (req->vkey == hotkey->vkey &&
2893 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2895 release_object( desktop );
2896 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2897 return;
2899 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2900 new_hotkey = hotkey;
2903 if (new_hotkey)
2905 reply->replaced = 1;
2906 reply->flags = new_hotkey->flags;
2907 reply->vkey = new_hotkey->vkey;
2909 else
2911 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2912 if (new_hotkey)
2914 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2915 new_hotkey->queue = current->queue;
2916 new_hotkey->win = win_handle;
2917 new_hotkey->id = req->id;
2921 if (new_hotkey)
2923 new_hotkey->flags = req->flags;
2924 new_hotkey->vkey = req->vkey;
2927 release_object( desktop );
2930 DECL_HANDLER(unregister_hotkey)
2932 struct desktop *desktop;
2933 user_handle_t win_handle = req->window;
2934 struct hotkey *hotkey;
2935 struct thread *thread;
2937 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2939 if (win_handle)
2941 if (!(win_handle = get_valid_window_handle( win_handle )))
2943 release_object( desktop );
2944 return;
2947 thread = get_window_thread( win_handle );
2948 if (thread) release_object( thread );
2950 if (thread != current)
2952 release_object( desktop );
2953 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2954 return;
2958 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2960 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2961 goto found;
2964 release_object( desktop );
2965 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2966 return;
2968 found:
2969 reply->flags = hotkey->flags;
2970 reply->vkey = hotkey->vkey;
2971 list_remove( &hotkey->entry );
2972 free( hotkey );
2973 release_object( desktop );
2976 /* attach (or detach) thread inputs */
2977 DECL_HANDLER(attach_thread_input)
2979 struct thread *thread_from = get_thread_from_id( req->tid_from );
2980 struct thread *thread_to = get_thread_from_id( req->tid_to );
2982 if (!thread_from || !thread_to)
2984 if (thread_from) release_object( thread_from );
2985 if (thread_to) release_object( thread_to );
2986 return;
2988 if (thread_from != thread_to)
2990 if (req->attach)
2992 if ((thread_to->queue || thread_to == current) &&
2993 (thread_from->queue || thread_from == current))
2994 attach_thread_input( thread_from, thread_to );
2995 else
2996 set_error( STATUS_INVALID_PARAMETER );
2998 else
3000 if (thread_from->queue && thread_to->queue &&
3001 thread_from->queue->input == thread_to->queue->input)
3002 detach_thread_input( thread_from );
3003 else
3004 set_error( STATUS_ACCESS_DENIED );
3007 else set_error( STATUS_ACCESS_DENIED );
3008 release_object( thread_from );
3009 release_object( thread_to );
3013 /* get thread input data */
3014 DECL_HANDLER(get_thread_input)
3016 struct thread *thread = NULL;
3017 struct desktop *desktop;
3018 struct thread_input *input;
3020 if (req->tid)
3022 if (!(thread = get_thread_from_id( req->tid ))) return;
3023 if (!(desktop = get_thread_desktop( thread, 0 )))
3025 release_object( thread );
3026 return;
3028 input = thread->queue ? thread->queue->input : NULL;
3030 else
3032 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3033 input = desktop->foreground_input; /* get the foreground thread info */
3036 if (input)
3038 reply->focus = input->focus;
3039 reply->capture = input->capture;
3040 reply->active = input->active;
3041 reply->menu_owner = input->menu_owner;
3042 reply->move_size = input->move_size;
3043 reply->caret = input->caret;
3044 reply->cursor = input->cursor;
3045 reply->show_count = input->cursor_count;
3046 reply->rect = input->caret_rect;
3049 /* foreground window is active window of foreground thread */
3050 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
3051 if (thread) release_object( thread );
3052 release_object( desktop );
3056 /* retrieve queue keyboard state for current thread or global async state */
3057 DECL_HANDLER(get_key_state)
3059 struct desktop *desktop;
3060 data_size_t size = min( 256, get_reply_max_size() );
3062 if (req->async) /* get global async key state */
3064 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3065 if (req->key >= 0)
3067 reply->state = desktop->keystate[req->key & 0xff];
3068 desktop->keystate[req->key & 0xff] &= ~0x40;
3070 set_reply_data( desktop->keystate, size );
3071 release_object( desktop );
3073 else
3075 struct msg_queue *queue = get_current_queue();
3076 unsigned char *keystate = queue->input->keystate;
3077 if (req->key >= 0)
3079 sync_input_keystate( queue->input );
3080 reply->state = keystate[req->key & 0xff];
3082 set_reply_data( keystate, size );
3087 /* set queue keyboard state for current thread */
3088 DECL_HANDLER(set_key_state)
3090 struct desktop *desktop;
3091 struct msg_queue *queue = get_current_queue();
3092 data_size_t size = min( 256, get_req_data_size() );
3094 memcpy( queue->input->keystate, get_req_data(), size );
3095 memcpy( queue->input->desktop_keystate, queue->input->desktop->keystate, 256 );
3096 if (req->async && (desktop = get_thread_desktop( current, 0 )))
3098 memcpy( desktop->keystate, get_req_data(), size );
3099 release_object( desktop );
3104 /* set the system foreground window */
3105 DECL_HANDLER(set_foreground_window)
3107 struct thread *thread = NULL;
3108 struct desktop *desktop;
3109 struct msg_queue *queue = get_current_queue();
3111 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3112 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
3113 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
3114 reply->send_msg_new = FALSE;
3116 if (is_valid_foreground_window( req->handle ) &&
3117 (thread = get_window_thread( req->handle )) &&
3118 thread->queue->input->desktop == desktop)
3120 set_foreground_input( desktop, thread->queue->input );
3121 reply->send_msg_new = (desktop->foreground_input != queue->input);
3123 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
3125 if (thread) release_object( thread );
3126 release_object( desktop );
3130 /* set the current thread focus window */
3131 DECL_HANDLER(set_focus_window)
3133 struct msg_queue *queue = get_current_queue();
3135 reply->previous = 0;
3136 if (queue && check_queue_input_window( queue, req->handle ))
3138 reply->previous = queue->input->focus;
3139 queue->input->focus = get_user_full_handle( req->handle );
3144 /* set the current thread active window */
3145 DECL_HANDLER(set_active_window)
3147 struct msg_queue *queue = get_current_queue();
3149 reply->previous = 0;
3150 if (queue && check_queue_input_window( queue, req->handle ))
3152 if (!req->handle || make_window_active( req->handle ))
3154 reply->previous = queue->input->active;
3155 queue->input->active = get_user_full_handle( req->handle );
3157 else set_error( STATUS_INVALID_HANDLE );
3162 /* set the current thread capture window */
3163 DECL_HANDLER(set_capture_window)
3165 struct msg_queue *queue = get_current_queue();
3167 reply->previous = reply->full_handle = 0;
3168 if (queue && check_queue_input_window( queue, req->handle ))
3170 struct thread_input *input = queue->input;
3172 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
3173 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
3175 set_error(STATUS_ACCESS_DENIED);
3176 return;
3178 reply->previous = input->capture;
3179 input->capture = get_user_full_handle( req->handle );
3180 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
3181 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
3182 reply->full_handle = input->capture;
3187 /* Set the current thread caret window */
3188 DECL_HANDLER(set_caret_window)
3190 struct msg_queue *queue = get_current_queue();
3192 reply->previous = 0;
3193 if (queue && check_queue_input_window( queue, req->handle ))
3195 struct thread_input *input = queue->input;
3197 reply->previous = input->caret;
3198 reply->old_rect = input->caret_rect;
3199 reply->old_hide = input->caret_hide;
3200 reply->old_state = input->caret_state;
3202 set_caret_window( input, get_user_full_handle(req->handle) );
3203 input->caret_rect.right = input->caret_rect.left + req->width;
3204 input->caret_rect.bottom = input->caret_rect.top + req->height;
3209 /* Set the current thread caret information */
3210 DECL_HANDLER(set_caret_info)
3212 struct msg_queue *queue = get_current_queue();
3213 struct thread_input *input;
3215 if (!queue) return;
3216 input = queue->input;
3217 reply->full_handle = input->caret;
3218 reply->old_rect = input->caret_rect;
3219 reply->old_hide = input->caret_hide;
3220 reply->old_state = input->caret_state;
3222 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3224 set_error( STATUS_ACCESS_DENIED );
3225 return;
3227 if (req->flags & SET_CARET_POS)
3229 input->caret_rect.right += req->x - input->caret_rect.left;
3230 input->caret_rect.bottom += req->y - input->caret_rect.top;
3231 input->caret_rect.left = req->x;
3232 input->caret_rect.top = req->y;
3234 if (req->flags & SET_CARET_HIDE)
3236 input->caret_hide += req->hide;
3237 if (input->caret_hide < 0) input->caret_hide = 0;
3239 if (req->flags & SET_CARET_STATE)
3241 switch (req->state)
3243 case CARET_STATE_OFF: input->caret_state = 0; break;
3244 case CARET_STATE_ON: input->caret_state = 1; break;
3245 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3246 case CARET_STATE_ON_IF_MOVED:
3247 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3248 break;
3254 /* get the time of the last input event */
3255 DECL_HANDLER(get_last_input_time)
3257 reply->time = last_input_time;
3260 /* set/get the current cursor */
3261 DECL_HANDLER(set_cursor)
3263 struct msg_queue *queue = get_current_queue();
3264 struct thread_input *input;
3265 struct desktop *desktop;
3267 if (!queue) return;
3268 input = queue->input;
3269 desktop = input->desktop;
3271 reply->prev_handle = input->cursor;
3272 reply->prev_count = input->cursor_count;
3273 reply->prev_x = desktop->cursor.x;
3274 reply->prev_y = desktop->cursor.y;
3276 if (req->flags & SET_CURSOR_HANDLE)
3278 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3280 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3281 return;
3283 input->cursor = req->handle;
3285 if (req->flags & SET_CURSOR_COUNT)
3287 queue->cursor_count += req->show_count;
3288 input->cursor_count += req->show_count;
3290 if (req->flags & SET_CURSOR_POS) set_cursor_pos( desktop, req->x, req->y );
3291 if (req->flags & SET_CURSOR_CLIP) set_clip_rectangle( desktop, &req->clip, 0 );
3292 if (req->flags & SET_CURSOR_NOCLIP) set_clip_rectangle( desktop, NULL, 0 );
3294 reply->new_x = desktop->cursor.x;
3295 reply->new_y = desktop->cursor.y;
3296 reply->new_clip = desktop->cursor.clip;
3297 reply->last_change = desktop->cursor.last_change;
3300 /* Get the history of the 64 last cursor positions */
3301 DECL_HANDLER(get_cursor_history)
3303 cursor_pos_t *pos;
3304 unsigned int i, count = min( 64, get_reply_max_size() / sizeof(*pos) );
3306 if ((pos = set_reply_data_size( count * sizeof(*pos) )))
3307 for (i = 0; i < count; i++)
3308 pos[i] = cursor_history[(i + cursor_history_latest) % ARRAY_SIZE(cursor_history)];
3311 DECL_HANDLER(get_rawinput_buffer)
3313 struct thread_input *input = current->queue->input;
3314 data_size_t size = 0, next_size = 0, pos = 0;
3315 struct list *ptr;
3316 char *buf, *tmp;
3317 int count = 0, buf_size = 16 * sizeof(struct hardware_msg_data);
3319 if (!req->buffer_size) buf = NULL;
3320 else if (!(buf = mem_alloc( buf_size ))) return;
3322 ptr = list_head( &input->msg_list );
3323 while (ptr)
3325 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
3326 struct hardware_msg_data *data = msg->data;
3327 data_size_t extra_size = data->size - sizeof(*data);
3329 ptr = list_next( &input->msg_list, ptr );
3330 if (msg->msg != WM_INPUT) continue;
3332 next_size = req->rawinput_size + extra_size;
3333 if (size + next_size > req->buffer_size) break;
3334 if (pos + data->size > get_reply_max_size()) break;
3335 if (pos + data->size > buf_size)
3337 buf_size += buf_size / 2 + extra_size;
3338 if (!(tmp = realloc( buf, buf_size )))
3340 free( buf );
3341 set_error( STATUS_NO_MEMORY );
3342 return;
3344 buf = tmp;
3347 memcpy( buf + pos, data, data->size );
3348 list_remove( &msg->entry );
3349 free_message( msg );
3351 size += next_size;
3352 pos += sizeof(*data) + extra_size;
3353 count++;
3356 reply->next_size = next_size;
3357 reply->count = count;
3358 set_reply_data_ptr( buf, pos );
3361 DECL_HANDLER(update_rawinput_devices)
3363 const struct rawinput_device *tmp, *devices = get_req_data();
3364 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3365 size_t size = device_count * sizeof(*devices);
3366 struct process *process = current->process;
3368 if (!size)
3370 process->rawinput_device_count = 0;
3371 process->rawinput_mouse = NULL;
3372 process->rawinput_kbd = NULL;
3373 return;
3376 if (!(tmp = realloc( process->rawinput_devices, size )))
3378 set_error( STATUS_NO_MEMORY );
3379 return;
3381 process->rawinput_devices = (struct rawinput_device *)tmp;
3382 process->rawinput_device_count = device_count;
3383 memcpy( process->rawinput_devices, devices, size );
3385 process->rawinput_mouse = find_rawinput_device( process, 1, 2 );
3386 process->rawinput_kbd = find_rawinput_device( process, 1, 6 );