msvcr120: Add [_]strtoimax[_l] and [_]strtoumax[_l].
[wine.git] / server / queue.c
blobc1016016051afbdf1e0c711cff4c67974d52c744
1 /*
2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #ifdef HAVE_POLL_H
29 # include <poll.h>
30 #endif
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "winternl.h"
40 #include "handle.h"
41 #include "file.h"
42 #include "thread.h"
43 #include "process.h"
44 #include "request.h"
45 #include "user.h"
47 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
48 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
50 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
51 #define NB_MSG_KINDS (POST_MESSAGE+1)
54 struct message_result
56 struct list sender_entry; /* entry in sender list */
57 struct message *msg; /* message the result is for */
58 struct message_result *recv_next; /* next in receiver list */
59 struct msg_queue *sender; /* sender queue */
60 struct msg_queue *receiver; /* receiver queue */
61 int replied; /* has it been replied to? */
62 unsigned int error; /* error code to pass back to sender */
63 lparam_t result; /* reply result */
64 struct message *hardware_msg; /* hardware message if low-level hook result */
65 struct desktop *desktop; /* desktop for hardware message */
66 struct message *callback_msg; /* message to queue for callback */
67 void *data; /* message reply data */
68 unsigned int data_size; /* size of message reply data */
69 struct timeout_user *timeout; /* result timeout */
72 struct message
74 struct list entry; /* entry in message list */
75 enum message_type type; /* message type */
76 user_handle_t win; /* window handle */
77 unsigned int msg; /* message code */
78 lparam_t wparam; /* parameters */
79 lparam_t lparam; /* parameters */
80 int x; /* message position */
81 int y;
82 unsigned int time; /* message time */
83 void *data; /* message data for sent messages */
84 unsigned int data_size; /* size of message data */
85 unsigned int unique_id; /* unique id for nested hw message waits */
86 struct message_result *result; /* result in sender queue */
89 struct timer
91 struct list entry; /* entry in timer list */
92 abstime_t when; /* next expiration */
93 unsigned int rate; /* timer rate in ms */
94 user_handle_t win; /* window handle */
95 unsigned int msg; /* message to post */
96 lparam_t id; /* timer id */
97 lparam_t lparam; /* lparam for message */
100 struct thread_input
102 struct object obj; /* object header */
103 struct desktop *desktop; /* desktop that this thread input belongs to */
104 user_handle_t focus; /* focus window */
105 user_handle_t capture; /* capture window */
106 user_handle_t active; /* active window */
107 user_handle_t menu_owner; /* current menu owner window */
108 user_handle_t move_size; /* current moving/resizing window */
109 user_handle_t caret; /* caret window */
110 rectangle_t caret_rect; /* caret rectangle */
111 int caret_hide; /* caret hide count */
112 int caret_state; /* caret on/off state */
113 user_handle_t cursor; /* current cursor */
114 int cursor_count; /* cursor show count */
115 struct list msg_list; /* list of hardware messages */
116 unsigned char keystate[256]; /* state of each key */
119 struct msg_queue
121 struct object obj; /* object header */
122 struct fd *fd; /* optional file descriptor to poll */
123 unsigned int wake_bits; /* wakeup bits */
124 unsigned int wake_mask; /* wakeup mask */
125 unsigned int changed_bits; /* changed wakeup bits */
126 unsigned int changed_mask; /* changed wakeup mask */
127 int paint_count; /* pending paint messages count */
128 int hotkey_count; /* pending hotkey messages count */
129 int quit_message; /* is there a pending quit message? */
130 int exit_code; /* exit code of pending quit message */
131 int cursor_count; /* per-queue cursor show count */
132 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
133 struct list send_result; /* stack of sent messages waiting for result */
134 struct list callback_result; /* list of callback messages waiting for result */
135 struct message_result *recv_result; /* stack of received messages waiting for result */
136 struct list pending_timers; /* list of pending timers */
137 struct list expired_timers; /* list of expired timers */
138 lparam_t next_timer_id; /* id for the next timer with a 0 window */
139 struct timeout_user *timeout; /* timeout for next timer to expire */
140 struct thread_input *input; /* thread input descriptor */
141 struct hook_table *hooks; /* hook table */
142 timeout_t last_get_msg; /* time of last get message call */
145 struct hotkey
147 struct list entry; /* entry in desktop hotkey list */
148 struct msg_queue *queue; /* queue owning this hotkey */
149 user_handle_t win; /* window handle */
150 int id; /* hotkey id */
151 unsigned int vkey; /* virtual key code */
152 unsigned int flags; /* key modifiers */
155 static void msg_queue_dump( struct object *obj, int verbose );
156 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
157 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
158 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
159 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
160 static void msg_queue_destroy( struct object *obj );
161 static void msg_queue_poll_event( struct fd *fd, int event );
162 static void thread_input_dump( struct object *obj, int verbose );
163 static void thread_input_destroy( struct object *obj );
164 static void timer_callback( void *private );
166 static const struct object_ops msg_queue_ops =
168 sizeof(struct msg_queue), /* size */
169 msg_queue_dump, /* dump */
170 no_get_type, /* get_type */
171 msg_queue_add_queue, /* add_queue */
172 msg_queue_remove_queue, /* remove_queue */
173 msg_queue_signaled, /* signaled */
174 msg_queue_satisfied, /* satisfied */
175 no_signal, /* signal */
176 no_get_fd, /* get_fd */
177 no_map_access, /* map_access */
178 default_get_sd, /* get_sd */
179 default_set_sd, /* set_sd */
180 no_lookup_name, /* lookup_name */
181 no_link_name, /* link_name */
182 NULL, /* unlink_name */
183 no_open_file, /* open_file */
184 no_kernel_obj_list, /* get_kernel_obj_list */
185 no_close_handle, /* close_handle */
186 msg_queue_destroy /* destroy */
189 static const struct fd_ops msg_queue_fd_ops =
191 NULL, /* get_poll_events */
192 msg_queue_poll_event, /* poll_event */
193 NULL, /* flush */
194 NULL, /* get_fd_type */
195 NULL, /* ioctl */
196 NULL, /* queue_async */
197 NULL, /* reselect_async */
198 NULL /* cancel async */
202 static const struct object_ops thread_input_ops =
204 sizeof(struct thread_input), /* size */
205 thread_input_dump, /* dump */
206 no_get_type, /* get_type */
207 no_add_queue, /* add_queue */
208 NULL, /* remove_queue */
209 NULL, /* signaled */
210 NULL, /* satisfied */
211 no_signal, /* signal */
212 no_get_fd, /* get_fd */
213 no_map_access, /* map_access */
214 default_get_sd, /* get_sd */
215 default_set_sd, /* set_sd */
216 no_lookup_name, /* lookup_name */
217 no_link_name, /* link_name */
218 NULL, /* unlink_name */
219 no_open_file, /* open_file */
220 no_kernel_obj_list, /* get_kernel_obj_list */
221 no_close_handle, /* close_handle */
222 thread_input_destroy /* destroy */
225 /* pointer to input structure of foreground thread */
226 static unsigned int last_input_time;
228 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
229 static void free_message( struct message *msg );
231 /* set the caret window in a given thread input */
232 static void set_caret_window( struct thread_input *input, user_handle_t win )
234 if (!win || win != input->caret)
236 input->caret_rect.left = 0;
237 input->caret_rect.top = 0;
238 input->caret_rect.right = 0;
239 input->caret_rect.bottom = 0;
241 input->caret = win;
242 input->caret_hide = 1;
243 input->caret_state = 0;
246 /* create a thread input object */
247 static struct thread_input *create_thread_input( struct thread *thread )
249 struct thread_input *input;
251 if ((input = alloc_object( &thread_input_ops )))
253 input->focus = 0;
254 input->capture = 0;
255 input->active = 0;
256 input->menu_owner = 0;
257 input->move_size = 0;
258 input->cursor = 0;
259 input->cursor_count = 0;
260 list_init( &input->msg_list );
261 set_caret_window( input, 0 );
262 memset( input->keystate, 0, sizeof(input->keystate) );
264 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
266 release_object( input );
267 return NULL;
270 return input;
273 /* create a message queue object */
274 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
276 struct thread_input *new_input = NULL;
277 struct msg_queue *queue;
278 int i;
280 if (!input)
282 if (!(new_input = create_thread_input( thread ))) return NULL;
283 input = new_input;
286 if ((queue = alloc_object( &msg_queue_ops )))
288 queue->fd = NULL;
289 queue->wake_bits = 0;
290 queue->wake_mask = 0;
291 queue->changed_bits = 0;
292 queue->changed_mask = 0;
293 queue->paint_count = 0;
294 queue->hotkey_count = 0;
295 queue->quit_message = 0;
296 queue->cursor_count = 0;
297 queue->recv_result = NULL;
298 queue->next_timer_id = 0x7fff;
299 queue->timeout = NULL;
300 queue->input = (struct thread_input *)grab_object( input );
301 queue->hooks = NULL;
302 queue->last_get_msg = current_time;
303 list_init( &queue->send_result );
304 list_init( &queue->callback_result );
305 list_init( &queue->pending_timers );
306 list_init( &queue->expired_timers );
307 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
309 thread->queue = queue;
311 if (new_input) release_object( new_input );
312 return queue;
315 /* free the message queue of a thread at thread exit */
316 void free_msg_queue( struct thread *thread )
318 remove_thread_hooks( thread );
319 if (!thread->queue) return;
320 release_object( thread->queue );
321 thread->queue = NULL;
324 /* change the thread input data of a given thread */
325 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
327 struct msg_queue *queue = thread->queue;
329 if (!queue)
331 thread->queue = create_msg_queue( thread, new_input );
332 return thread->queue != NULL;
334 if (queue->input)
336 queue->input->cursor_count -= queue->cursor_count;
337 release_object( queue->input );
339 queue->input = (struct thread_input *)grab_object( new_input );
340 new_input->cursor_count += queue->cursor_count;
341 return 1;
344 /* allocate a hardware message and its data */
345 static struct message *alloc_hardware_message( lparam_t info, struct hw_msg_source source,
346 unsigned int time )
348 struct hardware_msg_data *msg_data;
349 struct message *msg;
351 if (!(msg = mem_alloc( sizeof(*msg) ))) return NULL;
352 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
354 free( msg );
355 return NULL;
357 memset( msg, 0, sizeof(*msg) );
358 msg->type = MSG_HARDWARE;
359 msg->time = time;
360 msg->data = msg_data;
361 msg->data_size = sizeof(*msg_data);
363 memset( msg_data, 0, sizeof(*msg_data) );
364 msg_data->info = info;
365 msg_data->source = source;
366 return msg;
369 static int update_desktop_cursor_pos( struct desktop *desktop, int x, int y )
371 int updated;
373 x = max( min( x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
374 y = max( min( y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
375 updated = (desktop->cursor.x != x || desktop->cursor.y != y);
376 desktop->cursor.x = x;
377 desktop->cursor.y = y;
378 desktop->cursor.last_change = get_tick_count();
380 return updated;
383 /* set the cursor position and queue the corresponding mouse message */
384 static void set_cursor_pos( struct desktop *desktop, int x, int y )
386 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
387 const struct rawinput_device *device;
388 struct message *msg;
390 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
392 update_desktop_cursor_pos( desktop, x, y );
393 return;
396 if (!(msg = alloc_hardware_message( 0, source, get_tick_count() ))) return;
398 msg->msg = WM_MOUSEMOVE;
399 msg->x = x;
400 msg->y = y;
401 queue_hardware_message( desktop, msg, 1 );
404 /* retrieve default position and time for synthesized messages */
405 static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
407 struct desktop *desktop = queue->input->desktop;
409 *x = desktop->cursor.x;
410 *y = desktop->cursor.y;
411 *time = get_tick_count();
414 /* set the cursor clip rectangle */
415 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect, int send_clip_msg )
417 rectangle_t top_rect;
418 int x, y;
420 get_top_window_rectangle( desktop, &top_rect );
421 if (rect)
423 rectangle_t new_rect = *rect;
424 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
425 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
426 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
427 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
428 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
429 desktop->cursor.clip = new_rect;
431 else desktop->cursor.clip = top_rect;
433 if (desktop->cursor.clip_msg && send_clip_msg)
434 post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
436 /* warp the mouse to be inside the clip rect */
437 x = max( min( desktop->cursor.x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
438 y = max( min( desktop->cursor.y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
439 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
442 /* change the foreground input and reset the cursor clip rect */
443 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
445 if (desktop->foreground_input == input) return;
446 set_clip_rectangle( desktop, NULL, 1 );
447 desktop->foreground_input = input;
450 /* get the hook table for a given thread */
451 struct hook_table *get_queue_hooks( struct thread *thread )
453 if (!thread->queue) return NULL;
454 return thread->queue->hooks;
457 /* set the hook table for a given thread, allocating the queue if needed */
458 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
460 struct msg_queue *queue = thread->queue;
461 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
462 if (queue->hooks) release_object( queue->hooks );
463 queue->hooks = hooks;
466 /* check the queue status */
467 static inline int is_signaled( struct msg_queue *queue )
469 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
472 /* set some queue bits */
473 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
475 queue->wake_bits |= bits;
476 queue->changed_bits |= bits;
477 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
480 /* clear some queue bits */
481 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
483 queue->wake_bits &= ~bits;
484 queue->changed_bits &= ~bits;
487 /* check whether msg is a keyboard message */
488 static inline int is_keyboard_msg( struct message *msg )
490 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
493 /* check if message is matched by the filter */
494 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
496 return (msg >= first && msg <= last);
499 /* check whether a message filter contains at least one potential hardware message */
500 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
502 /* hardware message ranges are (in numerical order):
503 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
504 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
505 * WM_MOUSEFIRST .. WM_MOUSELAST
507 if (last < WM_NCMOUSEFIRST) return 0;
508 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
509 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
510 if (first > WM_MOUSELAST) return 0;
511 return 1;
514 /* get the QS_* bit corresponding to a given hardware message */
515 static inline int get_hardware_msg_bit( struct message *msg )
517 if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
518 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
519 if (is_keyboard_msg( msg )) return QS_KEY;
520 return QS_MOUSEBUTTON;
523 /* get the current thread queue, creating it if needed */
524 static inline struct msg_queue *get_current_queue(void)
526 struct msg_queue *queue = current->queue;
527 if (!queue) queue = create_msg_queue( current, NULL );
528 return queue;
531 /* get a (pseudo-)unique id to tag hardware messages */
532 static inline unsigned int get_unique_id(void)
534 static unsigned int id;
535 if (!++id) id = 1; /* avoid an id of 0 */
536 return id;
539 /* try to merge a message with the last in the list; return 1 if successful */
540 static int merge_message( struct thread_input *input, const struct message *msg )
542 struct message *prev;
543 struct list *ptr;
545 if (msg->msg != WM_MOUSEMOVE) return 0;
546 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
548 prev = LIST_ENTRY( ptr, struct message, entry );
549 if (prev->msg != WM_INPUT) break;
551 if (!ptr) return 0;
552 if (prev->result) return 0;
553 if (prev->win && msg->win && prev->win != msg->win) return 0;
554 if (prev->msg != msg->msg) return 0;
555 if (prev->type != msg->type) return 0;
556 /* now we can merge it */
557 prev->wparam = msg->wparam;
558 prev->lparam = msg->lparam;
559 prev->x = msg->x;
560 prev->y = msg->y;
561 prev->time = msg->time;
562 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
564 struct hardware_msg_data *prev_data = prev->data;
565 struct hardware_msg_data *msg_data = msg->data;
566 prev_data->info = msg_data->info;
568 list_remove( ptr );
569 list_add_tail( &input->msg_list, ptr );
570 return 1;
573 /* free a result structure */
574 static void free_result( struct message_result *result )
576 if (result->timeout) remove_timeout_user( result->timeout );
577 free( result->data );
578 if (result->callback_msg) free_message( result->callback_msg );
579 if (result->hardware_msg) free_message( result->hardware_msg );
580 if (result->desktop) release_object( result->desktop );
581 free( result );
584 /* remove the result from the sender list it is on */
585 static inline void remove_result_from_sender( struct message_result *result )
587 assert( result->sender );
589 list_remove( &result->sender_entry );
590 result->sender = NULL;
591 if (!result->receiver) free_result( result );
594 /* store the message result in the appropriate structure */
595 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
597 res->result = result;
598 res->error = error;
599 res->replied = 1;
600 if (res->timeout)
602 remove_timeout_user( res->timeout );
603 res->timeout = NULL;
606 if (res->hardware_msg)
608 if (!error && result) /* rejected by the hook */
609 free_message( res->hardware_msg );
610 else
611 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
613 res->hardware_msg = NULL;
616 if (res->sender)
618 if (res->callback_msg)
620 /* queue the callback message in the sender queue */
621 struct callback_msg_data *data = res->callback_msg->data;
622 data->result = result;
623 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
624 set_queue_bits( res->sender, QS_SENDMESSAGE );
625 res->callback_msg = NULL;
626 remove_result_from_sender( res );
628 else
630 /* wake sender queue if waiting on this result */
631 if (list_head(&res->sender->send_result) == &res->sender_entry)
632 set_queue_bits( res->sender, QS_SMRESULT );
635 else if (!res->receiver) free_result( res );
638 /* free a message when deleting a queue or window */
639 static void free_message( struct message *msg )
641 struct message_result *result = msg->result;
642 if (result)
644 result->msg = NULL;
645 result->receiver = NULL;
646 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
648 free( msg->data );
649 free( msg );
652 /* remove (and free) a message from a message list */
653 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
654 enum message_kind kind )
656 list_remove( &msg->entry );
657 switch(kind)
659 case SEND_MESSAGE:
660 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
661 break;
662 case POST_MESSAGE:
663 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
664 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
665 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
666 clear_queue_bits( queue, QS_HOTKEY );
667 break;
669 free_message( msg );
672 /* message timed out without getting a reply */
673 static void result_timeout( void *private )
675 struct message_result *result = private;
677 assert( !result->replied );
679 result->timeout = NULL;
681 if (result->msg) /* not received yet */
683 struct message *msg = result->msg;
685 result->msg = NULL;
686 msg->result = NULL;
687 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
688 result->receiver = NULL;
690 store_message_result( result, 0, STATUS_TIMEOUT );
693 /* allocate and fill a message result structure */
694 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
695 struct msg_queue *recv_queue,
696 struct message *msg, timeout_t timeout )
698 struct message_result *result = mem_alloc( sizeof(*result) );
699 if (result)
701 result->msg = msg;
702 result->sender = send_queue;
703 result->receiver = recv_queue;
704 result->replied = 0;
705 result->data = NULL;
706 result->data_size = 0;
707 result->timeout = NULL;
708 result->hardware_msg = NULL;
709 result->desktop = NULL;
710 result->callback_msg = NULL;
712 if (msg->type == MSG_CALLBACK)
714 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
716 if (!callback_msg)
718 free( result );
719 return NULL;
721 callback_msg->type = MSG_CALLBACK_RESULT;
722 callback_msg->win = msg->win;
723 callback_msg->msg = msg->msg;
724 callback_msg->wparam = 0;
725 callback_msg->lparam = 0;
726 callback_msg->time = get_tick_count();
727 callback_msg->result = NULL;
728 /* steal the data from the original message */
729 callback_msg->data = msg->data;
730 callback_msg->data_size = msg->data_size;
731 msg->data = NULL;
732 msg->data_size = 0;
734 result->callback_msg = callback_msg;
735 list_add_head( &send_queue->callback_result, &result->sender_entry );
737 else if (send_queue)
739 list_add_head( &send_queue->send_result, &result->sender_entry );
740 clear_queue_bits( send_queue, QS_SMRESULT );
743 if (timeout != TIMEOUT_INFINITE)
744 result->timeout = add_timeout_user( timeout, result_timeout, result );
746 return result;
749 /* receive a message, removing it from the sent queue */
750 static void receive_message( struct msg_queue *queue, struct message *msg,
751 struct get_message_reply *reply )
753 struct message_result *result = msg->result;
755 reply->total = msg->data_size;
756 if (msg->data_size > get_reply_max_size())
758 set_error( STATUS_BUFFER_OVERFLOW );
759 return;
761 reply->type = msg->type;
762 reply->win = msg->win;
763 reply->msg = msg->msg;
764 reply->wparam = msg->wparam;
765 reply->lparam = msg->lparam;
766 reply->x = msg->x;
767 reply->y = msg->y;
768 reply->time = msg->time;
770 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
772 list_remove( &msg->entry );
773 /* put the result on the receiver result stack */
774 if (result)
776 result->msg = NULL;
777 result->recv_next = queue->recv_result;
778 queue->recv_result = result;
780 free( msg );
781 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
784 /* set the result of the current received message */
785 static void reply_message( struct msg_queue *queue, lparam_t result,
786 unsigned int error, int remove, const void *data, data_size_t len )
788 struct message_result *res = queue->recv_result;
790 if (remove)
792 queue->recv_result = res->recv_next;
793 res->receiver = NULL;
794 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
796 free_result( res );
797 return;
800 if (!res->replied)
802 if (len && (res->data = memdup( data, len ))) res->data_size = len;
803 store_message_result( res, result, error );
807 static int match_window( user_handle_t win, user_handle_t msg_win )
809 if (!win) return 1;
810 if (win == -1 || win == 1) return !msg_win;
811 if (msg_win == win) return 1;
812 return is_child_window( win, msg_win );
815 /* retrieve a posted message */
816 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
817 unsigned int first, unsigned int last, unsigned int flags,
818 struct get_message_reply *reply )
820 struct message *msg;
822 /* check against the filters */
823 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
825 if (!match_window( win, msg->win )) continue;
826 if (!check_msg_filter( msg->msg, first, last )) continue;
827 goto found; /* found one */
829 return 0;
831 /* return it to the app */
832 found:
833 reply->total = msg->data_size;
834 if (msg->data_size > get_reply_max_size())
836 set_error( STATUS_BUFFER_OVERFLOW );
837 return 1;
839 reply->type = msg->type;
840 reply->win = msg->win;
841 reply->msg = msg->msg;
842 reply->wparam = msg->wparam;
843 reply->lparam = msg->lparam;
844 reply->x = msg->x;
845 reply->y = msg->y;
846 reply->time = msg->time;
848 if (flags & PM_REMOVE)
850 if (msg->data)
852 set_reply_data_ptr( msg->data, msg->data_size );
853 msg->data = NULL;
854 msg->data_size = 0;
856 remove_queue_message( queue, msg, POST_MESSAGE );
858 else if (msg->data) set_reply_data( msg->data, msg->data_size );
860 return 1;
863 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
864 struct get_message_reply *reply )
866 if (queue->quit_message)
868 reply->total = 0;
869 reply->type = MSG_POSTED;
870 reply->win = 0;
871 reply->msg = WM_QUIT;
872 reply->wparam = queue->exit_code;
873 reply->lparam = 0;
875 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
877 if (flags & PM_REMOVE)
879 queue->quit_message = 0;
880 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
881 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
883 return 1;
885 else
886 return 0;
889 /* empty a message list and free all the messages */
890 static void empty_msg_list( struct list *list )
892 struct list *ptr;
894 while ((ptr = list_head( list )) != NULL)
896 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
897 list_remove( &msg->entry );
898 free_message( msg );
902 /* cleanup all pending results when deleting a queue */
903 static void cleanup_results( struct msg_queue *queue )
905 struct list *entry;
907 while ((entry = list_head( &queue->send_result )) != NULL)
909 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
912 while ((entry = list_head( &queue->callback_result )) != NULL)
914 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
917 while (queue->recv_result)
918 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
921 /* check if the thread owning the queue is hung (not checking for messages) */
922 static int is_queue_hung( struct msg_queue *queue )
924 struct wait_queue_entry *entry;
926 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
927 return 0; /* less than 5 seconds since last get message -> not hung */
929 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
931 if (get_wait_queue_thread(entry)->queue == queue)
932 return 0; /* thread is waiting on queue -> not hung */
934 return 1;
937 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
939 struct msg_queue *queue = (struct msg_queue *)obj;
940 struct process *process = get_wait_queue_thread(entry)->process;
942 /* a thread can only wait on its own queue */
943 if (get_wait_queue_thread(entry)->queue != queue)
945 set_error( STATUS_ACCESS_DENIED );
946 return 0;
948 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
950 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
951 set_fd_events( queue->fd, POLLIN );
952 add_queue( obj, entry );
953 return 1;
956 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
958 struct msg_queue *queue = (struct msg_queue *)obj;
960 remove_queue( obj, entry );
961 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
962 set_fd_events( queue->fd, 0 );
965 static void msg_queue_dump( struct object *obj, int verbose )
967 struct msg_queue *queue = (struct msg_queue *)obj;
968 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
969 queue->wake_bits, queue->wake_mask );
972 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
974 struct msg_queue *queue = (struct msg_queue *)obj;
975 int ret = 0;
977 if (queue->fd)
979 if ((ret = check_fd_events( queue->fd, POLLIN )))
980 /* stop waiting on select() if we are signaled */
981 set_fd_events( queue->fd, 0 );
982 else if (!list_empty( &obj->wait_queue ))
983 /* restart waiting on poll() if we are no longer signaled */
984 set_fd_events( queue->fd, POLLIN );
986 return ret || is_signaled( queue );
989 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
991 struct msg_queue *queue = (struct msg_queue *)obj;
992 queue->wake_mask = 0;
993 queue->changed_mask = 0;
996 static void msg_queue_destroy( struct object *obj )
998 struct msg_queue *queue = (struct msg_queue *)obj;
999 struct list *ptr;
1000 struct hotkey *hotkey, *hotkey2;
1001 int i;
1003 cleanup_results( queue );
1004 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
1006 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
1008 if (hotkey->queue == queue)
1010 list_remove( &hotkey->entry );
1011 free( hotkey );
1015 while ((ptr = list_head( &queue->pending_timers )))
1017 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1018 list_remove( &timer->entry );
1019 free( timer );
1021 while ((ptr = list_head( &queue->expired_timers )))
1023 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1024 list_remove( &timer->entry );
1025 free( timer );
1027 if (queue->timeout) remove_timeout_user( queue->timeout );
1028 queue->input->cursor_count -= queue->cursor_count;
1029 release_object( queue->input );
1030 if (queue->hooks) release_object( queue->hooks );
1031 if (queue->fd) release_object( queue->fd );
1034 static void msg_queue_poll_event( struct fd *fd, int event )
1036 struct msg_queue *queue = get_fd_user( fd );
1037 assert( queue->obj.ops == &msg_queue_ops );
1039 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1040 else set_fd_events( queue->fd, 0 );
1041 wake_up( &queue->obj, 0 );
1044 static void thread_input_dump( struct object *obj, int verbose )
1046 struct thread_input *input = (struct thread_input *)obj;
1047 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
1048 input->focus, input->capture, input->active );
1051 static void thread_input_destroy( struct object *obj )
1053 struct thread_input *input = (struct thread_input *)obj;
1055 empty_msg_list( &input->msg_list );
1056 if (input->desktop)
1058 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
1059 release_object( input->desktop );
1063 /* fix the thread input data when a window is destroyed */
1064 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1066 struct thread_input *input = queue->input;
1068 if (window == input->focus) input->focus = 0;
1069 if (window == input->capture) input->capture = 0;
1070 if (window == input->active) input->active = 0;
1071 if (window == input->menu_owner) input->menu_owner = 0;
1072 if (window == input->move_size) input->move_size = 0;
1073 if (window == input->caret) set_caret_window( input, 0 );
1076 /* check if the specified window can be set in the input data of a given queue */
1077 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1079 struct thread *thread;
1080 int ret = 0;
1082 if (!window) return 1; /* we can always clear the data */
1084 if ((thread = get_window_thread( window )))
1086 ret = (queue->input == thread->queue->input);
1087 if (!ret) set_error( STATUS_ACCESS_DENIED );
1088 release_object( thread );
1090 else set_error( STATUS_INVALID_HANDLE );
1092 return ret;
1095 /* make sure the specified thread has a queue */
1096 int init_thread_queue( struct thread *thread )
1098 if (thread->queue) return 1;
1099 return (create_msg_queue( thread, NULL ) != NULL);
1102 /* attach two thread input data structures */
1103 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1105 struct desktop *desktop;
1106 struct thread_input *input;
1107 int ret;
1109 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1110 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1111 input = (struct thread_input *)grab_object( thread_to->queue->input );
1112 if (input->desktop != desktop)
1114 set_error( STATUS_ACCESS_DENIED );
1115 release_object( input );
1116 release_object( desktop );
1117 return 0;
1119 release_object( desktop );
1121 if (thread_from->queue)
1123 if (!input->focus) input->focus = thread_from->queue->input->focus;
1124 if (!input->active) input->active = thread_from->queue->input->active;
1127 ret = assign_thread_input( thread_from, input );
1128 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1129 release_object( input );
1130 return ret;
1133 /* detach two thread input data structures */
1134 void detach_thread_input( struct thread *thread_from )
1136 struct thread *thread;
1137 struct thread_input *input, *old_input = thread_from->queue->input;
1139 if ((input = create_thread_input( thread_from )))
1141 if (old_input->focus && (thread = get_window_thread( old_input->focus )))
1143 if (thread == thread_from)
1145 input->focus = old_input->focus;
1146 old_input->focus = 0;
1148 release_object( thread );
1150 if (old_input->active && (thread = get_window_thread( old_input->active )))
1152 if (thread == thread_from)
1154 input->active = old_input->active;
1155 old_input->active = 0;
1157 release_object( thread );
1159 assign_thread_input( thread_from, input );
1160 release_object( input );
1165 /* set the next timer to expire */
1166 static void set_next_timer( struct msg_queue *queue )
1168 struct list *ptr;
1170 if (queue->timeout)
1172 remove_timeout_user( queue->timeout );
1173 queue->timeout = NULL;
1175 if ((ptr = list_head( &queue->pending_timers )))
1177 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1178 queue->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, queue );
1180 /* set/clear QS_TIMER bit */
1181 if (list_empty( &queue->expired_timers ))
1182 clear_queue_bits( queue, QS_TIMER );
1183 else
1184 set_queue_bits( queue, QS_TIMER );
1187 /* find a timer from its window and id */
1188 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1189 unsigned int msg, lparam_t id )
1191 struct list *ptr;
1193 /* we need to search both lists */
1195 LIST_FOR_EACH( ptr, &queue->pending_timers )
1197 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1198 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1200 LIST_FOR_EACH( ptr, &queue->expired_timers )
1202 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1203 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1205 return NULL;
1208 /* callback for the next timer expiration */
1209 static void timer_callback( void *private )
1211 struct msg_queue *queue = private;
1212 struct list *ptr;
1214 queue->timeout = NULL;
1215 /* move on to the next timer */
1216 ptr = list_head( &queue->pending_timers );
1217 list_remove( ptr );
1218 list_add_tail( &queue->expired_timers, ptr );
1219 set_next_timer( queue );
1222 /* link a timer at its rightful place in the queue list */
1223 static void link_timer( struct msg_queue *queue, struct timer *timer )
1225 struct list *ptr;
1227 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1229 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1230 if (t->when <= timer->when) break;
1232 list_add_before( ptr, &timer->entry );
1235 /* remove a timer from the queue timer list and free it */
1236 static void free_timer( struct msg_queue *queue, struct timer *timer )
1238 list_remove( &timer->entry );
1239 free( timer );
1240 set_next_timer( queue );
1243 /* restart an expired timer */
1244 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1246 list_remove( &timer->entry );
1247 while (-timer->when <= monotonic_time) timer->when -= (timeout_t)timer->rate * 10000;
1248 link_timer( queue, timer );
1249 set_next_timer( queue );
1252 /* find an expired timer matching the filtering parameters */
1253 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1254 unsigned int get_first, unsigned int get_last,
1255 int remove )
1257 struct list *ptr;
1259 LIST_FOR_EACH( ptr, &queue->expired_timers )
1261 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1262 if (win && timer->win != win) continue;
1263 if (check_msg_filter( timer->msg, get_first, get_last ))
1265 if (remove) restart_timer( queue, timer );
1266 return timer;
1269 return NULL;
1272 /* add a timer */
1273 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1275 struct timer *timer = mem_alloc( sizeof(*timer) );
1276 if (timer)
1278 timer->rate = max( rate, 1 );
1279 timer->when = -monotonic_time - (timeout_t)timer->rate * 10000;
1280 link_timer( queue, timer );
1281 /* check if we replaced the next timer */
1282 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1284 return timer;
1287 /* change the input key state for a given key */
1288 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1290 if (down)
1292 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1293 keystate[key] |= down;
1295 else keystate[key] &= ~0x80;
1298 /* update the input key state for a keyboard message */
1299 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1300 unsigned int msg, lparam_t wparam )
1302 unsigned char key;
1303 int down = 0;
1305 switch (msg)
1307 case WM_LBUTTONDOWN:
1308 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1309 /* fall through */
1310 case WM_LBUTTONUP:
1311 set_input_key_state( keystate, VK_LBUTTON, down );
1312 break;
1313 case WM_MBUTTONDOWN:
1314 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1315 /* fall through */
1316 case WM_MBUTTONUP:
1317 set_input_key_state( keystate, VK_MBUTTON, down );
1318 break;
1319 case WM_RBUTTONDOWN:
1320 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1321 /* fall through */
1322 case WM_RBUTTONUP:
1323 set_input_key_state( keystate, VK_RBUTTON, down );
1324 break;
1325 case WM_XBUTTONDOWN:
1326 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1327 /* fall through */
1328 case WM_XBUTTONUP:
1329 if (wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1330 else if (wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1331 break;
1332 case WM_KEYDOWN:
1333 case WM_SYSKEYDOWN:
1334 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1335 /* fall through */
1336 case WM_KEYUP:
1337 case WM_SYSKEYUP:
1338 key = (unsigned char)wparam;
1339 set_input_key_state( keystate, key, down );
1340 switch(key)
1342 case VK_LCONTROL:
1343 case VK_RCONTROL:
1344 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1345 set_input_key_state( keystate, VK_CONTROL, down );
1346 break;
1347 case VK_LMENU:
1348 case VK_RMENU:
1349 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1350 set_input_key_state( keystate, VK_MENU, down );
1351 break;
1352 case VK_LSHIFT:
1353 case VK_RSHIFT:
1354 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1355 set_input_key_state( keystate, VK_SHIFT, down );
1356 break;
1358 break;
1362 /* update the desktop key state according to a mouse message flags */
1363 static void update_desktop_mouse_state( struct desktop *desktop, unsigned int flags,
1364 int x, int y, lparam_t wparam )
1366 if (flags & MOUSEEVENTF_MOVE)
1367 update_desktop_cursor_pos( desktop, x, y );
1368 if (flags & MOUSEEVENTF_LEFTDOWN)
1369 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONDOWN, wparam );
1370 if (flags & MOUSEEVENTF_LEFTUP)
1371 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONUP, wparam );
1372 if (flags & MOUSEEVENTF_RIGHTDOWN)
1373 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONDOWN, wparam );
1374 if (flags & MOUSEEVENTF_RIGHTUP)
1375 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONUP, wparam );
1376 if (flags & MOUSEEVENTF_MIDDLEDOWN)
1377 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONDOWN, wparam );
1378 if (flags & MOUSEEVENTF_MIDDLEUP)
1379 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONUP, wparam );
1380 if (flags & MOUSEEVENTF_XDOWN)
1381 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONDOWN, wparam );
1382 if (flags & MOUSEEVENTF_XUP)
1383 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONUP, wparam );
1386 /* release the hardware message currently being processed by the given thread */
1387 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id )
1389 struct thread_input *input = queue->input;
1390 struct message *msg, *other;
1391 int clr_bit;
1393 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1395 if (msg->unique_id == hw_id) break;
1397 if (&msg->entry == &input->msg_list) return; /* not found */
1399 /* clear the queue bit for that message */
1400 clr_bit = get_hardware_msg_bit( msg );
1401 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1403 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1405 clr_bit = 0;
1406 break;
1409 if (clr_bit) clear_queue_bits( queue, clr_bit );
1411 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1412 list_remove( &msg->entry );
1413 free_message( msg );
1416 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1418 struct hotkey *hotkey;
1419 unsigned int modifiers = 0;
1421 if (msg->msg != WM_KEYDOWN) return 0;
1423 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1424 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1425 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1426 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1428 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1430 if (hotkey->vkey != msg->wparam) continue;
1431 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1434 return 0;
1436 found:
1437 msg->type = MSG_POSTED;
1438 msg->win = hotkey->win;
1439 msg->msg = WM_HOTKEY;
1440 msg->wparam = hotkey->id;
1441 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1443 free( msg->data );
1444 msg->data = NULL;
1445 msg->data_size = 0;
1447 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1448 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1449 hotkey->queue->hotkey_count++;
1450 return 1;
1453 /* find the window that should receive a given hardware message */
1454 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1455 struct message *msg, unsigned int *msg_code,
1456 struct thread **thread )
1458 user_handle_t win = 0;
1460 *thread = NULL;
1461 *msg_code = msg->msg;
1462 if (msg->msg == WM_INPUT)
1464 if (!(win = msg->win) && input) win = input->focus;
1466 else if (is_keyboard_msg( msg ))
1468 if (input && !(win = input->focus))
1470 win = input->active;
1471 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1474 else if (!input || !(win = input->capture)) /* mouse message */
1476 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1477 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1479 *thread = window_thread_from_point( win, msg->x, msg->y );
1482 if (!*thread)
1483 *thread = get_window_thread( win );
1484 return win;
1487 static struct rawinput_device_entry *find_rawinput_device( unsigned short usage_page, unsigned short usage )
1489 struct rawinput_device_entry *e;
1491 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
1493 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1494 return e;
1497 return NULL;
1500 static void update_rawinput_device(const struct rawinput_device *device)
1502 struct rawinput_device_entry *e;
1504 if (!(e = find_rawinput_device( device->usage_page, device->usage )))
1506 if (!(e = mem_alloc( sizeof(*e) ))) return;
1507 list_add_tail( &current->process->rawinput_devices, &e->entry );
1510 if (device->flags & RIDEV_REMOVE)
1512 list_remove( &e->entry );
1513 free( e );
1514 return;
1517 e->device = *device;
1518 e->device.target = get_user_full_handle( e->device.target );
1521 /* queue a hardware message into a given thread input */
1522 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1524 user_handle_t win;
1525 struct thread *thread;
1526 struct thread_input *input;
1527 unsigned int msg_code;
1529 update_input_key_state( desktop, desktop->keystate, msg->msg, msg->wparam );
1530 last_input_time = get_tick_count();
1531 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1533 if (is_keyboard_msg( msg ))
1535 if (queue_hotkey_message( desktop, msg )) return;
1536 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1537 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1538 msg->lparam &= ~(KF_EXTENDED << 16);
1540 else if (msg->msg != WM_INPUT)
1542 if (msg->msg == WM_MOUSEMOVE && update_desktop_cursor_pos( desktop, msg->x, msg->y )) always_queue = 1;
1543 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1544 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1545 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1546 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1547 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1548 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1549 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1551 msg->x = desktop->cursor.x;
1552 msg->y = desktop->cursor.y;
1554 if (msg->win && (thread = get_window_thread( msg->win )))
1556 input = thread->queue->input;
1557 release_object( thread );
1559 else input = desktop->foreground_input;
1561 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1562 if (!win || !thread)
1564 if (input) update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1565 free_message( msg );
1566 return;
1568 input = thread->queue->input;
1570 if (win != desktop->cursor.win) always_queue = 1;
1571 desktop->cursor.win = win;
1573 if (!always_queue || merge_message( input, msg )) free_message( msg );
1574 else
1576 msg->unique_id = 0; /* will be set once we return it to the app */
1577 list_add_tail( &input->msg_list, &msg->entry );
1578 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1580 release_object( thread );
1583 /* send the low-level hook message for a given hardware message */
1584 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1585 const hw_input_t *input, struct msg_queue *sender )
1587 struct thread *hook_thread;
1588 struct msg_queue *queue;
1589 struct message *msg;
1590 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1591 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1593 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1594 if (!(queue = hook_thread->queue)) return 0;
1595 if (is_queue_hung( queue )) return 0;
1597 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1599 msg->type = MSG_HOOK_LL;
1600 msg->win = 0;
1601 msg->msg = id;
1602 msg->wparam = hardware_msg->msg;
1603 msg->x = hardware_msg->x;
1604 msg->y = hardware_msg->y;
1605 msg->time = hardware_msg->time;
1606 msg->data_size = hardware_msg->data_size;
1607 msg->result = NULL;
1609 if (input->type == INPUT_KEYBOARD)
1611 unsigned short vkey = input->kbd.vkey;
1612 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1613 msg->lparam = (input->kbd.scan << 16) | vkey;
1615 else msg->lparam = input->mouse.data << 16;
1617 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1618 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1620 free_message( msg );
1621 return 0;
1623 msg->result->hardware_msg = hardware_msg;
1624 msg->result->desktop = (struct desktop *)grab_object( desktop );
1625 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1626 set_queue_bits( queue, QS_SENDMESSAGE );
1627 return 1;
1630 /* get the foreground thread for a desktop and a window receiving input */
1631 static struct thread *get_foreground_thread( struct desktop *desktop, user_handle_t window )
1633 /* if desktop has no foreground process, assume the receiving window is */
1634 if (desktop->foreground_input) return get_window_thread( desktop->foreground_input->focus );
1635 if (window) return get_window_thread( window );
1636 return NULL;
1639 struct rawinput_message
1641 struct thread *foreground;
1642 struct desktop *desktop;
1643 struct hw_msg_source source;
1644 unsigned int time;
1645 struct hardware_msg_data data;
1648 /* check if process is supposed to receive a WM_INPUT message and eventually queue it */
1649 static int queue_rawinput_message( struct process* process, void *arg )
1651 const struct rawinput_message* raw_msg = arg;
1652 const struct rawinput_device *device = NULL;
1653 struct desktop *target_desktop = NULL;
1654 struct thread *target_thread = NULL;
1655 struct message *msg;
1656 int wparam = RIM_INPUT;
1658 if (raw_msg->data.rawinput.type == RIM_TYPEMOUSE)
1659 device = process->rawinput_mouse;
1660 else if (raw_msg->data.rawinput.type == RIM_TYPEKEYBOARD)
1661 device = process->rawinput_kbd;
1662 if (!device) return 0;
1664 if (process != raw_msg->foreground->process)
1666 if (!(device->flags & RIDEV_INPUTSINK)) goto done;
1667 if (!(target_thread = get_window_thread( device->target ))) goto done;
1668 if (!(target_desktop = get_thread_desktop( target_thread, 0 ))) goto done;
1669 if (target_desktop != raw_msg->desktop) goto done;
1670 wparam = RIM_INPUTSINK;
1673 if (!(msg = alloc_hardware_message( raw_msg->data.info, raw_msg->source, raw_msg->time )))
1674 goto done;
1676 msg->win = device->target;
1677 msg->msg = WM_INPUT;
1678 msg->wparam = wparam;
1679 msg->lparam = 0;
1680 memcpy( msg->data, &raw_msg->data, sizeof(raw_msg->data) );
1682 queue_hardware_message( raw_msg->desktop, msg, 1 );
1684 done:
1685 if (target_thread) release_object( target_thread );
1686 if (target_desktop) release_object( target_desktop );
1687 return 0;
1690 /* queue a hardware message for a mouse event */
1691 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1692 unsigned int origin, struct msg_queue *sender )
1694 const struct rawinput_device *device;
1695 struct hardware_msg_data *msg_data;
1696 struct rawinput_message raw_msg;
1697 struct message *msg;
1698 struct thread *foreground;
1699 unsigned int i, time, flags;
1700 struct hw_msg_source source = { IMDT_MOUSE, origin };
1701 int wait = 0, x, y;
1703 static const unsigned int messages[] =
1705 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1706 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1707 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1708 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1709 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1710 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1711 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1712 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1713 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1714 0, /* 0x0200 = unused */
1715 0, /* 0x0400 = unused */
1716 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1717 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1720 desktop->cursor.last_change = get_tick_count();
1721 flags = input->mouse.flags;
1722 time = input->mouse.time;
1723 if (!time) time = desktop->cursor.last_change;
1725 if (flags & MOUSEEVENTF_MOVE)
1727 if (flags & MOUSEEVENTF_ABSOLUTE)
1729 x = input->mouse.x;
1730 y = input->mouse.y;
1731 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1732 x == desktop->cursor.x && y == desktop->cursor.y)
1733 flags &= ~MOUSEEVENTF_MOVE;
1735 else
1737 x = desktop->cursor.x + input->mouse.x;
1738 y = desktop->cursor.y + input->mouse.y;
1741 else
1743 x = desktop->cursor.x;
1744 y = desktop->cursor.y;
1747 if ((foreground = get_foreground_thread( desktop, win )))
1749 raw_msg.foreground = foreground;
1750 raw_msg.desktop = desktop;
1751 raw_msg.source = source;
1752 raw_msg.time = time;
1754 msg_data = &raw_msg.data;
1755 msg_data->info = input->mouse.info;
1756 msg_data->flags = flags;
1757 msg_data->rawinput.type = RIM_TYPEMOUSE;
1758 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1759 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1760 msg_data->rawinput.mouse.data = input->mouse.data;
1762 enum_processes( queue_rawinput_message, &raw_msg );
1763 release_object( foreground );
1766 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
1768 update_desktop_mouse_state( desktop, flags, x, y, input->mouse.data << 16 );
1769 return 0;
1772 for (i = 0; i < ARRAY_SIZE( messages ); i++)
1774 if (!messages[i]) continue;
1775 if (!(flags & (1 << i))) continue;
1776 flags &= ~(1 << i);
1778 if (!(msg = alloc_hardware_message( input->mouse.info, source, time ))) return 0;
1779 msg_data = msg->data;
1781 msg->win = get_user_full_handle( win );
1782 msg->msg = messages[i];
1783 msg->wparam = input->mouse.data << 16;
1784 msg->lparam = 0;
1785 msg->x = x;
1786 msg->y = y;
1787 if (origin == IMO_INJECTED) msg_data->flags = LLMHF_INJECTED;
1789 /* specify a sender only when sending the last message */
1790 if (!(flags & ((1 << ARRAY_SIZE( messages )) - 1)))
1792 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1793 queue_hardware_message( desktop, msg, 0 );
1795 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1796 queue_hardware_message( desktop, msg, 0 );
1798 return wait;
1801 /* queue a hardware message for a keyboard event */
1802 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1803 unsigned int origin, struct msg_queue *sender )
1805 struct hw_msg_source source = { IMDT_KEYBOARD, origin };
1806 const struct rawinput_device *device;
1807 struct hardware_msg_data *msg_data;
1808 struct rawinput_message raw_msg;
1809 struct message *msg;
1810 struct thread *foreground;
1811 unsigned char vkey = input->kbd.vkey;
1812 unsigned int message_code, time;
1813 int wait;
1815 if (!(time = input->kbd.time)) time = get_tick_count();
1817 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1819 switch (vkey)
1821 case VK_MENU:
1822 case VK_LMENU:
1823 case VK_RMENU:
1824 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1825 break;
1826 case VK_CONTROL:
1827 case VK_LCONTROL:
1828 case VK_RCONTROL:
1829 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1830 break;
1831 case VK_SHIFT:
1832 case VK_LSHIFT:
1833 case VK_RSHIFT:
1834 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1835 break;
1839 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1840 switch (vkey)
1842 case VK_LMENU:
1843 case VK_RMENU:
1844 if (input->kbd.flags & KEYEVENTF_KEYUP)
1846 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1847 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1848 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1849 message_code = WM_SYSKEYUP;
1850 desktop->keystate[VK_MENU] &= ~0x02;
1852 else
1854 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1855 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1856 message_code = WM_SYSKEYDOWN;
1857 desktop->keystate[VK_MENU] |= 0x02;
1859 break;
1861 case VK_LCONTROL:
1862 case VK_RCONTROL:
1863 /* send WM_SYSKEYUP on release if Alt still pressed */
1864 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1865 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1866 message_code = WM_SYSKEYUP;
1867 desktop->keystate[VK_MENU] &= ~0x02;
1868 break;
1870 default:
1871 /* send WM_SYSKEY for Alt-anykey and for F10 */
1872 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1873 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1874 /* fall through */
1875 case VK_F10:
1876 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1877 desktop->keystate[VK_MENU] &= ~0x02;
1878 break;
1881 if ((foreground = get_foreground_thread( desktop, win )))
1883 raw_msg.foreground = foreground;
1884 raw_msg.desktop = desktop;
1885 raw_msg.source = source;
1886 raw_msg.time = time;
1888 msg_data = &raw_msg.data;
1889 msg_data->info = input->kbd.info;
1890 msg_data->flags = input->kbd.flags;
1891 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1892 msg_data->rawinput.kbd.message = message_code;
1893 msg_data->rawinput.kbd.vkey = vkey;
1894 msg_data->rawinput.kbd.scan = input->kbd.scan;
1896 enum_processes( queue_rawinput_message, &raw_msg );
1897 release_object( foreground );
1900 if ((device = current->process->rawinput_kbd) && (device->flags & RIDEV_NOLEGACY))
1902 update_input_key_state( desktop, desktop->keystate, message_code, vkey );
1903 return 0;
1906 if (!(msg = alloc_hardware_message( input->kbd.info, source, time ))) return 0;
1907 msg_data = msg->data;
1909 msg->win = get_user_full_handle( win );
1910 msg->msg = message_code;
1911 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1912 if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
1914 if (input->kbd.flags & KEYEVENTF_UNICODE && !vkey)
1916 msg->wparam = VK_PACKET;
1918 else
1920 unsigned int flags = 0;
1921 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1922 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1923 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1924 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1926 msg->wparam = vkey;
1927 msg->lparam |= flags << 16;
1928 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1931 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1932 queue_hardware_message( desktop, msg, 1 );
1934 return wait;
1937 /* queue a hardware message for a custom type of event */
1938 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1939 unsigned int origin, const hw_input_t *input )
1941 struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
1942 struct message *msg;
1944 if (!(msg = alloc_hardware_message( 0, source, get_tick_count() ))) return;
1946 msg->win = get_user_full_handle( win );
1947 msg->msg = input->hw.msg;
1948 msg->wparam = 0;
1949 msg->lparam = input->hw.lparam;
1950 msg->x = desktop->cursor.x;
1951 msg->y = desktop->cursor.y;
1953 queue_hardware_message( desktop, msg, 1 );
1956 /* check message filter for a hardware message */
1957 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1958 user_handle_t filter_win, unsigned int first, unsigned int last )
1960 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1962 /* we can only test the window for a keyboard message since the
1963 * dest window for a mouse message depends on hittest */
1964 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1965 return 0;
1966 /* the message code is final for a keyboard message, we can simply check it */
1967 return check_msg_filter( msg_code, first, last );
1969 else /* mouse message */
1971 /* we need to check all possible values that the message can have in the end */
1973 if (check_msg_filter( msg_code, first, last )) return 1;
1974 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1976 /* all other messages can become non-client messages */
1977 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1979 /* clicks can become double-clicks or non-client double-clicks */
1980 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1981 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1983 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1984 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1986 return 0;
1991 /* find a hardware message for the given queue */
1992 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1993 unsigned int first, unsigned int last, unsigned int flags,
1994 struct get_message_reply *reply )
1996 struct thread_input *input = thread->queue->input;
1997 struct thread *win_thread;
1998 struct list *ptr;
1999 user_handle_t win;
2000 int clear_bits, got_one = 0;
2001 unsigned int msg_code;
2003 ptr = list_head( &input->msg_list );
2004 if (hw_id)
2006 while (ptr)
2008 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2009 if (msg->unique_id == hw_id) break;
2010 ptr = list_next( &input->msg_list, ptr );
2012 if (!ptr) ptr = list_head( &input->msg_list );
2013 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
2016 if (ptr == list_head( &input->msg_list ))
2017 clear_bits = QS_INPUT;
2018 else
2019 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
2021 while (ptr)
2023 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2024 struct hardware_msg_data *data = msg->data;
2026 ptr = list_next( &input->msg_list, ptr );
2027 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
2028 if (!win || !win_thread)
2030 /* no window at all, remove it */
2031 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2032 list_remove( &msg->entry );
2033 free_message( msg );
2034 continue;
2036 if (win_thread != thread)
2038 if (win_thread->queue->input == input)
2040 /* wake the other thread */
2041 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
2042 got_one = 1;
2044 else
2046 /* for another thread input, drop it */
2047 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2048 list_remove( &msg->entry );
2049 free_message( msg );
2051 release_object( win_thread );
2052 continue;
2054 release_object( win_thread );
2056 /* if we already got a message for another thread, or if it doesn't
2057 * match the filter we skip it */
2058 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
2060 clear_bits &= ~get_hardware_msg_bit( msg );
2061 continue;
2063 /* now we can return it */
2064 if (!msg->unique_id) msg->unique_id = get_unique_id();
2065 reply->type = MSG_HARDWARE;
2066 reply->win = win;
2067 reply->msg = msg_code;
2068 reply->wparam = msg->wparam;
2069 reply->lparam = msg->lparam;
2070 reply->x = msg->x;
2071 reply->y = msg->y;
2072 reply->time = msg->time;
2074 data->hw_id = msg->unique_id;
2075 set_reply_data( msg->data, msg->data_size );
2076 if (msg->msg == WM_INPUT && (flags & PM_REMOVE))
2077 release_hardware_message( current->queue, data->hw_id );
2078 return 1;
2080 /* nothing found, clear the hardware queue bits */
2081 clear_queue_bits( thread->queue, clear_bits );
2082 return 0;
2085 /* increment (or decrement if 'incr' is negative) the queue paint count */
2086 void inc_queue_paint_count( struct thread *thread, int incr )
2088 struct msg_queue *queue = thread->queue;
2090 assert( queue );
2092 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2094 if (queue->paint_count)
2095 set_queue_bits( queue, QS_PAINT );
2096 else
2097 clear_queue_bits( queue, QS_PAINT );
2101 /* remove all messages and timers belonging to a certain window */
2102 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2104 struct msg_queue *queue = thread->queue;
2105 struct list *ptr;
2106 int i;
2108 if (!queue) return;
2110 /* remove timers */
2112 ptr = list_head( &queue->pending_timers );
2113 while (ptr)
2115 struct list *next = list_next( &queue->pending_timers, ptr );
2116 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2117 if (timer->win == win) free_timer( queue, timer );
2118 ptr = next;
2120 ptr = list_head( &queue->expired_timers );
2121 while (ptr)
2123 struct list *next = list_next( &queue->expired_timers, ptr );
2124 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2125 if (timer->win == win) free_timer( queue, timer );
2126 ptr = next;
2129 /* remove messages */
2130 for (i = 0; i < NB_MSG_KINDS; i++)
2132 struct list *ptr, *next;
2134 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2136 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2137 if (msg->win == win)
2139 if (msg->msg == WM_QUIT && !queue->quit_message)
2141 queue->quit_message = 1;
2142 queue->exit_code = msg->wparam;
2144 remove_queue_message( queue, msg, i );
2149 thread_input_cleanup_window( queue, win );
2152 /* post a message to a window */
2153 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2155 struct message *msg;
2156 struct thread *thread = get_window_thread( win );
2158 if (!thread) return;
2160 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2162 msg->type = MSG_POSTED;
2163 msg->win = get_user_full_handle( win );
2164 msg->msg = message;
2165 msg->wparam = wparam;
2166 msg->lparam = lparam;
2167 msg->result = NULL;
2168 msg->data = NULL;
2169 msg->data_size = 0;
2171 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2173 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2174 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2175 if (message == WM_HOTKEY)
2177 set_queue_bits( thread->queue, QS_HOTKEY );
2178 thread->queue->hotkey_count++;
2181 release_object( thread );
2184 /* send a notify message to a window */
2185 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2187 struct message *msg;
2188 struct thread *thread = get_window_thread( win );
2190 if (!thread) return;
2192 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2194 msg->type = MSG_NOTIFY;
2195 msg->win = get_user_full_handle( win );
2196 msg->msg = message;
2197 msg->wparam = wparam;
2198 msg->lparam = lparam;
2199 msg->result = NULL;
2200 msg->data = NULL;
2201 msg->data_size = 0;
2203 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2205 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2206 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2208 release_object( thread );
2211 /* post a win event */
2212 void post_win_event( struct thread *thread, unsigned int event,
2213 user_handle_t win, unsigned int object_id,
2214 unsigned int child_id, client_ptr_t hook_proc,
2215 const WCHAR *module, data_size_t module_size,
2216 user_handle_t hook)
2218 struct message *msg;
2220 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2222 struct winevent_msg_data *data;
2224 msg->type = MSG_WINEVENT;
2225 msg->win = get_user_full_handle( win );
2226 msg->msg = event;
2227 msg->wparam = object_id;
2228 msg->lparam = child_id;
2229 msg->time = get_tick_count();
2230 msg->result = NULL;
2232 if ((data = malloc( sizeof(*data) + module_size )))
2234 data->hook = hook;
2235 data->tid = get_thread_id( current );
2236 data->hook_proc = hook_proc;
2237 memcpy( data + 1, module, module_size );
2239 msg->data = data;
2240 msg->data_size = sizeof(*data) + module_size;
2242 if (debug_level > 1)
2243 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2244 get_thread_id(thread), event, win, object_id, child_id );
2245 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2246 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2248 else
2249 free( msg );
2253 /* free all hotkeys on a desktop, optionally filtering by window */
2254 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2256 struct hotkey *hotkey, *hotkey2;
2258 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2260 if (!window || hotkey->win == window)
2262 list_remove( &hotkey->entry );
2263 free( hotkey );
2269 /* check if the thread owning the window is hung */
2270 DECL_HANDLER(is_window_hung)
2272 struct thread *thread;
2274 thread = get_window_thread( req->win );
2275 if (thread)
2277 reply->is_hung = is_queue_hung( thread->queue );
2278 release_object( thread );
2280 else reply->is_hung = 0;
2284 /* get the message queue of the current thread */
2285 DECL_HANDLER(get_msg_queue)
2287 struct msg_queue *queue = get_current_queue();
2289 reply->handle = 0;
2290 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2294 /* set the file descriptor associated to the current thread queue */
2295 DECL_HANDLER(set_queue_fd)
2297 struct msg_queue *queue = get_current_queue();
2298 struct file *file;
2299 int unix_fd;
2301 if (queue->fd) /* fd can only be set once */
2303 set_error( STATUS_ACCESS_DENIED );
2304 return;
2306 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2308 if ((unix_fd = get_file_unix_fd( file )) != -1)
2310 if ((unix_fd = dup( unix_fd )) != -1)
2311 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2312 else
2313 file_set_error();
2315 release_object( file );
2319 /* set the current message queue wakeup mask */
2320 DECL_HANDLER(set_queue_mask)
2322 struct msg_queue *queue = get_current_queue();
2324 if (queue)
2326 queue->wake_mask = req->wake_mask;
2327 queue->changed_mask = req->changed_mask;
2328 reply->wake_bits = queue->wake_bits;
2329 reply->changed_bits = queue->changed_bits;
2330 if (is_signaled( queue ))
2332 /* if skip wait is set, do what would have been done in the subsequent wait */
2333 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2334 else wake_up( &queue->obj, 0 );
2340 /* get the current message queue status */
2341 DECL_HANDLER(get_queue_status)
2343 struct msg_queue *queue = current->queue;
2344 if (queue)
2346 reply->wake_bits = queue->wake_bits;
2347 reply->changed_bits = queue->changed_bits;
2348 queue->changed_bits &= ~req->clear_bits;
2350 else reply->wake_bits = reply->changed_bits = 0;
2354 /* send a message to a thread queue */
2355 DECL_HANDLER(send_message)
2357 struct message *msg;
2358 struct msg_queue *send_queue = get_current_queue();
2359 struct msg_queue *recv_queue = NULL;
2360 struct thread *thread = NULL;
2362 if (!(thread = get_thread_from_id( req->id ))) return;
2364 if (!(recv_queue = thread->queue))
2366 set_error( STATUS_INVALID_PARAMETER );
2367 release_object( thread );
2368 return;
2370 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2372 set_error( STATUS_TIMEOUT );
2373 release_object( thread );
2374 return;
2377 if ((msg = mem_alloc( sizeof(*msg) )))
2379 msg->type = req->type;
2380 msg->win = get_user_full_handle( req->win );
2381 msg->msg = req->msg;
2382 msg->wparam = req->wparam;
2383 msg->lparam = req->lparam;
2384 msg->result = NULL;
2385 msg->data = NULL;
2386 msg->data_size = get_req_data_size();
2388 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2390 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2392 free( msg );
2393 release_object( thread );
2394 return;
2397 switch(msg->type)
2399 case MSG_OTHER_PROCESS:
2400 case MSG_ASCII:
2401 case MSG_UNICODE:
2402 case MSG_CALLBACK:
2403 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2405 free_message( msg );
2406 break;
2408 /* fall through */
2409 case MSG_NOTIFY:
2410 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2411 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2412 break;
2413 case MSG_POSTED:
2414 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2415 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2416 if (msg->msg == WM_HOTKEY)
2418 set_queue_bits( recv_queue, QS_HOTKEY );
2419 recv_queue->hotkey_count++;
2421 break;
2422 case MSG_HARDWARE: /* should use send_hardware_message instead */
2423 case MSG_CALLBACK_RESULT: /* cannot send this one */
2424 case MSG_HOOK_LL: /* generated internally */
2425 default:
2426 set_error( STATUS_INVALID_PARAMETER );
2427 free( msg );
2428 break;
2431 release_object( thread );
2434 /* send a hardware message to a thread queue */
2435 DECL_HANDLER(send_hardware_message)
2437 struct thread *thread = NULL;
2438 struct desktop *desktop;
2439 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2440 struct msg_queue *sender = get_current_queue();
2441 data_size_t size = min( 256, get_reply_max_size() );
2443 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2445 if (req->win)
2447 if (!(thread = get_window_thread( req->win ))) return;
2448 if (desktop != thread->queue->input->desktop)
2450 /* don't allow queuing events to a different desktop */
2451 release_object( desktop );
2452 return;
2456 reply->prev_x = desktop->cursor.x;
2457 reply->prev_y = desktop->cursor.y;
2459 switch (req->input.type)
2461 case INPUT_MOUSE:
2462 reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2463 break;
2464 case INPUT_KEYBOARD:
2465 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2466 break;
2467 case INPUT_HARDWARE:
2468 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2469 break;
2470 default:
2471 set_error( STATUS_INVALID_PARAMETER );
2473 if (thread) release_object( thread );
2475 reply->new_x = desktop->cursor.x;
2476 reply->new_y = desktop->cursor.y;
2477 set_reply_data( desktop->keystate, size );
2478 release_object( desktop );
2481 /* post a quit message to the current queue */
2482 DECL_HANDLER(post_quit_message)
2484 struct msg_queue *queue = get_current_queue();
2486 if (!queue)
2487 return;
2489 queue->quit_message = 1;
2490 queue->exit_code = req->exit_code;
2491 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2494 /* get a message from the current queue */
2495 DECL_HANDLER(get_message)
2497 struct timer *timer;
2498 struct list *ptr;
2499 struct msg_queue *queue = get_current_queue();
2500 user_handle_t get_win = get_user_full_handle( req->get_win );
2501 unsigned int filter = req->flags >> 16;
2503 reply->active_hooks = get_active_hooks();
2505 if (get_win && get_win != 1 && get_win != -1 && !get_user_object( get_win, USER_WINDOW ))
2507 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2508 return;
2511 if (!queue) return;
2512 queue->last_get_msg = current_time;
2513 if (!filter) filter = QS_ALLINPUT;
2515 /* first check for sent messages */
2516 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2518 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2519 receive_message( queue, msg, reply );
2520 return;
2523 /* clear changed bits so we can wait on them if we don't find a message */
2524 if (filter & QS_POSTMESSAGE)
2526 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2527 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2529 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2530 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2532 /* then check for posted messages */
2533 if ((filter & QS_POSTMESSAGE) &&
2534 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2535 return;
2537 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2538 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2539 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2540 return;
2542 /* only check for quit messages if not posted messages pending */
2543 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2544 return;
2546 /* then check for any raw hardware message */
2547 if ((filter & QS_INPUT) &&
2548 filter_contains_hw_range( req->get_first, req->get_last ) &&
2549 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2550 return;
2552 /* now check for WM_PAINT */
2553 if ((filter & QS_PAINT) &&
2554 queue->paint_count &&
2555 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2556 (reply->win = find_window_to_repaint( get_win, current )))
2558 reply->type = MSG_POSTED;
2559 reply->msg = WM_PAINT;
2560 reply->wparam = 0;
2561 reply->lparam = 0;
2562 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2563 return;
2566 /* now check for timer */
2567 if ((filter & QS_TIMER) &&
2568 (timer = find_expired_timer( queue, get_win, req->get_first,
2569 req->get_last, (req->flags & PM_REMOVE) )))
2571 reply->type = MSG_POSTED;
2572 reply->win = timer->win;
2573 reply->msg = timer->msg;
2574 reply->wparam = timer->id;
2575 reply->lparam = timer->lparam;
2576 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2577 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2578 set_event( current->process->idle_event );
2579 return;
2582 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2583 queue->wake_mask = req->wake_mask;
2584 queue->changed_mask = req->changed_mask;
2585 set_error( STATUS_PENDING ); /* FIXME */
2589 /* reply to a sent message */
2590 DECL_HANDLER(reply_message)
2592 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2593 else if (current->queue->recv_result)
2594 reply_message( current->queue, req->result, 0, req->remove,
2595 get_req_data(), get_req_data_size() );
2599 /* accept the current hardware message */
2600 DECL_HANDLER(accept_hardware_message)
2602 if (current->queue)
2603 release_hardware_message( current->queue, req->hw_id );
2604 else
2605 set_error( STATUS_ACCESS_DENIED );
2609 /* retrieve the reply for the last message sent */
2610 DECL_HANDLER(get_message_reply)
2612 struct message_result *result;
2613 struct list *entry;
2614 struct msg_queue *queue = current->queue;
2616 if (queue)
2618 set_error( STATUS_PENDING );
2619 reply->result = 0;
2621 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2623 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2624 if (result->replied || req->cancel)
2626 if (result->replied)
2628 reply->result = result->result;
2629 set_error( result->error );
2630 if (result->data)
2632 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2633 set_reply_data_ptr( result->data, data_len );
2634 result->data = NULL;
2635 result->data_size = 0;
2638 remove_result_from_sender( result );
2640 entry = list_head( &queue->send_result );
2641 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2642 else
2644 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2645 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2646 else clear_queue_bits( queue, QS_SMRESULT );
2650 else set_error( STATUS_ACCESS_DENIED );
2654 /* set a window timer */
2655 DECL_HANDLER(set_win_timer)
2657 struct timer *timer;
2658 struct msg_queue *queue;
2659 struct thread *thread = NULL;
2660 user_handle_t win = 0;
2661 lparam_t id = req->id;
2663 if (req->win)
2665 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2667 set_error( STATUS_INVALID_HANDLE );
2668 return;
2670 if (thread->process != current->process)
2672 release_object( thread );
2673 set_error( STATUS_ACCESS_DENIED );
2674 return;
2676 queue = thread->queue;
2677 /* remove it if it existed already */
2678 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2680 else
2682 queue = get_current_queue();
2683 /* look for a timer with this id */
2684 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2686 /* free and reuse id */
2687 free_timer( queue, timer );
2689 else
2691 lparam_t end_id = queue->next_timer_id;
2693 /* find a free id for it */
2694 while (1)
2696 id = queue->next_timer_id;
2697 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2699 if (!find_timer( queue, 0, req->msg, id )) break;
2700 if (queue->next_timer_id == end_id)
2702 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2703 return;
2709 if ((timer = set_timer( queue, req->rate )))
2711 timer->win = win;
2712 timer->msg = req->msg;
2713 timer->id = id;
2714 timer->lparam = req->lparam;
2715 reply->id = id;
2717 if (thread) release_object( thread );
2720 /* kill a window timer */
2721 DECL_HANDLER(kill_win_timer)
2723 struct timer *timer;
2724 struct thread *thread;
2725 user_handle_t win = 0;
2727 if (req->win)
2729 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2731 set_error( STATUS_INVALID_HANDLE );
2732 return;
2734 if (thread->process != current->process)
2736 release_object( thread );
2737 set_error( STATUS_ACCESS_DENIED );
2738 return;
2741 else thread = (struct thread *)grab_object( current );
2743 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2744 free_timer( thread->queue, timer );
2745 else
2746 set_error( STATUS_INVALID_PARAMETER );
2748 release_object( thread );
2751 DECL_HANDLER(register_hotkey)
2753 struct desktop *desktop;
2754 user_handle_t win_handle = req->window;
2755 struct hotkey *hotkey;
2756 struct hotkey *new_hotkey = NULL;
2757 struct thread *thread;
2758 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2760 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2762 if (win_handle)
2764 if (!(win_handle = get_valid_window_handle( win_handle )))
2766 release_object( desktop );
2767 return;
2770 thread = get_window_thread( win_handle );
2771 if (thread) release_object( thread );
2773 if (thread != current)
2775 release_object( desktop );
2776 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2777 return;
2781 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2783 if (req->vkey == hotkey->vkey &&
2784 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2786 release_object( desktop );
2787 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2788 return;
2790 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2791 new_hotkey = hotkey;
2794 if (new_hotkey)
2796 reply->replaced = 1;
2797 reply->flags = new_hotkey->flags;
2798 reply->vkey = new_hotkey->vkey;
2800 else
2802 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2803 if (new_hotkey)
2805 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2806 new_hotkey->queue = current->queue;
2807 new_hotkey->win = win_handle;
2808 new_hotkey->id = req->id;
2812 if (new_hotkey)
2814 new_hotkey->flags = req->flags;
2815 new_hotkey->vkey = req->vkey;
2818 release_object( desktop );
2821 DECL_HANDLER(unregister_hotkey)
2823 struct desktop *desktop;
2824 user_handle_t win_handle = req->window;
2825 struct hotkey *hotkey;
2826 struct thread *thread;
2828 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2830 if (win_handle)
2832 if (!(win_handle = get_valid_window_handle( win_handle )))
2834 release_object( desktop );
2835 return;
2838 thread = get_window_thread( win_handle );
2839 if (thread) release_object( thread );
2841 if (thread != current)
2843 release_object( desktop );
2844 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2845 return;
2849 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2851 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2852 goto found;
2855 release_object( desktop );
2856 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2857 return;
2859 found:
2860 reply->flags = hotkey->flags;
2861 reply->vkey = hotkey->vkey;
2862 list_remove( &hotkey->entry );
2863 free( hotkey );
2864 release_object( desktop );
2867 /* attach (or detach) thread inputs */
2868 DECL_HANDLER(attach_thread_input)
2870 struct thread *thread_from = get_thread_from_id( req->tid_from );
2871 struct thread *thread_to = get_thread_from_id( req->tid_to );
2873 if (!thread_from || !thread_to)
2875 if (thread_from) release_object( thread_from );
2876 if (thread_to) release_object( thread_to );
2877 return;
2879 if (thread_from != thread_to)
2881 if (req->attach)
2883 if ((thread_to->queue || thread_to == current) &&
2884 (thread_from->queue || thread_from == current))
2885 attach_thread_input( thread_from, thread_to );
2886 else
2887 set_error( STATUS_INVALID_PARAMETER );
2889 else
2891 if (thread_from->queue && thread_to->queue &&
2892 thread_from->queue->input == thread_to->queue->input)
2893 detach_thread_input( thread_from );
2894 else
2895 set_error( STATUS_ACCESS_DENIED );
2898 else set_error( STATUS_ACCESS_DENIED );
2899 release_object( thread_from );
2900 release_object( thread_to );
2904 /* get thread input data */
2905 DECL_HANDLER(get_thread_input)
2907 struct thread *thread = NULL;
2908 struct desktop *desktop;
2909 struct thread_input *input;
2911 if (req->tid)
2913 if (!(thread = get_thread_from_id( req->tid ))) return;
2914 if (!(desktop = get_thread_desktop( thread, 0 )))
2916 release_object( thread );
2917 return;
2919 input = thread->queue ? thread->queue->input : NULL;
2921 else
2923 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2924 input = desktop->foreground_input; /* get the foreground thread info */
2927 if (input)
2929 reply->focus = input->focus;
2930 reply->capture = input->capture;
2931 reply->active = input->active;
2932 reply->menu_owner = input->menu_owner;
2933 reply->move_size = input->move_size;
2934 reply->caret = input->caret;
2935 reply->cursor = input->cursor;
2936 reply->show_count = input->cursor_count;
2937 reply->rect = input->caret_rect;
2940 /* foreground window is active window of foreground thread */
2941 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2942 if (thread) release_object( thread );
2943 release_object( desktop );
2947 /* retrieve queue keyboard state for a given thread */
2948 DECL_HANDLER(get_key_state)
2950 struct thread *thread;
2951 struct desktop *desktop;
2952 data_size_t size = min( 256, get_reply_max_size() );
2954 if (!req->tid) /* get global async key state */
2956 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2957 if (req->key >= 0)
2959 reply->state = desktop->keystate[req->key & 0xff];
2960 desktop->keystate[req->key & 0xff] &= ~0x40;
2962 set_reply_data( desktop->keystate, size );
2963 release_object( desktop );
2965 else
2967 unsigned char *keystate;
2968 if (!(thread = get_thread_from_id( req->tid ))) return;
2969 if (thread->queue)
2971 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2972 set_reply_data( thread->queue->input->keystate, size );
2973 release_object( thread );
2974 return;
2976 release_object( thread );
2978 /* fallback to desktop keystate */
2979 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2980 if (req->key >= 0) reply->state = desktop->keystate[req->key & 0xff] & ~0x40;
2981 if ((keystate = set_reply_data_size( size )))
2983 unsigned int i;
2984 for (i = 0; i < size; i++) keystate[i] = desktop->keystate[i] & ~0x40;
2986 release_object( desktop );
2991 /* set queue keyboard state for a given thread */
2992 DECL_HANDLER(set_key_state)
2994 struct thread *thread;
2995 struct desktop *desktop;
2996 data_size_t size = min( 256, get_req_data_size() );
2998 if (!req->tid) /* set global async key state */
3000 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3001 memcpy( desktop->keystate, get_req_data(), size );
3002 release_object( desktop );
3004 else
3006 if (!(thread = get_thread_from_id( req->tid ))) return;
3007 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
3008 if (req->async && (desktop = get_thread_desktop( thread, 0 )))
3010 memcpy( desktop->keystate, get_req_data(), size );
3011 release_object( desktop );
3013 release_object( thread );
3018 /* set the system foreground window */
3019 DECL_HANDLER(set_foreground_window)
3021 struct thread *thread = NULL;
3022 struct desktop *desktop;
3023 struct msg_queue *queue = get_current_queue();
3025 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3026 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
3027 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
3028 reply->send_msg_new = FALSE;
3030 if (is_valid_foreground_window( req->handle ) &&
3031 (thread = get_window_thread( req->handle )) &&
3032 thread->queue->input->desktop == desktop)
3034 set_foreground_input( desktop, thread->queue->input );
3035 reply->send_msg_new = (desktop->foreground_input != queue->input);
3037 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
3039 if (thread) release_object( thread );
3040 release_object( desktop );
3044 /* set the current thread focus window */
3045 DECL_HANDLER(set_focus_window)
3047 struct msg_queue *queue = get_current_queue();
3049 reply->previous = 0;
3050 if (queue && check_queue_input_window( queue, req->handle ))
3052 reply->previous = queue->input->focus;
3053 queue->input->focus = get_user_full_handle( req->handle );
3058 /* set the current thread active window */
3059 DECL_HANDLER(set_active_window)
3061 struct msg_queue *queue = get_current_queue();
3063 reply->previous = 0;
3064 if (queue && check_queue_input_window( queue, req->handle ))
3066 if (!req->handle || make_window_active( req->handle ))
3068 reply->previous = queue->input->active;
3069 queue->input->active = get_user_full_handle( req->handle );
3071 else set_error( STATUS_INVALID_HANDLE );
3076 /* set the current thread capture window */
3077 DECL_HANDLER(set_capture_window)
3079 struct msg_queue *queue = get_current_queue();
3081 reply->previous = reply->full_handle = 0;
3082 if (queue && check_queue_input_window( queue, req->handle ))
3084 struct thread_input *input = queue->input;
3086 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
3087 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
3089 set_error(STATUS_ACCESS_DENIED);
3090 return;
3092 reply->previous = input->capture;
3093 input->capture = get_user_full_handle( req->handle );
3094 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
3095 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
3096 reply->full_handle = input->capture;
3101 /* Set the current thread caret window */
3102 DECL_HANDLER(set_caret_window)
3104 struct msg_queue *queue = get_current_queue();
3106 reply->previous = 0;
3107 if (queue && check_queue_input_window( queue, req->handle ))
3109 struct thread_input *input = queue->input;
3111 reply->previous = input->caret;
3112 reply->old_rect = input->caret_rect;
3113 reply->old_hide = input->caret_hide;
3114 reply->old_state = input->caret_state;
3116 set_caret_window( input, get_user_full_handle(req->handle) );
3117 input->caret_rect.right = input->caret_rect.left + req->width;
3118 input->caret_rect.bottom = input->caret_rect.top + req->height;
3123 /* Set the current thread caret information */
3124 DECL_HANDLER(set_caret_info)
3126 struct msg_queue *queue = get_current_queue();
3127 struct thread_input *input;
3129 if (!queue) return;
3130 input = queue->input;
3131 reply->full_handle = input->caret;
3132 reply->old_rect = input->caret_rect;
3133 reply->old_hide = input->caret_hide;
3134 reply->old_state = input->caret_state;
3136 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3138 set_error( STATUS_ACCESS_DENIED );
3139 return;
3141 if (req->flags & SET_CARET_POS)
3143 input->caret_rect.right += req->x - input->caret_rect.left;
3144 input->caret_rect.bottom += req->y - input->caret_rect.top;
3145 input->caret_rect.left = req->x;
3146 input->caret_rect.top = req->y;
3148 if (req->flags & SET_CARET_HIDE)
3150 input->caret_hide += req->hide;
3151 if (input->caret_hide < 0) input->caret_hide = 0;
3153 if (req->flags & SET_CARET_STATE)
3155 switch (req->state)
3157 case CARET_STATE_OFF: input->caret_state = 0; break;
3158 case CARET_STATE_ON: input->caret_state = 1; break;
3159 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3160 case CARET_STATE_ON_IF_MOVED:
3161 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3162 break;
3168 /* get the time of the last input event */
3169 DECL_HANDLER(get_last_input_time)
3171 reply->time = last_input_time;
3174 /* set/get the current cursor */
3175 DECL_HANDLER(set_cursor)
3177 struct msg_queue *queue = get_current_queue();
3178 struct thread_input *input;
3180 if (!queue) return;
3181 input = queue->input;
3183 reply->prev_handle = input->cursor;
3184 reply->prev_count = input->cursor_count;
3185 reply->prev_x = input->desktop->cursor.x;
3186 reply->prev_y = input->desktop->cursor.y;
3188 if (req->flags & SET_CURSOR_HANDLE)
3190 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3192 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3193 return;
3195 input->cursor = req->handle;
3197 if (req->flags & SET_CURSOR_COUNT)
3199 queue->cursor_count += req->show_count;
3200 input->cursor_count += req->show_count;
3202 if (req->flags & SET_CURSOR_POS)
3204 set_cursor_pos( input->desktop, req->x, req->y );
3206 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3208 struct desktop *desktop = input->desktop;
3210 /* only the desktop owner can set the message */
3211 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3212 desktop->cursor.clip_msg = req->clip_msg;
3214 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip, 0 );
3217 reply->new_x = input->desktop->cursor.x;
3218 reply->new_y = input->desktop->cursor.y;
3219 reply->new_clip = input->desktop->cursor.clip;
3220 reply->last_change = input->desktop->cursor.last_change;
3223 DECL_HANDLER(get_rawinput_buffer)
3225 struct thread_input *input = current->queue->input;
3226 data_size_t size = 0, next_size = 0;
3227 struct list *ptr;
3228 char *buf, *cur;
3229 int count = 0;
3231 if (!req->buffer_size) buf = NULL;
3232 else if (!(buf = mem_alloc( get_reply_max_size() )))
3233 return;
3235 cur = buf;
3236 ptr = list_head( &input->msg_list );
3237 while (ptr)
3239 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
3240 struct hardware_msg_data *data = msg->data;
3242 ptr = list_next( &input->msg_list, ptr );
3243 if (msg->msg != WM_INPUT) continue;
3245 next_size = req->rawinput_size;
3246 if (size + next_size > req->buffer_size) break;
3247 if (cur + sizeof(*data) > buf + get_reply_max_size()) break;
3249 memcpy(cur, data, sizeof(*data));
3250 list_remove( &msg->entry );
3251 free_message( msg );
3253 size += next_size;
3254 cur += sizeof(*data);
3255 count++;
3258 reply->next_size = next_size;
3259 reply->count = count;
3260 set_reply_data_ptr( buf, cur - buf );
3263 DECL_HANDLER(update_rawinput_devices)
3265 const struct rawinput_device *devices = get_req_data();
3266 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3267 const struct rawinput_device_entry *e;
3268 unsigned int i;
3270 for (i = 0; i < device_count; ++i)
3272 update_rawinput_device(&devices[i]);
3275 e = find_rawinput_device( 1, 2 );
3276 current->process->rawinput_mouse = e ? &e->device : NULL;
3277 e = find_rawinput_device( 1, 6 );
3278 current->process->rawinput_kbd = e ? &e->device : NULL;
3281 DECL_HANDLER(get_rawinput_devices)
3283 struct rawinput_device_entry *e;
3284 struct rawinput_device *devices;
3285 unsigned int i = 0, device_count = list_count( &current->process->rawinput_devices );
3286 unsigned int size = device_count * sizeof(*devices);
3288 reply->device_count = device_count;
3290 /* no buffer provided, nothing else to do */
3291 if (!get_reply_max_size()) return;
3293 if (size > get_reply_max_size())
3294 set_error( STATUS_BUFFER_TOO_SMALL );
3295 else if ((devices = set_reply_data_size( size )))
3297 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
3298 devices[i++] = e->device;