riched20: Use wide-char string literals.
[wine.git] / server / queue.c
blob7e7e6fbdf293009724162c94b227c5d25fe9822f
1 /*
2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #ifdef HAVE_POLL_H
29 # include <poll.h>
30 #endif
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "winternl.h"
40 #include "handle.h"
41 #include "file.h"
42 #include "thread.h"
43 #include "process.h"
44 #include "request.h"
45 #include "user.h"
47 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
48 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
50 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
51 #define NB_MSG_KINDS (POST_MESSAGE+1)
54 struct message_result
56 struct list sender_entry; /* entry in sender list */
57 struct message *msg; /* message the result is for */
58 struct message_result *recv_next; /* next in receiver list */
59 struct msg_queue *sender; /* sender queue */
60 struct msg_queue *receiver; /* receiver queue */
61 int replied; /* has it been replied to? */
62 unsigned int error; /* error code to pass back to sender */
63 lparam_t result; /* reply result */
64 struct message *hardware_msg; /* hardware message if low-level hook result */
65 struct desktop *desktop; /* desktop for hardware message */
66 struct message *callback_msg; /* message to queue for callback */
67 void *data; /* message reply data */
68 unsigned int data_size; /* size of message reply data */
69 struct timeout_user *timeout; /* result timeout */
72 struct message
74 struct list entry; /* entry in message list */
75 enum message_type type; /* message type */
76 user_handle_t win; /* window handle */
77 unsigned int msg; /* message code */
78 lparam_t wparam; /* parameters */
79 lparam_t lparam; /* parameters */
80 int x; /* message position */
81 int y;
82 unsigned int time; /* message time */
83 void *data; /* message data for sent messages */
84 unsigned int data_size; /* size of message data */
85 unsigned int unique_id; /* unique id for nested hw message waits */
86 struct message_result *result; /* result in sender queue */
89 struct timer
91 struct list entry; /* entry in timer list */
92 abstime_t when; /* next expiration */
93 unsigned int rate; /* timer rate in ms */
94 user_handle_t win; /* window handle */
95 unsigned int msg; /* message to post */
96 lparam_t id; /* timer id */
97 lparam_t lparam; /* lparam for message */
100 struct thread_input
102 struct object obj; /* object header */
103 struct desktop *desktop; /* desktop that this thread input belongs to */
104 user_handle_t focus; /* focus window */
105 user_handle_t capture; /* capture window */
106 user_handle_t active; /* active window */
107 user_handle_t menu_owner; /* current menu owner window */
108 user_handle_t move_size; /* current moving/resizing window */
109 user_handle_t caret; /* caret window */
110 rectangle_t caret_rect; /* caret rectangle */
111 int caret_hide; /* caret hide count */
112 int caret_state; /* caret on/off state */
113 user_handle_t cursor; /* current cursor */
114 int cursor_count; /* cursor show count */
115 struct list msg_list; /* list of hardware messages */
116 unsigned char keystate[256]; /* state of each key */
119 struct msg_queue
121 struct object obj; /* object header */
122 struct fd *fd; /* optional file descriptor to poll */
123 unsigned int wake_bits; /* wakeup bits */
124 unsigned int wake_mask; /* wakeup mask */
125 unsigned int changed_bits; /* changed wakeup bits */
126 unsigned int changed_mask; /* changed wakeup mask */
127 int paint_count; /* pending paint messages count */
128 int hotkey_count; /* pending hotkey messages count */
129 int quit_message; /* is there a pending quit message? */
130 int exit_code; /* exit code of pending quit message */
131 int cursor_count; /* per-queue cursor show count */
132 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
133 struct list send_result; /* stack of sent messages waiting for result */
134 struct list callback_result; /* list of callback messages waiting for result */
135 struct message_result *recv_result; /* stack of received messages waiting for result */
136 struct list pending_timers; /* list of pending timers */
137 struct list expired_timers; /* list of expired timers */
138 lparam_t next_timer_id; /* id for the next timer with a 0 window */
139 struct timeout_user *timeout; /* timeout for next timer to expire */
140 struct thread_input *input; /* thread input descriptor */
141 struct hook_table *hooks; /* hook table */
142 timeout_t last_get_msg; /* time of last get message call */
145 struct hotkey
147 struct list entry; /* entry in desktop hotkey list */
148 struct msg_queue *queue; /* queue owning this hotkey */
149 user_handle_t win; /* window handle */
150 int id; /* hotkey id */
151 unsigned int vkey; /* virtual key code */
152 unsigned int flags; /* key modifiers */
155 static void msg_queue_dump( struct object *obj, int verbose );
156 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
157 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
158 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
159 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
160 static void msg_queue_destroy( struct object *obj );
161 static void msg_queue_poll_event( struct fd *fd, int event );
162 static void thread_input_dump( struct object *obj, int verbose );
163 static void thread_input_destroy( struct object *obj );
164 static void timer_callback( void *private );
166 static const struct object_ops msg_queue_ops =
168 sizeof(struct msg_queue), /* size */
169 msg_queue_dump, /* dump */
170 no_get_type, /* get_type */
171 msg_queue_add_queue, /* add_queue */
172 msg_queue_remove_queue, /* remove_queue */
173 msg_queue_signaled, /* signaled */
174 msg_queue_satisfied, /* satisfied */
175 no_signal, /* signal */
176 no_get_fd, /* get_fd */
177 no_map_access, /* map_access */
178 default_get_sd, /* get_sd */
179 default_set_sd, /* set_sd */
180 no_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 thread_input_dump, /* dump */
207 no_get_type, /* get_type */
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 no_map_access, /* map_access */
215 default_get_sd, /* get_sd */
216 default_set_sd, /* set_sd */
217 no_get_full_name, /* get_full_name */
218 no_lookup_name, /* lookup_name */
219 no_link_name, /* link_name */
220 NULL, /* unlink_name */
221 no_open_file, /* open_file */
222 no_kernel_obj_list, /* get_kernel_obj_list */
223 no_close_handle, /* close_handle */
224 thread_input_destroy /* destroy */
227 /* pointer to input structure of foreground thread */
228 static unsigned int last_input_time;
230 static cursor_pos_t cursor_history[64];
231 static unsigned int cursor_history_latest;
233 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
234 static void free_message( struct message *msg );
236 /* set the caret window in a given thread input */
237 static void set_caret_window( struct thread_input *input, user_handle_t win )
239 if (!win || win != input->caret)
241 input->caret_rect.left = 0;
242 input->caret_rect.top = 0;
243 input->caret_rect.right = 0;
244 input->caret_rect.bottom = 0;
246 input->caret = win;
247 input->caret_hide = 1;
248 input->caret_state = 0;
251 /* create a thread input object */
252 static struct thread_input *create_thread_input( struct thread *thread )
254 struct thread_input *input;
256 if ((input = alloc_object( &thread_input_ops )))
258 input->focus = 0;
259 input->capture = 0;
260 input->active = 0;
261 input->menu_owner = 0;
262 input->move_size = 0;
263 input->cursor = 0;
264 input->cursor_count = 0;
265 list_init( &input->msg_list );
266 set_caret_window( input, 0 );
267 memset( input->keystate, 0, sizeof(input->keystate) );
269 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
271 release_object( input );
272 return NULL;
275 return input;
278 /* create a message queue object */
279 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
281 struct thread_input *new_input = NULL;
282 struct msg_queue *queue;
283 int i;
285 if (!input)
287 if (!(new_input = create_thread_input( thread ))) return NULL;
288 input = new_input;
291 if ((queue = alloc_object( &msg_queue_ops )))
293 queue->fd = NULL;
294 queue->wake_bits = 0;
295 queue->wake_mask = 0;
296 queue->changed_bits = 0;
297 queue->changed_mask = 0;
298 queue->paint_count = 0;
299 queue->hotkey_count = 0;
300 queue->quit_message = 0;
301 queue->cursor_count = 0;
302 queue->recv_result = NULL;
303 queue->next_timer_id = 0x7fff;
304 queue->timeout = NULL;
305 queue->input = (struct thread_input *)grab_object( input );
306 queue->hooks = NULL;
307 queue->last_get_msg = current_time;
308 list_init( &queue->send_result );
309 list_init( &queue->callback_result );
310 list_init( &queue->pending_timers );
311 list_init( &queue->expired_timers );
312 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
314 thread->queue = queue;
316 if (new_input) release_object( new_input );
317 return queue;
320 /* free the message queue of a thread at thread exit */
321 void free_msg_queue( struct thread *thread )
323 remove_thread_hooks( thread );
324 if (!thread->queue) return;
325 release_object( thread->queue );
326 thread->queue = NULL;
329 /* change the thread input data of a given thread */
330 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
332 struct msg_queue *queue = thread->queue;
334 if (!queue)
336 thread->queue = create_msg_queue( thread, new_input );
337 return thread->queue != NULL;
339 if (queue->input)
341 queue->input->cursor_count -= queue->cursor_count;
342 release_object( queue->input );
344 queue->input = (struct thread_input *)grab_object( new_input );
345 new_input->cursor_count += queue->cursor_count;
346 return 1;
349 /* allocate a hardware message and its data */
350 static struct message *alloc_hardware_message( lparam_t info, struct hw_msg_source source,
351 unsigned int time )
353 struct hardware_msg_data *msg_data;
354 struct message *msg;
356 if (!(msg = mem_alloc( sizeof(*msg) ))) return NULL;
357 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
359 free( msg );
360 return NULL;
362 memset( msg, 0, sizeof(*msg) );
363 msg->type = MSG_HARDWARE;
364 msg->time = time;
365 msg->data = msg_data;
366 msg->data_size = sizeof(*msg_data);
368 memset( msg_data, 0, sizeof(*msg_data) );
369 msg_data->info = info;
370 msg_data->source = source;
371 return msg;
374 static int update_desktop_cursor_pos( struct desktop *desktop, int x, int y )
376 int updated;
378 x = max( min( x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
379 y = max( min( y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
380 updated = (desktop->cursor.x != x || desktop->cursor.y != y);
381 desktop->cursor.x = x;
382 desktop->cursor.y = y;
383 desktop->cursor.last_change = get_tick_count();
385 return updated;
388 /* set the cursor position and queue the corresponding mouse message */
389 static void set_cursor_pos( struct desktop *desktop, int x, int y )
391 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
392 const struct rawinput_device *device;
393 struct message *msg;
395 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
397 update_desktop_cursor_pos( desktop, x, y );
398 return;
401 if (!(msg = alloc_hardware_message( 0, source, get_tick_count() ))) return;
403 msg->msg = WM_MOUSEMOVE;
404 msg->x = x;
405 msg->y = y;
406 queue_hardware_message( desktop, msg, 1 );
409 /* retrieve default position and time for synthesized messages */
410 static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
412 struct desktop *desktop = queue->input->desktop;
414 *x = desktop->cursor.x;
415 *y = desktop->cursor.y;
416 *time = get_tick_count();
419 /* set the cursor clip rectangle */
420 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect, int send_clip_msg )
422 rectangle_t top_rect;
423 int x, y;
425 get_top_window_rectangle( desktop, &top_rect );
426 if (rect)
428 rectangle_t new_rect = *rect;
429 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
430 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
431 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
432 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
433 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
434 desktop->cursor.clip = new_rect;
436 else desktop->cursor.clip = top_rect;
438 if (desktop->cursor.clip_msg && send_clip_msg)
439 post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
441 /* warp the mouse to be inside the clip rect */
442 x = max( min( desktop->cursor.x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
443 y = max( min( desktop->cursor.y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
444 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
447 /* change the foreground input and reset the cursor clip rect */
448 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
450 if (desktop->foreground_input == input) return;
451 set_clip_rectangle( desktop, NULL, 1 );
452 desktop->foreground_input = input;
455 /* get the hook table for a given thread */
456 struct hook_table *get_queue_hooks( struct thread *thread )
458 if (!thread->queue) return NULL;
459 return thread->queue->hooks;
462 /* set the hook table for a given thread, allocating the queue if needed */
463 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
465 struct msg_queue *queue = thread->queue;
466 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
467 if (queue->hooks) release_object( queue->hooks );
468 queue->hooks = hooks;
471 /* check the queue status */
472 static inline int is_signaled( struct msg_queue *queue )
474 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
477 /* set some queue bits */
478 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
480 queue->wake_bits |= bits;
481 queue->changed_bits |= bits;
482 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
485 /* clear some queue bits */
486 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
488 queue->wake_bits &= ~bits;
489 queue->changed_bits &= ~bits;
492 /* check whether msg is a keyboard message */
493 static inline int is_keyboard_msg( struct message *msg )
495 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
498 /* check if message is matched by the filter */
499 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
501 return (msg >= first && msg <= last);
504 /* check whether a message filter contains at least one potential hardware message */
505 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
507 /* hardware message ranges are (in numerical order):
508 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
509 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
510 * WM_MOUSEFIRST .. WM_MOUSELAST
512 if (last < WM_NCMOUSEFIRST) return 0;
513 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
514 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
515 if (first > WM_MOUSELAST) return 0;
516 return 1;
519 /* get the QS_* bit corresponding to a given hardware message */
520 static inline int get_hardware_msg_bit( struct message *msg )
522 if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
523 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
524 if (is_keyboard_msg( msg )) return QS_KEY;
525 return QS_MOUSEBUTTON;
528 /* get the current thread queue, creating it if needed */
529 static inline struct msg_queue *get_current_queue(void)
531 struct msg_queue *queue = current->queue;
532 if (!queue) queue = create_msg_queue( current, NULL );
533 return queue;
536 /* get a (pseudo-)unique id to tag hardware messages */
537 static inline unsigned int get_unique_id(void)
539 static unsigned int id;
540 if (!++id) id = 1; /* avoid an id of 0 */
541 return id;
544 /* try to merge a message with the last in the list; return 1 if successful */
545 static int merge_message( struct thread_input *input, const struct message *msg )
547 struct message *prev;
548 struct list *ptr;
550 if (msg->msg != WM_MOUSEMOVE) return 0;
551 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
553 prev = LIST_ENTRY( ptr, struct message, entry );
554 if (prev->msg != WM_INPUT) break;
556 if (!ptr) return 0;
557 if (prev->result) return 0;
558 if (prev->win && msg->win && prev->win != msg->win) return 0;
559 if (prev->msg != msg->msg) return 0;
560 if (prev->type != msg->type) return 0;
561 /* now we can merge it */
562 prev->wparam = msg->wparam;
563 prev->lparam = msg->lparam;
564 prev->x = msg->x;
565 prev->y = msg->y;
566 prev->time = msg->time;
567 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
569 struct hardware_msg_data *prev_data = prev->data;
570 struct hardware_msg_data *msg_data = msg->data;
571 prev_data->info = msg_data->info;
573 list_remove( ptr );
574 list_add_tail( &input->msg_list, ptr );
575 return 1;
578 /* free a result structure */
579 static void free_result( struct message_result *result )
581 if (result->timeout) remove_timeout_user( result->timeout );
582 free( result->data );
583 if (result->callback_msg) free_message( result->callback_msg );
584 if (result->hardware_msg) free_message( result->hardware_msg );
585 if (result->desktop) release_object( result->desktop );
586 free( result );
589 /* remove the result from the sender list it is on */
590 static inline void remove_result_from_sender( struct message_result *result )
592 assert( result->sender );
594 list_remove( &result->sender_entry );
595 result->sender = NULL;
596 if (!result->receiver) free_result( result );
599 /* store the message result in the appropriate structure */
600 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
602 res->result = result;
603 res->error = error;
604 res->replied = 1;
605 if (res->timeout)
607 remove_timeout_user( res->timeout );
608 res->timeout = NULL;
611 if (res->hardware_msg)
613 if (!error && result) /* rejected by the hook */
614 free_message( res->hardware_msg );
615 else
616 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
618 res->hardware_msg = NULL;
621 if (res->sender)
623 if (res->callback_msg)
625 /* queue the callback message in the sender queue */
626 struct callback_msg_data *data = res->callback_msg->data;
627 data->result = result;
628 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
629 set_queue_bits( res->sender, QS_SENDMESSAGE );
630 res->callback_msg = NULL;
631 remove_result_from_sender( res );
633 else
635 /* wake sender queue if waiting on this result */
636 if (list_head(&res->sender->send_result) == &res->sender_entry)
637 set_queue_bits( res->sender, QS_SMRESULT );
640 else if (!res->receiver) free_result( res );
643 /* free a message when deleting a queue or window */
644 static void free_message( struct message *msg )
646 struct message_result *result = msg->result;
647 if (result)
649 result->msg = NULL;
650 result->receiver = NULL;
651 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
653 free( msg->data );
654 free( msg );
657 /* remove (and free) a message from a message list */
658 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
659 enum message_kind kind )
661 list_remove( &msg->entry );
662 switch(kind)
664 case SEND_MESSAGE:
665 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
666 break;
667 case POST_MESSAGE:
668 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
669 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
670 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
671 clear_queue_bits( queue, QS_HOTKEY );
672 break;
674 free_message( msg );
677 /* message timed out without getting a reply */
678 static void result_timeout( void *private )
680 struct message_result *result = private;
682 assert( !result->replied );
684 result->timeout = NULL;
686 if (result->msg) /* not received yet */
688 struct message *msg = result->msg;
690 result->msg = NULL;
691 msg->result = NULL;
692 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
693 result->receiver = NULL;
695 store_message_result( result, 0, STATUS_TIMEOUT );
698 /* allocate and fill a message result structure */
699 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
700 struct msg_queue *recv_queue,
701 struct message *msg, timeout_t timeout )
703 struct message_result *result = mem_alloc( sizeof(*result) );
704 if (result)
706 result->msg = msg;
707 result->sender = send_queue;
708 result->receiver = recv_queue;
709 result->replied = 0;
710 result->data = NULL;
711 result->data_size = 0;
712 result->timeout = NULL;
713 result->hardware_msg = NULL;
714 result->desktop = NULL;
715 result->callback_msg = NULL;
717 if (msg->type == MSG_CALLBACK)
719 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
721 if (!callback_msg)
723 free( result );
724 return NULL;
726 callback_msg->type = MSG_CALLBACK_RESULT;
727 callback_msg->win = msg->win;
728 callback_msg->msg = msg->msg;
729 callback_msg->wparam = 0;
730 callback_msg->lparam = 0;
731 callback_msg->time = get_tick_count();
732 callback_msg->result = NULL;
733 /* steal the data from the original message */
734 callback_msg->data = msg->data;
735 callback_msg->data_size = msg->data_size;
736 msg->data = NULL;
737 msg->data_size = 0;
739 result->callback_msg = callback_msg;
740 list_add_head( &send_queue->callback_result, &result->sender_entry );
742 else if (send_queue)
744 list_add_head( &send_queue->send_result, &result->sender_entry );
745 clear_queue_bits( send_queue, QS_SMRESULT );
748 if (timeout != TIMEOUT_INFINITE)
749 result->timeout = add_timeout_user( timeout, result_timeout, result );
751 return result;
754 /* receive a message, removing it from the sent queue */
755 static void receive_message( struct msg_queue *queue, struct message *msg,
756 struct get_message_reply *reply )
758 struct message_result *result = msg->result;
760 reply->total = msg->data_size;
761 if (msg->data_size > get_reply_max_size())
763 set_error( STATUS_BUFFER_OVERFLOW );
764 return;
766 reply->type = msg->type;
767 reply->win = msg->win;
768 reply->msg = msg->msg;
769 reply->wparam = msg->wparam;
770 reply->lparam = msg->lparam;
771 reply->x = msg->x;
772 reply->y = msg->y;
773 reply->time = msg->time;
775 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
777 list_remove( &msg->entry );
778 /* put the result on the receiver result stack */
779 if (result)
781 result->msg = NULL;
782 result->recv_next = queue->recv_result;
783 queue->recv_result = result;
785 free( msg );
786 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
789 /* set the result of the current received message */
790 static void reply_message( struct msg_queue *queue, lparam_t result,
791 unsigned int error, int remove, const void *data, data_size_t len )
793 struct message_result *res = queue->recv_result;
795 if (remove)
797 queue->recv_result = res->recv_next;
798 res->receiver = NULL;
799 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
801 free_result( res );
802 return;
805 if (!res->replied)
807 if (len && (res->data = memdup( data, len ))) res->data_size = len;
808 store_message_result( res, result, error );
812 static int match_window( user_handle_t win, user_handle_t msg_win )
814 if (!win) return 1;
815 if (win == -1 || win == 1) return !msg_win;
816 if (msg_win == win) return 1;
817 return is_child_window( win, msg_win );
820 /* retrieve a posted message */
821 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
822 unsigned int first, unsigned int last, unsigned int flags,
823 struct get_message_reply *reply )
825 struct message *msg;
827 /* check against the filters */
828 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
830 if (!match_window( win, msg->win )) continue;
831 if (!check_msg_filter( msg->msg, first, last )) continue;
832 goto found; /* found one */
834 return 0;
836 /* return it to the app */
837 found:
838 reply->total = msg->data_size;
839 if (msg->data_size > get_reply_max_size())
841 set_error( STATUS_BUFFER_OVERFLOW );
842 return 1;
844 reply->type = msg->type;
845 reply->win = msg->win;
846 reply->msg = msg->msg;
847 reply->wparam = msg->wparam;
848 reply->lparam = msg->lparam;
849 reply->x = msg->x;
850 reply->y = msg->y;
851 reply->time = msg->time;
853 if (flags & PM_REMOVE)
855 if (msg->data)
857 set_reply_data_ptr( msg->data, msg->data_size );
858 msg->data = NULL;
859 msg->data_size = 0;
861 remove_queue_message( queue, msg, POST_MESSAGE );
863 else if (msg->data) set_reply_data( msg->data, msg->data_size );
865 return 1;
868 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
869 struct get_message_reply *reply )
871 if (queue->quit_message)
873 reply->total = 0;
874 reply->type = MSG_POSTED;
875 reply->win = 0;
876 reply->msg = WM_QUIT;
877 reply->wparam = queue->exit_code;
878 reply->lparam = 0;
880 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
882 if (flags & PM_REMOVE)
884 queue->quit_message = 0;
885 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
886 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
888 return 1;
890 else
891 return 0;
894 /* empty a message list and free all the messages */
895 static void empty_msg_list( struct list *list )
897 struct list *ptr;
899 while ((ptr = list_head( list )) != NULL)
901 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
902 list_remove( &msg->entry );
903 free_message( msg );
907 /* cleanup all pending results when deleting a queue */
908 static void cleanup_results( struct msg_queue *queue )
910 struct list *entry;
912 while ((entry = list_head( &queue->send_result )) != NULL)
914 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
917 while ((entry = list_head( &queue->callback_result )) != NULL)
919 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
922 while (queue->recv_result)
923 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
926 /* check if the thread owning the queue is hung (not checking for messages) */
927 static int is_queue_hung( struct msg_queue *queue )
929 struct wait_queue_entry *entry;
931 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
932 return 0; /* less than 5 seconds since last get message -> not hung */
934 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
936 if (get_wait_queue_thread(entry)->queue == queue)
937 return 0; /* thread is waiting on queue -> not hung */
939 return 1;
942 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
944 struct msg_queue *queue = (struct msg_queue *)obj;
945 struct process *process = get_wait_queue_thread(entry)->process;
947 /* a thread can only wait on its own queue */
948 if (get_wait_queue_thread(entry)->queue != queue)
950 set_error( STATUS_ACCESS_DENIED );
951 return 0;
953 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
955 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
956 set_fd_events( queue->fd, POLLIN );
957 add_queue( obj, entry );
958 return 1;
961 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
963 struct msg_queue *queue = (struct msg_queue *)obj;
965 remove_queue( obj, entry );
966 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
967 set_fd_events( queue->fd, 0 );
970 static void msg_queue_dump( struct object *obj, int verbose )
972 struct msg_queue *queue = (struct msg_queue *)obj;
973 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
974 queue->wake_bits, queue->wake_mask );
977 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
979 struct msg_queue *queue = (struct msg_queue *)obj;
980 int ret = 0;
982 if (queue->fd)
984 if ((ret = check_fd_events( queue->fd, POLLIN )))
985 /* stop waiting on select() if we are signaled */
986 set_fd_events( queue->fd, 0 );
987 else if (!list_empty( &obj->wait_queue ))
988 /* restart waiting on poll() if we are no longer signaled */
989 set_fd_events( queue->fd, POLLIN );
991 return ret || is_signaled( queue );
994 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
996 struct msg_queue *queue = (struct msg_queue *)obj;
997 queue->wake_mask = 0;
998 queue->changed_mask = 0;
1001 static void msg_queue_destroy( struct object *obj )
1003 struct msg_queue *queue = (struct msg_queue *)obj;
1004 struct list *ptr;
1005 struct hotkey *hotkey, *hotkey2;
1006 int i;
1008 cleanup_results( queue );
1009 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
1011 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
1013 if (hotkey->queue == queue)
1015 list_remove( &hotkey->entry );
1016 free( hotkey );
1020 while ((ptr = list_head( &queue->pending_timers )))
1022 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1023 list_remove( &timer->entry );
1024 free( timer );
1026 while ((ptr = list_head( &queue->expired_timers )))
1028 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1029 list_remove( &timer->entry );
1030 free( timer );
1032 if (queue->timeout) remove_timeout_user( queue->timeout );
1033 queue->input->cursor_count -= queue->cursor_count;
1034 release_object( queue->input );
1035 if (queue->hooks) release_object( queue->hooks );
1036 if (queue->fd) release_object( queue->fd );
1039 static void msg_queue_poll_event( struct fd *fd, int event )
1041 struct msg_queue *queue = get_fd_user( fd );
1042 assert( queue->obj.ops == &msg_queue_ops );
1044 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1045 else set_fd_events( queue->fd, 0 );
1046 wake_up( &queue->obj, 0 );
1049 static void thread_input_dump( struct object *obj, int verbose )
1051 struct thread_input *input = (struct thread_input *)obj;
1052 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
1053 input->focus, input->capture, input->active );
1056 static void thread_input_destroy( struct object *obj )
1058 struct thread_input *input = (struct thread_input *)obj;
1060 empty_msg_list( &input->msg_list );
1061 if (input->desktop)
1063 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
1064 release_object( input->desktop );
1068 /* fix the thread input data when a window is destroyed */
1069 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1071 struct thread_input *input = queue->input;
1073 if (window == input->focus) input->focus = 0;
1074 if (window == input->capture) input->capture = 0;
1075 if (window == input->active) input->active = 0;
1076 if (window == input->menu_owner) input->menu_owner = 0;
1077 if (window == input->move_size) input->move_size = 0;
1078 if (window == input->caret) set_caret_window( input, 0 );
1081 /* check if the specified window can be set in the input data of a given queue */
1082 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1084 struct thread *thread;
1085 int ret = 0;
1087 if (!window) return 1; /* we can always clear the data */
1089 if ((thread = get_window_thread( window )))
1091 ret = (queue->input == thread->queue->input);
1092 if (!ret) set_error( STATUS_ACCESS_DENIED );
1093 release_object( thread );
1095 else set_error( STATUS_INVALID_HANDLE );
1097 return ret;
1100 /* make sure the specified thread has a queue */
1101 int init_thread_queue( struct thread *thread )
1103 if (thread->queue) return 1;
1104 return (create_msg_queue( thread, NULL ) != NULL);
1107 /* attach two thread input data structures */
1108 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1110 struct desktop *desktop;
1111 struct thread_input *input;
1112 int ret;
1114 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1115 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1116 input = (struct thread_input *)grab_object( thread_to->queue->input );
1117 if (input->desktop != desktop)
1119 set_error( STATUS_ACCESS_DENIED );
1120 release_object( input );
1121 release_object( desktop );
1122 return 0;
1124 release_object( desktop );
1126 if (thread_from->queue)
1128 if (!input->focus) input->focus = thread_from->queue->input->focus;
1129 if (!input->active) input->active = thread_from->queue->input->active;
1132 ret = assign_thread_input( thread_from, input );
1133 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1134 release_object( input );
1135 return ret;
1138 /* detach two thread input data structures */
1139 void detach_thread_input( struct thread *thread_from )
1141 struct thread *thread;
1142 struct thread_input *input, *old_input = thread_from->queue->input;
1144 if ((input = create_thread_input( thread_from )))
1146 if (old_input->focus && (thread = get_window_thread( old_input->focus )))
1148 if (thread == thread_from)
1150 input->focus = old_input->focus;
1151 old_input->focus = 0;
1153 release_object( thread );
1155 if (old_input->active && (thread = get_window_thread( old_input->active )))
1157 if (thread == thread_from)
1159 input->active = old_input->active;
1160 old_input->active = 0;
1162 release_object( thread );
1164 assign_thread_input( thread_from, input );
1165 release_object( input );
1170 /* set the next timer to expire */
1171 static void set_next_timer( struct msg_queue *queue )
1173 struct list *ptr;
1175 if (queue->timeout)
1177 remove_timeout_user( queue->timeout );
1178 queue->timeout = NULL;
1180 if ((ptr = list_head( &queue->pending_timers )))
1182 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1183 queue->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, queue );
1185 /* set/clear QS_TIMER bit */
1186 if (list_empty( &queue->expired_timers ))
1187 clear_queue_bits( queue, QS_TIMER );
1188 else
1189 set_queue_bits( queue, QS_TIMER );
1192 /* find a timer from its window and id */
1193 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1194 unsigned int msg, lparam_t id )
1196 struct list *ptr;
1198 /* we need to search both lists */
1200 LIST_FOR_EACH( ptr, &queue->pending_timers )
1202 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1203 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1205 LIST_FOR_EACH( ptr, &queue->expired_timers )
1207 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1208 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1210 return NULL;
1213 /* callback for the next timer expiration */
1214 static void timer_callback( void *private )
1216 struct msg_queue *queue = private;
1217 struct list *ptr;
1219 queue->timeout = NULL;
1220 /* move on to the next timer */
1221 ptr = list_head( &queue->pending_timers );
1222 list_remove( ptr );
1223 list_add_tail( &queue->expired_timers, ptr );
1224 set_next_timer( queue );
1227 /* link a timer at its rightful place in the queue list */
1228 static void link_timer( struct msg_queue *queue, struct timer *timer )
1230 struct list *ptr;
1232 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1234 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1235 if (t->when <= timer->when) break;
1237 list_add_before( ptr, &timer->entry );
1240 /* remove a timer from the queue timer list and free it */
1241 static void free_timer( struct msg_queue *queue, struct timer *timer )
1243 list_remove( &timer->entry );
1244 free( timer );
1245 set_next_timer( queue );
1248 /* restart an expired timer */
1249 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1251 list_remove( &timer->entry );
1252 while (-timer->when <= monotonic_time) timer->when -= (timeout_t)timer->rate * 10000;
1253 link_timer( queue, timer );
1254 set_next_timer( queue );
1257 /* find an expired timer matching the filtering parameters */
1258 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1259 unsigned int get_first, unsigned int get_last,
1260 int remove )
1262 struct list *ptr;
1264 LIST_FOR_EACH( ptr, &queue->expired_timers )
1266 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1267 if (win && timer->win != win) continue;
1268 if (check_msg_filter( timer->msg, get_first, get_last ))
1270 if (remove) restart_timer( queue, timer );
1271 return timer;
1274 return NULL;
1277 /* add a timer */
1278 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1280 struct timer *timer = mem_alloc( sizeof(*timer) );
1281 if (timer)
1283 timer->rate = max( rate, 1 );
1284 timer->when = -monotonic_time - (timeout_t)timer->rate * 10000;
1285 link_timer( queue, timer );
1286 /* check if we replaced the next timer */
1287 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1289 return timer;
1292 /* change the input key state for a given key */
1293 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1295 if (down)
1297 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1298 keystate[key] |= down;
1300 else keystate[key] &= ~0x80;
1303 /* update the input key state for a keyboard message */
1304 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1305 unsigned int msg, lparam_t wparam )
1307 unsigned char key;
1308 int down = 0;
1310 switch (msg)
1312 case WM_LBUTTONDOWN:
1313 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1314 /* fall through */
1315 case WM_LBUTTONUP:
1316 set_input_key_state( keystate, VK_LBUTTON, down );
1317 break;
1318 case WM_MBUTTONDOWN:
1319 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1320 /* fall through */
1321 case WM_MBUTTONUP:
1322 set_input_key_state( keystate, VK_MBUTTON, down );
1323 break;
1324 case WM_RBUTTONDOWN:
1325 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1326 /* fall through */
1327 case WM_RBUTTONUP:
1328 set_input_key_state( keystate, VK_RBUTTON, down );
1329 break;
1330 case WM_XBUTTONDOWN:
1331 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1332 /* fall through */
1333 case WM_XBUTTONUP:
1334 if (wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1335 else if (wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1336 break;
1337 case WM_KEYDOWN:
1338 case WM_SYSKEYDOWN:
1339 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1340 /* fall through */
1341 case WM_KEYUP:
1342 case WM_SYSKEYUP:
1343 key = (unsigned char)wparam;
1344 set_input_key_state( keystate, key, down );
1345 switch(key)
1347 case VK_LCONTROL:
1348 case VK_RCONTROL:
1349 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1350 set_input_key_state( keystate, VK_CONTROL, down );
1351 break;
1352 case VK_LMENU:
1353 case VK_RMENU:
1354 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1355 set_input_key_state( keystate, VK_MENU, down );
1356 break;
1357 case VK_LSHIFT:
1358 case VK_RSHIFT:
1359 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1360 set_input_key_state( keystate, VK_SHIFT, down );
1361 break;
1363 break;
1367 /* update the desktop key state according to a mouse message flags */
1368 static void update_desktop_mouse_state( struct desktop *desktop, unsigned int flags,
1369 int x, int y, lparam_t wparam )
1371 if (flags & MOUSEEVENTF_MOVE)
1372 update_desktop_cursor_pos( desktop, x, y );
1373 if (flags & MOUSEEVENTF_LEFTDOWN)
1374 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONDOWN, wparam );
1375 if (flags & MOUSEEVENTF_LEFTUP)
1376 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONUP, wparam );
1377 if (flags & MOUSEEVENTF_RIGHTDOWN)
1378 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONDOWN, wparam );
1379 if (flags & MOUSEEVENTF_RIGHTUP)
1380 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONUP, wparam );
1381 if (flags & MOUSEEVENTF_MIDDLEDOWN)
1382 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONDOWN, wparam );
1383 if (flags & MOUSEEVENTF_MIDDLEUP)
1384 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONUP, wparam );
1385 if (flags & MOUSEEVENTF_XDOWN)
1386 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONDOWN, wparam );
1387 if (flags & MOUSEEVENTF_XUP)
1388 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONUP, wparam );
1391 /* release the hardware message currently being processed by the given thread */
1392 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id )
1394 struct thread_input *input = queue->input;
1395 struct message *msg, *other;
1396 int clr_bit;
1398 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1400 if (msg->unique_id == hw_id) break;
1402 if (&msg->entry == &input->msg_list) return; /* not found */
1404 /* clear the queue bit for that message */
1405 clr_bit = get_hardware_msg_bit( msg );
1406 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1408 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1410 clr_bit = 0;
1411 break;
1414 if (clr_bit) clear_queue_bits( queue, clr_bit );
1416 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1417 list_remove( &msg->entry );
1418 free_message( msg );
1421 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1423 struct hotkey *hotkey;
1424 unsigned int modifiers = 0;
1426 if (msg->msg != WM_KEYDOWN) return 0;
1428 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1429 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1430 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1431 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1433 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1435 if (hotkey->vkey != msg->wparam) continue;
1436 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1439 return 0;
1441 found:
1442 msg->type = MSG_POSTED;
1443 msg->win = hotkey->win;
1444 msg->msg = WM_HOTKEY;
1445 msg->wparam = hotkey->id;
1446 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1448 free( msg->data );
1449 msg->data = NULL;
1450 msg->data_size = 0;
1452 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1453 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1454 hotkey->queue->hotkey_count++;
1455 return 1;
1458 /* find the window that should receive a given hardware message */
1459 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1460 struct message *msg, unsigned int *msg_code,
1461 struct thread **thread )
1463 user_handle_t win = 0;
1465 *thread = NULL;
1466 *msg_code = msg->msg;
1467 if (msg->msg == WM_INPUT)
1469 if (!(win = msg->win) && input) win = input->focus;
1471 else if (is_keyboard_msg( msg ))
1473 if (input && !(win = input->focus))
1475 win = input->active;
1476 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1479 else if (!input || !(win = input->capture)) /* mouse message */
1481 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1482 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1484 *thread = window_thread_from_point( win, msg->x, msg->y );
1487 if (!*thread)
1488 *thread = get_window_thread( win );
1489 return win;
1492 static struct rawinput_device_entry *find_rawinput_device( unsigned short usage_page, unsigned short usage )
1494 struct rawinput_device_entry *e;
1496 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
1498 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1499 return e;
1502 return NULL;
1505 static void update_rawinput_device(const struct rawinput_device *device)
1507 struct rawinput_device_entry *e;
1509 if (!(e = find_rawinput_device( device->usage_page, device->usage )))
1511 if (!(e = mem_alloc( sizeof(*e) ))) return;
1512 list_add_tail( &current->process->rawinput_devices, &e->entry );
1515 if (device->flags & RIDEV_REMOVE)
1517 list_remove( &e->entry );
1518 free( e );
1519 return;
1522 e->device = *device;
1523 e->device.target = get_user_full_handle( e->device.target );
1526 static void prepend_cursor_history( int x, int y, unsigned int time, lparam_t info )
1528 cursor_pos_t *pos = &cursor_history[--cursor_history_latest % ARRAY_SIZE(cursor_history)];
1530 pos->x = x;
1531 pos->y = y;
1532 pos->time = time;
1533 pos->info = info;
1536 /* queue a hardware message into a given thread input */
1537 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1539 user_handle_t win;
1540 struct thread *thread;
1541 struct thread_input *input;
1542 struct hardware_msg_data *msg_data = msg->data;
1543 unsigned int msg_code;
1545 update_input_key_state( desktop, desktop->keystate, msg->msg, msg->wparam );
1546 last_input_time = get_tick_count();
1547 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1549 if (is_keyboard_msg( msg ))
1551 if (queue_hotkey_message( desktop, msg )) return;
1552 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1553 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1554 msg->lparam &= ~(KF_EXTENDED << 16);
1556 else if (msg->msg != WM_INPUT)
1558 if (msg->msg == WM_MOUSEMOVE)
1560 prepend_cursor_history( msg->x, msg->y, msg->time, msg_data->info );
1561 if (update_desktop_cursor_pos( desktop, msg->x, msg->y )) always_queue = 1;
1563 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1564 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1565 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1566 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1567 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1568 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1569 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1571 msg->x = desktop->cursor.x;
1572 msg->y = desktop->cursor.y;
1574 if (msg->win && (thread = get_window_thread( msg->win )))
1576 input = thread->queue->input;
1577 release_object( thread );
1579 else input = desktop->foreground_input;
1581 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1582 if (!win || !thread)
1584 if (input) update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1585 free_message( msg );
1586 return;
1588 input = thread->queue->input;
1590 if (win != desktop->cursor.win) always_queue = 1;
1591 desktop->cursor.win = win;
1593 if (!always_queue || merge_message( input, msg )) free_message( msg );
1594 else
1596 msg->unique_id = 0; /* will be set once we return it to the app */
1597 list_add_tail( &input->msg_list, &msg->entry );
1598 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1600 release_object( thread );
1603 /* send the low-level hook message for a given hardware message */
1604 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1605 const hw_input_t *input, struct msg_queue *sender )
1607 struct thread *hook_thread;
1608 struct msg_queue *queue;
1609 struct message *msg;
1610 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1611 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1613 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1614 if (!(queue = hook_thread->queue)) return 0;
1615 if (is_queue_hung( queue )) return 0;
1617 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1619 msg->type = MSG_HOOK_LL;
1620 msg->win = 0;
1621 msg->msg = id;
1622 msg->wparam = hardware_msg->msg;
1623 msg->x = hardware_msg->x;
1624 msg->y = hardware_msg->y;
1625 msg->time = hardware_msg->time;
1626 msg->data_size = hardware_msg->data_size;
1627 msg->result = NULL;
1629 if (input->type == INPUT_KEYBOARD)
1631 unsigned short vkey = input->kbd.vkey;
1632 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1633 msg->lparam = (input->kbd.scan << 16) | vkey;
1635 else msg->lparam = input->mouse.data << 16;
1637 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1638 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1640 free_message( msg );
1641 return 0;
1643 msg->result->hardware_msg = hardware_msg;
1644 msg->result->desktop = (struct desktop *)grab_object( desktop );
1645 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1646 set_queue_bits( queue, QS_SENDMESSAGE );
1647 return 1;
1650 /* get the foreground thread for a desktop and a window receiving input */
1651 static struct thread *get_foreground_thread( struct desktop *desktop, user_handle_t window )
1653 /* if desktop has no foreground process, assume the receiving window is */
1654 if (desktop->foreground_input) return get_window_thread( desktop->foreground_input->focus );
1655 if (window) return get_window_thread( window );
1656 return NULL;
1659 struct rawinput_message
1661 struct thread *foreground;
1662 struct desktop *desktop;
1663 struct hw_msg_source source;
1664 unsigned int time;
1665 struct hardware_msg_data data;
1668 /* check if process is supposed to receive a WM_INPUT message and eventually queue it */
1669 static int queue_rawinput_message( struct process* process, void *arg )
1671 const struct rawinput_message* raw_msg = arg;
1672 const struct rawinput_device *device = NULL;
1673 struct desktop *target_desktop = NULL;
1674 struct thread *target_thread = NULL;
1675 struct message *msg;
1676 int wparam = RIM_INPUT;
1678 if (raw_msg->data.rawinput.type == RIM_TYPEMOUSE)
1679 device = process->rawinput_mouse;
1680 else if (raw_msg->data.rawinput.type == RIM_TYPEKEYBOARD)
1681 device = process->rawinput_kbd;
1682 if (!device) return 0;
1684 if (process != raw_msg->foreground->process)
1686 if (!(device->flags & RIDEV_INPUTSINK)) goto done;
1687 if (!(target_thread = get_window_thread( device->target ))) goto done;
1688 if (!(target_desktop = get_thread_desktop( target_thread, 0 ))) goto done;
1689 if (target_desktop != raw_msg->desktop) goto done;
1690 wparam = RIM_INPUTSINK;
1693 if (!(msg = alloc_hardware_message( raw_msg->data.info, raw_msg->source, raw_msg->time )))
1694 goto done;
1696 msg->win = device->target;
1697 msg->msg = WM_INPUT;
1698 msg->wparam = wparam;
1699 msg->lparam = 0;
1700 memcpy( msg->data, &raw_msg->data, sizeof(raw_msg->data) );
1702 queue_hardware_message( raw_msg->desktop, msg, 1 );
1704 done:
1705 if (target_thread) release_object( target_thread );
1706 if (target_desktop) release_object( target_desktop );
1707 return 0;
1710 /* queue a hardware message for a mouse event */
1711 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1712 unsigned int origin, struct msg_queue *sender )
1714 const struct rawinput_device *device;
1715 struct hardware_msg_data *msg_data;
1716 struct rawinput_message raw_msg;
1717 struct message *msg;
1718 struct thread *foreground;
1719 unsigned int i, time, flags;
1720 struct hw_msg_source source = { IMDT_MOUSE, origin };
1721 int wait = 0, x, y;
1723 static const unsigned int messages[] =
1725 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1726 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1727 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1728 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1729 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1730 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1731 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1732 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1733 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1734 0, /* 0x0200 = unused */
1735 0, /* 0x0400 = unused */
1736 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1737 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1740 desktop->cursor.last_change = get_tick_count();
1741 flags = input->mouse.flags;
1742 time = input->mouse.time;
1743 if (!time) time = desktop->cursor.last_change;
1745 if (flags & MOUSEEVENTF_MOVE)
1747 if (flags & MOUSEEVENTF_ABSOLUTE)
1749 x = input->mouse.x;
1750 y = input->mouse.y;
1751 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1752 x == desktop->cursor.x && y == desktop->cursor.y)
1753 flags &= ~MOUSEEVENTF_MOVE;
1755 else
1757 x = desktop->cursor.x + input->mouse.x;
1758 y = desktop->cursor.y + input->mouse.y;
1761 else
1763 x = desktop->cursor.x;
1764 y = desktop->cursor.y;
1767 if ((foreground = get_foreground_thread( desktop, win )))
1769 raw_msg.foreground = foreground;
1770 raw_msg.desktop = desktop;
1771 raw_msg.source = source;
1772 raw_msg.time = time;
1774 msg_data = &raw_msg.data;
1775 msg_data->info = input->mouse.info;
1776 msg_data->flags = flags;
1777 msg_data->rawinput.type = RIM_TYPEMOUSE;
1778 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1779 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1780 msg_data->rawinput.mouse.data = input->mouse.data;
1782 enum_processes( queue_rawinput_message, &raw_msg );
1783 release_object( foreground );
1786 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
1788 update_desktop_mouse_state( desktop, flags, x, y, input->mouse.data << 16 );
1789 return 0;
1792 for (i = 0; i < ARRAY_SIZE( messages ); i++)
1794 if (!messages[i]) continue;
1795 if (!(flags & (1 << i))) continue;
1796 flags &= ~(1 << i);
1798 if (!(msg = alloc_hardware_message( input->mouse.info, source, time ))) return 0;
1799 msg_data = msg->data;
1801 msg->win = get_user_full_handle( win );
1802 msg->msg = messages[i];
1803 msg->wparam = input->mouse.data << 16;
1804 msg->lparam = 0;
1805 msg->x = x;
1806 msg->y = y;
1807 if (origin == IMO_INJECTED) msg_data->flags = LLMHF_INJECTED;
1809 /* specify a sender only when sending the last message */
1810 if (!(flags & ((1 << ARRAY_SIZE( messages )) - 1)))
1812 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1813 queue_hardware_message( desktop, msg, 0 );
1815 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1816 queue_hardware_message( desktop, msg, 0 );
1818 return wait;
1821 /* queue a hardware message for a keyboard event */
1822 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1823 unsigned int origin, struct msg_queue *sender )
1825 struct hw_msg_source source = { IMDT_KEYBOARD, origin };
1826 const struct rawinput_device *device;
1827 struct hardware_msg_data *msg_data;
1828 struct rawinput_message raw_msg;
1829 struct message *msg;
1830 struct thread *foreground;
1831 unsigned char vkey = input->kbd.vkey;
1832 unsigned int message_code, time;
1833 int wait;
1835 if (!(time = input->kbd.time)) time = get_tick_count();
1837 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1839 switch (vkey)
1841 case VK_MENU:
1842 case VK_LMENU:
1843 case VK_RMENU:
1844 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1845 break;
1846 case VK_CONTROL:
1847 case VK_LCONTROL:
1848 case VK_RCONTROL:
1849 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1850 break;
1851 case VK_SHIFT:
1852 case VK_LSHIFT:
1853 case VK_RSHIFT:
1854 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1855 break;
1859 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1860 switch (vkey)
1862 case VK_LMENU:
1863 case VK_RMENU:
1864 if (input->kbd.flags & KEYEVENTF_KEYUP)
1866 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1867 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1868 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1869 message_code = WM_SYSKEYUP;
1870 desktop->keystate[VK_MENU] &= ~0x02;
1872 else
1874 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1875 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1876 message_code = WM_SYSKEYDOWN;
1877 desktop->keystate[VK_MENU] |= 0x02;
1879 break;
1881 case VK_LCONTROL:
1882 case VK_RCONTROL:
1883 /* send WM_SYSKEYUP on release if Alt still pressed */
1884 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1885 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1886 message_code = WM_SYSKEYUP;
1887 desktop->keystate[VK_MENU] &= ~0x02;
1888 break;
1890 default:
1891 /* send WM_SYSKEY for Alt-anykey and for F10 */
1892 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1893 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1894 /* fall through */
1895 case VK_F10:
1896 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1897 desktop->keystate[VK_MENU] &= ~0x02;
1898 break;
1901 if ((foreground = get_foreground_thread( desktop, win )))
1903 raw_msg.foreground = foreground;
1904 raw_msg.desktop = desktop;
1905 raw_msg.source = source;
1906 raw_msg.time = time;
1908 msg_data = &raw_msg.data;
1909 msg_data->info = input->kbd.info;
1910 msg_data->flags = input->kbd.flags;
1911 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1912 msg_data->rawinput.kbd.message = message_code;
1913 msg_data->rawinput.kbd.vkey = vkey;
1914 msg_data->rawinput.kbd.scan = input->kbd.scan;
1916 enum_processes( queue_rawinput_message, &raw_msg );
1917 release_object( foreground );
1920 if ((device = current->process->rawinput_kbd) && (device->flags & RIDEV_NOLEGACY))
1922 update_input_key_state( desktop, desktop->keystate, message_code, vkey );
1923 return 0;
1926 if (!(msg = alloc_hardware_message( input->kbd.info, source, time ))) return 0;
1927 msg_data = msg->data;
1929 msg->win = get_user_full_handle( win );
1930 msg->msg = message_code;
1931 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1932 if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
1934 if (input->kbd.flags & KEYEVENTF_UNICODE && !vkey)
1936 msg->wparam = VK_PACKET;
1938 else
1940 unsigned int flags = 0;
1941 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1942 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1943 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1944 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1946 msg->wparam = vkey;
1947 msg->lparam |= flags << 16;
1948 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1951 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1952 queue_hardware_message( desktop, msg, 1 );
1954 return wait;
1957 /* queue a hardware message for a custom type of event */
1958 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1959 unsigned int origin, const hw_input_t *input )
1961 struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
1962 struct message *msg;
1964 if (!(msg = alloc_hardware_message( 0, source, get_tick_count() ))) return;
1966 msg->win = get_user_full_handle( win );
1967 msg->msg = input->hw.msg;
1968 msg->wparam = 0;
1969 msg->lparam = input->hw.lparam;
1970 msg->x = desktop->cursor.x;
1971 msg->y = desktop->cursor.y;
1973 queue_hardware_message( desktop, msg, 1 );
1976 /* check message filter for a hardware message */
1977 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1978 user_handle_t filter_win, unsigned int first, unsigned int last )
1980 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1982 /* we can only test the window for a keyboard message since the
1983 * dest window for a mouse message depends on hittest */
1984 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1985 return 0;
1986 /* the message code is final for a keyboard message, we can simply check it */
1987 return check_msg_filter( msg_code, first, last );
1989 else /* mouse message */
1991 /* we need to check all possible values that the message can have in the end */
1993 if (check_msg_filter( msg_code, first, last )) return 1;
1994 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1996 /* all other messages can become non-client messages */
1997 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1999 /* clicks can become double-clicks or non-client double-clicks */
2000 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
2001 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
2003 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2004 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2006 return 0;
2011 /* find a hardware message for the given queue */
2012 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
2013 unsigned int first, unsigned int last, unsigned int flags,
2014 struct get_message_reply *reply )
2016 struct thread_input *input = thread->queue->input;
2017 struct thread *win_thread;
2018 struct list *ptr;
2019 user_handle_t win;
2020 int clear_bits, got_one = 0;
2021 unsigned int msg_code;
2023 ptr = list_head( &input->msg_list );
2024 if (hw_id)
2026 while (ptr)
2028 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2029 if (msg->unique_id == hw_id) break;
2030 ptr = list_next( &input->msg_list, ptr );
2032 if (!ptr) ptr = list_head( &input->msg_list );
2033 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
2036 if (ptr == list_head( &input->msg_list ))
2037 clear_bits = QS_INPUT;
2038 else
2039 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
2041 while (ptr)
2043 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2044 struct hardware_msg_data *data = msg->data;
2046 ptr = list_next( &input->msg_list, ptr );
2047 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
2048 if (!win || !win_thread)
2050 /* no window at all, remove it */
2051 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2052 list_remove( &msg->entry );
2053 free_message( msg );
2054 continue;
2056 if (win_thread != thread)
2058 if (win_thread->queue->input == input)
2060 /* wake the other thread */
2061 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
2062 got_one = 1;
2064 else
2066 /* for another thread input, drop it */
2067 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2068 list_remove( &msg->entry );
2069 free_message( msg );
2071 release_object( win_thread );
2072 continue;
2074 release_object( win_thread );
2076 /* if we already got a message for another thread, or if it doesn't
2077 * match the filter we skip it */
2078 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
2080 clear_bits &= ~get_hardware_msg_bit( msg );
2081 continue;
2083 /* now we can return it */
2084 if (!msg->unique_id) msg->unique_id = get_unique_id();
2085 reply->type = MSG_HARDWARE;
2086 reply->win = win;
2087 reply->msg = msg_code;
2088 reply->wparam = msg->wparam;
2089 reply->lparam = msg->lparam;
2090 reply->x = msg->x;
2091 reply->y = msg->y;
2092 reply->time = msg->time;
2094 data->hw_id = msg->unique_id;
2095 set_reply_data( msg->data, msg->data_size );
2096 if (msg->msg == WM_INPUT && (flags & PM_REMOVE))
2097 release_hardware_message( current->queue, data->hw_id );
2098 return 1;
2100 /* nothing found, clear the hardware queue bits */
2101 clear_queue_bits( thread->queue, clear_bits );
2102 return 0;
2105 /* increment (or decrement if 'incr' is negative) the queue paint count */
2106 void inc_queue_paint_count( struct thread *thread, int incr )
2108 struct msg_queue *queue = thread->queue;
2110 assert( queue );
2112 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2114 if (queue->paint_count)
2115 set_queue_bits( queue, QS_PAINT );
2116 else
2117 clear_queue_bits( queue, QS_PAINT );
2121 /* remove all messages and timers belonging to a certain window */
2122 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2124 struct msg_queue *queue = thread->queue;
2125 struct list *ptr;
2126 int i;
2128 if (!queue) return;
2130 /* remove timers */
2132 ptr = list_head( &queue->pending_timers );
2133 while (ptr)
2135 struct list *next = list_next( &queue->pending_timers, ptr );
2136 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2137 if (timer->win == win) free_timer( queue, timer );
2138 ptr = next;
2140 ptr = list_head( &queue->expired_timers );
2141 while (ptr)
2143 struct list *next = list_next( &queue->expired_timers, ptr );
2144 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2145 if (timer->win == win) free_timer( queue, timer );
2146 ptr = next;
2149 /* remove messages */
2150 for (i = 0; i < NB_MSG_KINDS; i++)
2152 struct list *ptr, *next;
2154 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2156 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2157 if (msg->win == win)
2159 if (msg->msg == WM_QUIT && !queue->quit_message)
2161 queue->quit_message = 1;
2162 queue->exit_code = msg->wparam;
2164 remove_queue_message( queue, msg, i );
2169 thread_input_cleanup_window( queue, win );
2172 /* post a message to a window */
2173 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2175 struct message *msg;
2176 struct thread *thread = get_window_thread( win );
2178 if (!thread) return;
2180 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2182 msg->type = MSG_POSTED;
2183 msg->win = get_user_full_handle( win );
2184 msg->msg = message;
2185 msg->wparam = wparam;
2186 msg->lparam = lparam;
2187 msg->result = NULL;
2188 msg->data = NULL;
2189 msg->data_size = 0;
2191 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2193 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2194 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2195 if (message == WM_HOTKEY)
2197 set_queue_bits( thread->queue, QS_HOTKEY );
2198 thread->queue->hotkey_count++;
2201 release_object( thread );
2204 /* send a notify message to a window */
2205 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2207 struct message *msg;
2208 struct thread *thread = get_window_thread( win );
2210 if (!thread) return;
2212 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2214 msg->type = MSG_NOTIFY;
2215 msg->win = get_user_full_handle( win );
2216 msg->msg = message;
2217 msg->wparam = wparam;
2218 msg->lparam = lparam;
2219 msg->result = NULL;
2220 msg->data = NULL;
2221 msg->data_size = 0;
2223 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2225 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2226 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2228 release_object( thread );
2231 /* post a win event */
2232 void post_win_event( struct thread *thread, unsigned int event,
2233 user_handle_t win, unsigned int object_id,
2234 unsigned int child_id, client_ptr_t hook_proc,
2235 const WCHAR *module, data_size_t module_size,
2236 user_handle_t hook)
2238 struct message *msg;
2240 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2242 struct winevent_msg_data *data;
2244 msg->type = MSG_WINEVENT;
2245 msg->win = get_user_full_handle( win );
2246 msg->msg = event;
2247 msg->wparam = object_id;
2248 msg->lparam = child_id;
2249 msg->time = get_tick_count();
2250 msg->result = NULL;
2252 if ((data = malloc( sizeof(*data) + module_size )))
2254 data->hook = hook;
2255 data->tid = get_thread_id( current );
2256 data->hook_proc = hook_proc;
2257 memcpy( data + 1, module, module_size );
2259 msg->data = data;
2260 msg->data_size = sizeof(*data) + module_size;
2262 if (debug_level > 1)
2263 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2264 get_thread_id(thread), event, win, object_id, child_id );
2265 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2266 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2268 else
2269 free( msg );
2273 /* free all hotkeys on a desktop, optionally filtering by window */
2274 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2276 struct hotkey *hotkey, *hotkey2;
2278 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2280 if (!window || hotkey->win == window)
2282 list_remove( &hotkey->entry );
2283 free( hotkey );
2289 /* check if the thread owning the window is hung */
2290 DECL_HANDLER(is_window_hung)
2292 struct thread *thread;
2294 thread = get_window_thread( req->win );
2295 if (thread)
2297 reply->is_hung = is_queue_hung( thread->queue );
2298 release_object( thread );
2300 else reply->is_hung = 0;
2304 /* get the message queue of the current thread */
2305 DECL_HANDLER(get_msg_queue)
2307 struct msg_queue *queue = get_current_queue();
2309 reply->handle = 0;
2310 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2314 /* set the file descriptor associated to the current thread queue */
2315 DECL_HANDLER(set_queue_fd)
2317 struct msg_queue *queue = get_current_queue();
2318 struct file *file;
2319 int unix_fd;
2321 if (queue->fd) /* fd can only be set once */
2323 set_error( STATUS_ACCESS_DENIED );
2324 return;
2326 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2328 if ((unix_fd = get_file_unix_fd( file )) != -1)
2330 if ((unix_fd = dup( unix_fd )) != -1)
2331 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2332 else
2333 file_set_error();
2335 release_object( file );
2339 /* set the current message queue wakeup mask */
2340 DECL_HANDLER(set_queue_mask)
2342 struct msg_queue *queue = get_current_queue();
2344 if (queue)
2346 queue->wake_mask = req->wake_mask;
2347 queue->changed_mask = req->changed_mask;
2348 reply->wake_bits = queue->wake_bits;
2349 reply->changed_bits = queue->changed_bits;
2350 if (is_signaled( queue ))
2352 /* if skip wait is set, do what would have been done in the subsequent wait */
2353 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2354 else wake_up( &queue->obj, 0 );
2360 /* get the current message queue status */
2361 DECL_HANDLER(get_queue_status)
2363 struct msg_queue *queue = current->queue;
2364 if (queue)
2366 reply->wake_bits = queue->wake_bits;
2367 reply->changed_bits = queue->changed_bits;
2368 queue->changed_bits &= ~req->clear_bits;
2370 else reply->wake_bits = reply->changed_bits = 0;
2374 /* send a message to a thread queue */
2375 DECL_HANDLER(send_message)
2377 struct message *msg;
2378 struct msg_queue *send_queue = get_current_queue();
2379 struct msg_queue *recv_queue = NULL;
2380 struct thread *thread = NULL;
2382 if (!(thread = get_thread_from_id( req->id ))) return;
2384 if (!(recv_queue = thread->queue))
2386 set_error( STATUS_INVALID_PARAMETER );
2387 release_object( thread );
2388 return;
2390 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2392 set_error( STATUS_TIMEOUT );
2393 release_object( thread );
2394 return;
2397 if ((msg = mem_alloc( sizeof(*msg) )))
2399 msg->type = req->type;
2400 msg->win = get_user_full_handle( req->win );
2401 msg->msg = req->msg;
2402 msg->wparam = req->wparam;
2403 msg->lparam = req->lparam;
2404 msg->result = NULL;
2405 msg->data = NULL;
2406 msg->data_size = get_req_data_size();
2408 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2410 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2412 free( msg );
2413 release_object( thread );
2414 return;
2417 switch(msg->type)
2419 case MSG_OTHER_PROCESS:
2420 case MSG_ASCII:
2421 case MSG_UNICODE:
2422 case MSG_CALLBACK:
2423 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2425 free_message( msg );
2426 break;
2428 /* fall through */
2429 case MSG_NOTIFY:
2430 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2431 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2432 break;
2433 case MSG_POSTED:
2434 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2435 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2436 if (msg->msg == WM_HOTKEY)
2438 set_queue_bits( recv_queue, QS_HOTKEY );
2439 recv_queue->hotkey_count++;
2441 break;
2442 case MSG_HARDWARE: /* should use send_hardware_message instead */
2443 case MSG_CALLBACK_RESULT: /* cannot send this one */
2444 case MSG_HOOK_LL: /* generated internally */
2445 default:
2446 set_error( STATUS_INVALID_PARAMETER );
2447 free( msg );
2448 break;
2451 release_object( thread );
2454 /* send a hardware message to a thread queue */
2455 DECL_HANDLER(send_hardware_message)
2457 struct thread *thread = NULL;
2458 struct desktop *desktop;
2459 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2460 struct msg_queue *sender = get_current_queue();
2461 data_size_t size = min( 256, get_reply_max_size() );
2463 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2465 if (req->win)
2467 if (!(thread = get_window_thread( req->win ))) return;
2468 if (desktop != thread->queue->input->desktop)
2470 /* don't allow queuing events to a different desktop */
2471 release_object( desktop );
2472 return;
2476 reply->prev_x = desktop->cursor.x;
2477 reply->prev_y = desktop->cursor.y;
2479 switch (req->input.type)
2481 case INPUT_MOUSE:
2482 reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2483 break;
2484 case INPUT_KEYBOARD:
2485 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2486 break;
2487 case INPUT_HARDWARE:
2488 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2489 break;
2490 default:
2491 set_error( STATUS_INVALID_PARAMETER );
2493 if (thread) release_object( thread );
2495 reply->new_x = desktop->cursor.x;
2496 reply->new_y = desktop->cursor.y;
2497 set_reply_data( desktop->keystate, size );
2498 release_object( desktop );
2501 /* post a quit message to the current queue */
2502 DECL_HANDLER(post_quit_message)
2504 struct msg_queue *queue = get_current_queue();
2506 if (!queue)
2507 return;
2509 queue->quit_message = 1;
2510 queue->exit_code = req->exit_code;
2511 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2514 /* get a message from the current queue */
2515 DECL_HANDLER(get_message)
2517 struct timer *timer;
2518 struct list *ptr;
2519 struct msg_queue *queue = get_current_queue();
2520 user_handle_t get_win = get_user_full_handle( req->get_win );
2521 unsigned int filter = req->flags >> 16;
2523 reply->active_hooks = get_active_hooks();
2525 if (get_win && get_win != 1 && get_win != -1 && !get_user_object( get_win, USER_WINDOW ))
2527 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2528 return;
2531 if (!queue) return;
2532 queue->last_get_msg = current_time;
2533 if (!filter) filter = QS_ALLINPUT;
2535 /* first check for sent messages */
2536 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2538 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2539 receive_message( queue, msg, reply );
2540 return;
2543 /* clear changed bits so we can wait on them if we don't find a message */
2544 if (filter & QS_POSTMESSAGE)
2546 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2547 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2549 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2550 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2552 /* then check for posted messages */
2553 if ((filter & QS_POSTMESSAGE) &&
2554 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2555 return;
2557 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2558 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2559 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2560 return;
2562 /* only check for quit messages if not posted messages pending */
2563 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2564 return;
2566 /* then check for any raw hardware message */
2567 if ((filter & QS_INPUT) &&
2568 filter_contains_hw_range( req->get_first, req->get_last ) &&
2569 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2570 return;
2572 /* now check for WM_PAINT */
2573 if ((filter & QS_PAINT) &&
2574 queue->paint_count &&
2575 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2576 (reply->win = find_window_to_repaint( get_win, current )))
2578 reply->type = MSG_POSTED;
2579 reply->msg = WM_PAINT;
2580 reply->wparam = 0;
2581 reply->lparam = 0;
2582 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2583 return;
2586 /* now check for timer */
2587 if ((filter & QS_TIMER) &&
2588 (timer = find_expired_timer( queue, get_win, req->get_first,
2589 req->get_last, (req->flags & PM_REMOVE) )))
2591 reply->type = MSG_POSTED;
2592 reply->win = timer->win;
2593 reply->msg = timer->msg;
2594 reply->wparam = timer->id;
2595 reply->lparam = timer->lparam;
2596 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2597 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2598 set_event( current->process->idle_event );
2599 return;
2602 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2603 queue->wake_mask = req->wake_mask;
2604 queue->changed_mask = req->changed_mask;
2605 set_error( STATUS_PENDING ); /* FIXME */
2609 /* reply to a sent message */
2610 DECL_HANDLER(reply_message)
2612 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2613 else if (current->queue->recv_result)
2614 reply_message( current->queue, req->result, 0, req->remove,
2615 get_req_data(), get_req_data_size() );
2619 /* accept the current hardware message */
2620 DECL_HANDLER(accept_hardware_message)
2622 if (current->queue)
2623 release_hardware_message( current->queue, req->hw_id );
2624 else
2625 set_error( STATUS_ACCESS_DENIED );
2629 /* retrieve the reply for the last message sent */
2630 DECL_HANDLER(get_message_reply)
2632 struct message_result *result;
2633 struct list *entry;
2634 struct msg_queue *queue = current->queue;
2636 if (queue)
2638 set_error( STATUS_PENDING );
2639 reply->result = 0;
2641 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2643 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2644 if (result->replied || req->cancel)
2646 if (result->replied)
2648 reply->result = result->result;
2649 set_error( result->error );
2650 if (result->data)
2652 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2653 set_reply_data_ptr( result->data, data_len );
2654 result->data = NULL;
2655 result->data_size = 0;
2658 remove_result_from_sender( result );
2660 entry = list_head( &queue->send_result );
2661 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2662 else
2664 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2665 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2666 else clear_queue_bits( queue, QS_SMRESULT );
2670 else set_error( STATUS_ACCESS_DENIED );
2674 /* set a window timer */
2675 DECL_HANDLER(set_win_timer)
2677 struct timer *timer;
2678 struct msg_queue *queue;
2679 struct thread *thread = NULL;
2680 user_handle_t win = 0;
2681 lparam_t id = req->id;
2683 if (req->win)
2685 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2687 set_error( STATUS_INVALID_HANDLE );
2688 return;
2690 if (thread->process != current->process)
2692 release_object( thread );
2693 set_error( STATUS_ACCESS_DENIED );
2694 return;
2696 queue = thread->queue;
2697 /* remove it if it existed already */
2698 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2700 else
2702 queue = get_current_queue();
2703 /* look for a timer with this id */
2704 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2706 /* free and reuse id */
2707 free_timer( queue, timer );
2709 else
2711 lparam_t end_id = queue->next_timer_id;
2713 /* find a free id for it */
2714 while (1)
2716 id = queue->next_timer_id;
2717 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2719 if (!find_timer( queue, 0, req->msg, id )) break;
2720 if (queue->next_timer_id == end_id)
2722 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2723 return;
2729 if ((timer = set_timer( queue, req->rate )))
2731 timer->win = win;
2732 timer->msg = req->msg;
2733 timer->id = id;
2734 timer->lparam = req->lparam;
2735 reply->id = id;
2737 if (thread) release_object( thread );
2740 /* kill a window timer */
2741 DECL_HANDLER(kill_win_timer)
2743 struct timer *timer;
2744 struct thread *thread;
2745 user_handle_t win = 0;
2747 if (req->win)
2749 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2751 set_error( STATUS_INVALID_HANDLE );
2752 return;
2754 if (thread->process != current->process)
2756 release_object( thread );
2757 set_error( STATUS_ACCESS_DENIED );
2758 return;
2761 else thread = (struct thread *)grab_object( current );
2763 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2764 free_timer( thread->queue, timer );
2765 else
2766 set_error( STATUS_INVALID_PARAMETER );
2768 release_object( thread );
2771 DECL_HANDLER(register_hotkey)
2773 struct desktop *desktop;
2774 user_handle_t win_handle = req->window;
2775 struct hotkey *hotkey;
2776 struct hotkey *new_hotkey = NULL;
2777 struct thread *thread;
2778 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2780 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2782 if (win_handle)
2784 if (!(win_handle = get_valid_window_handle( win_handle )))
2786 release_object( desktop );
2787 return;
2790 thread = get_window_thread( win_handle );
2791 if (thread) release_object( thread );
2793 if (thread != current)
2795 release_object( desktop );
2796 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2797 return;
2801 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2803 if (req->vkey == hotkey->vkey &&
2804 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2806 release_object( desktop );
2807 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2808 return;
2810 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2811 new_hotkey = hotkey;
2814 if (new_hotkey)
2816 reply->replaced = 1;
2817 reply->flags = new_hotkey->flags;
2818 reply->vkey = new_hotkey->vkey;
2820 else
2822 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2823 if (new_hotkey)
2825 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2826 new_hotkey->queue = current->queue;
2827 new_hotkey->win = win_handle;
2828 new_hotkey->id = req->id;
2832 if (new_hotkey)
2834 new_hotkey->flags = req->flags;
2835 new_hotkey->vkey = req->vkey;
2838 release_object( desktop );
2841 DECL_HANDLER(unregister_hotkey)
2843 struct desktop *desktop;
2844 user_handle_t win_handle = req->window;
2845 struct hotkey *hotkey;
2846 struct thread *thread;
2848 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2850 if (win_handle)
2852 if (!(win_handle = get_valid_window_handle( win_handle )))
2854 release_object( desktop );
2855 return;
2858 thread = get_window_thread( win_handle );
2859 if (thread) release_object( thread );
2861 if (thread != current)
2863 release_object( desktop );
2864 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2865 return;
2869 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2871 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2872 goto found;
2875 release_object( desktop );
2876 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2877 return;
2879 found:
2880 reply->flags = hotkey->flags;
2881 reply->vkey = hotkey->vkey;
2882 list_remove( &hotkey->entry );
2883 free( hotkey );
2884 release_object( desktop );
2887 /* attach (or detach) thread inputs */
2888 DECL_HANDLER(attach_thread_input)
2890 struct thread *thread_from = get_thread_from_id( req->tid_from );
2891 struct thread *thread_to = get_thread_from_id( req->tid_to );
2893 if (!thread_from || !thread_to)
2895 if (thread_from) release_object( thread_from );
2896 if (thread_to) release_object( thread_to );
2897 return;
2899 if (thread_from != thread_to)
2901 if (req->attach)
2903 if ((thread_to->queue || thread_to == current) &&
2904 (thread_from->queue || thread_from == current))
2905 attach_thread_input( thread_from, thread_to );
2906 else
2907 set_error( STATUS_INVALID_PARAMETER );
2909 else
2911 if (thread_from->queue && thread_to->queue &&
2912 thread_from->queue->input == thread_to->queue->input)
2913 detach_thread_input( thread_from );
2914 else
2915 set_error( STATUS_ACCESS_DENIED );
2918 else set_error( STATUS_ACCESS_DENIED );
2919 release_object( thread_from );
2920 release_object( thread_to );
2924 /* get thread input data */
2925 DECL_HANDLER(get_thread_input)
2927 struct thread *thread = NULL;
2928 struct desktop *desktop;
2929 struct thread_input *input;
2931 if (req->tid)
2933 if (!(thread = get_thread_from_id( req->tid ))) return;
2934 if (!(desktop = get_thread_desktop( thread, 0 )))
2936 release_object( thread );
2937 return;
2939 input = thread->queue ? thread->queue->input : NULL;
2941 else
2943 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2944 input = desktop->foreground_input; /* get the foreground thread info */
2947 if (input)
2949 reply->focus = input->focus;
2950 reply->capture = input->capture;
2951 reply->active = input->active;
2952 reply->menu_owner = input->menu_owner;
2953 reply->move_size = input->move_size;
2954 reply->caret = input->caret;
2955 reply->cursor = input->cursor;
2956 reply->show_count = input->cursor_count;
2957 reply->rect = input->caret_rect;
2960 /* foreground window is active window of foreground thread */
2961 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2962 if (thread) release_object( thread );
2963 release_object( desktop );
2967 /* retrieve queue keyboard state for a given thread */
2968 DECL_HANDLER(get_key_state)
2970 struct thread *thread;
2971 struct desktop *desktop;
2972 data_size_t size = min( 256, get_reply_max_size() );
2974 if (!req->tid) /* get global async key state */
2976 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2977 if (req->key >= 0)
2979 reply->state = desktop->keystate[req->key & 0xff];
2980 desktop->keystate[req->key & 0xff] &= ~0x40;
2982 set_reply_data( desktop->keystate, size );
2983 release_object( desktop );
2985 else
2987 unsigned char *keystate;
2988 if (!(thread = get_thread_from_id( req->tid ))) return;
2989 if (thread->queue)
2991 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2992 set_reply_data( thread->queue->input->keystate, size );
2993 release_object( thread );
2994 return;
2996 release_object( thread );
2998 /* fallback to desktop keystate */
2999 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3000 if (req->key >= 0) reply->state = desktop->keystate[req->key & 0xff] & ~0x40;
3001 if ((keystate = set_reply_data_size( size )))
3003 unsigned int i;
3004 for (i = 0; i < size; i++) keystate[i] = desktop->keystate[i] & ~0x40;
3006 release_object( desktop );
3011 /* set queue keyboard state for a given thread */
3012 DECL_HANDLER(set_key_state)
3014 struct thread *thread;
3015 struct desktop *desktop;
3016 data_size_t size = min( 256, get_req_data_size() );
3018 if (!req->tid) /* set global async key state */
3020 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3021 memcpy( desktop->keystate, get_req_data(), size );
3022 release_object( desktop );
3024 else
3026 if (!(thread = get_thread_from_id( req->tid ))) return;
3027 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
3028 if (req->async && (desktop = get_thread_desktop( thread, 0 )))
3030 memcpy( desktop->keystate, get_req_data(), size );
3031 release_object( desktop );
3033 release_object( thread );
3038 /* set the system foreground window */
3039 DECL_HANDLER(set_foreground_window)
3041 struct thread *thread = NULL;
3042 struct desktop *desktop;
3043 struct msg_queue *queue = get_current_queue();
3045 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3046 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
3047 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
3048 reply->send_msg_new = FALSE;
3050 if (is_valid_foreground_window( req->handle ) &&
3051 (thread = get_window_thread( req->handle )) &&
3052 thread->queue->input->desktop == desktop)
3054 set_foreground_input( desktop, thread->queue->input );
3055 reply->send_msg_new = (desktop->foreground_input != queue->input);
3057 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
3059 if (thread) release_object( thread );
3060 release_object( desktop );
3064 /* set the current thread focus window */
3065 DECL_HANDLER(set_focus_window)
3067 struct msg_queue *queue = get_current_queue();
3069 reply->previous = 0;
3070 if (queue && check_queue_input_window( queue, req->handle ))
3072 reply->previous = queue->input->focus;
3073 queue->input->focus = get_user_full_handle( req->handle );
3078 /* set the current thread active window */
3079 DECL_HANDLER(set_active_window)
3081 struct msg_queue *queue = get_current_queue();
3083 reply->previous = 0;
3084 if (queue && check_queue_input_window( queue, req->handle ))
3086 if (!req->handle || make_window_active( req->handle ))
3088 reply->previous = queue->input->active;
3089 queue->input->active = get_user_full_handle( req->handle );
3091 else set_error( STATUS_INVALID_HANDLE );
3096 /* set the current thread capture window */
3097 DECL_HANDLER(set_capture_window)
3099 struct msg_queue *queue = get_current_queue();
3101 reply->previous = reply->full_handle = 0;
3102 if (queue && check_queue_input_window( queue, req->handle ))
3104 struct thread_input *input = queue->input;
3106 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
3107 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
3109 set_error(STATUS_ACCESS_DENIED);
3110 return;
3112 reply->previous = input->capture;
3113 input->capture = get_user_full_handle( req->handle );
3114 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
3115 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
3116 reply->full_handle = input->capture;
3121 /* Set the current thread caret window */
3122 DECL_HANDLER(set_caret_window)
3124 struct msg_queue *queue = get_current_queue();
3126 reply->previous = 0;
3127 if (queue && check_queue_input_window( queue, req->handle ))
3129 struct thread_input *input = queue->input;
3131 reply->previous = input->caret;
3132 reply->old_rect = input->caret_rect;
3133 reply->old_hide = input->caret_hide;
3134 reply->old_state = input->caret_state;
3136 set_caret_window( input, get_user_full_handle(req->handle) );
3137 input->caret_rect.right = input->caret_rect.left + req->width;
3138 input->caret_rect.bottom = input->caret_rect.top + req->height;
3143 /* Set the current thread caret information */
3144 DECL_HANDLER(set_caret_info)
3146 struct msg_queue *queue = get_current_queue();
3147 struct thread_input *input;
3149 if (!queue) return;
3150 input = queue->input;
3151 reply->full_handle = input->caret;
3152 reply->old_rect = input->caret_rect;
3153 reply->old_hide = input->caret_hide;
3154 reply->old_state = input->caret_state;
3156 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3158 set_error( STATUS_ACCESS_DENIED );
3159 return;
3161 if (req->flags & SET_CARET_POS)
3163 input->caret_rect.right += req->x - input->caret_rect.left;
3164 input->caret_rect.bottom += req->y - input->caret_rect.top;
3165 input->caret_rect.left = req->x;
3166 input->caret_rect.top = req->y;
3168 if (req->flags & SET_CARET_HIDE)
3170 input->caret_hide += req->hide;
3171 if (input->caret_hide < 0) input->caret_hide = 0;
3173 if (req->flags & SET_CARET_STATE)
3175 switch (req->state)
3177 case CARET_STATE_OFF: input->caret_state = 0; break;
3178 case CARET_STATE_ON: input->caret_state = 1; break;
3179 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3180 case CARET_STATE_ON_IF_MOVED:
3181 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3182 break;
3188 /* get the time of the last input event */
3189 DECL_HANDLER(get_last_input_time)
3191 reply->time = last_input_time;
3194 /* set/get the current cursor */
3195 DECL_HANDLER(set_cursor)
3197 struct msg_queue *queue = get_current_queue();
3198 struct thread_input *input;
3200 if (!queue) return;
3201 input = queue->input;
3203 reply->prev_handle = input->cursor;
3204 reply->prev_count = input->cursor_count;
3205 reply->prev_x = input->desktop->cursor.x;
3206 reply->prev_y = input->desktop->cursor.y;
3208 if (req->flags & SET_CURSOR_HANDLE)
3210 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3212 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3213 return;
3215 input->cursor = req->handle;
3217 if (req->flags & SET_CURSOR_COUNT)
3219 queue->cursor_count += req->show_count;
3220 input->cursor_count += req->show_count;
3222 if (req->flags & SET_CURSOR_POS)
3224 set_cursor_pos( input->desktop, req->x, req->y );
3226 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3228 struct desktop *desktop = input->desktop;
3230 /* only the desktop owner can set the message */
3231 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3232 desktop->cursor.clip_msg = req->clip_msg;
3234 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip, 0 );
3237 reply->new_x = input->desktop->cursor.x;
3238 reply->new_y = input->desktop->cursor.y;
3239 reply->new_clip = input->desktop->cursor.clip;
3240 reply->last_change = input->desktop->cursor.last_change;
3243 /* Get the history of the 64 last cursor positions */
3244 DECL_HANDLER(get_cursor_history)
3246 cursor_pos_t *pos;
3247 unsigned int i, count = min( 64, get_reply_max_size() / sizeof(*pos) );
3249 if ((pos = set_reply_data_size( count * sizeof(*pos) )))
3250 for (i = 0; i < count; i++)
3251 pos[i] = cursor_history[(i + cursor_history_latest) % ARRAY_SIZE(cursor_history)];
3254 DECL_HANDLER(get_rawinput_buffer)
3256 struct thread_input *input = current->queue->input;
3257 data_size_t size = 0, next_size = 0;
3258 struct list *ptr;
3259 char *buf, *cur;
3260 int count = 0;
3262 if (!req->buffer_size) buf = NULL;
3263 else if (!(buf = mem_alloc( get_reply_max_size() )))
3264 return;
3266 cur = buf;
3267 ptr = list_head( &input->msg_list );
3268 while (ptr)
3270 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
3271 struct hardware_msg_data *data = msg->data;
3273 ptr = list_next( &input->msg_list, ptr );
3274 if (msg->msg != WM_INPUT) continue;
3276 next_size = req->rawinput_size;
3277 if (size + next_size > req->buffer_size) break;
3278 if (cur + sizeof(*data) > buf + get_reply_max_size()) break;
3280 memcpy(cur, data, sizeof(*data));
3281 list_remove( &msg->entry );
3282 free_message( msg );
3284 size += next_size;
3285 cur += sizeof(*data);
3286 count++;
3289 reply->next_size = next_size;
3290 reply->count = count;
3291 set_reply_data_ptr( buf, cur - buf );
3294 DECL_HANDLER(update_rawinput_devices)
3296 const struct rawinput_device *devices = get_req_data();
3297 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3298 const struct rawinput_device_entry *e;
3299 unsigned int i;
3301 for (i = 0; i < device_count; ++i)
3303 update_rawinput_device(&devices[i]);
3306 e = find_rawinput_device( 1, 2 );
3307 current->process->rawinput_mouse = e ? &e->device : NULL;
3308 e = find_rawinput_device( 1, 6 );
3309 current->process->rawinput_kbd = e ? &e->device : NULL;
3312 DECL_HANDLER(get_rawinput_devices)
3314 struct rawinput_device_entry *e;
3315 struct rawinput_device *devices;
3316 unsigned int i = 0, device_count = list_count( &current->process->rawinput_devices );
3317 unsigned int size = device_count * sizeof(*devices);
3319 reply->device_count = device_count;
3321 /* no buffer provided, nothing else to do */
3322 if (!get_reply_max_size()) return;
3324 if (size > get_reply_max_size())
3325 set_error( STATUS_BUFFER_TOO_SMALL );
3326 else if ((devices = set_reply_data_size( size )))
3328 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
3329 devices[i++] = e->device;