ucrtbase: Add __std_terminate export.
[wine.git] / server / queue.c
blobf38c7e9fb17b6687db82a1347075c544605a1fc0
1 /*
2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #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, data_size_t extra_size )
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) + extra_size )))
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) + extra_size;
368 memset( msg_data, 0, sizeof(*msg_data) + extra_size );
369 msg_data->info = info;
370 msg_data->size = msg->data_size;
371 msg_data->source = source;
372 return msg;
375 static int update_desktop_cursor_pos( struct desktop *desktop, int x, int y )
377 int updated;
379 x = max( min( x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
380 y = max( min( y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
381 updated = (desktop->cursor.x != x || desktop->cursor.y != y);
382 desktop->cursor.x = x;
383 desktop->cursor.y = y;
384 desktop->cursor.last_change = get_tick_count();
386 return updated;
389 /* set the cursor position and queue the corresponding mouse message */
390 static void set_cursor_pos( struct desktop *desktop, int x, int y )
392 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
393 const struct rawinput_device *device;
394 struct message *msg;
396 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
398 update_desktop_cursor_pos( desktop, x, y );
399 return;
402 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
404 msg->msg = WM_MOUSEMOVE;
405 msg->x = x;
406 msg->y = y;
407 queue_hardware_message( desktop, msg, 1 );
410 /* retrieve default position and time for synthesized messages */
411 static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
413 struct desktop *desktop = queue->input->desktop;
415 *x = desktop->cursor.x;
416 *y = desktop->cursor.y;
417 *time = get_tick_count();
420 /* set the cursor clip rectangle */
421 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect, int send_clip_msg )
423 rectangle_t top_rect;
424 int x, y;
426 get_top_window_rectangle( desktop, &top_rect );
427 if (rect)
429 rectangle_t new_rect = *rect;
430 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
431 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
432 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
433 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
434 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
435 desktop->cursor.clip = new_rect;
437 else desktop->cursor.clip = top_rect;
439 if (desktop->cursor.clip_msg && send_clip_msg)
440 post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
442 /* warp the mouse to be inside the clip rect */
443 x = max( min( desktop->cursor.x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
444 y = max( min( desktop->cursor.y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
445 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
448 /* change the foreground input and reset the cursor clip rect */
449 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
451 if (desktop->foreground_input == input) return;
452 set_clip_rectangle( desktop, NULL, 1 );
453 desktop->foreground_input = input;
456 /* get the hook table for a given thread */
457 struct hook_table *get_queue_hooks( struct thread *thread )
459 if (!thread->queue) return NULL;
460 return thread->queue->hooks;
463 /* set the hook table for a given thread, allocating the queue if needed */
464 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
466 struct msg_queue *queue = thread->queue;
467 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
468 if (queue->hooks) release_object( queue->hooks );
469 queue->hooks = hooks;
472 /* check the queue status */
473 static inline int is_signaled( struct msg_queue *queue )
475 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
478 /* set some queue bits */
479 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
481 queue->wake_bits |= bits;
482 queue->changed_bits |= bits;
483 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
486 /* clear some queue bits */
487 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
489 queue->wake_bits &= ~bits;
490 queue->changed_bits &= ~bits;
493 /* check whether msg is a keyboard message */
494 static inline int is_keyboard_msg( struct message *msg )
496 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
499 /* check if message is matched by the filter */
500 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
502 return (msg >= first && msg <= last);
505 /* check whether a message filter contains at least one potential hardware message */
506 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
508 /* hardware message ranges are (in numerical order):
509 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
510 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
511 * WM_MOUSEFIRST .. WM_MOUSELAST
513 if (last < WM_NCMOUSEFIRST) return 0;
514 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
515 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
516 if (first > WM_MOUSELAST) return 0;
517 return 1;
520 /* get the QS_* bit corresponding to a given hardware message */
521 static inline int get_hardware_msg_bit( struct message *msg )
523 if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
524 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
525 if (is_keyboard_msg( msg )) return QS_KEY;
526 return QS_MOUSEBUTTON;
529 /* get the current thread queue, creating it if needed */
530 static inline struct msg_queue *get_current_queue(void)
532 struct msg_queue *queue = current->queue;
533 if (!queue) queue = create_msg_queue( current, NULL );
534 return queue;
537 /* get a (pseudo-)unique id to tag hardware messages */
538 static inline unsigned int get_unique_id(void)
540 static unsigned int id;
541 if (!++id) id = 1; /* avoid an id of 0 */
542 return id;
545 /* try to merge a message with the last in the list; return 1 if successful */
546 static int merge_message( struct thread_input *input, const struct message *msg )
548 struct message *prev;
549 struct list *ptr;
551 if (msg->msg != WM_MOUSEMOVE) return 0;
552 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
554 prev = LIST_ENTRY( ptr, struct message, entry );
555 if (prev->msg != WM_INPUT) break;
557 if (!ptr) return 0;
558 if (prev->result) return 0;
559 if (prev->win && msg->win && prev->win != msg->win) return 0;
560 if (prev->msg != msg->msg) return 0;
561 if (prev->type != msg->type) return 0;
562 /* now we can merge it */
563 prev->wparam = msg->wparam;
564 prev->lparam = msg->lparam;
565 prev->x = msg->x;
566 prev->y = msg->y;
567 prev->time = msg->time;
568 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
570 struct hardware_msg_data *prev_data = prev->data;
571 struct hardware_msg_data *msg_data = msg->data;
572 prev_data->info = msg_data->info;
574 list_remove( ptr );
575 list_add_tail( &input->msg_list, ptr );
576 return 1;
579 /* free a result structure */
580 static void free_result( struct message_result *result )
582 if (result->timeout) remove_timeout_user( result->timeout );
583 free( result->data );
584 if (result->callback_msg) free_message( result->callback_msg );
585 if (result->hardware_msg) free_message( result->hardware_msg );
586 if (result->desktop) release_object( result->desktop );
587 free( result );
590 /* remove the result from the sender list it is on */
591 static inline void remove_result_from_sender( struct message_result *result )
593 assert( result->sender );
595 list_remove( &result->sender_entry );
596 result->sender = NULL;
597 if (!result->receiver) free_result( result );
600 /* store the message result in the appropriate structure */
601 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
603 res->result = result;
604 res->error = error;
605 res->replied = 1;
606 if (res->timeout)
608 remove_timeout_user( res->timeout );
609 res->timeout = NULL;
612 if (res->hardware_msg)
614 if (!error && result) /* rejected by the hook */
615 free_message( res->hardware_msg );
616 else
617 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
619 res->hardware_msg = NULL;
622 if (res->sender)
624 if (res->callback_msg)
626 /* queue the callback message in the sender queue */
627 struct callback_msg_data *data = res->callback_msg->data;
628 data->result = result;
629 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
630 set_queue_bits( res->sender, QS_SENDMESSAGE );
631 res->callback_msg = NULL;
632 remove_result_from_sender( res );
634 else
636 /* wake sender queue if waiting on this result */
637 if (list_head(&res->sender->send_result) == &res->sender_entry)
638 set_queue_bits( res->sender, QS_SMRESULT );
641 else if (!res->receiver) free_result( res );
644 /* free a message when deleting a queue or window */
645 static void free_message( struct message *msg )
647 struct message_result *result = msg->result;
648 if (result)
650 result->msg = NULL;
651 result->receiver = NULL;
652 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
654 free( msg->data );
655 free( msg );
658 /* remove (and free) a message from a message list */
659 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
660 enum message_kind kind )
662 list_remove( &msg->entry );
663 switch(kind)
665 case SEND_MESSAGE:
666 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
667 break;
668 case POST_MESSAGE:
669 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
670 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
671 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
672 clear_queue_bits( queue, QS_HOTKEY );
673 break;
675 free_message( msg );
678 /* message timed out without getting a reply */
679 static void result_timeout( void *private )
681 struct message_result *result = private;
683 assert( !result->replied );
685 result->timeout = NULL;
687 if (result->msg) /* not received yet */
689 struct message *msg = result->msg;
691 result->msg = NULL;
692 msg->result = NULL;
693 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
694 result->receiver = NULL;
696 store_message_result( result, 0, STATUS_TIMEOUT );
699 /* allocate and fill a message result structure */
700 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
701 struct msg_queue *recv_queue,
702 struct message *msg, timeout_t timeout )
704 struct message_result *result = mem_alloc( sizeof(*result) );
705 if (result)
707 result->msg = msg;
708 result->sender = send_queue;
709 result->receiver = recv_queue;
710 result->replied = 0;
711 result->data = NULL;
712 result->data_size = 0;
713 result->timeout = NULL;
714 result->hardware_msg = NULL;
715 result->desktop = NULL;
716 result->callback_msg = NULL;
718 if (msg->type == MSG_CALLBACK)
720 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
722 if (!callback_msg)
724 free( result );
725 return NULL;
727 callback_msg->type = MSG_CALLBACK_RESULT;
728 callback_msg->win = msg->win;
729 callback_msg->msg = msg->msg;
730 callback_msg->wparam = 0;
731 callback_msg->lparam = 0;
732 callback_msg->time = get_tick_count();
733 callback_msg->result = NULL;
734 /* steal the data from the original message */
735 callback_msg->data = msg->data;
736 callback_msg->data_size = msg->data_size;
737 msg->data = NULL;
738 msg->data_size = 0;
740 result->callback_msg = callback_msg;
741 list_add_head( &send_queue->callback_result, &result->sender_entry );
743 else if (send_queue)
745 list_add_head( &send_queue->send_result, &result->sender_entry );
746 clear_queue_bits( send_queue, QS_SMRESULT );
749 if (timeout != TIMEOUT_INFINITE)
750 result->timeout = add_timeout_user( timeout, result_timeout, result );
752 return result;
755 /* receive a message, removing it from the sent queue */
756 static void receive_message( struct msg_queue *queue, struct message *msg,
757 struct get_message_reply *reply )
759 struct message_result *result = msg->result;
761 reply->total = msg->data_size;
762 if (msg->data_size > get_reply_max_size())
764 set_error( STATUS_BUFFER_OVERFLOW );
765 return;
767 reply->type = msg->type;
768 reply->win = msg->win;
769 reply->msg = msg->msg;
770 reply->wparam = msg->wparam;
771 reply->lparam = msg->lparam;
772 reply->x = msg->x;
773 reply->y = msg->y;
774 reply->time = msg->time;
776 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
778 list_remove( &msg->entry );
779 /* put the result on the receiver result stack */
780 if (result)
782 result->msg = NULL;
783 result->recv_next = queue->recv_result;
784 queue->recv_result = result;
786 free( msg );
787 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
790 /* set the result of the current received message */
791 static void reply_message( struct msg_queue *queue, lparam_t result,
792 unsigned int error, int remove, const void *data, data_size_t len )
794 struct message_result *res = queue->recv_result;
796 if (remove)
798 queue->recv_result = res->recv_next;
799 res->receiver = NULL;
800 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
802 free_result( res );
803 return;
806 if (!res->replied)
808 if (len && (res->data = memdup( data, len ))) res->data_size = len;
809 store_message_result( res, result, error );
813 static int match_window( user_handle_t win, user_handle_t msg_win )
815 if (!win) return 1;
816 if (win == -1 || win == 1) return !msg_win;
817 if (msg_win == win) return 1;
818 return is_child_window( win, msg_win );
821 /* retrieve a posted message */
822 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
823 unsigned int first, unsigned int last, unsigned int flags,
824 struct get_message_reply *reply )
826 struct message *msg;
828 /* check against the filters */
829 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
831 if (!match_window( win, msg->win )) continue;
832 if (!check_msg_filter( msg->msg, first, last )) continue;
833 goto found; /* found one */
835 return 0;
837 /* return it to the app */
838 found:
839 reply->total = msg->data_size;
840 if (msg->data_size > get_reply_max_size())
842 set_error( STATUS_BUFFER_OVERFLOW );
843 return 1;
845 reply->type = msg->type;
846 reply->win = msg->win;
847 reply->msg = msg->msg;
848 reply->wparam = msg->wparam;
849 reply->lparam = msg->lparam;
850 reply->x = msg->x;
851 reply->y = msg->y;
852 reply->time = msg->time;
854 if (flags & PM_REMOVE)
856 if (msg->data)
858 set_reply_data_ptr( msg->data, msg->data_size );
859 msg->data = NULL;
860 msg->data_size = 0;
862 remove_queue_message( queue, msg, POST_MESSAGE );
864 else if (msg->data) set_reply_data( msg->data, msg->data_size );
866 return 1;
869 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
870 struct get_message_reply *reply )
872 if (queue->quit_message)
874 reply->total = 0;
875 reply->type = MSG_POSTED;
876 reply->win = 0;
877 reply->msg = WM_QUIT;
878 reply->wparam = queue->exit_code;
879 reply->lparam = 0;
881 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
883 if (flags & PM_REMOVE)
885 queue->quit_message = 0;
886 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
887 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
889 return 1;
891 else
892 return 0;
895 /* empty a message list and free all the messages */
896 static void empty_msg_list( struct list *list )
898 struct list *ptr;
900 while ((ptr = list_head( list )) != NULL)
902 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
903 list_remove( &msg->entry );
904 free_message( msg );
908 /* cleanup all pending results when deleting a queue */
909 static void cleanup_results( struct msg_queue *queue )
911 struct list *entry;
913 while ((entry = list_head( &queue->send_result )) != NULL)
915 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
918 while ((entry = list_head( &queue->callback_result )) != NULL)
920 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
923 while (queue->recv_result)
924 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
927 /* check if the thread owning the queue is hung (not checking for messages) */
928 static int is_queue_hung( struct msg_queue *queue )
930 struct wait_queue_entry *entry;
932 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
933 return 0; /* less than 5 seconds since last get message -> not hung */
935 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
937 if (get_wait_queue_thread(entry)->queue == queue)
938 return 0; /* thread is waiting on queue -> not hung */
940 return 1;
943 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
945 struct msg_queue *queue = (struct msg_queue *)obj;
946 struct process *process = get_wait_queue_thread(entry)->process;
948 /* a thread can only wait on its own queue */
949 if (get_wait_queue_thread(entry)->queue != queue)
951 set_error( STATUS_ACCESS_DENIED );
952 return 0;
954 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
956 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
957 set_fd_events( queue->fd, POLLIN );
958 add_queue( obj, entry );
959 return 1;
962 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
964 struct msg_queue *queue = (struct msg_queue *)obj;
966 remove_queue( obj, entry );
967 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
968 set_fd_events( queue->fd, 0 );
971 static void msg_queue_dump( struct object *obj, int verbose )
973 struct msg_queue *queue = (struct msg_queue *)obj;
974 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
975 queue->wake_bits, queue->wake_mask );
978 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
980 struct msg_queue *queue = (struct msg_queue *)obj;
981 int ret = 0;
983 if (queue->fd)
985 if ((ret = check_fd_events( queue->fd, POLLIN )))
986 /* stop waiting on select() if we are signaled */
987 set_fd_events( queue->fd, 0 );
988 else if (!list_empty( &obj->wait_queue ))
989 /* restart waiting on poll() if we are no longer signaled */
990 set_fd_events( queue->fd, POLLIN );
992 return ret || is_signaled( queue );
995 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
997 struct msg_queue *queue = (struct msg_queue *)obj;
998 queue->wake_mask = 0;
999 queue->changed_mask = 0;
1002 static void msg_queue_destroy( struct object *obj )
1004 struct msg_queue *queue = (struct msg_queue *)obj;
1005 struct list *ptr;
1006 struct hotkey *hotkey, *hotkey2;
1007 int i;
1009 cleanup_results( queue );
1010 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
1012 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
1014 if (hotkey->queue == queue)
1016 list_remove( &hotkey->entry );
1017 free( hotkey );
1021 while ((ptr = list_head( &queue->pending_timers )))
1023 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1024 list_remove( &timer->entry );
1025 free( timer );
1027 while ((ptr = list_head( &queue->expired_timers )))
1029 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1030 list_remove( &timer->entry );
1031 free( timer );
1033 if (queue->timeout) remove_timeout_user( queue->timeout );
1034 queue->input->cursor_count -= queue->cursor_count;
1035 release_object( queue->input );
1036 if (queue->hooks) release_object( queue->hooks );
1037 if (queue->fd) release_object( queue->fd );
1040 static void msg_queue_poll_event( struct fd *fd, int event )
1042 struct msg_queue *queue = get_fd_user( fd );
1043 assert( queue->obj.ops == &msg_queue_ops );
1045 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1046 else set_fd_events( queue->fd, 0 );
1047 wake_up( &queue->obj, 0 );
1050 static void thread_input_dump( struct object *obj, int verbose )
1052 struct thread_input *input = (struct thread_input *)obj;
1053 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
1054 input->focus, input->capture, input->active );
1057 static void thread_input_destroy( struct object *obj )
1059 struct thread_input *input = (struct thread_input *)obj;
1061 empty_msg_list( &input->msg_list );
1062 if (input->desktop)
1064 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
1065 release_object( input->desktop );
1069 /* fix the thread input data when a window is destroyed */
1070 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1072 struct thread_input *input = queue->input;
1074 if (window == input->focus) input->focus = 0;
1075 if (window == input->capture) input->capture = 0;
1076 if (window == input->active) input->active = 0;
1077 if (window == input->menu_owner) input->menu_owner = 0;
1078 if (window == input->move_size) input->move_size = 0;
1079 if (window == input->caret) set_caret_window( input, 0 );
1082 /* check if the specified window can be set in the input data of a given queue */
1083 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1085 struct thread *thread;
1086 int ret = 0;
1088 if (!window) return 1; /* we can always clear the data */
1090 if ((thread = get_window_thread( window )))
1092 ret = (queue->input == thread->queue->input);
1093 if (!ret) set_error( STATUS_ACCESS_DENIED );
1094 release_object( thread );
1096 else set_error( STATUS_INVALID_HANDLE );
1098 return ret;
1101 /* make sure the specified thread has a queue */
1102 int init_thread_queue( struct thread *thread )
1104 if (thread->queue) return 1;
1105 return (create_msg_queue( thread, NULL ) != NULL);
1108 /* attach two thread input data structures */
1109 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1111 struct desktop *desktop;
1112 struct thread_input *input;
1113 int ret;
1115 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1116 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1117 input = (struct thread_input *)grab_object( thread_to->queue->input );
1118 if (input->desktop != desktop)
1120 set_error( STATUS_ACCESS_DENIED );
1121 release_object( input );
1122 release_object( desktop );
1123 return 0;
1125 release_object( desktop );
1127 if (thread_from->queue)
1129 if (!input->focus) input->focus = thread_from->queue->input->focus;
1130 if (!input->active) input->active = thread_from->queue->input->active;
1133 ret = assign_thread_input( thread_from, input );
1134 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1135 release_object( input );
1136 return ret;
1139 /* detach two thread input data structures */
1140 void detach_thread_input( struct thread *thread_from )
1142 struct thread *thread;
1143 struct thread_input *input, *old_input = thread_from->queue->input;
1145 if ((input = create_thread_input( thread_from )))
1147 if (old_input->focus && (thread = get_window_thread( old_input->focus )))
1149 if (thread == thread_from)
1151 input->focus = old_input->focus;
1152 old_input->focus = 0;
1154 release_object( thread );
1156 if (old_input->active && (thread = get_window_thread( old_input->active )))
1158 if (thread == thread_from)
1160 input->active = old_input->active;
1161 old_input->active = 0;
1163 release_object( thread );
1165 assign_thread_input( thread_from, input );
1166 release_object( input );
1171 /* set the next timer to expire */
1172 static void set_next_timer( struct msg_queue *queue )
1174 struct list *ptr;
1176 if (queue->timeout)
1178 remove_timeout_user( queue->timeout );
1179 queue->timeout = NULL;
1181 if ((ptr = list_head( &queue->pending_timers )))
1183 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1184 queue->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, queue );
1186 /* set/clear QS_TIMER bit */
1187 if (list_empty( &queue->expired_timers ))
1188 clear_queue_bits( queue, QS_TIMER );
1189 else
1190 set_queue_bits( queue, QS_TIMER );
1193 /* find a timer from its window and id */
1194 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1195 unsigned int msg, lparam_t id )
1197 struct list *ptr;
1199 /* we need to search both lists */
1201 LIST_FOR_EACH( ptr, &queue->pending_timers )
1203 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1204 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1206 LIST_FOR_EACH( ptr, &queue->expired_timers )
1208 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1209 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1211 return NULL;
1214 /* callback for the next timer expiration */
1215 static void timer_callback( void *private )
1217 struct msg_queue *queue = private;
1218 struct list *ptr;
1220 queue->timeout = NULL;
1221 /* move on to the next timer */
1222 ptr = list_head( &queue->pending_timers );
1223 list_remove( ptr );
1224 list_add_tail( &queue->expired_timers, ptr );
1225 set_next_timer( queue );
1228 /* link a timer at its rightful place in the queue list */
1229 static void link_timer( struct msg_queue *queue, struct timer *timer )
1231 struct list *ptr;
1233 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1235 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1236 if (t->when <= timer->when) break;
1238 list_add_before( ptr, &timer->entry );
1241 /* remove a timer from the queue timer list and free it */
1242 static void free_timer( struct msg_queue *queue, struct timer *timer )
1244 list_remove( &timer->entry );
1245 free( timer );
1246 set_next_timer( queue );
1249 /* restart an expired timer */
1250 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1252 list_remove( &timer->entry );
1253 while (-timer->when <= monotonic_time) timer->when -= (timeout_t)timer->rate * 10000;
1254 link_timer( queue, timer );
1255 set_next_timer( queue );
1258 /* find an expired timer matching the filtering parameters */
1259 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1260 unsigned int get_first, unsigned int get_last,
1261 int remove )
1263 struct list *ptr;
1265 LIST_FOR_EACH( ptr, &queue->expired_timers )
1267 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1268 if (win && timer->win != win) continue;
1269 if (check_msg_filter( timer->msg, get_first, get_last ))
1271 if (remove) restart_timer( queue, timer );
1272 return timer;
1275 return NULL;
1278 /* add a timer */
1279 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1281 struct timer *timer = mem_alloc( sizeof(*timer) );
1282 if (timer)
1284 timer->rate = max( rate, 1 );
1285 timer->when = -monotonic_time - (timeout_t)timer->rate * 10000;
1286 link_timer( queue, timer );
1287 /* check if we replaced the next timer */
1288 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1290 return timer;
1293 /* change the input key state for a given key */
1294 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1296 if (down)
1298 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1299 keystate[key] |= down;
1301 else keystate[key] &= ~0x80;
1304 /* update the input key state for a keyboard message */
1305 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1306 unsigned int msg, lparam_t wparam )
1308 unsigned char key;
1309 int down = 0;
1311 switch (msg)
1313 case WM_LBUTTONDOWN:
1314 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1315 /* fall through */
1316 case WM_LBUTTONUP:
1317 set_input_key_state( keystate, VK_LBUTTON, down );
1318 break;
1319 case WM_MBUTTONDOWN:
1320 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1321 /* fall through */
1322 case WM_MBUTTONUP:
1323 set_input_key_state( keystate, VK_MBUTTON, down );
1324 break;
1325 case WM_RBUTTONDOWN:
1326 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1327 /* fall through */
1328 case WM_RBUTTONUP:
1329 set_input_key_state( keystate, VK_RBUTTON, down );
1330 break;
1331 case WM_XBUTTONDOWN:
1332 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1333 /* fall through */
1334 case WM_XBUTTONUP:
1335 if (wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1336 else if (wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1337 break;
1338 case WM_KEYDOWN:
1339 case WM_SYSKEYDOWN:
1340 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1341 /* fall through */
1342 case WM_KEYUP:
1343 case WM_SYSKEYUP:
1344 key = (unsigned char)wparam;
1345 set_input_key_state( keystate, key, down );
1346 switch(key)
1348 case VK_LCONTROL:
1349 case VK_RCONTROL:
1350 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1351 set_input_key_state( keystate, VK_CONTROL, down );
1352 break;
1353 case VK_LMENU:
1354 case VK_RMENU:
1355 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1356 set_input_key_state( keystate, VK_MENU, down );
1357 break;
1358 case VK_LSHIFT:
1359 case VK_RSHIFT:
1360 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1361 set_input_key_state( keystate, VK_SHIFT, down );
1362 break;
1364 break;
1368 /* update the desktop key state according to a mouse message flags */
1369 static void update_desktop_mouse_state( struct desktop *desktop, unsigned int flags,
1370 int x, int y, lparam_t wparam )
1372 if (flags & MOUSEEVENTF_MOVE)
1373 update_desktop_cursor_pos( desktop, x, y );
1374 if (flags & MOUSEEVENTF_LEFTDOWN)
1375 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONDOWN, wparam );
1376 if (flags & MOUSEEVENTF_LEFTUP)
1377 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONUP, wparam );
1378 if (flags & MOUSEEVENTF_RIGHTDOWN)
1379 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONDOWN, wparam );
1380 if (flags & MOUSEEVENTF_RIGHTUP)
1381 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONUP, wparam );
1382 if (flags & MOUSEEVENTF_MIDDLEDOWN)
1383 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONDOWN, wparam );
1384 if (flags & MOUSEEVENTF_MIDDLEUP)
1385 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONUP, wparam );
1386 if (flags & MOUSEEVENTF_XDOWN)
1387 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONDOWN, wparam );
1388 if (flags & MOUSEEVENTF_XUP)
1389 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONUP, wparam );
1392 /* release the hardware message currently being processed by the given thread */
1393 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id )
1395 struct thread_input *input = queue->input;
1396 struct message *msg, *other;
1397 int clr_bit;
1399 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1401 if (msg->unique_id == hw_id) break;
1403 if (&msg->entry == &input->msg_list) return; /* not found */
1405 /* clear the queue bit for that message */
1406 clr_bit = get_hardware_msg_bit( msg );
1407 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1409 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1411 clr_bit = 0;
1412 break;
1415 if (clr_bit) clear_queue_bits( queue, clr_bit );
1417 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1418 list_remove( &msg->entry );
1419 free_message( msg );
1422 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1424 struct hotkey *hotkey;
1425 unsigned int modifiers = 0;
1427 if (msg->msg != WM_KEYDOWN) return 0;
1429 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1430 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1431 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1432 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1434 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1436 if (hotkey->vkey != msg->wparam) continue;
1437 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1440 return 0;
1442 found:
1443 msg->type = MSG_POSTED;
1444 msg->win = hotkey->win;
1445 msg->msg = WM_HOTKEY;
1446 msg->wparam = hotkey->id;
1447 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1449 free( msg->data );
1450 msg->data = NULL;
1451 msg->data_size = 0;
1453 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1454 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1455 hotkey->queue->hotkey_count++;
1456 return 1;
1459 /* find the window that should receive a given hardware message */
1460 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1461 struct message *msg, unsigned int *msg_code,
1462 struct thread **thread )
1464 user_handle_t win = 0;
1466 *thread = NULL;
1467 *msg_code = msg->msg;
1468 if (msg->msg == WM_INPUT || msg->msg == WM_INPUT_DEVICE_CHANGE)
1470 if (!(win = msg->win) && input) win = input->focus;
1472 else if (is_keyboard_msg( msg ))
1474 if (input && !(win = input->focus))
1476 win = input->active;
1477 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1480 else if (!input || !(win = input->capture)) /* mouse message */
1482 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1483 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1485 *thread = window_thread_from_point( win, msg->x, msg->y );
1488 if (!*thread)
1489 *thread = get_window_thread( win );
1490 return win;
1493 static struct rawinput_device_entry *find_rawinput_device( struct process *process, unsigned short usage_page, unsigned short usage )
1495 struct rawinput_device_entry *e;
1497 LIST_FOR_EACH_ENTRY( e, &process->rawinput_devices, struct rawinput_device_entry, entry )
1499 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1500 return e;
1503 return NULL;
1506 static void update_rawinput_device(const struct rawinput_device *device)
1508 struct rawinput_device_entry *e;
1510 if (!(e = find_rawinput_device( current->process, device->usage_page, device->usage )))
1512 if (!(e = mem_alloc( sizeof(*e) ))) return;
1513 list_add_tail( &current->process->rawinput_devices, &e->entry );
1516 if (device->flags & RIDEV_REMOVE)
1518 list_remove( &e->entry );
1519 free( e );
1520 return;
1523 e->device = *device;
1524 e->device.target = get_user_full_handle( e->device.target );
1527 static void prepend_cursor_history( int x, int y, unsigned int time, lparam_t info )
1529 cursor_pos_t *pos = &cursor_history[--cursor_history_latest % ARRAY_SIZE(cursor_history)];
1531 pos->x = x;
1532 pos->y = y;
1533 pos->time = time;
1534 pos->info = info;
1537 /* queue a hardware message into a given thread input */
1538 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1540 user_handle_t win;
1541 struct thread *thread;
1542 struct thread_input *input;
1543 struct hardware_msg_data *msg_data = msg->data;
1544 unsigned int msg_code;
1546 update_input_key_state( desktop, desktop->keystate, msg->msg, msg->wparam );
1547 last_input_time = get_tick_count();
1548 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1550 if (is_keyboard_msg( msg ))
1552 if (queue_hotkey_message( desktop, msg )) return;
1553 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1554 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1555 msg->lparam &= ~(KF_EXTENDED << 16);
1557 else if (msg->msg != WM_INPUT && msg->msg != WM_INPUT_DEVICE_CHANGE)
1559 if (msg->msg == WM_MOUSEMOVE)
1561 prepend_cursor_history( msg->x, msg->y, msg->time, msg_data->info );
1562 if (update_desktop_cursor_pos( desktop, msg->x, msg->y )) always_queue = 1;
1564 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1565 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1566 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1567 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1568 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1569 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1570 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1572 msg->x = desktop->cursor.x;
1573 msg->y = desktop->cursor.y;
1575 if (msg->win && (thread = get_window_thread( msg->win )))
1577 input = thread->queue->input;
1578 release_object( thread );
1580 else input = desktop->foreground_input;
1582 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1583 if (!win || !thread)
1585 if (input) update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1586 free_message( msg );
1587 return;
1589 input = thread->queue->input;
1591 if (win != desktop->cursor.win) always_queue = 1;
1592 desktop->cursor.win = win;
1594 if (!always_queue || merge_message( input, msg )) free_message( msg );
1595 else
1597 msg->unique_id = 0; /* will be set once we return it to the app */
1598 list_add_tail( &input->msg_list, &msg->entry );
1599 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1601 release_object( thread );
1604 /* send the low-level hook message for a given hardware message */
1605 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1606 const hw_input_t *input, struct msg_queue *sender )
1608 struct thread *hook_thread;
1609 struct msg_queue *queue;
1610 struct message *msg;
1611 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1612 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1614 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1615 if (!(queue = hook_thread->queue)) return 0;
1616 if (is_queue_hung( queue )) return 0;
1618 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1620 msg->type = MSG_HOOK_LL;
1621 msg->win = 0;
1622 msg->msg = id;
1623 msg->wparam = hardware_msg->msg;
1624 msg->x = hardware_msg->x;
1625 msg->y = hardware_msg->y;
1626 msg->time = hardware_msg->time;
1627 msg->data_size = hardware_msg->data_size;
1628 msg->result = NULL;
1630 if (input->type == INPUT_KEYBOARD)
1632 unsigned short vkey = input->kbd.vkey;
1633 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1634 msg->lparam = (input->kbd.scan << 16) | vkey;
1636 else msg->lparam = input->mouse.data << 16;
1638 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1639 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1641 free_message( msg );
1642 return 0;
1644 msg->result->hardware_msg = hardware_msg;
1645 msg->result->desktop = (struct desktop *)grab_object( desktop );
1646 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1647 set_queue_bits( queue, QS_SENDMESSAGE );
1648 return 1;
1651 /* get the foreground thread for a desktop and a window receiving input */
1652 static struct thread *get_foreground_thread( struct desktop *desktop, user_handle_t window )
1654 /* if desktop has no foreground process, assume the receiving window is */
1655 if (desktop->foreground_input) return get_window_thread( desktop->foreground_input->focus );
1656 if (window) return get_window_thread( window );
1657 return NULL;
1660 struct rawinput_message
1662 struct thread *foreground;
1663 struct desktop *desktop;
1664 struct hw_msg_source source;
1665 unsigned int time;
1666 unsigned int message;
1667 struct hardware_msg_data data;
1668 const void *hid_report;
1671 /* check if process is supposed to receive a WM_INPUT message and eventually queue it */
1672 static int queue_rawinput_message( struct process* process, void *arg )
1674 const struct rawinput_message* raw_msg = arg;
1675 const struct rawinput_device_entry *entry;
1676 const struct rawinput_device *device = NULL;
1677 struct desktop *target_desktop = NULL, *desktop = NULL;
1678 struct thread *target_thread = NULL, *foreground = NULL;
1679 struct message *msg;
1680 data_size_t report_size;
1681 int wparam = RIM_INPUT;
1683 if (raw_msg->data.rawinput.type == RIM_TYPEMOUSE)
1684 device = process->rawinput_mouse;
1685 else if (raw_msg->data.rawinput.type == RIM_TYPEKEYBOARD)
1686 device = process->rawinput_kbd;
1687 else if ((entry = find_rawinput_device( process, raw_msg->data.rawinput.hid.usage_page, raw_msg->data.rawinput.hid.usage )))
1688 device = &entry->device;
1689 if (!device) return 0;
1691 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && !(device->flags & RIDEV_DEVNOTIFY)) return 0;
1693 if (raw_msg->desktop) desktop = (struct desktop *)grab_object( raw_msg->desktop );
1694 else if (!(desktop = get_desktop_obj( process, process->desktop, 0 ))) goto done;
1696 if (raw_msg->foreground) foreground = (struct thread *)grab_object( raw_msg->foreground );
1697 else if (!(foreground = get_foreground_thread( desktop, 0 ))) goto done;
1699 if (process != foreground->process)
1701 if (raw_msg->message == WM_INPUT && !(device->flags & RIDEV_INPUTSINK)) goto done;
1702 if (!(target_thread = get_window_thread( device->target ))) goto done;
1703 if (!(target_desktop = get_thread_desktop( target_thread, 0 ))) goto done;
1704 if (target_desktop != desktop) goto done;
1705 wparam = RIM_INPUTSINK;
1708 if (raw_msg->data.rawinput.type != RIM_TYPEHID || !raw_msg->hid_report) report_size = 0;
1709 else report_size = raw_msg->data.size - sizeof(raw_msg->data);
1711 if (!(msg = alloc_hardware_message( raw_msg->data.info, raw_msg->source, raw_msg->time, report_size )))
1712 goto done;
1714 msg->win = device->target;
1715 msg->msg = raw_msg->message;
1716 msg->wparam = wparam;
1717 msg->lparam = 0;
1718 memcpy( msg->data, &raw_msg->data, sizeof(raw_msg->data) );
1719 if (report_size) memcpy( (struct hardware_msg_data *)msg->data + 1, raw_msg->hid_report, report_size );
1721 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && raw_msg->data.rawinput.type == RIM_TYPEHID)
1723 msg->wparam = raw_msg->data.rawinput.hid.param;
1724 msg->lparam = raw_msg->data.rawinput.hid.device;
1727 queue_hardware_message( desktop, msg, 1 );
1729 done:
1730 if (target_thread) release_object( target_thread );
1731 if (target_desktop) release_object( target_desktop );
1732 if (foreground) release_object( foreground );
1733 if (desktop) release_object( desktop );
1734 return 0;
1737 /* queue a hardware message for a mouse event */
1738 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1739 unsigned int origin, struct msg_queue *sender )
1741 const struct rawinput_device *device;
1742 struct hardware_msg_data *msg_data;
1743 struct rawinput_message raw_msg;
1744 struct message *msg;
1745 struct thread *foreground;
1746 unsigned int i, time, flags;
1747 struct hw_msg_source source = { IMDT_MOUSE, origin };
1748 int wait = 0, x, y;
1750 static const unsigned int messages[] =
1752 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1753 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1754 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1755 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1756 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1757 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1758 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1759 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1760 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1761 0, /* 0x0200 = unused */
1762 0, /* 0x0400 = unused */
1763 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1764 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1767 desktop->cursor.last_change = get_tick_count();
1768 flags = input->mouse.flags;
1769 time = input->mouse.time;
1770 if (!time) time = desktop->cursor.last_change;
1772 if (flags & MOUSEEVENTF_MOVE)
1774 if (flags & MOUSEEVENTF_ABSOLUTE)
1776 x = input->mouse.x;
1777 y = input->mouse.y;
1778 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1779 x == desktop->cursor.x && y == desktop->cursor.y)
1780 flags &= ~MOUSEEVENTF_MOVE;
1782 else
1784 x = desktop->cursor.x + input->mouse.x;
1785 y = desktop->cursor.y + input->mouse.y;
1788 else
1790 x = desktop->cursor.x;
1791 y = desktop->cursor.y;
1794 if ((foreground = get_foreground_thread( desktop, win )))
1796 raw_msg.foreground = foreground;
1797 raw_msg.desktop = desktop;
1798 raw_msg.source = source;
1799 raw_msg.time = time;
1800 raw_msg.message = WM_INPUT;
1801 raw_msg.hid_report = NULL;
1803 msg_data = &raw_msg.data;
1804 msg_data->info = input->mouse.info;
1805 msg_data->size = sizeof(*msg_data);
1806 msg_data->flags = flags;
1807 msg_data->rawinput.type = RIM_TYPEMOUSE;
1808 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1809 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1810 msg_data->rawinput.mouse.data = input->mouse.data;
1812 enum_processes( queue_rawinput_message, &raw_msg );
1813 release_object( foreground );
1816 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
1818 update_desktop_mouse_state( desktop, flags, x, y, input->mouse.data << 16 );
1819 return 0;
1822 for (i = 0; i < ARRAY_SIZE( messages ); i++)
1824 if (!messages[i]) continue;
1825 if (!(flags & (1 << i))) continue;
1826 flags &= ~(1 << i);
1828 if (!(msg = alloc_hardware_message( input->mouse.info, source, time, 0 ))) return 0;
1829 msg_data = msg->data;
1831 msg->win = get_user_full_handle( win );
1832 msg->msg = messages[i];
1833 msg->wparam = input->mouse.data << 16;
1834 msg->lparam = 0;
1835 msg->x = x;
1836 msg->y = y;
1837 if (origin == IMO_INJECTED) msg_data->flags = LLMHF_INJECTED;
1839 /* specify a sender only when sending the last message */
1840 if (!(flags & ((1 << ARRAY_SIZE( messages )) - 1)))
1842 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1843 queue_hardware_message( desktop, msg, 0 );
1845 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1846 queue_hardware_message( desktop, msg, 0 );
1848 return wait;
1851 /* queue a hardware message for a keyboard event */
1852 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1853 unsigned int origin, struct msg_queue *sender )
1855 struct hw_msg_source source = { IMDT_KEYBOARD, origin };
1856 const struct rawinput_device *device;
1857 struct hardware_msg_data *msg_data;
1858 struct rawinput_message raw_msg;
1859 struct message *msg;
1860 struct thread *foreground;
1861 unsigned char vkey = input->kbd.vkey;
1862 unsigned int message_code, time;
1863 int wait;
1865 if (!(time = input->kbd.time)) time = get_tick_count();
1867 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1869 switch (vkey)
1871 case VK_MENU:
1872 case VK_LMENU:
1873 case VK_RMENU:
1874 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1875 break;
1876 case VK_CONTROL:
1877 case VK_LCONTROL:
1878 case VK_RCONTROL:
1879 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1880 break;
1881 case VK_SHIFT:
1882 case VK_LSHIFT:
1883 case VK_RSHIFT:
1884 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1885 break;
1889 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1890 switch (vkey)
1892 case VK_LMENU:
1893 case VK_RMENU:
1894 if (input->kbd.flags & KEYEVENTF_KEYUP)
1896 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1897 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1898 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1899 message_code = WM_SYSKEYUP;
1900 desktop->keystate[VK_MENU] &= ~0x02;
1902 else
1904 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1905 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1906 message_code = WM_SYSKEYDOWN;
1907 desktop->keystate[VK_MENU] |= 0x02;
1909 break;
1911 case VK_LCONTROL:
1912 case VK_RCONTROL:
1913 /* send WM_SYSKEYUP on release if Alt still pressed */
1914 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1915 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1916 message_code = WM_SYSKEYUP;
1917 desktop->keystate[VK_MENU] &= ~0x02;
1918 break;
1920 default:
1921 /* send WM_SYSKEY for Alt-anykey and for F10 */
1922 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1923 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1924 /* fall through */
1925 case VK_F10:
1926 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1927 desktop->keystate[VK_MENU] &= ~0x02;
1928 break;
1931 if ((foreground = get_foreground_thread( desktop, win )))
1933 raw_msg.foreground = foreground;
1934 raw_msg.desktop = desktop;
1935 raw_msg.source = source;
1936 raw_msg.time = time;
1937 raw_msg.message = WM_INPUT;
1938 raw_msg.hid_report = NULL;
1940 msg_data = &raw_msg.data;
1941 msg_data->info = input->kbd.info;
1942 msg_data->size = sizeof(*msg_data);
1943 msg_data->flags = input->kbd.flags;
1944 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1945 msg_data->rawinput.kbd.message = message_code;
1946 msg_data->rawinput.kbd.vkey = vkey;
1947 msg_data->rawinput.kbd.scan = input->kbd.scan;
1949 enum_processes( queue_rawinput_message, &raw_msg );
1950 release_object( foreground );
1953 if ((device = current->process->rawinput_kbd) && (device->flags & RIDEV_NOLEGACY))
1955 update_input_key_state( desktop, desktop->keystate, message_code, vkey );
1956 return 0;
1959 if (!(msg = alloc_hardware_message( input->kbd.info, source, time, 0 ))) return 0;
1960 msg_data = msg->data;
1962 msg->win = get_user_full_handle( win );
1963 msg->msg = message_code;
1964 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1965 if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
1967 if (input->kbd.flags & KEYEVENTF_UNICODE && !vkey)
1969 msg->wparam = VK_PACKET;
1971 else
1973 unsigned int flags = 0;
1974 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1975 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1976 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1977 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1979 msg->wparam = vkey;
1980 msg->lparam |= flags << 16;
1981 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1984 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1985 queue_hardware_message( desktop, msg, 1 );
1987 return wait;
1990 /* queue a hardware message for a custom type of event */
1991 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1992 unsigned int origin, const hw_input_t *input )
1994 struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
1995 struct hardware_msg_data *msg_data;
1996 struct rawinput_message raw_msg;
1997 struct message *msg;
1998 data_size_t report_size = 0;
2000 switch (input->hw.msg)
2002 case WM_INPUT:
2003 case WM_INPUT_DEVICE_CHANGE:
2004 raw_msg.foreground = NULL;
2005 raw_msg.desktop = NULL;
2006 raw_msg.source = source;
2007 raw_msg.time = get_tick_count();
2008 raw_msg.message = input->hw.msg;
2009 raw_msg.hid_report = NULL;
2011 if (input->hw.rawinput.type == RIM_TYPEHID)
2013 raw_msg.hid_report = get_req_data();
2014 report_size = input->hw.rawinput.hid.length * input->hw.rawinput.hid.count;
2017 if (report_size != get_req_data_size())
2019 set_error( STATUS_INVALID_PARAMETER );
2020 return;
2023 msg_data = &raw_msg.data;
2024 msg_data->info = 0;
2025 msg_data->size = sizeof(*msg_data) + report_size;
2026 msg_data->flags = 0;
2027 msg_data->rawinput = input->hw.rawinput;
2029 enum_processes( queue_rawinput_message, &raw_msg );
2031 if (raw_msg.foreground) release_object( raw_msg.foreground );
2032 return;
2035 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
2037 msg->win = get_user_full_handle( win );
2038 msg->msg = input->hw.msg;
2039 msg->wparam = 0;
2040 msg->lparam = input->hw.lparam;
2041 msg->x = desktop->cursor.x;
2042 msg->y = desktop->cursor.y;
2044 queue_hardware_message( desktop, msg, 1 );
2047 /* check message filter for a hardware message */
2048 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
2049 user_handle_t filter_win, unsigned int first, unsigned int last )
2051 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
2053 /* we can only test the window for a keyboard message since the
2054 * dest window for a mouse message depends on hittest */
2055 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
2056 return 0;
2057 /* the message code is final for a keyboard message, we can simply check it */
2058 return check_msg_filter( msg_code, first, last );
2060 else /* mouse message */
2062 /* we need to check all possible values that the message can have in the end */
2064 if (check_msg_filter( msg_code, first, last )) return 1;
2065 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
2067 /* all other messages can become non-client messages */
2068 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
2070 /* clicks can become double-clicks or non-client double-clicks */
2071 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
2072 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
2074 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2075 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2077 return 0;
2082 /* find a hardware message for the given queue */
2083 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
2084 unsigned int first, unsigned int last, unsigned int flags,
2085 struct get_message_reply *reply )
2087 struct thread_input *input = thread->queue->input;
2088 struct thread *win_thread;
2089 struct list *ptr;
2090 user_handle_t win;
2091 int clear_bits, got_one = 0;
2092 unsigned int msg_code;
2094 ptr = list_head( &input->msg_list );
2095 if (hw_id)
2097 while (ptr)
2099 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2100 if (msg->unique_id == hw_id) break;
2101 ptr = list_next( &input->msg_list, ptr );
2103 if (!ptr) ptr = list_head( &input->msg_list );
2104 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
2107 if (ptr == list_head( &input->msg_list ))
2108 clear_bits = QS_INPUT;
2109 else
2110 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
2112 while (ptr)
2114 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2115 struct hardware_msg_data *data = msg->data;
2117 ptr = list_next( &input->msg_list, ptr );
2118 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
2119 if (!win || !win_thread)
2121 /* no window at all, remove it */
2122 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2123 list_remove( &msg->entry );
2124 free_message( msg );
2125 continue;
2127 if (win_thread != thread)
2129 if (win_thread->queue->input == input)
2131 /* wake the other thread */
2132 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
2133 got_one = 1;
2135 else
2137 /* for another thread input, drop it */
2138 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2139 list_remove( &msg->entry );
2140 free_message( msg );
2142 release_object( win_thread );
2143 continue;
2145 release_object( win_thread );
2147 /* if we already got a message for another thread, or if it doesn't
2148 * match the filter we skip it */
2149 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
2151 clear_bits &= ~get_hardware_msg_bit( msg );
2152 continue;
2154 /* now we can return it */
2155 if (!msg->unique_id) msg->unique_id = get_unique_id();
2156 reply->type = MSG_HARDWARE;
2157 reply->win = win;
2158 reply->msg = msg_code;
2159 reply->wparam = msg->wparam;
2160 reply->lparam = msg->lparam;
2161 reply->x = msg->x;
2162 reply->y = msg->y;
2163 reply->time = msg->time;
2165 data->hw_id = msg->unique_id;
2166 set_reply_data( msg->data, msg->data_size );
2167 if ((msg->msg == WM_INPUT || msg->msg == WM_INPUT_DEVICE_CHANGE) && (flags & PM_REMOVE))
2168 release_hardware_message( current->queue, data->hw_id );
2169 return 1;
2171 /* nothing found, clear the hardware queue bits */
2172 clear_queue_bits( thread->queue, clear_bits );
2173 return 0;
2176 /* increment (or decrement if 'incr' is negative) the queue paint count */
2177 void inc_queue_paint_count( struct thread *thread, int incr )
2179 struct msg_queue *queue = thread->queue;
2181 assert( queue );
2183 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2185 if (queue->paint_count)
2186 set_queue_bits( queue, QS_PAINT );
2187 else
2188 clear_queue_bits( queue, QS_PAINT );
2192 /* remove all messages and timers belonging to a certain window */
2193 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2195 struct msg_queue *queue = thread->queue;
2196 struct list *ptr;
2197 int i;
2199 if (!queue) return;
2201 /* remove timers */
2203 ptr = list_head( &queue->pending_timers );
2204 while (ptr)
2206 struct list *next = list_next( &queue->pending_timers, ptr );
2207 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2208 if (timer->win == win) free_timer( queue, timer );
2209 ptr = next;
2211 ptr = list_head( &queue->expired_timers );
2212 while (ptr)
2214 struct list *next = list_next( &queue->expired_timers, ptr );
2215 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2216 if (timer->win == win) free_timer( queue, timer );
2217 ptr = next;
2220 /* remove messages */
2221 for (i = 0; i < NB_MSG_KINDS; i++)
2223 struct list *ptr, *next;
2225 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2227 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2228 if (msg->win == win)
2230 if (msg->msg == WM_QUIT && !queue->quit_message)
2232 queue->quit_message = 1;
2233 queue->exit_code = msg->wparam;
2235 remove_queue_message( queue, msg, i );
2240 thread_input_cleanup_window( queue, win );
2243 /* post a message to a window */
2244 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2246 struct message *msg;
2247 struct thread *thread = get_window_thread( win );
2249 if (!thread) return;
2251 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2253 msg->type = MSG_POSTED;
2254 msg->win = get_user_full_handle( win );
2255 msg->msg = message;
2256 msg->wparam = wparam;
2257 msg->lparam = lparam;
2258 msg->result = NULL;
2259 msg->data = NULL;
2260 msg->data_size = 0;
2262 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2264 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2265 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2266 if (message == WM_HOTKEY)
2268 set_queue_bits( thread->queue, QS_HOTKEY );
2269 thread->queue->hotkey_count++;
2272 release_object( thread );
2275 /* send a notify message to a window */
2276 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2278 struct message *msg;
2279 struct thread *thread = get_window_thread( win );
2281 if (!thread) return;
2283 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2285 msg->type = MSG_NOTIFY;
2286 msg->win = get_user_full_handle( win );
2287 msg->msg = message;
2288 msg->wparam = wparam;
2289 msg->lparam = lparam;
2290 msg->result = NULL;
2291 msg->data = NULL;
2292 msg->data_size = 0;
2294 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2296 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2297 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2299 release_object( thread );
2302 /* post a win event */
2303 void post_win_event( struct thread *thread, unsigned int event,
2304 user_handle_t win, unsigned int object_id,
2305 unsigned int child_id, client_ptr_t hook_proc,
2306 const WCHAR *module, data_size_t module_size,
2307 user_handle_t hook)
2309 struct message *msg;
2311 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2313 struct winevent_msg_data *data;
2315 msg->type = MSG_WINEVENT;
2316 msg->win = get_user_full_handle( win );
2317 msg->msg = event;
2318 msg->wparam = object_id;
2319 msg->lparam = child_id;
2320 msg->time = get_tick_count();
2321 msg->result = NULL;
2323 if ((data = malloc( sizeof(*data) + module_size )))
2325 data->hook = hook;
2326 data->tid = get_thread_id( current );
2327 data->hook_proc = hook_proc;
2328 memcpy( data + 1, module, module_size );
2330 msg->data = data;
2331 msg->data_size = sizeof(*data) + module_size;
2333 if (debug_level > 1)
2334 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2335 get_thread_id(thread), event, win, object_id, child_id );
2336 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2337 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2339 else
2340 free( msg );
2344 /* free all hotkeys on a desktop, optionally filtering by window */
2345 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2347 struct hotkey *hotkey, *hotkey2;
2349 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2351 if (!window || hotkey->win == window)
2353 list_remove( &hotkey->entry );
2354 free( hotkey );
2360 /* check if the thread owning the window is hung */
2361 DECL_HANDLER(is_window_hung)
2363 struct thread *thread;
2365 thread = get_window_thread( req->win );
2366 if (thread)
2368 reply->is_hung = is_queue_hung( thread->queue );
2369 release_object( thread );
2371 else reply->is_hung = 0;
2375 /* get the message queue of the current thread */
2376 DECL_HANDLER(get_msg_queue)
2378 struct msg_queue *queue = get_current_queue();
2380 reply->handle = 0;
2381 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2385 /* set the file descriptor associated to the current thread queue */
2386 DECL_HANDLER(set_queue_fd)
2388 struct msg_queue *queue = get_current_queue();
2389 struct file *file;
2390 int unix_fd;
2392 if (queue->fd) /* fd can only be set once */
2394 set_error( STATUS_ACCESS_DENIED );
2395 return;
2397 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2399 if ((unix_fd = get_file_unix_fd( file )) != -1)
2401 if ((unix_fd = dup( unix_fd )) != -1)
2402 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2403 else
2404 file_set_error();
2406 release_object( file );
2410 /* set the current message queue wakeup mask */
2411 DECL_HANDLER(set_queue_mask)
2413 struct msg_queue *queue = get_current_queue();
2415 if (queue)
2417 queue->wake_mask = req->wake_mask;
2418 queue->changed_mask = req->changed_mask;
2419 reply->wake_bits = queue->wake_bits;
2420 reply->changed_bits = queue->changed_bits;
2421 if (is_signaled( queue ))
2423 /* if skip wait is set, do what would have been done in the subsequent wait */
2424 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2425 else wake_up( &queue->obj, 0 );
2431 /* get the current message queue status */
2432 DECL_HANDLER(get_queue_status)
2434 struct msg_queue *queue = current->queue;
2435 if (queue)
2437 reply->wake_bits = queue->wake_bits;
2438 reply->changed_bits = queue->changed_bits;
2439 queue->changed_bits &= ~req->clear_bits;
2441 else reply->wake_bits = reply->changed_bits = 0;
2445 /* send a message to a thread queue */
2446 DECL_HANDLER(send_message)
2448 struct message *msg;
2449 struct msg_queue *send_queue = get_current_queue();
2450 struct msg_queue *recv_queue = NULL;
2451 struct thread *thread = NULL;
2453 if (!(thread = get_thread_from_id( req->id ))) return;
2455 if (!(recv_queue = thread->queue))
2457 set_error( STATUS_INVALID_PARAMETER );
2458 release_object( thread );
2459 return;
2461 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2463 set_error( STATUS_TIMEOUT );
2464 release_object( thread );
2465 return;
2468 if ((msg = mem_alloc( sizeof(*msg) )))
2470 msg->type = req->type;
2471 msg->win = get_user_full_handle( req->win );
2472 msg->msg = req->msg;
2473 msg->wparam = req->wparam;
2474 msg->lparam = req->lparam;
2475 msg->result = NULL;
2476 msg->data = NULL;
2477 msg->data_size = get_req_data_size();
2479 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2481 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2483 free( msg );
2484 release_object( thread );
2485 return;
2488 switch(msg->type)
2490 case MSG_OTHER_PROCESS:
2491 case MSG_ASCII:
2492 case MSG_UNICODE:
2493 case MSG_CALLBACK:
2494 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2496 free_message( msg );
2497 break;
2499 /* fall through */
2500 case MSG_NOTIFY:
2501 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2502 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2503 break;
2504 case MSG_POSTED:
2505 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2506 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2507 if (msg->msg == WM_HOTKEY)
2509 set_queue_bits( recv_queue, QS_HOTKEY );
2510 recv_queue->hotkey_count++;
2512 break;
2513 case MSG_HARDWARE: /* should use send_hardware_message instead */
2514 case MSG_CALLBACK_RESULT: /* cannot send this one */
2515 case MSG_HOOK_LL: /* generated internally */
2516 default:
2517 set_error( STATUS_INVALID_PARAMETER );
2518 free( msg );
2519 break;
2522 release_object( thread );
2525 /* send a hardware message to a thread queue */
2526 DECL_HANDLER(send_hardware_message)
2528 struct thread *thread = NULL;
2529 struct desktop *desktop;
2530 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2531 struct msg_queue *sender = get_current_queue();
2532 data_size_t size = min( 256, get_reply_max_size() );
2534 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2536 if (req->win)
2538 if (!(thread = get_window_thread( req->win ))) return;
2539 if (desktop != thread->queue->input->desktop)
2541 /* don't allow queuing events to a different desktop */
2542 release_object( desktop );
2543 return;
2547 reply->prev_x = desktop->cursor.x;
2548 reply->prev_y = desktop->cursor.y;
2550 switch (req->input.type)
2552 case INPUT_MOUSE:
2553 reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2554 break;
2555 case INPUT_KEYBOARD:
2556 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2557 break;
2558 case INPUT_HARDWARE:
2559 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2560 break;
2561 default:
2562 set_error( STATUS_INVALID_PARAMETER );
2564 if (thread) release_object( thread );
2566 reply->new_x = desktop->cursor.x;
2567 reply->new_y = desktop->cursor.y;
2568 set_reply_data( desktop->keystate, size );
2569 release_object( desktop );
2572 /* post a quit message to the current queue */
2573 DECL_HANDLER(post_quit_message)
2575 struct msg_queue *queue = get_current_queue();
2577 if (!queue)
2578 return;
2580 queue->quit_message = 1;
2581 queue->exit_code = req->exit_code;
2582 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2585 /* get a message from the current queue */
2586 DECL_HANDLER(get_message)
2588 struct timer *timer;
2589 struct list *ptr;
2590 struct msg_queue *queue = get_current_queue();
2591 user_handle_t get_win = get_user_full_handle( req->get_win );
2592 unsigned int filter = req->flags >> 16;
2594 reply->active_hooks = get_active_hooks();
2596 if (get_win && get_win != 1 && get_win != -1 && !get_user_object( get_win, USER_WINDOW ))
2598 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2599 return;
2602 if (!queue) return;
2603 queue->last_get_msg = current_time;
2604 if (!filter) filter = QS_ALLINPUT;
2606 /* first check for sent messages */
2607 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2609 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2610 receive_message( queue, msg, reply );
2611 return;
2614 /* clear changed bits so we can wait on them if we don't find a message */
2615 if (filter & QS_POSTMESSAGE)
2617 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2618 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2620 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2621 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2623 /* then check for posted messages */
2624 if ((filter & QS_POSTMESSAGE) &&
2625 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2626 return;
2628 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2629 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2630 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2631 return;
2633 /* only check for quit messages if not posted messages pending */
2634 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2635 return;
2637 /* then check for any raw hardware message */
2638 if ((filter & QS_INPUT) &&
2639 filter_contains_hw_range( req->get_first, req->get_last ) &&
2640 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2641 return;
2643 /* now check for WM_PAINT */
2644 if ((filter & QS_PAINT) &&
2645 queue->paint_count &&
2646 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2647 (reply->win = find_window_to_repaint( get_win, current )))
2649 reply->type = MSG_POSTED;
2650 reply->msg = WM_PAINT;
2651 reply->wparam = 0;
2652 reply->lparam = 0;
2653 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2654 return;
2657 /* now check for timer */
2658 if ((filter & QS_TIMER) &&
2659 (timer = find_expired_timer( queue, get_win, req->get_first,
2660 req->get_last, (req->flags & PM_REMOVE) )))
2662 reply->type = MSG_POSTED;
2663 reply->win = timer->win;
2664 reply->msg = timer->msg;
2665 reply->wparam = timer->id;
2666 reply->lparam = timer->lparam;
2667 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2668 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2669 set_event( current->process->idle_event );
2670 return;
2673 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2674 queue->wake_mask = req->wake_mask;
2675 queue->changed_mask = req->changed_mask;
2676 set_error( STATUS_PENDING ); /* FIXME */
2680 /* reply to a sent message */
2681 DECL_HANDLER(reply_message)
2683 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2684 else if (current->queue->recv_result)
2685 reply_message( current->queue, req->result, 0, req->remove,
2686 get_req_data(), get_req_data_size() );
2690 /* accept the current hardware message */
2691 DECL_HANDLER(accept_hardware_message)
2693 if (current->queue)
2694 release_hardware_message( current->queue, req->hw_id );
2695 else
2696 set_error( STATUS_ACCESS_DENIED );
2700 /* retrieve the reply for the last message sent */
2701 DECL_HANDLER(get_message_reply)
2703 struct message_result *result;
2704 struct list *entry;
2705 struct msg_queue *queue = current->queue;
2707 if (queue)
2709 set_error( STATUS_PENDING );
2710 reply->result = 0;
2712 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2714 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2715 if (result->replied || req->cancel)
2717 if (result->replied)
2719 reply->result = result->result;
2720 set_error( result->error );
2721 if (result->data)
2723 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2724 set_reply_data_ptr( result->data, data_len );
2725 result->data = NULL;
2726 result->data_size = 0;
2729 remove_result_from_sender( result );
2731 entry = list_head( &queue->send_result );
2732 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2733 else
2735 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2736 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2737 else clear_queue_bits( queue, QS_SMRESULT );
2741 else set_error( STATUS_ACCESS_DENIED );
2745 /* set a window timer */
2746 DECL_HANDLER(set_win_timer)
2748 struct timer *timer;
2749 struct msg_queue *queue;
2750 struct thread *thread = NULL;
2751 user_handle_t win = 0;
2752 lparam_t id = req->id;
2754 if (req->win)
2756 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2758 set_error( STATUS_INVALID_HANDLE );
2759 return;
2761 if (thread->process != current->process)
2763 release_object( thread );
2764 set_error( STATUS_ACCESS_DENIED );
2765 return;
2767 queue = thread->queue;
2768 /* remove it if it existed already */
2769 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2771 else
2773 queue = get_current_queue();
2774 /* look for a timer with this id */
2775 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2777 /* free and reuse id */
2778 free_timer( queue, timer );
2780 else
2782 lparam_t end_id = queue->next_timer_id;
2784 /* find a free id for it */
2785 while (1)
2787 id = queue->next_timer_id;
2788 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2790 if (!find_timer( queue, 0, req->msg, id )) break;
2791 if (queue->next_timer_id == end_id)
2793 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2794 return;
2800 if ((timer = set_timer( queue, req->rate )))
2802 timer->win = win;
2803 timer->msg = req->msg;
2804 timer->id = id;
2805 timer->lparam = req->lparam;
2806 reply->id = id;
2808 if (thread) release_object( thread );
2811 /* kill a window timer */
2812 DECL_HANDLER(kill_win_timer)
2814 struct timer *timer;
2815 struct thread *thread;
2816 user_handle_t win = 0;
2818 if (req->win)
2820 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2822 set_error( STATUS_INVALID_HANDLE );
2823 return;
2825 if (thread->process != current->process)
2827 release_object( thread );
2828 set_error( STATUS_ACCESS_DENIED );
2829 return;
2832 else thread = (struct thread *)grab_object( current );
2834 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2835 free_timer( thread->queue, timer );
2836 else
2837 set_error( STATUS_INVALID_PARAMETER );
2839 release_object( thread );
2842 DECL_HANDLER(register_hotkey)
2844 struct desktop *desktop;
2845 user_handle_t win_handle = req->window;
2846 struct hotkey *hotkey;
2847 struct hotkey *new_hotkey = NULL;
2848 struct thread *thread;
2849 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2851 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2853 if (win_handle)
2855 if (!(win_handle = get_valid_window_handle( win_handle )))
2857 release_object( desktop );
2858 return;
2861 thread = get_window_thread( win_handle );
2862 if (thread) release_object( thread );
2864 if (thread != current)
2866 release_object( desktop );
2867 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2868 return;
2872 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2874 if (req->vkey == hotkey->vkey &&
2875 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2877 release_object( desktop );
2878 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2879 return;
2881 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2882 new_hotkey = hotkey;
2885 if (new_hotkey)
2887 reply->replaced = 1;
2888 reply->flags = new_hotkey->flags;
2889 reply->vkey = new_hotkey->vkey;
2891 else
2893 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2894 if (new_hotkey)
2896 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2897 new_hotkey->queue = current->queue;
2898 new_hotkey->win = win_handle;
2899 new_hotkey->id = req->id;
2903 if (new_hotkey)
2905 new_hotkey->flags = req->flags;
2906 new_hotkey->vkey = req->vkey;
2909 release_object( desktop );
2912 DECL_HANDLER(unregister_hotkey)
2914 struct desktop *desktop;
2915 user_handle_t win_handle = req->window;
2916 struct hotkey *hotkey;
2917 struct thread *thread;
2919 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2921 if (win_handle)
2923 if (!(win_handle = get_valid_window_handle( win_handle )))
2925 release_object( desktop );
2926 return;
2929 thread = get_window_thread( win_handle );
2930 if (thread) release_object( thread );
2932 if (thread != current)
2934 release_object( desktop );
2935 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2936 return;
2940 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2942 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2943 goto found;
2946 release_object( desktop );
2947 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2948 return;
2950 found:
2951 reply->flags = hotkey->flags;
2952 reply->vkey = hotkey->vkey;
2953 list_remove( &hotkey->entry );
2954 free( hotkey );
2955 release_object( desktop );
2958 /* attach (or detach) thread inputs */
2959 DECL_HANDLER(attach_thread_input)
2961 struct thread *thread_from = get_thread_from_id( req->tid_from );
2962 struct thread *thread_to = get_thread_from_id( req->tid_to );
2964 if (!thread_from || !thread_to)
2966 if (thread_from) release_object( thread_from );
2967 if (thread_to) release_object( thread_to );
2968 return;
2970 if (thread_from != thread_to)
2972 if (req->attach)
2974 if ((thread_to->queue || thread_to == current) &&
2975 (thread_from->queue || thread_from == current))
2976 attach_thread_input( thread_from, thread_to );
2977 else
2978 set_error( STATUS_INVALID_PARAMETER );
2980 else
2982 if (thread_from->queue && thread_to->queue &&
2983 thread_from->queue->input == thread_to->queue->input)
2984 detach_thread_input( thread_from );
2985 else
2986 set_error( STATUS_ACCESS_DENIED );
2989 else set_error( STATUS_ACCESS_DENIED );
2990 release_object( thread_from );
2991 release_object( thread_to );
2995 /* get thread input data */
2996 DECL_HANDLER(get_thread_input)
2998 struct thread *thread = NULL;
2999 struct desktop *desktop;
3000 struct thread_input *input;
3002 if (req->tid)
3004 if (!(thread = get_thread_from_id( req->tid ))) return;
3005 if (!(desktop = get_thread_desktop( thread, 0 )))
3007 release_object( thread );
3008 return;
3010 input = thread->queue ? thread->queue->input : NULL;
3012 else
3014 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3015 input = desktop->foreground_input; /* get the foreground thread info */
3018 if (input)
3020 reply->focus = input->focus;
3021 reply->capture = input->capture;
3022 reply->active = input->active;
3023 reply->menu_owner = input->menu_owner;
3024 reply->move_size = input->move_size;
3025 reply->caret = input->caret;
3026 reply->cursor = input->cursor;
3027 reply->show_count = input->cursor_count;
3028 reply->rect = input->caret_rect;
3031 /* foreground window is active window of foreground thread */
3032 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
3033 if (thread) release_object( thread );
3034 release_object( desktop );
3038 /* retrieve queue keyboard state for current thread or global async state */
3039 DECL_HANDLER(get_key_state)
3041 struct desktop *desktop;
3042 data_size_t size = min( 256, get_reply_max_size() );
3044 if (req->async) /* get global async key state */
3046 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3047 if (req->key >= 0)
3049 reply->state = desktop->keystate[req->key & 0xff];
3050 desktop->keystate[req->key & 0xff] &= ~0x40;
3052 set_reply_data( desktop->keystate, size );
3053 release_object( desktop );
3055 else if (!current->queue)
3057 unsigned char *keystate;
3058 /* fallback to desktop keystate */
3059 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3060 if (req->key >= 0) reply->state = desktop->keystate[req->key & 0xff] & ~0x40;
3061 if ((keystate = set_reply_data_size( size )))
3063 unsigned int i;
3064 for (i = 0; i < size; i++) keystate[i] = desktop->keystate[i] & ~0x40;
3066 release_object( desktop );
3068 else
3070 unsigned char *keystate = current->queue->input->keystate;
3071 if (req->key >= 0) reply->state = keystate[req->key & 0xff];
3072 set_reply_data( keystate, size );
3077 /* set queue keyboard state for current thread */
3078 DECL_HANDLER(set_key_state)
3080 struct desktop *desktop;
3081 data_size_t size = min( 256, get_req_data_size() );
3083 if (current->queue) memcpy( current->queue->input->keystate, get_req_data(), size );
3084 if (req->async && (desktop = get_thread_desktop( current, 0 )))
3086 memcpy( desktop->keystate, get_req_data(), size );
3087 release_object( desktop );
3092 /* set the system foreground window */
3093 DECL_HANDLER(set_foreground_window)
3095 struct thread *thread = NULL;
3096 struct desktop *desktop;
3097 struct msg_queue *queue = get_current_queue();
3099 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3100 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
3101 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
3102 reply->send_msg_new = FALSE;
3104 if (is_valid_foreground_window( req->handle ) &&
3105 (thread = get_window_thread( req->handle )) &&
3106 thread->queue->input->desktop == desktop)
3108 set_foreground_input( desktop, thread->queue->input );
3109 reply->send_msg_new = (desktop->foreground_input != queue->input);
3111 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
3113 if (thread) release_object( thread );
3114 release_object( desktop );
3118 /* set the current thread focus window */
3119 DECL_HANDLER(set_focus_window)
3121 struct msg_queue *queue = get_current_queue();
3123 reply->previous = 0;
3124 if (queue && check_queue_input_window( queue, req->handle ))
3126 reply->previous = queue->input->focus;
3127 queue->input->focus = get_user_full_handle( req->handle );
3132 /* set the current thread active window */
3133 DECL_HANDLER(set_active_window)
3135 struct msg_queue *queue = get_current_queue();
3137 reply->previous = 0;
3138 if (queue && check_queue_input_window( queue, req->handle ))
3140 if (!req->handle || make_window_active( req->handle ))
3142 reply->previous = queue->input->active;
3143 queue->input->active = get_user_full_handle( req->handle );
3145 else set_error( STATUS_INVALID_HANDLE );
3150 /* set the current thread capture window */
3151 DECL_HANDLER(set_capture_window)
3153 struct msg_queue *queue = get_current_queue();
3155 reply->previous = reply->full_handle = 0;
3156 if (queue && check_queue_input_window( queue, req->handle ))
3158 struct thread_input *input = queue->input;
3160 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
3161 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
3163 set_error(STATUS_ACCESS_DENIED);
3164 return;
3166 reply->previous = input->capture;
3167 input->capture = get_user_full_handle( req->handle );
3168 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
3169 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
3170 reply->full_handle = input->capture;
3175 /* Set the current thread caret window */
3176 DECL_HANDLER(set_caret_window)
3178 struct msg_queue *queue = get_current_queue();
3180 reply->previous = 0;
3181 if (queue && check_queue_input_window( queue, req->handle ))
3183 struct thread_input *input = queue->input;
3185 reply->previous = input->caret;
3186 reply->old_rect = input->caret_rect;
3187 reply->old_hide = input->caret_hide;
3188 reply->old_state = input->caret_state;
3190 set_caret_window( input, get_user_full_handle(req->handle) );
3191 input->caret_rect.right = input->caret_rect.left + req->width;
3192 input->caret_rect.bottom = input->caret_rect.top + req->height;
3197 /* Set the current thread caret information */
3198 DECL_HANDLER(set_caret_info)
3200 struct msg_queue *queue = get_current_queue();
3201 struct thread_input *input;
3203 if (!queue) return;
3204 input = queue->input;
3205 reply->full_handle = input->caret;
3206 reply->old_rect = input->caret_rect;
3207 reply->old_hide = input->caret_hide;
3208 reply->old_state = input->caret_state;
3210 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3212 set_error( STATUS_ACCESS_DENIED );
3213 return;
3215 if (req->flags & SET_CARET_POS)
3217 input->caret_rect.right += req->x - input->caret_rect.left;
3218 input->caret_rect.bottom += req->y - input->caret_rect.top;
3219 input->caret_rect.left = req->x;
3220 input->caret_rect.top = req->y;
3222 if (req->flags & SET_CARET_HIDE)
3224 input->caret_hide += req->hide;
3225 if (input->caret_hide < 0) input->caret_hide = 0;
3227 if (req->flags & SET_CARET_STATE)
3229 switch (req->state)
3231 case CARET_STATE_OFF: input->caret_state = 0; break;
3232 case CARET_STATE_ON: input->caret_state = 1; break;
3233 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3234 case CARET_STATE_ON_IF_MOVED:
3235 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3236 break;
3242 /* get the time of the last input event */
3243 DECL_HANDLER(get_last_input_time)
3245 reply->time = last_input_time;
3248 /* set/get the current cursor */
3249 DECL_HANDLER(set_cursor)
3251 struct msg_queue *queue = get_current_queue();
3252 struct thread_input *input;
3254 if (!queue) return;
3255 input = queue->input;
3257 reply->prev_handle = input->cursor;
3258 reply->prev_count = input->cursor_count;
3259 reply->prev_x = input->desktop->cursor.x;
3260 reply->prev_y = input->desktop->cursor.y;
3262 if (req->flags & SET_CURSOR_HANDLE)
3264 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3266 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3267 return;
3269 input->cursor = req->handle;
3271 if (req->flags & SET_CURSOR_COUNT)
3273 queue->cursor_count += req->show_count;
3274 input->cursor_count += req->show_count;
3276 if (req->flags & SET_CURSOR_POS)
3278 set_cursor_pos( input->desktop, req->x, req->y );
3280 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3282 struct desktop *desktop = input->desktop;
3284 /* only the desktop owner can set the message */
3285 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3286 desktop->cursor.clip_msg = req->clip_msg;
3288 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip, 0 );
3291 reply->new_x = input->desktop->cursor.x;
3292 reply->new_y = input->desktop->cursor.y;
3293 reply->new_clip = input->desktop->cursor.clip;
3294 reply->last_change = input->desktop->cursor.last_change;
3297 /* Get the history of the 64 last cursor positions */
3298 DECL_HANDLER(get_cursor_history)
3300 cursor_pos_t *pos;
3301 unsigned int i, count = min( 64, get_reply_max_size() / sizeof(*pos) );
3303 if ((pos = set_reply_data_size( count * sizeof(*pos) )))
3304 for (i = 0; i < count; i++)
3305 pos[i] = cursor_history[(i + cursor_history_latest) % ARRAY_SIZE(cursor_history)];
3308 DECL_HANDLER(get_rawinput_buffer)
3310 struct thread_input *input = current->queue->input;
3311 data_size_t size = 0, next_size = 0;
3312 struct list *ptr;
3313 char *buf, *cur, *tmp;
3314 int count = 0, buf_size = 16 * sizeof(struct hardware_msg_data);
3316 if (!req->buffer_size) buf = NULL;
3317 else if (!(buf = mem_alloc( buf_size ))) return;
3319 cur = buf;
3320 ptr = list_head( &input->msg_list );
3321 while (ptr)
3323 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
3324 struct hardware_msg_data *data = msg->data;
3325 data_size_t extra_size = data->size - sizeof(*data);
3327 ptr = list_next( &input->msg_list, ptr );
3328 if (msg->msg != WM_INPUT) continue;
3330 next_size = req->rawinput_size + extra_size;
3331 if (size + next_size > req->buffer_size) break;
3332 if (cur + data->size > buf + get_reply_max_size()) break;
3333 if (cur + data->size > buf + buf_size)
3335 buf_size += buf_size / 2 + extra_size;
3336 if (!(tmp = realloc( buf, buf_size )))
3338 set_error( STATUS_NO_MEMORY );
3339 return;
3341 cur = tmp + (cur - buf);
3342 buf = tmp;
3345 memcpy( cur, data, data->size );
3346 list_remove( &msg->entry );
3347 free_message( msg );
3349 size += next_size;
3350 cur += sizeof(*data);
3351 count++;
3354 reply->next_size = next_size;
3355 reply->count = count;
3356 set_reply_data_ptr( buf, cur - buf );
3359 DECL_HANDLER(update_rawinput_devices)
3361 const struct rawinput_device *devices = get_req_data();
3362 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3363 const struct rawinput_device_entry *e;
3364 unsigned int i;
3366 for (i = 0; i < device_count; ++i)
3368 update_rawinput_device(&devices[i]);
3371 e = find_rawinput_device( current->process, 1, 2 );
3372 current->process->rawinput_mouse = e ? &e->device : NULL;
3373 e = find_rawinput_device( current->process, 1, 6 );
3374 current->process->rawinput_kbd = e ? &e->device : NULL;
3377 DECL_HANDLER(get_rawinput_devices)
3379 struct rawinput_device_entry *e;
3380 struct rawinput_device *devices;
3381 unsigned int i = 0, device_count = list_count( &current->process->rawinput_devices );
3382 unsigned int size = device_count * sizeof(*devices);
3384 reply->device_count = device_count;
3386 /* no buffer provided, nothing else to do */
3387 if (!get_reply_max_size()) return;
3389 if (size > get_reply_max_size())
3390 set_error( STATUS_BUFFER_TOO_SMALL );
3391 else if ((devices = set_reply_data_size( size )))
3393 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
3394 devices[i++] = e->device;