msvcrt: Import modff implementation from musl.
[wine.git] / server / queue.c
blob5d65e030112e8e450ca0d3421d960a779d9385e3
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 &no_type, /* type */
170 msg_queue_dump, /* dump */
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 default_map_access, /* map_access */
178 default_get_sd, /* get_sd */
179 default_set_sd, /* set_sd */
180 no_get_full_name, /* get_full_name */
181 no_lookup_name, /* lookup_name */
182 no_link_name, /* link_name */
183 NULL, /* unlink_name */
184 no_open_file, /* open_file */
185 no_kernel_obj_list, /* get_kernel_obj_list */
186 no_close_handle, /* close_handle */
187 msg_queue_destroy /* destroy */
190 static const struct fd_ops msg_queue_fd_ops =
192 NULL, /* get_poll_events */
193 msg_queue_poll_event, /* poll_event */
194 NULL, /* flush */
195 NULL, /* get_fd_type */
196 NULL, /* ioctl */
197 NULL, /* queue_async */
198 NULL, /* reselect_async */
199 NULL /* cancel async */
203 static const struct object_ops thread_input_ops =
205 sizeof(struct thread_input), /* size */
206 &no_type, /* type */
207 thread_input_dump, /* dump */
208 no_add_queue, /* add_queue */
209 NULL, /* remove_queue */
210 NULL, /* signaled */
211 NULL, /* satisfied */
212 no_signal, /* signal */
213 no_get_fd, /* get_fd */
214 default_map_access, /* map_access */
215 default_get_sd, /* get_sd */
216 default_set_sd, /* set_sd */
217 no_get_full_name, /* get_full_name */
218 no_lookup_name, /* lookup_name */
219 no_link_name, /* link_name */
220 NULL, /* unlink_name */
221 no_open_file, /* open_file */
222 no_kernel_obj_list, /* get_kernel_obj_list */
223 no_close_handle, /* close_handle */
224 thread_input_destroy /* destroy */
227 /* pointer to input structure of foreground thread */
228 static unsigned int last_input_time;
230 static cursor_pos_t cursor_history[64];
231 static unsigned int cursor_history_latest;
233 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
234 static void free_message( struct message *msg );
236 /* set the caret window in a given thread input */
237 static void set_caret_window( struct thread_input *input, user_handle_t win )
239 if (!win || win != input->caret)
241 input->caret_rect.left = 0;
242 input->caret_rect.top = 0;
243 input->caret_rect.right = 0;
244 input->caret_rect.bottom = 0;
246 input->caret = win;
247 input->caret_hide = 1;
248 input->caret_state = 0;
251 /* create a thread input object */
252 static struct thread_input *create_thread_input( struct thread *thread )
254 struct thread_input *input;
256 if ((input = alloc_object( &thread_input_ops )))
258 input->focus = 0;
259 input->capture = 0;
260 input->active = 0;
261 input->menu_owner = 0;
262 input->move_size = 0;
263 input->cursor = 0;
264 input->cursor_count = 0;
265 list_init( &input->msg_list );
266 set_caret_window( input, 0 );
267 memset( input->keystate, 0, sizeof(input->keystate) );
269 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
271 release_object( input );
272 return NULL;
275 return input;
278 /* create a message queue object */
279 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
281 struct thread_input *new_input = NULL;
282 struct msg_queue *queue;
283 int i;
285 if (!input)
287 if (!(new_input = create_thread_input( thread ))) return NULL;
288 input = new_input;
291 if ((queue = alloc_object( &msg_queue_ops )))
293 queue->fd = NULL;
294 queue->wake_bits = 0;
295 queue->wake_mask = 0;
296 queue->changed_bits = 0;
297 queue->changed_mask = 0;
298 queue->paint_count = 0;
299 queue->hotkey_count = 0;
300 queue->quit_message = 0;
301 queue->cursor_count = 0;
302 queue->recv_result = NULL;
303 queue->next_timer_id = 0x7fff;
304 queue->timeout = NULL;
305 queue->input = (struct thread_input *)grab_object( input );
306 queue->hooks = NULL;
307 queue->last_get_msg = current_time;
308 list_init( &queue->send_result );
309 list_init( &queue->callback_result );
310 list_init( &queue->pending_timers );
311 list_init( &queue->expired_timers );
312 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
314 thread->queue = queue;
316 if (new_input) release_object( new_input );
317 return queue;
320 /* free the message queue of a thread at thread exit */
321 void free_msg_queue( struct thread *thread )
323 remove_thread_hooks( thread );
324 if (!thread->queue) return;
325 release_object( thread->queue );
326 thread->queue = NULL;
329 /* change the thread input data of a given thread */
330 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
332 struct msg_queue *queue = thread->queue;
334 if (!queue)
336 thread->queue = create_msg_queue( thread, new_input );
337 return thread->queue != NULL;
339 if (queue->input)
341 queue->input->cursor_count -= queue->cursor_count;
342 release_object( queue->input );
344 queue->input = (struct thread_input *)grab_object( new_input );
345 new_input->cursor_count += queue->cursor_count;
346 return 1;
349 /* allocate a hardware message and its data */
350 static struct message *alloc_hardware_message( lparam_t info, struct hw_msg_source source,
351 unsigned int time )
353 struct hardware_msg_data *msg_data;
354 struct message *msg;
356 if (!(msg = mem_alloc( sizeof(*msg) ))) return NULL;
357 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
359 free( msg );
360 return NULL;
362 memset( msg, 0, sizeof(*msg) );
363 msg->type = MSG_HARDWARE;
364 msg->time = time;
365 msg->data = msg_data;
366 msg->data_size = sizeof(*msg_data);
368 memset( msg_data, 0, sizeof(*msg_data) );
369 msg_data->info = info;
370 msg_data->source = source;
371 return msg;
374 static int update_desktop_cursor_pos( struct desktop *desktop, int x, int y )
376 int updated;
378 x = max( min( x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
379 y = max( min( y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
380 updated = (desktop->cursor.x != x || desktop->cursor.y != y);
381 desktop->cursor.x = x;
382 desktop->cursor.y = y;
383 desktop->cursor.last_change = get_tick_count();
385 return updated;
388 /* set the cursor position and queue the corresponding mouse message */
389 static void set_cursor_pos( struct desktop *desktop, int x, int y )
391 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
392 const struct rawinput_device *device;
393 struct message *msg;
395 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
397 update_desktop_cursor_pos( desktop, x, y );
398 return;
401 if (!(msg = alloc_hardware_message( 0, source, get_tick_count() ))) return;
403 msg->msg = WM_MOUSEMOVE;
404 msg->x = x;
405 msg->y = y;
406 queue_hardware_message( desktop, msg, 1 );
409 /* retrieve default position and time for synthesized messages */
410 static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
412 struct desktop *desktop = queue->input->desktop;
414 *x = desktop->cursor.x;
415 *y = desktop->cursor.y;
416 *time = get_tick_count();
419 /* set the cursor clip rectangle */
420 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect, int send_clip_msg )
422 rectangle_t top_rect;
423 int x, y;
425 get_top_window_rectangle( desktop, &top_rect );
426 if (rect)
428 rectangle_t new_rect = *rect;
429 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
430 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
431 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
432 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
433 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
434 desktop->cursor.clip = new_rect;
436 else desktop->cursor.clip = top_rect;
438 if (desktop->cursor.clip_msg && send_clip_msg)
439 post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
441 /* warp the mouse to be inside the clip rect */
442 x = max( min( desktop->cursor.x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
443 y = max( min( desktop->cursor.y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
444 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
447 /* change the foreground input and reset the cursor clip rect */
448 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
450 if (desktop->foreground_input == input) return;
451 set_clip_rectangle( desktop, NULL, 1 );
452 desktop->foreground_input = input;
455 /* get the hook table for a given thread */
456 struct hook_table *get_queue_hooks( struct thread *thread )
458 if (!thread->queue) return NULL;
459 return thread->queue->hooks;
462 /* set the hook table for a given thread, allocating the queue if needed */
463 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
465 struct msg_queue *queue = thread->queue;
466 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
467 if (queue->hooks) release_object( queue->hooks );
468 queue->hooks = hooks;
471 /* check the queue status */
472 static inline int is_signaled( struct msg_queue *queue )
474 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
477 /* set some queue bits */
478 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
480 queue->wake_bits |= bits;
481 queue->changed_bits |= bits;
482 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
485 /* clear some queue bits */
486 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
488 queue->wake_bits &= ~bits;
489 queue->changed_bits &= ~bits;
492 /* check whether msg is a keyboard message */
493 static inline int is_keyboard_msg( struct message *msg )
495 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
498 /* check if message is matched by the filter */
499 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
501 return (msg >= first && msg <= last);
504 /* check whether a message filter contains at least one potential hardware message */
505 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
507 /* hardware message ranges are (in numerical order):
508 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
509 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
510 * WM_MOUSEFIRST .. WM_MOUSELAST
512 if (last < WM_NCMOUSEFIRST) return 0;
513 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
514 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
515 if (first > WM_MOUSELAST) return 0;
516 return 1;
519 /* get the QS_* bit corresponding to a given hardware message */
520 static inline int get_hardware_msg_bit( struct message *msg )
522 if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
523 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
524 if (is_keyboard_msg( msg )) return QS_KEY;
525 return QS_MOUSEBUTTON;
528 /* get the current thread queue, creating it if needed */
529 static inline struct msg_queue *get_current_queue(void)
531 struct msg_queue *queue = current->queue;
532 if (!queue) queue = create_msg_queue( current, NULL );
533 return queue;
536 /* get a (pseudo-)unique id to tag hardware messages */
537 static inline unsigned int get_unique_id(void)
539 static unsigned int id;
540 if (!++id) id = 1; /* avoid an id of 0 */
541 return id;
544 /* try to merge a message with the last in the list; return 1 if successful */
545 static int merge_message( struct thread_input *input, const struct message *msg )
547 struct message *prev;
548 struct list *ptr;
550 if (msg->msg != WM_MOUSEMOVE) return 0;
551 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
553 prev = LIST_ENTRY( ptr, struct message, entry );
554 if (prev->msg != WM_INPUT) break;
556 if (!ptr) return 0;
557 if (prev->result) return 0;
558 if (prev->win && msg->win && prev->win != msg->win) return 0;
559 if (prev->msg != msg->msg) return 0;
560 if (prev->type != msg->type) return 0;
561 /* now we can merge it */
562 prev->wparam = msg->wparam;
563 prev->lparam = msg->lparam;
564 prev->x = msg->x;
565 prev->y = msg->y;
566 prev->time = msg->time;
567 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
569 struct hardware_msg_data *prev_data = prev->data;
570 struct hardware_msg_data *msg_data = msg->data;
571 prev_data->info = msg_data->info;
573 list_remove( ptr );
574 list_add_tail( &input->msg_list, ptr );
575 return 1;
578 /* free a result structure */
579 static void free_result( struct message_result *result )
581 if (result->timeout) remove_timeout_user( result->timeout );
582 free( result->data );
583 if (result->callback_msg) free_message( result->callback_msg );
584 if (result->hardware_msg) free_message( result->hardware_msg );
585 if (result->desktop) release_object( result->desktop );
586 free( result );
589 /* remove the result from the sender list it is on */
590 static inline void remove_result_from_sender( struct message_result *result )
592 assert( result->sender );
594 list_remove( &result->sender_entry );
595 result->sender = NULL;
596 if (!result->receiver) free_result( result );
599 /* store the message result in the appropriate structure */
600 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
602 res->result = result;
603 res->error = error;
604 res->replied = 1;
605 if (res->timeout)
607 remove_timeout_user( res->timeout );
608 res->timeout = NULL;
611 if (res->hardware_msg)
613 if (!error && result) /* rejected by the hook */
614 free_message( res->hardware_msg );
615 else
616 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
618 res->hardware_msg = NULL;
621 if (res->sender)
623 if (res->callback_msg)
625 /* queue the callback message in the sender queue */
626 struct callback_msg_data *data = res->callback_msg->data;
627 data->result = result;
628 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
629 set_queue_bits( res->sender, QS_SENDMESSAGE );
630 res->callback_msg = NULL;
631 remove_result_from_sender( res );
633 else
635 /* wake sender queue if waiting on this result */
636 if (list_head(&res->sender->send_result) == &res->sender_entry)
637 set_queue_bits( res->sender, QS_SMRESULT );
640 else if (!res->receiver) free_result( res );
643 /* free a message when deleting a queue or window */
644 static void free_message( struct message *msg )
646 struct message_result *result = msg->result;
647 if (result)
649 result->msg = NULL;
650 result->receiver = NULL;
651 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
653 free( msg->data );
654 free( msg );
657 /* remove (and free) a message from a message list */
658 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
659 enum message_kind kind )
661 list_remove( &msg->entry );
662 switch(kind)
664 case SEND_MESSAGE:
665 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
666 break;
667 case POST_MESSAGE:
668 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
669 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
670 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
671 clear_queue_bits( queue, QS_HOTKEY );
672 break;
674 free_message( msg );
677 /* message timed out without getting a reply */
678 static void result_timeout( void *private )
680 struct message_result *result = private;
682 assert( !result->replied );
684 result->timeout = NULL;
686 if (result->msg) /* not received yet */
688 struct message *msg = result->msg;
690 result->msg = NULL;
691 msg->result = NULL;
692 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
693 result->receiver = NULL;
695 store_message_result( result, 0, STATUS_TIMEOUT );
698 /* allocate and fill a message result structure */
699 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
700 struct msg_queue *recv_queue,
701 struct message *msg, timeout_t timeout )
703 struct message_result *result = mem_alloc( sizeof(*result) );
704 if (result)
706 result->msg = msg;
707 result->sender = send_queue;
708 result->receiver = recv_queue;
709 result->replied = 0;
710 result->data = NULL;
711 result->data_size = 0;
712 result->timeout = NULL;
713 result->hardware_msg = NULL;
714 result->desktop = NULL;
715 result->callback_msg = NULL;
717 if (msg->type == MSG_CALLBACK)
719 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
721 if (!callback_msg)
723 free( result );
724 return NULL;
726 callback_msg->type = MSG_CALLBACK_RESULT;
727 callback_msg->win = msg->win;
728 callback_msg->msg = msg->msg;
729 callback_msg->wparam = 0;
730 callback_msg->lparam = 0;
731 callback_msg->time = get_tick_count();
732 callback_msg->result = NULL;
733 /* steal the data from the original message */
734 callback_msg->data = msg->data;
735 callback_msg->data_size = msg->data_size;
736 msg->data = NULL;
737 msg->data_size = 0;
739 result->callback_msg = callback_msg;
740 list_add_head( &send_queue->callback_result, &result->sender_entry );
742 else if (send_queue)
744 list_add_head( &send_queue->send_result, &result->sender_entry );
745 clear_queue_bits( send_queue, QS_SMRESULT );
748 if (timeout != TIMEOUT_INFINITE)
749 result->timeout = add_timeout_user( timeout, result_timeout, result );
751 return result;
754 /* receive a message, removing it from the sent queue */
755 static void receive_message( struct msg_queue *queue, struct message *msg,
756 struct get_message_reply *reply )
758 struct message_result *result = msg->result;
760 reply->total = msg->data_size;
761 if (msg->data_size > get_reply_max_size())
763 set_error( STATUS_BUFFER_OVERFLOW );
764 return;
766 reply->type = msg->type;
767 reply->win = msg->win;
768 reply->msg = msg->msg;
769 reply->wparam = msg->wparam;
770 reply->lparam = msg->lparam;
771 reply->x = msg->x;
772 reply->y = msg->y;
773 reply->time = msg->time;
775 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
777 list_remove( &msg->entry );
778 /* put the result on the receiver result stack */
779 if (result)
781 result->msg = NULL;
782 result->recv_next = queue->recv_result;
783 queue->recv_result = result;
785 free( msg );
786 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
789 /* set the result of the current received message */
790 static void reply_message( struct msg_queue *queue, lparam_t result,
791 unsigned int error, int remove, const void *data, data_size_t len )
793 struct message_result *res = queue->recv_result;
795 if (remove)
797 queue->recv_result = res->recv_next;
798 res->receiver = NULL;
799 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
801 free_result( res );
802 return;
805 if (!res->replied)
807 if (len && (res->data = memdup( data, len ))) res->data_size = len;
808 store_message_result( res, result, error );
812 static int match_window( user_handle_t win, user_handle_t msg_win )
814 if (!win) return 1;
815 if (win == -1 || win == 1) return !msg_win;
816 if (msg_win == win) return 1;
817 return is_child_window( win, msg_win );
820 /* retrieve a posted message */
821 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
822 unsigned int first, unsigned int last, unsigned int flags,
823 struct get_message_reply *reply )
825 struct message *msg;
827 /* check against the filters */
828 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
830 if (!match_window( win, msg->win )) continue;
831 if (!check_msg_filter( msg->msg, first, last )) continue;
832 goto found; /* found one */
834 return 0;
836 /* return it to the app */
837 found:
838 reply->total = msg->data_size;
839 if (msg->data_size > get_reply_max_size())
841 set_error( STATUS_BUFFER_OVERFLOW );
842 return 1;
844 reply->type = msg->type;
845 reply->win = msg->win;
846 reply->msg = msg->msg;
847 reply->wparam = msg->wparam;
848 reply->lparam = msg->lparam;
849 reply->x = msg->x;
850 reply->y = msg->y;
851 reply->time = msg->time;
853 if (flags & PM_REMOVE)
855 if (msg->data)
857 set_reply_data_ptr( msg->data, msg->data_size );
858 msg->data = NULL;
859 msg->data_size = 0;
861 remove_queue_message( queue, msg, POST_MESSAGE );
863 else if (msg->data) set_reply_data( msg->data, msg->data_size );
865 return 1;
868 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
869 struct get_message_reply *reply )
871 if (queue->quit_message)
873 reply->total = 0;
874 reply->type = MSG_POSTED;
875 reply->win = 0;
876 reply->msg = WM_QUIT;
877 reply->wparam = queue->exit_code;
878 reply->lparam = 0;
880 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
882 if (flags & PM_REMOVE)
884 queue->quit_message = 0;
885 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
886 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
888 return 1;
890 else
891 return 0;
894 /* empty a message list and free all the messages */
895 static void empty_msg_list( struct list *list )
897 struct list *ptr;
899 while ((ptr = list_head( list )) != NULL)
901 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
902 list_remove( &msg->entry );
903 free_message( msg );
907 /* cleanup all pending results when deleting a queue */
908 static void cleanup_results( struct msg_queue *queue )
910 struct list *entry;
912 while ((entry = list_head( &queue->send_result )) != NULL)
914 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
917 while ((entry = list_head( &queue->callback_result )) != NULL)
919 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
922 while (queue->recv_result)
923 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
926 /* check if the thread owning the queue is hung (not checking for messages) */
927 static int is_queue_hung( struct msg_queue *queue )
929 struct wait_queue_entry *entry;
931 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
932 return 0; /* less than 5 seconds since last get message -> not hung */
934 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
936 if (get_wait_queue_thread(entry)->queue == queue)
937 return 0; /* thread is waiting on queue -> not hung */
939 return 1;
942 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
944 struct msg_queue *queue = (struct msg_queue *)obj;
945 struct process *process = get_wait_queue_thread(entry)->process;
947 /* a thread can only wait on its own queue */
948 if (get_wait_queue_thread(entry)->queue != queue)
950 set_error( STATUS_ACCESS_DENIED );
951 return 0;
953 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
955 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
956 set_fd_events( queue->fd, POLLIN );
957 add_queue( obj, entry );
958 return 1;
961 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
963 struct msg_queue *queue = (struct msg_queue *)obj;
965 remove_queue( obj, entry );
966 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
967 set_fd_events( queue->fd, 0 );
970 static void msg_queue_dump( struct object *obj, int verbose )
972 struct msg_queue *queue = (struct msg_queue *)obj;
973 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
974 queue->wake_bits, queue->wake_mask );
977 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
979 struct msg_queue *queue = (struct msg_queue *)obj;
980 int ret = 0;
982 if (queue->fd)
984 if ((ret = check_fd_events( queue->fd, POLLIN )))
985 /* stop waiting on select() if we are signaled */
986 set_fd_events( queue->fd, 0 );
987 else if (!list_empty( &obj->wait_queue ))
988 /* restart waiting on poll() if we are no longer signaled */
989 set_fd_events( queue->fd, POLLIN );
991 return ret || is_signaled( queue );
994 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
996 struct msg_queue *queue = (struct msg_queue *)obj;
997 queue->wake_mask = 0;
998 queue->changed_mask = 0;
1001 static void msg_queue_destroy( struct object *obj )
1003 struct msg_queue *queue = (struct msg_queue *)obj;
1004 struct list *ptr;
1005 struct hotkey *hotkey, *hotkey2;
1006 int i;
1008 cleanup_results( queue );
1009 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
1011 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
1013 if (hotkey->queue == queue)
1015 list_remove( &hotkey->entry );
1016 free( hotkey );
1020 while ((ptr = list_head( &queue->pending_timers )))
1022 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1023 list_remove( &timer->entry );
1024 free( timer );
1026 while ((ptr = list_head( &queue->expired_timers )))
1028 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1029 list_remove( &timer->entry );
1030 free( timer );
1032 if (queue->timeout) remove_timeout_user( queue->timeout );
1033 queue->input->cursor_count -= queue->cursor_count;
1034 release_object( queue->input );
1035 if (queue->hooks) release_object( queue->hooks );
1036 if (queue->fd) release_object( queue->fd );
1039 static void msg_queue_poll_event( struct fd *fd, int event )
1041 struct msg_queue *queue = get_fd_user( fd );
1042 assert( queue->obj.ops == &msg_queue_ops );
1044 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1045 else set_fd_events( queue->fd, 0 );
1046 wake_up( &queue->obj, 0 );
1049 static void thread_input_dump( struct object *obj, int verbose )
1051 struct thread_input *input = (struct thread_input *)obj;
1052 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
1053 input->focus, input->capture, input->active );
1056 static void thread_input_destroy( struct object *obj )
1058 struct thread_input *input = (struct thread_input *)obj;
1060 empty_msg_list( &input->msg_list );
1061 if (input->desktop)
1063 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
1064 release_object( input->desktop );
1068 /* fix the thread input data when a window is destroyed */
1069 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1071 struct thread_input *input = queue->input;
1073 if (window == input->focus) input->focus = 0;
1074 if (window == input->capture) input->capture = 0;
1075 if (window == input->active) input->active = 0;
1076 if (window == input->menu_owner) input->menu_owner = 0;
1077 if (window == input->move_size) input->move_size = 0;
1078 if (window == input->caret) set_caret_window( input, 0 );
1081 /* check if the specified window can be set in the input data of a given queue */
1082 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1084 struct thread *thread;
1085 int ret = 0;
1087 if (!window) return 1; /* we can always clear the data */
1089 if ((thread = get_window_thread( window )))
1091 ret = (queue->input == thread->queue->input);
1092 if (!ret) set_error( STATUS_ACCESS_DENIED );
1093 release_object( thread );
1095 else set_error( STATUS_INVALID_HANDLE );
1097 return ret;
1100 /* make sure the specified thread has a queue */
1101 int init_thread_queue( struct thread *thread )
1103 if (thread->queue) return 1;
1104 return (create_msg_queue( thread, NULL ) != NULL);
1107 /* attach two thread input data structures */
1108 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1110 struct desktop *desktop;
1111 struct thread_input *input;
1112 int ret;
1114 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1115 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1116 input = (struct thread_input *)grab_object( thread_to->queue->input );
1117 if (input->desktop != desktop)
1119 set_error( STATUS_ACCESS_DENIED );
1120 release_object( input );
1121 release_object( desktop );
1122 return 0;
1124 release_object( desktop );
1126 if (thread_from->queue)
1128 if (!input->focus) input->focus = thread_from->queue->input->focus;
1129 if (!input->active) input->active = thread_from->queue->input->active;
1132 ret = assign_thread_input( thread_from, input );
1133 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1134 release_object( input );
1135 return ret;
1138 /* detach two thread input data structures */
1139 void detach_thread_input( struct thread *thread_from )
1141 struct thread *thread;
1142 struct thread_input *input, *old_input = thread_from->queue->input;
1144 if ((input = create_thread_input( thread_from )))
1146 if (old_input->focus && (thread = get_window_thread( old_input->focus )))
1148 if (thread == thread_from)
1150 input->focus = old_input->focus;
1151 old_input->focus = 0;
1153 release_object( thread );
1155 if (old_input->active && (thread = get_window_thread( old_input->active )))
1157 if (thread == thread_from)
1159 input->active = old_input->active;
1160 old_input->active = 0;
1162 release_object( thread );
1164 assign_thread_input( thread_from, input );
1165 release_object( input );
1170 /* set the next timer to expire */
1171 static void set_next_timer( struct msg_queue *queue )
1173 struct list *ptr;
1175 if (queue->timeout)
1177 remove_timeout_user( queue->timeout );
1178 queue->timeout = NULL;
1180 if ((ptr = list_head( &queue->pending_timers )))
1182 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1183 queue->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, queue );
1185 /* set/clear QS_TIMER bit */
1186 if (list_empty( &queue->expired_timers ))
1187 clear_queue_bits( queue, QS_TIMER );
1188 else
1189 set_queue_bits( queue, QS_TIMER );
1192 /* find a timer from its window and id */
1193 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1194 unsigned int msg, lparam_t id )
1196 struct list *ptr;
1198 /* we need to search both lists */
1200 LIST_FOR_EACH( ptr, &queue->pending_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 LIST_FOR_EACH( ptr, &queue->expired_timers )
1207 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1208 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1210 return NULL;
1213 /* callback for the next timer expiration */
1214 static void timer_callback( void *private )
1216 struct msg_queue *queue = private;
1217 struct list *ptr;
1219 queue->timeout = NULL;
1220 /* move on to the next timer */
1221 ptr = list_head( &queue->pending_timers );
1222 list_remove( ptr );
1223 list_add_tail( &queue->expired_timers, ptr );
1224 set_next_timer( queue );
1227 /* link a timer at its rightful place in the queue list */
1228 static void link_timer( struct msg_queue *queue, struct timer *timer )
1230 struct list *ptr;
1232 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1234 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1235 if (t->when <= timer->when) break;
1237 list_add_before( ptr, &timer->entry );
1240 /* remove a timer from the queue timer list and free it */
1241 static void free_timer( struct msg_queue *queue, struct timer *timer )
1243 list_remove( &timer->entry );
1244 free( timer );
1245 set_next_timer( queue );
1248 /* restart an expired timer */
1249 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1251 list_remove( &timer->entry );
1252 while (-timer->when <= monotonic_time) timer->when -= (timeout_t)timer->rate * 10000;
1253 link_timer( queue, timer );
1254 set_next_timer( queue );
1257 /* find an expired timer matching the filtering parameters */
1258 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1259 unsigned int get_first, unsigned int get_last,
1260 int remove )
1262 struct list *ptr;
1264 LIST_FOR_EACH( ptr, &queue->expired_timers )
1266 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1267 if (win && timer->win != win) continue;
1268 if (check_msg_filter( timer->msg, get_first, get_last ))
1270 if (remove) restart_timer( queue, timer );
1271 return timer;
1274 return NULL;
1277 /* add a timer */
1278 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1280 struct timer *timer = mem_alloc( sizeof(*timer) );
1281 if (timer)
1283 timer->rate = max( rate, 1 );
1284 timer->when = -monotonic_time - (timeout_t)timer->rate * 10000;
1285 link_timer( queue, timer );
1286 /* check if we replaced the next timer */
1287 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1289 return timer;
1292 /* change the input key state for a given key */
1293 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1295 if (down)
1297 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1298 keystate[key] |= down;
1300 else keystate[key] &= ~0x80;
1303 /* update the input key state for a keyboard message */
1304 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1305 unsigned int msg, lparam_t wparam )
1307 unsigned char key;
1308 int down = 0;
1310 switch (msg)
1312 case WM_LBUTTONDOWN:
1313 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1314 /* fall through */
1315 case WM_LBUTTONUP:
1316 set_input_key_state( keystate, VK_LBUTTON, down );
1317 break;
1318 case WM_MBUTTONDOWN:
1319 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1320 /* fall through */
1321 case WM_MBUTTONUP:
1322 set_input_key_state( keystate, VK_MBUTTON, down );
1323 break;
1324 case WM_RBUTTONDOWN:
1325 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1326 /* fall through */
1327 case WM_RBUTTONUP:
1328 set_input_key_state( keystate, VK_RBUTTON, down );
1329 break;
1330 case WM_XBUTTONDOWN:
1331 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1332 /* fall through */
1333 case WM_XBUTTONUP:
1334 if (wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1335 else if (wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1336 break;
1337 case WM_KEYDOWN:
1338 case WM_SYSKEYDOWN:
1339 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1340 /* fall through */
1341 case WM_KEYUP:
1342 case WM_SYSKEYUP:
1343 key = (unsigned char)wparam;
1344 set_input_key_state( keystate, key, down );
1345 switch(key)
1347 case VK_LCONTROL:
1348 case VK_RCONTROL:
1349 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1350 set_input_key_state( keystate, VK_CONTROL, down );
1351 break;
1352 case VK_LMENU:
1353 case VK_RMENU:
1354 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1355 set_input_key_state( keystate, VK_MENU, down );
1356 break;
1357 case VK_LSHIFT:
1358 case VK_RSHIFT:
1359 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1360 set_input_key_state( keystate, VK_SHIFT, down );
1361 break;
1363 break;
1367 /* update the desktop key state according to a mouse message flags */
1368 static void update_desktop_mouse_state( struct desktop *desktop, unsigned int flags,
1369 int x, int y, lparam_t wparam )
1371 if (flags & MOUSEEVENTF_MOVE)
1372 update_desktop_cursor_pos( desktop, x, y );
1373 if (flags & MOUSEEVENTF_LEFTDOWN)
1374 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONDOWN, wparam );
1375 if (flags & MOUSEEVENTF_LEFTUP)
1376 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONUP, wparam );
1377 if (flags & MOUSEEVENTF_RIGHTDOWN)
1378 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONDOWN, wparam );
1379 if (flags & MOUSEEVENTF_RIGHTUP)
1380 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONUP, wparam );
1381 if (flags & MOUSEEVENTF_MIDDLEDOWN)
1382 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONDOWN, wparam );
1383 if (flags & MOUSEEVENTF_MIDDLEUP)
1384 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONUP, wparam );
1385 if (flags & MOUSEEVENTF_XDOWN)
1386 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONDOWN, wparam );
1387 if (flags & MOUSEEVENTF_XUP)
1388 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONUP, wparam );
1391 /* release the hardware message currently being processed by the given thread */
1392 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id )
1394 struct thread_input *input = queue->input;
1395 struct message *msg, *other;
1396 int clr_bit;
1398 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1400 if (msg->unique_id == hw_id) break;
1402 if (&msg->entry == &input->msg_list) return; /* not found */
1404 /* clear the queue bit for that message */
1405 clr_bit = get_hardware_msg_bit( msg );
1406 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1408 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1410 clr_bit = 0;
1411 break;
1414 if (clr_bit) clear_queue_bits( queue, clr_bit );
1416 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1417 list_remove( &msg->entry );
1418 free_message( msg );
1421 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1423 struct hotkey *hotkey;
1424 unsigned int modifiers = 0;
1426 if (msg->msg != WM_KEYDOWN) return 0;
1428 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1429 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1430 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1431 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1433 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1435 if (hotkey->vkey != msg->wparam) continue;
1436 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1439 return 0;
1441 found:
1442 msg->type = MSG_POSTED;
1443 msg->win = hotkey->win;
1444 msg->msg = WM_HOTKEY;
1445 msg->wparam = hotkey->id;
1446 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1448 free( msg->data );
1449 msg->data = NULL;
1450 msg->data_size = 0;
1452 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1453 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1454 hotkey->queue->hotkey_count++;
1455 return 1;
1458 /* find the window that should receive a given hardware message */
1459 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1460 struct message *msg, unsigned int *msg_code,
1461 struct thread **thread )
1463 user_handle_t win = 0;
1465 *thread = NULL;
1466 *msg_code = msg->msg;
1467 if (msg->msg == WM_INPUT || msg->msg == WM_INPUT_DEVICE_CHANGE)
1469 if (!(win = msg->win) && input) win = input->focus;
1471 else if (is_keyboard_msg( msg ))
1473 if (input && !(win = input->focus))
1475 win = input->active;
1476 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1479 else if (!input || !(win = input->capture)) /* mouse message */
1481 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1482 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1484 *thread = window_thread_from_point( win, msg->x, msg->y );
1487 if (!*thread)
1488 *thread = get_window_thread( win );
1489 return win;
1492 static struct rawinput_device_entry *find_rawinput_device( struct process *process, unsigned short usage_page, unsigned short usage )
1494 struct rawinput_device_entry *e;
1496 LIST_FOR_EACH_ENTRY( e, &process->rawinput_devices, struct rawinput_device_entry, entry )
1498 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1499 return e;
1502 return NULL;
1505 static void update_rawinput_device(const struct rawinput_device *device)
1507 struct rawinput_device_entry *e;
1509 if (!(e = find_rawinput_device( current->process, device->usage_page, device->usage )))
1511 if (!(e = mem_alloc( sizeof(*e) ))) return;
1512 list_add_tail( &current->process->rawinput_devices, &e->entry );
1515 if (device->flags & RIDEV_REMOVE)
1517 list_remove( &e->entry );
1518 free( e );
1519 return;
1522 e->device = *device;
1523 e->device.target = get_user_full_handle( e->device.target );
1526 static void prepend_cursor_history( int x, int y, unsigned int time, lparam_t info )
1528 cursor_pos_t *pos = &cursor_history[--cursor_history_latest % ARRAY_SIZE(cursor_history)];
1530 pos->x = x;
1531 pos->y = y;
1532 pos->time = time;
1533 pos->info = info;
1536 /* queue a hardware message into a given thread input */
1537 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1539 user_handle_t win;
1540 struct thread *thread;
1541 struct thread_input *input;
1542 struct hardware_msg_data *msg_data = msg->data;
1543 unsigned int msg_code;
1545 update_input_key_state( desktop, desktop->keystate, msg->msg, msg->wparam );
1546 last_input_time = get_tick_count();
1547 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1549 if (is_keyboard_msg( msg ))
1551 if (queue_hotkey_message( desktop, msg )) return;
1552 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1553 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1554 msg->lparam &= ~(KF_EXTENDED << 16);
1556 else if (msg->msg != WM_INPUT && msg->msg != WM_INPUT_DEVICE_CHANGE)
1558 if (msg->msg == WM_MOUSEMOVE)
1560 prepend_cursor_history( msg->x, msg->y, msg->time, msg_data->info );
1561 if (update_desktop_cursor_pos( desktop, msg->x, msg->y )) always_queue = 1;
1563 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1564 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1565 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1566 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1567 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1568 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1569 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1571 msg->x = desktop->cursor.x;
1572 msg->y = desktop->cursor.y;
1574 if (msg->win && (thread = get_window_thread( msg->win )))
1576 input = thread->queue->input;
1577 release_object( thread );
1579 else input = desktop->foreground_input;
1581 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1582 if (!win || !thread)
1584 if (input) update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1585 free_message( msg );
1586 return;
1588 input = thread->queue->input;
1590 if (win != desktop->cursor.win) always_queue = 1;
1591 desktop->cursor.win = win;
1593 if (!always_queue || merge_message( input, msg )) free_message( msg );
1594 else
1596 msg->unique_id = 0; /* will be set once we return it to the app */
1597 list_add_tail( &input->msg_list, &msg->entry );
1598 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1600 release_object( thread );
1603 /* send the low-level hook message for a given hardware message */
1604 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1605 const hw_input_t *input, struct msg_queue *sender )
1607 struct thread *hook_thread;
1608 struct msg_queue *queue;
1609 struct message *msg;
1610 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1611 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1613 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1614 if (!(queue = hook_thread->queue)) return 0;
1615 if (is_queue_hung( queue )) return 0;
1617 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1619 msg->type = MSG_HOOK_LL;
1620 msg->win = 0;
1621 msg->msg = id;
1622 msg->wparam = hardware_msg->msg;
1623 msg->x = hardware_msg->x;
1624 msg->y = hardware_msg->y;
1625 msg->time = hardware_msg->time;
1626 msg->data_size = hardware_msg->data_size;
1627 msg->result = NULL;
1629 if (input->type == INPUT_KEYBOARD)
1631 unsigned short vkey = input->kbd.vkey;
1632 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1633 msg->lparam = (input->kbd.scan << 16) | vkey;
1635 else msg->lparam = input->mouse.data << 16;
1637 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1638 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1640 free_message( msg );
1641 return 0;
1643 msg->result->hardware_msg = hardware_msg;
1644 msg->result->desktop = (struct desktop *)grab_object( desktop );
1645 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1646 set_queue_bits( queue, QS_SENDMESSAGE );
1647 return 1;
1650 /* get the foreground thread for a desktop and a window receiving input */
1651 static struct thread *get_foreground_thread( struct desktop *desktop, user_handle_t window )
1653 /* if desktop has no foreground process, assume the receiving window is */
1654 if (desktop->foreground_input) return get_window_thread( desktop->foreground_input->focus );
1655 if (window) return get_window_thread( window );
1656 return NULL;
1659 struct rawinput_message
1661 struct thread *foreground;
1662 struct desktop *desktop;
1663 struct hw_msg_source source;
1664 unsigned int time;
1665 unsigned int message;
1666 struct hardware_msg_data data;
1669 /* check if process is supposed to receive a WM_INPUT message and eventually queue it */
1670 static int queue_rawinput_message( struct process* process, void *arg )
1672 const struct rawinput_message* raw_msg = arg;
1673 const struct rawinput_device_entry *entry;
1674 const struct rawinput_device *device = NULL;
1675 struct desktop *target_desktop = NULL, *desktop = NULL;
1676 struct thread *target_thread = NULL, *foreground = NULL;
1677 struct message *msg;
1678 int wparam = RIM_INPUT;
1680 if (raw_msg->data.rawinput.type == RIM_TYPEMOUSE)
1681 device = process->rawinput_mouse;
1682 else if (raw_msg->data.rawinput.type == RIM_TYPEKEYBOARD)
1683 device = process->rawinput_kbd;
1684 else if ((entry = find_rawinput_device( process, raw_msg->data.rawinput.hid.usage_page, raw_msg->data.rawinput.hid.usage )))
1685 device = &entry->device;
1686 if (!device) return 0;
1688 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && !(device->flags & RIDEV_DEVNOTIFY)) return 0;
1690 if (raw_msg->desktop) desktop = (struct desktop *)grab_object( raw_msg->desktop );
1691 else if (!(desktop = get_desktop_obj( process, process->desktop, 0 ))) goto done;
1693 if (raw_msg->foreground) foreground = (struct thread *)grab_object( raw_msg->foreground );
1694 else if (!(foreground = get_foreground_thread( desktop, 0 ))) goto done;
1696 if (process != foreground->process)
1698 if (raw_msg->message == WM_INPUT && !(device->flags & RIDEV_INPUTSINK)) goto done;
1699 if (!(target_thread = get_window_thread( device->target ))) goto done;
1700 if (!(target_desktop = get_thread_desktop( target_thread, 0 ))) goto done;
1701 if (target_desktop != desktop) goto done;
1702 wparam = RIM_INPUTSINK;
1705 if (!(msg = alloc_hardware_message( raw_msg->data.info, raw_msg->source, raw_msg->time )))
1706 goto done;
1708 msg->win = device->target;
1709 msg->msg = raw_msg->message;
1710 msg->wparam = wparam;
1711 msg->lparam = 0;
1712 memcpy( msg->data, &raw_msg->data, sizeof(raw_msg->data) );
1714 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && raw_msg->data.rawinput.type == RIM_TYPEHID)
1716 msg->wparam = raw_msg->data.rawinput.hid.param;
1717 msg->lparam = raw_msg->data.rawinput.hid.device;
1720 queue_hardware_message( desktop, msg, 1 );
1722 done:
1723 if (target_thread) release_object( target_thread );
1724 if (target_desktop) release_object( target_desktop );
1725 if (foreground) release_object( foreground );
1726 if (desktop) release_object( desktop );
1727 return 0;
1730 /* queue a hardware message for a mouse event */
1731 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1732 unsigned int origin, struct msg_queue *sender )
1734 const struct rawinput_device *device;
1735 struct hardware_msg_data *msg_data;
1736 struct rawinput_message raw_msg;
1737 struct message *msg;
1738 struct thread *foreground;
1739 unsigned int i, time, flags;
1740 struct hw_msg_source source = { IMDT_MOUSE, origin };
1741 int wait = 0, x, y;
1743 static const unsigned int messages[] =
1745 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1746 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1747 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1748 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1749 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1750 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1751 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1752 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1753 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1754 0, /* 0x0200 = unused */
1755 0, /* 0x0400 = unused */
1756 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1757 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1760 desktop->cursor.last_change = get_tick_count();
1761 flags = input->mouse.flags;
1762 time = input->mouse.time;
1763 if (!time) time = desktop->cursor.last_change;
1765 if (flags & MOUSEEVENTF_MOVE)
1767 if (flags & MOUSEEVENTF_ABSOLUTE)
1769 x = input->mouse.x;
1770 y = input->mouse.y;
1771 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1772 x == desktop->cursor.x && y == desktop->cursor.y)
1773 flags &= ~MOUSEEVENTF_MOVE;
1775 else
1777 x = desktop->cursor.x + input->mouse.x;
1778 y = desktop->cursor.y + input->mouse.y;
1781 else
1783 x = desktop->cursor.x;
1784 y = desktop->cursor.y;
1787 if ((foreground = get_foreground_thread( desktop, win )))
1789 raw_msg.foreground = foreground;
1790 raw_msg.desktop = desktop;
1791 raw_msg.source = source;
1792 raw_msg.time = time;
1793 raw_msg.message = WM_INPUT;
1795 msg_data = &raw_msg.data;
1796 msg_data->info = input->mouse.info;
1797 msg_data->flags = flags;
1798 msg_data->rawinput.type = RIM_TYPEMOUSE;
1799 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1800 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1801 msg_data->rawinput.mouse.data = input->mouse.data;
1803 enum_processes( queue_rawinput_message, &raw_msg );
1804 release_object( foreground );
1807 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
1809 update_desktop_mouse_state( desktop, flags, x, y, input->mouse.data << 16 );
1810 return 0;
1813 for (i = 0; i < ARRAY_SIZE( messages ); i++)
1815 if (!messages[i]) continue;
1816 if (!(flags & (1 << i))) continue;
1817 flags &= ~(1 << i);
1819 if (!(msg = alloc_hardware_message( input->mouse.info, source, time ))) return 0;
1820 msg_data = msg->data;
1822 msg->win = get_user_full_handle( win );
1823 msg->msg = messages[i];
1824 msg->wparam = input->mouse.data << 16;
1825 msg->lparam = 0;
1826 msg->x = x;
1827 msg->y = y;
1828 if (origin == IMO_INJECTED) msg_data->flags = LLMHF_INJECTED;
1830 /* specify a sender only when sending the last message */
1831 if (!(flags & ((1 << ARRAY_SIZE( messages )) - 1)))
1833 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1834 queue_hardware_message( desktop, msg, 0 );
1836 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1837 queue_hardware_message( desktop, msg, 0 );
1839 return wait;
1842 /* queue a hardware message for a keyboard event */
1843 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1844 unsigned int origin, struct msg_queue *sender )
1846 struct hw_msg_source source = { IMDT_KEYBOARD, origin };
1847 const struct rawinput_device *device;
1848 struct hardware_msg_data *msg_data;
1849 struct rawinput_message raw_msg;
1850 struct message *msg;
1851 struct thread *foreground;
1852 unsigned char vkey = input->kbd.vkey;
1853 unsigned int message_code, time;
1854 int wait;
1856 if (!(time = input->kbd.time)) time = get_tick_count();
1858 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1860 switch (vkey)
1862 case VK_MENU:
1863 case VK_LMENU:
1864 case VK_RMENU:
1865 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1866 break;
1867 case VK_CONTROL:
1868 case VK_LCONTROL:
1869 case VK_RCONTROL:
1870 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1871 break;
1872 case VK_SHIFT:
1873 case VK_LSHIFT:
1874 case VK_RSHIFT:
1875 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1876 break;
1880 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1881 switch (vkey)
1883 case VK_LMENU:
1884 case VK_RMENU:
1885 if (input->kbd.flags & KEYEVENTF_KEYUP)
1887 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1888 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1889 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1890 message_code = WM_SYSKEYUP;
1891 desktop->keystate[VK_MENU] &= ~0x02;
1893 else
1895 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1896 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1897 message_code = WM_SYSKEYDOWN;
1898 desktop->keystate[VK_MENU] |= 0x02;
1900 break;
1902 case VK_LCONTROL:
1903 case VK_RCONTROL:
1904 /* send WM_SYSKEYUP on release if Alt still pressed */
1905 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1906 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1907 message_code = WM_SYSKEYUP;
1908 desktop->keystate[VK_MENU] &= ~0x02;
1909 break;
1911 default:
1912 /* send WM_SYSKEY for Alt-anykey and for F10 */
1913 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1914 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1915 /* fall through */
1916 case VK_F10:
1917 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1918 desktop->keystate[VK_MENU] &= ~0x02;
1919 break;
1922 if ((foreground = get_foreground_thread( desktop, win )))
1924 raw_msg.foreground = foreground;
1925 raw_msg.desktop = desktop;
1926 raw_msg.source = source;
1927 raw_msg.time = time;
1928 raw_msg.message = WM_INPUT;
1930 msg_data = &raw_msg.data;
1931 msg_data->info = input->kbd.info;
1932 msg_data->flags = input->kbd.flags;
1933 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1934 msg_data->rawinput.kbd.message = message_code;
1935 msg_data->rawinput.kbd.vkey = vkey;
1936 msg_data->rawinput.kbd.scan = input->kbd.scan;
1938 enum_processes( queue_rawinput_message, &raw_msg );
1939 release_object( foreground );
1942 if ((device = current->process->rawinput_kbd) && (device->flags & RIDEV_NOLEGACY))
1944 update_input_key_state( desktop, desktop->keystate, message_code, vkey );
1945 return 0;
1948 if (!(msg = alloc_hardware_message( input->kbd.info, source, time ))) return 0;
1949 msg_data = msg->data;
1951 msg->win = get_user_full_handle( win );
1952 msg->msg = message_code;
1953 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1954 if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
1956 if (input->kbd.flags & KEYEVENTF_UNICODE && !vkey)
1958 msg->wparam = VK_PACKET;
1960 else
1962 unsigned int flags = 0;
1963 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1964 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1965 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1966 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1968 msg->wparam = vkey;
1969 msg->lparam |= flags << 16;
1970 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1973 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1974 queue_hardware_message( desktop, msg, 1 );
1976 return wait;
1979 /* queue a hardware message for a custom type of event */
1980 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1981 unsigned int origin, const hw_input_t *input )
1983 struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
1984 struct hardware_msg_data *msg_data;
1985 struct rawinput_message raw_msg;
1986 struct message *msg;
1988 switch (input->hw.msg)
1990 case WM_INPUT_DEVICE_CHANGE:
1991 raw_msg.foreground = NULL;
1992 raw_msg.desktop = NULL;
1993 raw_msg.source = source;
1994 raw_msg.time = get_tick_count();
1995 raw_msg.message = input->hw.msg;
1997 msg_data = &raw_msg.data;
1998 msg_data->info = 0;
1999 msg_data->flags = 0;
2000 msg_data->rawinput = input->hw.rawinput;
2002 enum_processes( queue_rawinput_message, &raw_msg );
2004 if (raw_msg.foreground) release_object( raw_msg.foreground );
2005 return;
2008 if (!(msg = alloc_hardware_message( 0, source, get_tick_count() ))) return;
2010 msg->win = get_user_full_handle( win );
2011 msg->msg = input->hw.msg;
2012 msg->wparam = 0;
2013 msg->lparam = input->hw.lparam;
2014 msg->x = desktop->cursor.x;
2015 msg->y = desktop->cursor.y;
2017 queue_hardware_message( desktop, msg, 1 );
2020 /* check message filter for a hardware message */
2021 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
2022 user_handle_t filter_win, unsigned int first, unsigned int last )
2024 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
2026 /* we can only test the window for a keyboard message since the
2027 * dest window for a mouse message depends on hittest */
2028 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
2029 return 0;
2030 /* the message code is final for a keyboard message, we can simply check it */
2031 return check_msg_filter( msg_code, first, last );
2033 else /* mouse message */
2035 /* we need to check all possible values that the message can have in the end */
2037 if (check_msg_filter( msg_code, first, last )) return 1;
2038 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
2040 /* all other messages can become non-client messages */
2041 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
2043 /* clicks can become double-clicks or non-client double-clicks */
2044 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
2045 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
2047 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2048 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2050 return 0;
2055 /* find a hardware message for the given queue */
2056 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
2057 unsigned int first, unsigned int last, unsigned int flags,
2058 struct get_message_reply *reply )
2060 struct thread_input *input = thread->queue->input;
2061 struct thread *win_thread;
2062 struct list *ptr;
2063 user_handle_t win;
2064 int clear_bits, got_one = 0;
2065 unsigned int msg_code;
2067 ptr = list_head( &input->msg_list );
2068 if (hw_id)
2070 while (ptr)
2072 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2073 if (msg->unique_id == hw_id) break;
2074 ptr = list_next( &input->msg_list, ptr );
2076 if (!ptr) ptr = list_head( &input->msg_list );
2077 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
2080 if (ptr == list_head( &input->msg_list ))
2081 clear_bits = QS_INPUT;
2082 else
2083 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
2085 while (ptr)
2087 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2088 struct hardware_msg_data *data = msg->data;
2090 ptr = list_next( &input->msg_list, ptr );
2091 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
2092 if (!win || !win_thread)
2094 /* no window at all, remove it */
2095 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2096 list_remove( &msg->entry );
2097 free_message( msg );
2098 continue;
2100 if (win_thread != thread)
2102 if (win_thread->queue->input == input)
2104 /* wake the other thread */
2105 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
2106 got_one = 1;
2108 else
2110 /* for another thread input, drop it */
2111 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2112 list_remove( &msg->entry );
2113 free_message( msg );
2115 release_object( win_thread );
2116 continue;
2118 release_object( win_thread );
2120 /* if we already got a message for another thread, or if it doesn't
2121 * match the filter we skip it */
2122 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
2124 clear_bits &= ~get_hardware_msg_bit( msg );
2125 continue;
2127 /* now we can return it */
2128 if (!msg->unique_id) msg->unique_id = get_unique_id();
2129 reply->type = MSG_HARDWARE;
2130 reply->win = win;
2131 reply->msg = msg_code;
2132 reply->wparam = msg->wparam;
2133 reply->lparam = msg->lparam;
2134 reply->x = msg->x;
2135 reply->y = msg->y;
2136 reply->time = msg->time;
2138 data->hw_id = msg->unique_id;
2139 set_reply_data( msg->data, msg->data_size );
2140 if ((msg->msg == WM_INPUT || msg->msg == WM_INPUT_DEVICE_CHANGE) && (flags & PM_REMOVE))
2141 release_hardware_message( current->queue, data->hw_id );
2142 return 1;
2144 /* nothing found, clear the hardware queue bits */
2145 clear_queue_bits( thread->queue, clear_bits );
2146 return 0;
2149 /* increment (or decrement if 'incr' is negative) the queue paint count */
2150 void inc_queue_paint_count( struct thread *thread, int incr )
2152 struct msg_queue *queue = thread->queue;
2154 assert( queue );
2156 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2158 if (queue->paint_count)
2159 set_queue_bits( queue, QS_PAINT );
2160 else
2161 clear_queue_bits( queue, QS_PAINT );
2165 /* remove all messages and timers belonging to a certain window */
2166 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2168 struct msg_queue *queue = thread->queue;
2169 struct list *ptr;
2170 int i;
2172 if (!queue) return;
2174 /* remove timers */
2176 ptr = list_head( &queue->pending_timers );
2177 while (ptr)
2179 struct list *next = list_next( &queue->pending_timers, ptr );
2180 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2181 if (timer->win == win) free_timer( queue, timer );
2182 ptr = next;
2184 ptr = list_head( &queue->expired_timers );
2185 while (ptr)
2187 struct list *next = list_next( &queue->expired_timers, ptr );
2188 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2189 if (timer->win == win) free_timer( queue, timer );
2190 ptr = next;
2193 /* remove messages */
2194 for (i = 0; i < NB_MSG_KINDS; i++)
2196 struct list *ptr, *next;
2198 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2200 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2201 if (msg->win == win)
2203 if (msg->msg == WM_QUIT && !queue->quit_message)
2205 queue->quit_message = 1;
2206 queue->exit_code = msg->wparam;
2208 remove_queue_message( queue, msg, i );
2213 thread_input_cleanup_window( queue, win );
2216 /* post a message to a window */
2217 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2219 struct message *msg;
2220 struct thread *thread = get_window_thread( win );
2222 if (!thread) return;
2224 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2226 msg->type = MSG_POSTED;
2227 msg->win = get_user_full_handle( win );
2228 msg->msg = message;
2229 msg->wparam = wparam;
2230 msg->lparam = lparam;
2231 msg->result = NULL;
2232 msg->data = NULL;
2233 msg->data_size = 0;
2235 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2237 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2238 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2239 if (message == WM_HOTKEY)
2241 set_queue_bits( thread->queue, QS_HOTKEY );
2242 thread->queue->hotkey_count++;
2245 release_object( thread );
2248 /* send a notify message to a window */
2249 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2251 struct message *msg;
2252 struct thread *thread = get_window_thread( win );
2254 if (!thread) return;
2256 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2258 msg->type = MSG_NOTIFY;
2259 msg->win = get_user_full_handle( win );
2260 msg->msg = message;
2261 msg->wparam = wparam;
2262 msg->lparam = lparam;
2263 msg->result = NULL;
2264 msg->data = NULL;
2265 msg->data_size = 0;
2267 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2269 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2270 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2272 release_object( thread );
2275 /* post a win event */
2276 void post_win_event( struct thread *thread, unsigned int event,
2277 user_handle_t win, unsigned int object_id,
2278 unsigned int child_id, client_ptr_t hook_proc,
2279 const WCHAR *module, data_size_t module_size,
2280 user_handle_t hook)
2282 struct message *msg;
2284 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2286 struct winevent_msg_data *data;
2288 msg->type = MSG_WINEVENT;
2289 msg->win = get_user_full_handle( win );
2290 msg->msg = event;
2291 msg->wparam = object_id;
2292 msg->lparam = child_id;
2293 msg->time = get_tick_count();
2294 msg->result = NULL;
2296 if ((data = malloc( sizeof(*data) + module_size )))
2298 data->hook = hook;
2299 data->tid = get_thread_id( current );
2300 data->hook_proc = hook_proc;
2301 memcpy( data + 1, module, module_size );
2303 msg->data = data;
2304 msg->data_size = sizeof(*data) + module_size;
2306 if (debug_level > 1)
2307 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2308 get_thread_id(thread), event, win, object_id, child_id );
2309 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2310 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2312 else
2313 free( msg );
2317 /* free all hotkeys on a desktop, optionally filtering by window */
2318 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2320 struct hotkey *hotkey, *hotkey2;
2322 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2324 if (!window || hotkey->win == window)
2326 list_remove( &hotkey->entry );
2327 free( hotkey );
2333 /* check if the thread owning the window is hung */
2334 DECL_HANDLER(is_window_hung)
2336 struct thread *thread;
2338 thread = get_window_thread( req->win );
2339 if (thread)
2341 reply->is_hung = is_queue_hung( thread->queue );
2342 release_object( thread );
2344 else reply->is_hung = 0;
2348 /* get the message queue of the current thread */
2349 DECL_HANDLER(get_msg_queue)
2351 struct msg_queue *queue = get_current_queue();
2353 reply->handle = 0;
2354 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2358 /* set the file descriptor associated to the current thread queue */
2359 DECL_HANDLER(set_queue_fd)
2361 struct msg_queue *queue = get_current_queue();
2362 struct file *file;
2363 int unix_fd;
2365 if (queue->fd) /* fd can only be set once */
2367 set_error( STATUS_ACCESS_DENIED );
2368 return;
2370 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2372 if ((unix_fd = get_file_unix_fd( file )) != -1)
2374 if ((unix_fd = dup( unix_fd )) != -1)
2375 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2376 else
2377 file_set_error();
2379 release_object( file );
2383 /* set the current message queue wakeup mask */
2384 DECL_HANDLER(set_queue_mask)
2386 struct msg_queue *queue = get_current_queue();
2388 if (queue)
2390 queue->wake_mask = req->wake_mask;
2391 queue->changed_mask = req->changed_mask;
2392 reply->wake_bits = queue->wake_bits;
2393 reply->changed_bits = queue->changed_bits;
2394 if (is_signaled( queue ))
2396 /* if skip wait is set, do what would have been done in the subsequent wait */
2397 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2398 else wake_up( &queue->obj, 0 );
2404 /* get the current message queue status */
2405 DECL_HANDLER(get_queue_status)
2407 struct msg_queue *queue = current->queue;
2408 if (queue)
2410 reply->wake_bits = queue->wake_bits;
2411 reply->changed_bits = queue->changed_bits;
2412 queue->changed_bits &= ~req->clear_bits;
2414 else reply->wake_bits = reply->changed_bits = 0;
2418 /* send a message to a thread queue */
2419 DECL_HANDLER(send_message)
2421 struct message *msg;
2422 struct msg_queue *send_queue = get_current_queue();
2423 struct msg_queue *recv_queue = NULL;
2424 struct thread *thread = NULL;
2426 if (!(thread = get_thread_from_id( req->id ))) return;
2428 if (!(recv_queue = thread->queue))
2430 set_error( STATUS_INVALID_PARAMETER );
2431 release_object( thread );
2432 return;
2434 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2436 set_error( STATUS_TIMEOUT );
2437 release_object( thread );
2438 return;
2441 if ((msg = mem_alloc( sizeof(*msg) )))
2443 msg->type = req->type;
2444 msg->win = get_user_full_handle( req->win );
2445 msg->msg = req->msg;
2446 msg->wparam = req->wparam;
2447 msg->lparam = req->lparam;
2448 msg->result = NULL;
2449 msg->data = NULL;
2450 msg->data_size = get_req_data_size();
2452 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2454 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2456 free( msg );
2457 release_object( thread );
2458 return;
2461 switch(msg->type)
2463 case MSG_OTHER_PROCESS:
2464 case MSG_ASCII:
2465 case MSG_UNICODE:
2466 case MSG_CALLBACK:
2467 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2469 free_message( msg );
2470 break;
2472 /* fall through */
2473 case MSG_NOTIFY:
2474 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2475 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2476 break;
2477 case MSG_POSTED:
2478 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2479 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2480 if (msg->msg == WM_HOTKEY)
2482 set_queue_bits( recv_queue, QS_HOTKEY );
2483 recv_queue->hotkey_count++;
2485 break;
2486 case MSG_HARDWARE: /* should use send_hardware_message instead */
2487 case MSG_CALLBACK_RESULT: /* cannot send this one */
2488 case MSG_HOOK_LL: /* generated internally */
2489 default:
2490 set_error( STATUS_INVALID_PARAMETER );
2491 free( msg );
2492 break;
2495 release_object( thread );
2498 /* send a hardware message to a thread queue */
2499 DECL_HANDLER(send_hardware_message)
2501 struct thread *thread = NULL;
2502 struct desktop *desktop;
2503 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2504 struct msg_queue *sender = get_current_queue();
2505 data_size_t size = min( 256, get_reply_max_size() );
2507 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2509 if (req->win)
2511 if (!(thread = get_window_thread( req->win ))) return;
2512 if (desktop != thread->queue->input->desktop)
2514 /* don't allow queuing events to a different desktop */
2515 release_object( desktop );
2516 return;
2520 reply->prev_x = desktop->cursor.x;
2521 reply->prev_y = desktop->cursor.y;
2523 switch (req->input.type)
2525 case INPUT_MOUSE:
2526 reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2527 break;
2528 case INPUT_KEYBOARD:
2529 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2530 break;
2531 case INPUT_HARDWARE:
2532 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2533 break;
2534 default:
2535 set_error( STATUS_INVALID_PARAMETER );
2537 if (thread) release_object( thread );
2539 reply->new_x = desktop->cursor.x;
2540 reply->new_y = desktop->cursor.y;
2541 set_reply_data( desktop->keystate, size );
2542 release_object( desktop );
2545 /* post a quit message to the current queue */
2546 DECL_HANDLER(post_quit_message)
2548 struct msg_queue *queue = get_current_queue();
2550 if (!queue)
2551 return;
2553 queue->quit_message = 1;
2554 queue->exit_code = req->exit_code;
2555 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2558 /* get a message from the current queue */
2559 DECL_HANDLER(get_message)
2561 struct timer *timer;
2562 struct list *ptr;
2563 struct msg_queue *queue = get_current_queue();
2564 user_handle_t get_win = get_user_full_handle( req->get_win );
2565 unsigned int filter = req->flags >> 16;
2567 reply->active_hooks = get_active_hooks();
2569 if (get_win && get_win != 1 && get_win != -1 && !get_user_object( get_win, USER_WINDOW ))
2571 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2572 return;
2575 if (!queue) return;
2576 queue->last_get_msg = current_time;
2577 if (!filter) filter = QS_ALLINPUT;
2579 /* first check for sent messages */
2580 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2582 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2583 receive_message( queue, msg, reply );
2584 return;
2587 /* clear changed bits so we can wait on them if we don't find a message */
2588 if (filter & QS_POSTMESSAGE)
2590 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2591 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2593 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2594 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2596 /* then check for posted messages */
2597 if ((filter & QS_POSTMESSAGE) &&
2598 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2599 return;
2601 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2602 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2603 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2604 return;
2606 /* only check for quit messages if not posted messages pending */
2607 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2608 return;
2610 /* then check for any raw hardware message */
2611 if ((filter & QS_INPUT) &&
2612 filter_contains_hw_range( req->get_first, req->get_last ) &&
2613 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2614 return;
2616 /* now check for WM_PAINT */
2617 if ((filter & QS_PAINT) &&
2618 queue->paint_count &&
2619 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2620 (reply->win = find_window_to_repaint( get_win, current )))
2622 reply->type = MSG_POSTED;
2623 reply->msg = WM_PAINT;
2624 reply->wparam = 0;
2625 reply->lparam = 0;
2626 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2627 return;
2630 /* now check for timer */
2631 if ((filter & QS_TIMER) &&
2632 (timer = find_expired_timer( queue, get_win, req->get_first,
2633 req->get_last, (req->flags & PM_REMOVE) )))
2635 reply->type = MSG_POSTED;
2636 reply->win = timer->win;
2637 reply->msg = timer->msg;
2638 reply->wparam = timer->id;
2639 reply->lparam = timer->lparam;
2640 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2641 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2642 set_event( current->process->idle_event );
2643 return;
2646 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2647 queue->wake_mask = req->wake_mask;
2648 queue->changed_mask = req->changed_mask;
2649 set_error( STATUS_PENDING ); /* FIXME */
2653 /* reply to a sent message */
2654 DECL_HANDLER(reply_message)
2656 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2657 else if (current->queue->recv_result)
2658 reply_message( current->queue, req->result, 0, req->remove,
2659 get_req_data(), get_req_data_size() );
2663 /* accept the current hardware message */
2664 DECL_HANDLER(accept_hardware_message)
2666 if (current->queue)
2667 release_hardware_message( current->queue, req->hw_id );
2668 else
2669 set_error( STATUS_ACCESS_DENIED );
2673 /* retrieve the reply for the last message sent */
2674 DECL_HANDLER(get_message_reply)
2676 struct message_result *result;
2677 struct list *entry;
2678 struct msg_queue *queue = current->queue;
2680 if (queue)
2682 set_error( STATUS_PENDING );
2683 reply->result = 0;
2685 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2687 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2688 if (result->replied || req->cancel)
2690 if (result->replied)
2692 reply->result = result->result;
2693 set_error( result->error );
2694 if (result->data)
2696 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2697 set_reply_data_ptr( result->data, data_len );
2698 result->data = NULL;
2699 result->data_size = 0;
2702 remove_result_from_sender( result );
2704 entry = list_head( &queue->send_result );
2705 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2706 else
2708 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2709 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2710 else clear_queue_bits( queue, QS_SMRESULT );
2714 else set_error( STATUS_ACCESS_DENIED );
2718 /* set a window timer */
2719 DECL_HANDLER(set_win_timer)
2721 struct timer *timer;
2722 struct msg_queue *queue;
2723 struct thread *thread = NULL;
2724 user_handle_t win = 0;
2725 lparam_t id = req->id;
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;
2740 queue = thread->queue;
2741 /* remove it if it existed already */
2742 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2744 else
2746 queue = get_current_queue();
2747 /* look for a timer with this id */
2748 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2750 /* free and reuse id */
2751 free_timer( queue, timer );
2753 else
2755 lparam_t end_id = queue->next_timer_id;
2757 /* find a free id for it */
2758 while (1)
2760 id = queue->next_timer_id;
2761 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2763 if (!find_timer( queue, 0, req->msg, id )) break;
2764 if (queue->next_timer_id == end_id)
2766 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2767 return;
2773 if ((timer = set_timer( queue, req->rate )))
2775 timer->win = win;
2776 timer->msg = req->msg;
2777 timer->id = id;
2778 timer->lparam = req->lparam;
2779 reply->id = id;
2781 if (thread) release_object( thread );
2784 /* kill a window timer */
2785 DECL_HANDLER(kill_win_timer)
2787 struct timer *timer;
2788 struct thread *thread;
2789 user_handle_t win = 0;
2791 if (req->win)
2793 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2795 set_error( STATUS_INVALID_HANDLE );
2796 return;
2798 if (thread->process != current->process)
2800 release_object( thread );
2801 set_error( STATUS_ACCESS_DENIED );
2802 return;
2805 else thread = (struct thread *)grab_object( current );
2807 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2808 free_timer( thread->queue, timer );
2809 else
2810 set_error( STATUS_INVALID_PARAMETER );
2812 release_object( thread );
2815 DECL_HANDLER(register_hotkey)
2817 struct desktop *desktop;
2818 user_handle_t win_handle = req->window;
2819 struct hotkey *hotkey;
2820 struct hotkey *new_hotkey = NULL;
2821 struct thread *thread;
2822 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2824 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2826 if (win_handle)
2828 if (!(win_handle = get_valid_window_handle( win_handle )))
2830 release_object( desktop );
2831 return;
2834 thread = get_window_thread( win_handle );
2835 if (thread) release_object( thread );
2837 if (thread != current)
2839 release_object( desktop );
2840 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2841 return;
2845 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2847 if (req->vkey == hotkey->vkey &&
2848 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2850 release_object( desktop );
2851 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2852 return;
2854 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2855 new_hotkey = hotkey;
2858 if (new_hotkey)
2860 reply->replaced = 1;
2861 reply->flags = new_hotkey->flags;
2862 reply->vkey = new_hotkey->vkey;
2864 else
2866 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2867 if (new_hotkey)
2869 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2870 new_hotkey->queue = current->queue;
2871 new_hotkey->win = win_handle;
2872 new_hotkey->id = req->id;
2876 if (new_hotkey)
2878 new_hotkey->flags = req->flags;
2879 new_hotkey->vkey = req->vkey;
2882 release_object( desktop );
2885 DECL_HANDLER(unregister_hotkey)
2887 struct desktop *desktop;
2888 user_handle_t win_handle = req->window;
2889 struct hotkey *hotkey;
2890 struct thread *thread;
2892 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2894 if (win_handle)
2896 if (!(win_handle = get_valid_window_handle( win_handle )))
2898 release_object( desktop );
2899 return;
2902 thread = get_window_thread( win_handle );
2903 if (thread) release_object( thread );
2905 if (thread != current)
2907 release_object( desktop );
2908 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2909 return;
2913 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2915 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2916 goto found;
2919 release_object( desktop );
2920 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2921 return;
2923 found:
2924 reply->flags = hotkey->flags;
2925 reply->vkey = hotkey->vkey;
2926 list_remove( &hotkey->entry );
2927 free( hotkey );
2928 release_object( desktop );
2931 /* attach (or detach) thread inputs */
2932 DECL_HANDLER(attach_thread_input)
2934 struct thread *thread_from = get_thread_from_id( req->tid_from );
2935 struct thread *thread_to = get_thread_from_id( req->tid_to );
2937 if (!thread_from || !thread_to)
2939 if (thread_from) release_object( thread_from );
2940 if (thread_to) release_object( thread_to );
2941 return;
2943 if (thread_from != thread_to)
2945 if (req->attach)
2947 if ((thread_to->queue || thread_to == current) &&
2948 (thread_from->queue || thread_from == current))
2949 attach_thread_input( thread_from, thread_to );
2950 else
2951 set_error( STATUS_INVALID_PARAMETER );
2953 else
2955 if (thread_from->queue && thread_to->queue &&
2956 thread_from->queue->input == thread_to->queue->input)
2957 detach_thread_input( thread_from );
2958 else
2959 set_error( STATUS_ACCESS_DENIED );
2962 else set_error( STATUS_ACCESS_DENIED );
2963 release_object( thread_from );
2964 release_object( thread_to );
2968 /* get thread input data */
2969 DECL_HANDLER(get_thread_input)
2971 struct thread *thread = NULL;
2972 struct desktop *desktop;
2973 struct thread_input *input;
2975 if (req->tid)
2977 if (!(thread = get_thread_from_id( req->tid ))) return;
2978 if (!(desktop = get_thread_desktop( thread, 0 )))
2980 release_object( thread );
2981 return;
2983 input = thread->queue ? thread->queue->input : NULL;
2985 else
2987 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2988 input = desktop->foreground_input; /* get the foreground thread info */
2991 if (input)
2993 reply->focus = input->focus;
2994 reply->capture = input->capture;
2995 reply->active = input->active;
2996 reply->menu_owner = input->menu_owner;
2997 reply->move_size = input->move_size;
2998 reply->caret = input->caret;
2999 reply->cursor = input->cursor;
3000 reply->show_count = input->cursor_count;
3001 reply->rect = input->caret_rect;
3004 /* foreground window is active window of foreground thread */
3005 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
3006 if (thread) release_object( thread );
3007 release_object( desktop );
3011 /* retrieve queue keyboard state for current thread or global async state */
3012 DECL_HANDLER(get_key_state)
3014 struct desktop *desktop;
3015 data_size_t size = min( 256, get_reply_max_size() );
3017 if (req->async) /* get global async key state */
3019 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3020 if (req->key >= 0)
3022 reply->state = desktop->keystate[req->key & 0xff];
3023 desktop->keystate[req->key & 0xff] &= ~0x40;
3025 set_reply_data( desktop->keystate, size );
3026 release_object( desktop );
3028 else if (!current->queue)
3030 unsigned char *keystate;
3031 /* fallback to desktop keystate */
3032 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3033 if (req->key >= 0) reply->state = desktop->keystate[req->key & 0xff] & ~0x40;
3034 if ((keystate = set_reply_data_size( size )))
3036 unsigned int i;
3037 for (i = 0; i < size; i++) keystate[i] = desktop->keystate[i] & ~0x40;
3039 release_object( desktop );
3041 else
3043 unsigned char *keystate = current->queue->input->keystate;
3044 if (req->key >= 0) reply->state = keystate[req->key & 0xff];
3045 set_reply_data( keystate, size );
3050 /* set queue keyboard state for current thread */
3051 DECL_HANDLER(set_key_state)
3053 struct desktop *desktop;
3054 data_size_t size = min( 256, get_req_data_size() );
3056 if (current->queue) memcpy( current->queue->input->keystate, get_req_data(), size );
3057 if (req->async && (desktop = get_thread_desktop( current, 0 )))
3059 memcpy( desktop->keystate, get_req_data(), size );
3060 release_object( desktop );
3065 /* set the system foreground window */
3066 DECL_HANDLER(set_foreground_window)
3068 struct thread *thread = NULL;
3069 struct desktop *desktop;
3070 struct msg_queue *queue = get_current_queue();
3072 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3073 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
3074 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
3075 reply->send_msg_new = FALSE;
3077 if (is_valid_foreground_window( req->handle ) &&
3078 (thread = get_window_thread( req->handle )) &&
3079 thread->queue->input->desktop == desktop)
3081 set_foreground_input( desktop, thread->queue->input );
3082 reply->send_msg_new = (desktop->foreground_input != queue->input);
3084 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
3086 if (thread) release_object( thread );
3087 release_object( desktop );
3091 /* set the current thread focus window */
3092 DECL_HANDLER(set_focus_window)
3094 struct msg_queue *queue = get_current_queue();
3096 reply->previous = 0;
3097 if (queue && check_queue_input_window( queue, req->handle ))
3099 reply->previous = queue->input->focus;
3100 queue->input->focus = get_user_full_handle( req->handle );
3105 /* set the current thread active window */
3106 DECL_HANDLER(set_active_window)
3108 struct msg_queue *queue = get_current_queue();
3110 reply->previous = 0;
3111 if (queue && check_queue_input_window( queue, req->handle ))
3113 if (!req->handle || make_window_active( req->handle ))
3115 reply->previous = queue->input->active;
3116 queue->input->active = get_user_full_handle( req->handle );
3118 else set_error( STATUS_INVALID_HANDLE );
3123 /* set the current thread capture window */
3124 DECL_HANDLER(set_capture_window)
3126 struct msg_queue *queue = get_current_queue();
3128 reply->previous = reply->full_handle = 0;
3129 if (queue && check_queue_input_window( queue, req->handle ))
3131 struct thread_input *input = queue->input;
3133 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
3134 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
3136 set_error(STATUS_ACCESS_DENIED);
3137 return;
3139 reply->previous = input->capture;
3140 input->capture = get_user_full_handle( req->handle );
3141 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
3142 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
3143 reply->full_handle = input->capture;
3148 /* Set the current thread caret window */
3149 DECL_HANDLER(set_caret_window)
3151 struct msg_queue *queue = get_current_queue();
3153 reply->previous = 0;
3154 if (queue && check_queue_input_window( queue, req->handle ))
3156 struct thread_input *input = queue->input;
3158 reply->previous = input->caret;
3159 reply->old_rect = input->caret_rect;
3160 reply->old_hide = input->caret_hide;
3161 reply->old_state = input->caret_state;
3163 set_caret_window( input, get_user_full_handle(req->handle) );
3164 input->caret_rect.right = input->caret_rect.left + req->width;
3165 input->caret_rect.bottom = input->caret_rect.top + req->height;
3170 /* Set the current thread caret information */
3171 DECL_HANDLER(set_caret_info)
3173 struct msg_queue *queue = get_current_queue();
3174 struct thread_input *input;
3176 if (!queue) return;
3177 input = queue->input;
3178 reply->full_handle = input->caret;
3179 reply->old_rect = input->caret_rect;
3180 reply->old_hide = input->caret_hide;
3181 reply->old_state = input->caret_state;
3183 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3185 set_error( STATUS_ACCESS_DENIED );
3186 return;
3188 if (req->flags & SET_CARET_POS)
3190 input->caret_rect.right += req->x - input->caret_rect.left;
3191 input->caret_rect.bottom += req->y - input->caret_rect.top;
3192 input->caret_rect.left = req->x;
3193 input->caret_rect.top = req->y;
3195 if (req->flags & SET_CARET_HIDE)
3197 input->caret_hide += req->hide;
3198 if (input->caret_hide < 0) input->caret_hide = 0;
3200 if (req->flags & SET_CARET_STATE)
3202 switch (req->state)
3204 case CARET_STATE_OFF: input->caret_state = 0; break;
3205 case CARET_STATE_ON: input->caret_state = 1; break;
3206 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3207 case CARET_STATE_ON_IF_MOVED:
3208 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3209 break;
3215 /* get the time of the last input event */
3216 DECL_HANDLER(get_last_input_time)
3218 reply->time = last_input_time;
3221 /* set/get the current cursor */
3222 DECL_HANDLER(set_cursor)
3224 struct msg_queue *queue = get_current_queue();
3225 struct thread_input *input;
3227 if (!queue) return;
3228 input = queue->input;
3230 reply->prev_handle = input->cursor;
3231 reply->prev_count = input->cursor_count;
3232 reply->prev_x = input->desktop->cursor.x;
3233 reply->prev_y = input->desktop->cursor.y;
3235 if (req->flags & SET_CURSOR_HANDLE)
3237 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3239 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3240 return;
3242 input->cursor = req->handle;
3244 if (req->flags & SET_CURSOR_COUNT)
3246 queue->cursor_count += req->show_count;
3247 input->cursor_count += req->show_count;
3249 if (req->flags & SET_CURSOR_POS)
3251 set_cursor_pos( input->desktop, req->x, req->y );
3253 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3255 struct desktop *desktop = input->desktop;
3257 /* only the desktop owner can set the message */
3258 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3259 desktop->cursor.clip_msg = req->clip_msg;
3261 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip, 0 );
3264 reply->new_x = input->desktop->cursor.x;
3265 reply->new_y = input->desktop->cursor.y;
3266 reply->new_clip = input->desktop->cursor.clip;
3267 reply->last_change = input->desktop->cursor.last_change;
3270 /* Get the history of the 64 last cursor positions */
3271 DECL_HANDLER(get_cursor_history)
3273 cursor_pos_t *pos;
3274 unsigned int i, count = min( 64, get_reply_max_size() / sizeof(*pos) );
3276 if ((pos = set_reply_data_size( count * sizeof(*pos) )))
3277 for (i = 0; i < count; i++)
3278 pos[i] = cursor_history[(i + cursor_history_latest) % ARRAY_SIZE(cursor_history)];
3281 DECL_HANDLER(get_rawinput_buffer)
3283 struct thread_input *input = current->queue->input;
3284 data_size_t size = 0, next_size = 0;
3285 struct list *ptr;
3286 char *buf, *cur, *tmp;
3287 int count = 0, buf_size = 16 * sizeof(struct hardware_msg_data);
3289 if (!req->buffer_size) buf = NULL;
3290 else if (!(buf = mem_alloc( buf_size ))) return;
3292 cur = buf;
3293 ptr = list_head( &input->msg_list );
3294 while (ptr)
3296 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
3297 struct hardware_msg_data *data = msg->data;
3299 ptr = list_next( &input->msg_list, ptr );
3300 if (msg->msg != WM_INPUT) continue;
3302 next_size = req->rawinput_size;
3303 if (size + next_size > req->buffer_size) break;
3304 if (cur + sizeof(*data) > buf + get_reply_max_size()) break;
3305 if (cur + sizeof(*data) > buf + buf_size)
3307 buf_size += buf_size / 2;
3308 if (!(tmp = realloc( buf, buf_size )))
3310 set_error( STATUS_NO_MEMORY );
3311 return;
3313 cur = tmp + (cur - buf);
3314 buf = tmp;
3317 memcpy(cur, data, sizeof(*data));
3318 list_remove( &msg->entry );
3319 free_message( msg );
3321 size += next_size;
3322 cur += sizeof(*data);
3323 count++;
3326 reply->next_size = next_size;
3327 reply->count = count;
3328 set_reply_data_ptr( buf, cur - buf );
3331 DECL_HANDLER(update_rawinput_devices)
3333 const struct rawinput_device *devices = get_req_data();
3334 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3335 const struct rawinput_device_entry *e;
3336 unsigned int i;
3338 for (i = 0; i < device_count; ++i)
3340 update_rawinput_device(&devices[i]);
3343 e = find_rawinput_device( current->process, 1, 2 );
3344 current->process->rawinput_mouse = e ? &e->device : NULL;
3345 e = find_rawinput_device( current->process, 1, 6 );
3346 current->process->rawinput_kbd = e ? &e->device : NULL;
3349 DECL_HANDLER(get_rawinput_devices)
3351 struct rawinput_device_entry *e;
3352 struct rawinput_device *devices;
3353 unsigned int i = 0, device_count = list_count( &current->process->rawinput_devices );
3354 unsigned int size = device_count * sizeof(*devices);
3356 reply->device_count = device_count;
3358 /* no buffer provided, nothing else to do */
3359 if (!get_reply_max_size()) return;
3361 if (size > get_reply_max_size())
3362 set_error( STATUS_BUFFER_TOO_SMALL );
3363 else if ((devices = set_reply_data_size( size )))
3365 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
3366 devices[i++] = e->device;