server: Support variable sized hardware_msg_data allocation.
[wine.git] / server / queue.c
blob76ee469ffefb5bffcd8fec1440ff30e6c1c5465a
1 /*
2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #ifdef HAVE_POLL_H
29 # include <poll.h>
30 #endif
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "winternl.h"
40 #include "handle.h"
41 #include "file.h"
42 #include "thread.h"
43 #include "process.h"
44 #include "request.h"
45 #include "user.h"
47 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
48 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
50 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
51 #define NB_MSG_KINDS (POST_MESSAGE+1)
54 struct message_result
56 struct list sender_entry; /* entry in sender list */
57 struct message *msg; /* message the result is for */
58 struct message_result *recv_next; /* next in receiver list */
59 struct msg_queue *sender; /* sender queue */
60 struct msg_queue *receiver; /* receiver queue */
61 int replied; /* has it been replied to? */
62 unsigned int error; /* error code to pass back to sender */
63 lparam_t result; /* reply result */
64 struct message *hardware_msg; /* hardware message if low-level hook result */
65 struct desktop *desktop; /* desktop for hardware message */
66 struct message *callback_msg; /* message to queue for callback */
67 void *data; /* message reply data */
68 unsigned int data_size; /* size of message reply data */
69 struct timeout_user *timeout; /* result timeout */
72 struct message
74 struct list entry; /* entry in message list */
75 enum message_type type; /* message type */
76 user_handle_t win; /* window handle */
77 unsigned int msg; /* message code */
78 lparam_t wparam; /* parameters */
79 lparam_t lparam; /* parameters */
80 int x; /* message position */
81 int y;
82 unsigned int time; /* message time */
83 void *data; /* message data for sent messages */
84 unsigned int data_size; /* size of message data */
85 unsigned int unique_id; /* unique id for nested hw message waits */
86 struct message_result *result; /* result in sender queue */
89 struct timer
91 struct list entry; /* entry in timer list */
92 abstime_t when; /* next expiration */
93 unsigned int rate; /* timer rate in ms */
94 user_handle_t win; /* window handle */
95 unsigned int msg; /* message to post */
96 lparam_t id; /* timer id */
97 lparam_t lparam; /* lparam for message */
100 struct thread_input
102 struct object obj; /* object header */
103 struct desktop *desktop; /* desktop that this thread input belongs to */
104 user_handle_t focus; /* focus window */
105 user_handle_t capture; /* capture window */
106 user_handle_t active; /* active window */
107 user_handle_t menu_owner; /* current menu owner window */
108 user_handle_t move_size; /* current moving/resizing window */
109 user_handle_t caret; /* caret window */
110 rectangle_t caret_rect; /* caret rectangle */
111 int caret_hide; /* caret hide count */
112 int caret_state; /* caret on/off state */
113 user_handle_t cursor; /* current cursor */
114 int cursor_count; /* cursor show count */
115 struct list msg_list; /* list of hardware messages */
116 unsigned char keystate[256]; /* state of each key */
119 struct msg_queue
121 struct object obj; /* object header */
122 struct fd *fd; /* optional file descriptor to poll */
123 unsigned int wake_bits; /* wakeup bits */
124 unsigned int wake_mask; /* wakeup mask */
125 unsigned int changed_bits; /* changed wakeup bits */
126 unsigned int changed_mask; /* changed wakeup mask */
127 int paint_count; /* pending paint messages count */
128 int hotkey_count; /* pending hotkey messages count */
129 int quit_message; /* is there a pending quit message? */
130 int exit_code; /* exit code of pending quit message */
131 int cursor_count; /* per-queue cursor show count */
132 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
133 struct list send_result; /* stack of sent messages waiting for result */
134 struct list callback_result; /* list of callback messages waiting for result */
135 struct message_result *recv_result; /* stack of received messages waiting for result */
136 struct list pending_timers; /* list of pending timers */
137 struct list expired_timers; /* list of expired timers */
138 lparam_t next_timer_id; /* id for the next timer with a 0 window */
139 struct timeout_user *timeout; /* timeout for next timer to expire */
140 struct thread_input *input; /* thread input descriptor */
141 struct hook_table *hooks; /* hook table */
142 timeout_t last_get_msg; /* time of last get message call */
145 struct hotkey
147 struct list entry; /* entry in desktop hotkey list */
148 struct msg_queue *queue; /* queue owning this hotkey */
149 user_handle_t win; /* window handle */
150 int id; /* hotkey id */
151 unsigned int vkey; /* virtual key code */
152 unsigned int flags; /* key modifiers */
155 static void msg_queue_dump( struct object *obj, int verbose );
156 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
157 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
158 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
159 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
160 static void msg_queue_destroy( struct object *obj );
161 static void msg_queue_poll_event( struct fd *fd, int event );
162 static void thread_input_dump( struct object *obj, int verbose );
163 static void thread_input_destroy( struct object *obj );
164 static void timer_callback( void *private );
166 static const struct object_ops msg_queue_ops =
168 sizeof(struct msg_queue), /* size */
169 &no_type, /* type */
170 msg_queue_dump, /* dump */
171 msg_queue_add_queue, /* add_queue */
172 msg_queue_remove_queue, /* remove_queue */
173 msg_queue_signaled, /* signaled */
174 msg_queue_satisfied, /* satisfied */
175 no_signal, /* signal */
176 no_get_fd, /* get_fd */
177 default_map_access, /* map_access */
178 default_get_sd, /* get_sd */
179 default_set_sd, /* set_sd */
180 no_get_full_name, /* get_full_name */
181 no_lookup_name, /* lookup_name */
182 no_link_name, /* link_name */
183 NULL, /* unlink_name */
184 no_open_file, /* open_file */
185 no_kernel_obj_list, /* get_kernel_obj_list */
186 no_close_handle, /* close_handle */
187 msg_queue_destroy /* destroy */
190 static const struct fd_ops msg_queue_fd_ops =
192 NULL, /* get_poll_events */
193 msg_queue_poll_event, /* poll_event */
194 NULL, /* flush */
195 NULL, /* get_fd_type */
196 NULL, /* ioctl */
197 NULL, /* queue_async */
198 NULL, /* reselect_async */
199 NULL /* cancel async */
203 static const struct object_ops thread_input_ops =
205 sizeof(struct thread_input), /* size */
206 &no_type, /* type */
207 thread_input_dump, /* dump */
208 no_add_queue, /* add_queue */
209 NULL, /* remove_queue */
210 NULL, /* signaled */
211 NULL, /* satisfied */
212 no_signal, /* signal */
213 no_get_fd, /* get_fd */
214 default_map_access, /* map_access */
215 default_get_sd, /* get_sd */
216 default_set_sd, /* set_sd */
217 no_get_full_name, /* get_full_name */
218 no_lookup_name, /* lookup_name */
219 no_link_name, /* link_name */
220 NULL, /* unlink_name */
221 no_open_file, /* open_file */
222 no_kernel_obj_list, /* get_kernel_obj_list */
223 no_close_handle, /* close_handle */
224 thread_input_destroy /* destroy */
227 /* pointer to input structure of foreground thread */
228 static unsigned int last_input_time;
230 static cursor_pos_t cursor_history[64];
231 static unsigned int cursor_history_latest;
233 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
234 static void free_message( struct message *msg );
236 /* set the caret window in a given thread input */
237 static void set_caret_window( struct thread_input *input, user_handle_t win )
239 if (!win || win != input->caret)
241 input->caret_rect.left = 0;
242 input->caret_rect.top = 0;
243 input->caret_rect.right = 0;
244 input->caret_rect.bottom = 0;
246 input->caret = win;
247 input->caret_hide = 1;
248 input->caret_state = 0;
251 /* create a thread input object */
252 static struct thread_input *create_thread_input( struct thread *thread )
254 struct thread_input *input;
256 if ((input = alloc_object( &thread_input_ops )))
258 input->focus = 0;
259 input->capture = 0;
260 input->active = 0;
261 input->menu_owner = 0;
262 input->move_size = 0;
263 input->cursor = 0;
264 input->cursor_count = 0;
265 list_init( &input->msg_list );
266 set_caret_window( input, 0 );
267 memset( input->keystate, 0, sizeof(input->keystate) );
269 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
271 release_object( input );
272 return NULL;
275 return input;
278 /* create a message queue object */
279 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
281 struct thread_input *new_input = NULL;
282 struct msg_queue *queue;
283 int i;
285 if (!input)
287 if (!(new_input = create_thread_input( thread ))) return NULL;
288 input = new_input;
291 if ((queue = alloc_object( &msg_queue_ops )))
293 queue->fd = NULL;
294 queue->wake_bits = 0;
295 queue->wake_mask = 0;
296 queue->changed_bits = 0;
297 queue->changed_mask = 0;
298 queue->paint_count = 0;
299 queue->hotkey_count = 0;
300 queue->quit_message = 0;
301 queue->cursor_count = 0;
302 queue->recv_result = NULL;
303 queue->next_timer_id = 0x7fff;
304 queue->timeout = NULL;
305 queue->input = (struct thread_input *)grab_object( input );
306 queue->hooks = NULL;
307 queue->last_get_msg = current_time;
308 list_init( &queue->send_result );
309 list_init( &queue->callback_result );
310 list_init( &queue->pending_timers );
311 list_init( &queue->expired_timers );
312 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
314 thread->queue = queue;
316 if (new_input) release_object( new_input );
317 return queue;
320 /* free the message queue of a thread at thread exit */
321 void free_msg_queue( struct thread *thread )
323 remove_thread_hooks( thread );
324 if (!thread->queue) return;
325 release_object( thread->queue );
326 thread->queue = NULL;
329 /* change the thread input data of a given thread */
330 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
332 struct msg_queue *queue = thread->queue;
334 if (!queue)
336 thread->queue = create_msg_queue( thread, new_input );
337 return thread->queue != NULL;
339 if (queue->input)
341 queue->input->cursor_count -= queue->cursor_count;
342 release_object( queue->input );
344 queue->input = (struct thread_input *)grab_object( new_input );
345 new_input->cursor_count += queue->cursor_count;
346 return 1;
349 /* allocate a hardware message and its data */
350 static struct message *alloc_hardware_message( lparam_t info, struct hw_msg_source source,
351 unsigned int time, data_size_t extra_size )
353 struct hardware_msg_data *msg_data;
354 struct message *msg;
356 if (!(msg = mem_alloc( sizeof(*msg) ))) return NULL;
357 if (!(msg_data = mem_alloc( sizeof(*msg_data) + extra_size )))
359 free( msg );
360 return NULL;
362 memset( msg, 0, sizeof(*msg) );
363 msg->type = MSG_HARDWARE;
364 msg->time = time;
365 msg->data = msg_data;
366 msg->data_size = sizeof(*msg_data) + extra_size;
368 memset( msg_data, 0, sizeof(*msg_data) + extra_size );
369 msg_data->info = info;
370 msg_data->size = msg->data_size;
371 msg_data->source = source;
372 return msg;
375 static int update_desktop_cursor_pos( struct desktop *desktop, int x, int y )
377 int updated;
379 x = max( min( x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
380 y = max( min( y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
381 updated = (desktop->cursor.x != x || desktop->cursor.y != y);
382 desktop->cursor.x = x;
383 desktop->cursor.y = y;
384 desktop->cursor.last_change = get_tick_count();
386 return updated;
389 /* set the cursor position and queue the corresponding mouse message */
390 static void set_cursor_pos( struct desktop *desktop, int x, int y )
392 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
393 const struct rawinput_device *device;
394 struct message *msg;
396 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
398 update_desktop_cursor_pos( desktop, x, y );
399 return;
402 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
404 msg->msg = WM_MOUSEMOVE;
405 msg->x = x;
406 msg->y = y;
407 queue_hardware_message( desktop, msg, 1 );
410 /* retrieve default position and time for synthesized messages */
411 static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
413 struct desktop *desktop = queue->input->desktop;
415 *x = desktop->cursor.x;
416 *y = desktop->cursor.y;
417 *time = get_tick_count();
420 /* set the cursor clip rectangle */
421 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect, int send_clip_msg )
423 rectangle_t top_rect;
424 int x, y;
426 get_top_window_rectangle( desktop, &top_rect );
427 if (rect)
429 rectangle_t new_rect = *rect;
430 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
431 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
432 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
433 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
434 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
435 desktop->cursor.clip = new_rect;
437 else desktop->cursor.clip = top_rect;
439 if (desktop->cursor.clip_msg && send_clip_msg)
440 post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
442 /* warp the mouse to be inside the clip rect */
443 x = max( min( desktop->cursor.x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
444 y = max( min( desktop->cursor.y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
445 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
448 /* change the foreground input and reset the cursor clip rect */
449 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
451 if (desktop->foreground_input == input) return;
452 set_clip_rectangle( desktop, NULL, 1 );
453 desktop->foreground_input = input;
456 /* get the hook table for a given thread */
457 struct hook_table *get_queue_hooks( struct thread *thread )
459 if (!thread->queue) return NULL;
460 return thread->queue->hooks;
463 /* set the hook table for a given thread, allocating the queue if needed */
464 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
466 struct msg_queue *queue = thread->queue;
467 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
468 if (queue->hooks) release_object( queue->hooks );
469 queue->hooks = hooks;
472 /* check the queue status */
473 static inline int is_signaled( struct msg_queue *queue )
475 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
478 /* set some queue bits */
479 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
481 queue->wake_bits |= bits;
482 queue->changed_bits |= bits;
483 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
486 /* clear some queue bits */
487 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
489 queue->wake_bits &= ~bits;
490 queue->changed_bits &= ~bits;
493 /* check whether msg is a keyboard message */
494 static inline int is_keyboard_msg( struct message *msg )
496 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
499 /* check if message is matched by the filter */
500 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
502 return (msg >= first && msg <= last);
505 /* check whether a message filter contains at least one potential hardware message */
506 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
508 /* hardware message ranges are (in numerical order):
509 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
510 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
511 * WM_MOUSEFIRST .. WM_MOUSELAST
513 if (last < WM_NCMOUSEFIRST) return 0;
514 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
515 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
516 if (first > WM_MOUSELAST) return 0;
517 return 1;
520 /* get the QS_* bit corresponding to a given hardware message */
521 static inline int get_hardware_msg_bit( struct message *msg )
523 if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
524 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
525 if (is_keyboard_msg( msg )) return QS_KEY;
526 return QS_MOUSEBUTTON;
529 /* get the current thread queue, creating it if needed */
530 static inline struct msg_queue *get_current_queue(void)
532 struct msg_queue *queue = current->queue;
533 if (!queue) queue = create_msg_queue( current, NULL );
534 return queue;
537 /* get a (pseudo-)unique id to tag hardware messages */
538 static inline unsigned int get_unique_id(void)
540 static unsigned int id;
541 if (!++id) id = 1; /* avoid an id of 0 */
542 return id;
545 /* try to merge a message with the last in the list; return 1 if successful */
546 static int merge_message( struct thread_input *input, const struct message *msg )
548 struct message *prev;
549 struct list *ptr;
551 if (msg->msg != WM_MOUSEMOVE) return 0;
552 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
554 prev = LIST_ENTRY( ptr, struct message, entry );
555 if (prev->msg != WM_INPUT) break;
557 if (!ptr) return 0;
558 if (prev->result) return 0;
559 if (prev->win && msg->win && prev->win != msg->win) return 0;
560 if (prev->msg != msg->msg) return 0;
561 if (prev->type != msg->type) return 0;
562 /* now we can merge it */
563 prev->wparam = msg->wparam;
564 prev->lparam = msg->lparam;
565 prev->x = msg->x;
566 prev->y = msg->y;
567 prev->time = msg->time;
568 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
570 struct hardware_msg_data *prev_data = prev->data;
571 struct hardware_msg_data *msg_data = msg->data;
572 prev_data->info = msg_data->info;
574 list_remove( ptr );
575 list_add_tail( &input->msg_list, ptr );
576 return 1;
579 /* free a result structure */
580 static void free_result( struct message_result *result )
582 if (result->timeout) remove_timeout_user( result->timeout );
583 free( result->data );
584 if (result->callback_msg) free_message( result->callback_msg );
585 if (result->hardware_msg) free_message( result->hardware_msg );
586 if (result->desktop) release_object( result->desktop );
587 free( result );
590 /* remove the result from the sender list it is on */
591 static inline void remove_result_from_sender( struct message_result *result )
593 assert( result->sender );
595 list_remove( &result->sender_entry );
596 result->sender = NULL;
597 if (!result->receiver) free_result( result );
600 /* store the message result in the appropriate structure */
601 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
603 res->result = result;
604 res->error = error;
605 res->replied = 1;
606 if (res->timeout)
608 remove_timeout_user( res->timeout );
609 res->timeout = NULL;
612 if (res->hardware_msg)
614 if (!error && result) /* rejected by the hook */
615 free_message( res->hardware_msg );
616 else
617 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
619 res->hardware_msg = NULL;
622 if (res->sender)
624 if (res->callback_msg)
626 /* queue the callback message in the sender queue */
627 struct callback_msg_data *data = res->callback_msg->data;
628 data->result = result;
629 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
630 set_queue_bits( res->sender, QS_SENDMESSAGE );
631 res->callback_msg = NULL;
632 remove_result_from_sender( res );
634 else
636 /* wake sender queue if waiting on this result */
637 if (list_head(&res->sender->send_result) == &res->sender_entry)
638 set_queue_bits( res->sender, QS_SMRESULT );
641 else if (!res->receiver) free_result( res );
644 /* free a message when deleting a queue or window */
645 static void free_message( struct message *msg )
647 struct message_result *result = msg->result;
648 if (result)
650 result->msg = NULL;
651 result->receiver = NULL;
652 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
654 free( msg->data );
655 free( msg );
658 /* remove (and free) a message from a message list */
659 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
660 enum message_kind kind )
662 list_remove( &msg->entry );
663 switch(kind)
665 case SEND_MESSAGE:
666 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
667 break;
668 case POST_MESSAGE:
669 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
670 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
671 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
672 clear_queue_bits( queue, QS_HOTKEY );
673 break;
675 free_message( msg );
678 /* message timed out without getting a reply */
679 static void result_timeout( void *private )
681 struct message_result *result = private;
683 assert( !result->replied );
685 result->timeout = NULL;
687 if (result->msg) /* not received yet */
689 struct message *msg = result->msg;
691 result->msg = NULL;
692 msg->result = NULL;
693 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
694 result->receiver = NULL;
696 store_message_result( result, 0, STATUS_TIMEOUT );
699 /* allocate and fill a message result structure */
700 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
701 struct msg_queue *recv_queue,
702 struct message *msg, timeout_t timeout )
704 struct message_result *result = mem_alloc( sizeof(*result) );
705 if (result)
707 result->msg = msg;
708 result->sender = send_queue;
709 result->receiver = recv_queue;
710 result->replied = 0;
711 result->data = NULL;
712 result->data_size = 0;
713 result->timeout = NULL;
714 result->hardware_msg = NULL;
715 result->desktop = NULL;
716 result->callback_msg = NULL;
718 if (msg->type == MSG_CALLBACK)
720 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
722 if (!callback_msg)
724 free( result );
725 return NULL;
727 callback_msg->type = MSG_CALLBACK_RESULT;
728 callback_msg->win = msg->win;
729 callback_msg->msg = msg->msg;
730 callback_msg->wparam = 0;
731 callback_msg->lparam = 0;
732 callback_msg->time = get_tick_count();
733 callback_msg->result = NULL;
734 /* steal the data from the original message */
735 callback_msg->data = msg->data;
736 callback_msg->data_size = msg->data_size;
737 msg->data = NULL;
738 msg->data_size = 0;
740 result->callback_msg = callback_msg;
741 list_add_head( &send_queue->callback_result, &result->sender_entry );
743 else if (send_queue)
745 list_add_head( &send_queue->send_result, &result->sender_entry );
746 clear_queue_bits( send_queue, QS_SMRESULT );
749 if (timeout != TIMEOUT_INFINITE)
750 result->timeout = add_timeout_user( timeout, result_timeout, result );
752 return result;
755 /* receive a message, removing it from the sent queue */
756 static void receive_message( struct msg_queue *queue, struct message *msg,
757 struct get_message_reply *reply )
759 struct message_result *result = msg->result;
761 reply->total = msg->data_size;
762 if (msg->data_size > get_reply_max_size())
764 set_error( STATUS_BUFFER_OVERFLOW );
765 return;
767 reply->type = msg->type;
768 reply->win = msg->win;
769 reply->msg = msg->msg;
770 reply->wparam = msg->wparam;
771 reply->lparam = msg->lparam;
772 reply->x = msg->x;
773 reply->y = msg->y;
774 reply->time = msg->time;
776 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
778 list_remove( &msg->entry );
779 /* put the result on the receiver result stack */
780 if (result)
782 result->msg = NULL;
783 result->recv_next = queue->recv_result;
784 queue->recv_result = result;
786 free( msg );
787 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
790 /* set the result of the current received message */
791 static void reply_message( struct msg_queue *queue, lparam_t result,
792 unsigned int error, int remove, const void *data, data_size_t len )
794 struct message_result *res = queue->recv_result;
796 if (remove)
798 queue->recv_result = res->recv_next;
799 res->receiver = NULL;
800 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
802 free_result( res );
803 return;
806 if (!res->replied)
808 if (len && (res->data = memdup( data, len ))) res->data_size = len;
809 store_message_result( res, result, error );
813 static int match_window( user_handle_t win, user_handle_t msg_win )
815 if (!win) return 1;
816 if (win == -1 || win == 1) return !msg_win;
817 if (msg_win == win) return 1;
818 return is_child_window( win, msg_win );
821 /* retrieve a posted message */
822 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
823 unsigned int first, unsigned int last, unsigned int flags,
824 struct get_message_reply *reply )
826 struct message *msg;
828 /* check against the filters */
829 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
831 if (!match_window( win, msg->win )) continue;
832 if (!check_msg_filter( msg->msg, first, last )) continue;
833 goto found; /* found one */
835 return 0;
837 /* return it to the app */
838 found:
839 reply->total = msg->data_size;
840 if (msg->data_size > get_reply_max_size())
842 set_error( STATUS_BUFFER_OVERFLOW );
843 return 1;
845 reply->type = msg->type;
846 reply->win = msg->win;
847 reply->msg = msg->msg;
848 reply->wparam = msg->wparam;
849 reply->lparam = msg->lparam;
850 reply->x = msg->x;
851 reply->y = msg->y;
852 reply->time = msg->time;
854 if (flags & PM_REMOVE)
856 if (msg->data)
858 set_reply_data_ptr( msg->data, msg->data_size );
859 msg->data = NULL;
860 msg->data_size = 0;
862 remove_queue_message( queue, msg, POST_MESSAGE );
864 else if (msg->data) set_reply_data( msg->data, msg->data_size );
866 return 1;
869 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
870 struct get_message_reply *reply )
872 if (queue->quit_message)
874 reply->total = 0;
875 reply->type = MSG_POSTED;
876 reply->win = 0;
877 reply->msg = WM_QUIT;
878 reply->wparam = queue->exit_code;
879 reply->lparam = 0;
881 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
883 if (flags & PM_REMOVE)
885 queue->quit_message = 0;
886 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
887 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
889 return 1;
891 else
892 return 0;
895 /* empty a message list and free all the messages */
896 static void empty_msg_list( struct list *list )
898 struct list *ptr;
900 while ((ptr = list_head( list )) != NULL)
902 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
903 list_remove( &msg->entry );
904 free_message( msg );
908 /* cleanup all pending results when deleting a queue */
909 static void cleanup_results( struct msg_queue *queue )
911 struct list *entry;
913 while ((entry = list_head( &queue->send_result )) != NULL)
915 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
918 while ((entry = list_head( &queue->callback_result )) != NULL)
920 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
923 while (queue->recv_result)
924 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
927 /* check if the thread owning the queue is hung (not checking for messages) */
928 static int is_queue_hung( struct msg_queue *queue )
930 struct wait_queue_entry *entry;
932 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
933 return 0; /* less than 5 seconds since last get message -> not hung */
935 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
937 if (get_wait_queue_thread(entry)->queue == queue)
938 return 0; /* thread is waiting on queue -> not hung */
940 return 1;
943 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
945 struct msg_queue *queue = (struct msg_queue *)obj;
946 struct process *process = get_wait_queue_thread(entry)->process;
948 /* a thread can only wait on its own queue */
949 if (get_wait_queue_thread(entry)->queue != queue)
951 set_error( STATUS_ACCESS_DENIED );
952 return 0;
954 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
956 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
957 set_fd_events( queue->fd, POLLIN );
958 add_queue( obj, entry );
959 return 1;
962 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
964 struct msg_queue *queue = (struct msg_queue *)obj;
966 remove_queue( obj, entry );
967 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
968 set_fd_events( queue->fd, 0 );
971 static void msg_queue_dump( struct object *obj, int verbose )
973 struct msg_queue *queue = (struct msg_queue *)obj;
974 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
975 queue->wake_bits, queue->wake_mask );
978 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
980 struct msg_queue *queue = (struct msg_queue *)obj;
981 int ret = 0;
983 if (queue->fd)
985 if ((ret = check_fd_events( queue->fd, POLLIN )))
986 /* stop waiting on select() if we are signaled */
987 set_fd_events( queue->fd, 0 );
988 else if (!list_empty( &obj->wait_queue ))
989 /* restart waiting on poll() if we are no longer signaled */
990 set_fd_events( queue->fd, POLLIN );
992 return ret || is_signaled( queue );
995 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
997 struct msg_queue *queue = (struct msg_queue *)obj;
998 queue->wake_mask = 0;
999 queue->changed_mask = 0;
1002 static void msg_queue_destroy( struct object *obj )
1004 struct msg_queue *queue = (struct msg_queue *)obj;
1005 struct list *ptr;
1006 struct hotkey *hotkey, *hotkey2;
1007 int i;
1009 cleanup_results( queue );
1010 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
1012 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
1014 if (hotkey->queue == queue)
1016 list_remove( &hotkey->entry );
1017 free( hotkey );
1021 while ((ptr = list_head( &queue->pending_timers )))
1023 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1024 list_remove( &timer->entry );
1025 free( timer );
1027 while ((ptr = list_head( &queue->expired_timers )))
1029 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1030 list_remove( &timer->entry );
1031 free( timer );
1033 if (queue->timeout) remove_timeout_user( queue->timeout );
1034 queue->input->cursor_count -= queue->cursor_count;
1035 release_object( queue->input );
1036 if (queue->hooks) release_object( queue->hooks );
1037 if (queue->fd) release_object( queue->fd );
1040 static void msg_queue_poll_event( struct fd *fd, int event )
1042 struct msg_queue *queue = get_fd_user( fd );
1043 assert( queue->obj.ops == &msg_queue_ops );
1045 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1046 else set_fd_events( queue->fd, 0 );
1047 wake_up( &queue->obj, 0 );
1050 static void thread_input_dump( struct object *obj, int verbose )
1052 struct thread_input *input = (struct thread_input *)obj;
1053 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
1054 input->focus, input->capture, input->active );
1057 static void thread_input_destroy( struct object *obj )
1059 struct thread_input *input = (struct thread_input *)obj;
1061 empty_msg_list( &input->msg_list );
1062 if (input->desktop)
1064 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
1065 release_object( input->desktop );
1069 /* fix the thread input data when a window is destroyed */
1070 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1072 struct thread_input *input = queue->input;
1074 if (window == input->focus) input->focus = 0;
1075 if (window == input->capture) input->capture = 0;
1076 if (window == input->active) input->active = 0;
1077 if (window == input->menu_owner) input->menu_owner = 0;
1078 if (window == input->move_size) input->move_size = 0;
1079 if (window == input->caret) set_caret_window( input, 0 );
1082 /* check if the specified window can be set in the input data of a given queue */
1083 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1085 struct thread *thread;
1086 int ret = 0;
1088 if (!window) return 1; /* we can always clear the data */
1090 if ((thread = get_window_thread( window )))
1092 ret = (queue->input == thread->queue->input);
1093 if (!ret) set_error( STATUS_ACCESS_DENIED );
1094 release_object( thread );
1096 else set_error( STATUS_INVALID_HANDLE );
1098 return ret;
1101 /* make sure the specified thread has a queue */
1102 int init_thread_queue( struct thread *thread )
1104 if (thread->queue) return 1;
1105 return (create_msg_queue( thread, NULL ) != NULL);
1108 /* attach two thread input data structures */
1109 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1111 struct desktop *desktop;
1112 struct thread_input *input;
1113 int ret;
1115 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1116 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1117 input = (struct thread_input *)grab_object( thread_to->queue->input );
1118 if (input->desktop != desktop)
1120 set_error( STATUS_ACCESS_DENIED );
1121 release_object( input );
1122 release_object( desktop );
1123 return 0;
1125 release_object( desktop );
1127 if (thread_from->queue)
1129 if (!input->focus) input->focus = thread_from->queue->input->focus;
1130 if (!input->active) input->active = thread_from->queue->input->active;
1133 ret = assign_thread_input( thread_from, input );
1134 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1135 release_object( input );
1136 return ret;
1139 /* detach two thread input data structures */
1140 void detach_thread_input( struct thread *thread_from )
1142 struct thread *thread;
1143 struct thread_input *input, *old_input = thread_from->queue->input;
1145 if ((input = create_thread_input( thread_from )))
1147 if (old_input->focus && (thread = get_window_thread( old_input->focus )))
1149 if (thread == thread_from)
1151 input->focus = old_input->focus;
1152 old_input->focus = 0;
1154 release_object( thread );
1156 if (old_input->active && (thread = get_window_thread( old_input->active )))
1158 if (thread == thread_from)
1160 input->active = old_input->active;
1161 old_input->active = 0;
1163 release_object( thread );
1165 assign_thread_input( thread_from, input );
1166 release_object( input );
1171 /* set the next timer to expire */
1172 static void set_next_timer( struct msg_queue *queue )
1174 struct list *ptr;
1176 if (queue->timeout)
1178 remove_timeout_user( queue->timeout );
1179 queue->timeout = NULL;
1181 if ((ptr = list_head( &queue->pending_timers )))
1183 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1184 queue->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, queue );
1186 /* set/clear QS_TIMER bit */
1187 if (list_empty( &queue->expired_timers ))
1188 clear_queue_bits( queue, QS_TIMER );
1189 else
1190 set_queue_bits( queue, QS_TIMER );
1193 /* find a timer from its window and id */
1194 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1195 unsigned int msg, lparam_t id )
1197 struct list *ptr;
1199 /* we need to search both lists */
1201 LIST_FOR_EACH( ptr, &queue->pending_timers )
1203 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1204 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1206 LIST_FOR_EACH( ptr, &queue->expired_timers )
1208 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1209 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1211 return NULL;
1214 /* callback for the next timer expiration */
1215 static void timer_callback( void *private )
1217 struct msg_queue *queue = private;
1218 struct list *ptr;
1220 queue->timeout = NULL;
1221 /* move on to the next timer */
1222 ptr = list_head( &queue->pending_timers );
1223 list_remove( ptr );
1224 list_add_tail( &queue->expired_timers, ptr );
1225 set_next_timer( queue );
1228 /* link a timer at its rightful place in the queue list */
1229 static void link_timer( struct msg_queue *queue, struct timer *timer )
1231 struct list *ptr;
1233 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1235 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1236 if (t->when <= timer->when) break;
1238 list_add_before( ptr, &timer->entry );
1241 /* remove a timer from the queue timer list and free it */
1242 static void free_timer( struct msg_queue *queue, struct timer *timer )
1244 list_remove( &timer->entry );
1245 free( timer );
1246 set_next_timer( queue );
1249 /* restart an expired timer */
1250 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1252 list_remove( &timer->entry );
1253 while (-timer->when <= monotonic_time) timer->when -= (timeout_t)timer->rate * 10000;
1254 link_timer( queue, timer );
1255 set_next_timer( queue );
1258 /* find an expired timer matching the filtering parameters */
1259 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1260 unsigned int get_first, unsigned int get_last,
1261 int remove )
1263 struct list *ptr;
1265 LIST_FOR_EACH( ptr, &queue->expired_timers )
1267 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1268 if (win && timer->win != win) continue;
1269 if (check_msg_filter( timer->msg, get_first, get_last ))
1271 if (remove) restart_timer( queue, timer );
1272 return timer;
1275 return NULL;
1278 /* add a timer */
1279 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1281 struct timer *timer = mem_alloc( sizeof(*timer) );
1282 if (timer)
1284 timer->rate = max( rate, 1 );
1285 timer->when = -monotonic_time - (timeout_t)timer->rate * 10000;
1286 link_timer( queue, timer );
1287 /* check if we replaced the next timer */
1288 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1290 return timer;
1293 /* change the input key state for a given key */
1294 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1296 if (down)
1298 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1299 keystate[key] |= down;
1301 else keystate[key] &= ~0x80;
1304 /* update the input key state for a keyboard message */
1305 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1306 unsigned int msg, lparam_t wparam )
1308 unsigned char key;
1309 int down = 0;
1311 switch (msg)
1313 case WM_LBUTTONDOWN:
1314 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1315 /* fall through */
1316 case WM_LBUTTONUP:
1317 set_input_key_state( keystate, VK_LBUTTON, down );
1318 break;
1319 case WM_MBUTTONDOWN:
1320 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1321 /* fall through */
1322 case WM_MBUTTONUP:
1323 set_input_key_state( keystate, VK_MBUTTON, down );
1324 break;
1325 case WM_RBUTTONDOWN:
1326 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1327 /* fall through */
1328 case WM_RBUTTONUP:
1329 set_input_key_state( keystate, VK_RBUTTON, down );
1330 break;
1331 case WM_XBUTTONDOWN:
1332 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1333 /* fall through */
1334 case WM_XBUTTONUP:
1335 if (wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1336 else if (wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1337 break;
1338 case WM_KEYDOWN:
1339 case WM_SYSKEYDOWN:
1340 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1341 /* fall through */
1342 case WM_KEYUP:
1343 case WM_SYSKEYUP:
1344 key = (unsigned char)wparam;
1345 set_input_key_state( keystate, key, down );
1346 switch(key)
1348 case VK_LCONTROL:
1349 case VK_RCONTROL:
1350 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1351 set_input_key_state( keystate, VK_CONTROL, down );
1352 break;
1353 case VK_LMENU:
1354 case VK_RMENU:
1355 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1356 set_input_key_state( keystate, VK_MENU, down );
1357 break;
1358 case VK_LSHIFT:
1359 case VK_RSHIFT:
1360 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1361 set_input_key_state( keystate, VK_SHIFT, down );
1362 break;
1364 break;
1368 /* update the desktop key state according to a mouse message flags */
1369 static void update_desktop_mouse_state( struct desktop *desktop, unsigned int flags,
1370 int x, int y, lparam_t wparam )
1372 if (flags & MOUSEEVENTF_MOVE)
1373 update_desktop_cursor_pos( desktop, x, y );
1374 if (flags & MOUSEEVENTF_LEFTDOWN)
1375 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONDOWN, wparam );
1376 if (flags & MOUSEEVENTF_LEFTUP)
1377 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONUP, wparam );
1378 if (flags & MOUSEEVENTF_RIGHTDOWN)
1379 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONDOWN, wparam );
1380 if (flags & MOUSEEVENTF_RIGHTUP)
1381 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONUP, wparam );
1382 if (flags & MOUSEEVENTF_MIDDLEDOWN)
1383 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONDOWN, wparam );
1384 if (flags & MOUSEEVENTF_MIDDLEUP)
1385 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONUP, wparam );
1386 if (flags & MOUSEEVENTF_XDOWN)
1387 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONDOWN, wparam );
1388 if (flags & MOUSEEVENTF_XUP)
1389 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONUP, wparam );
1392 /* release the hardware message currently being processed by the given thread */
1393 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id )
1395 struct thread_input *input = queue->input;
1396 struct message *msg, *other;
1397 int clr_bit;
1399 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1401 if (msg->unique_id == hw_id) break;
1403 if (&msg->entry == &input->msg_list) return; /* not found */
1405 /* clear the queue bit for that message */
1406 clr_bit = get_hardware_msg_bit( msg );
1407 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1409 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1411 clr_bit = 0;
1412 break;
1415 if (clr_bit) clear_queue_bits( queue, clr_bit );
1417 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1418 list_remove( &msg->entry );
1419 free_message( msg );
1422 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1424 struct hotkey *hotkey;
1425 unsigned int modifiers = 0;
1427 if (msg->msg != WM_KEYDOWN) return 0;
1429 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1430 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1431 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1432 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1434 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1436 if (hotkey->vkey != msg->wparam) continue;
1437 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1440 return 0;
1442 found:
1443 msg->type = MSG_POSTED;
1444 msg->win = hotkey->win;
1445 msg->msg = WM_HOTKEY;
1446 msg->wparam = hotkey->id;
1447 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1449 free( msg->data );
1450 msg->data = NULL;
1451 msg->data_size = 0;
1453 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1454 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1455 hotkey->queue->hotkey_count++;
1456 return 1;
1459 /* find the window that should receive a given hardware message */
1460 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1461 struct message *msg, unsigned int *msg_code,
1462 struct thread **thread )
1464 user_handle_t win = 0;
1466 *thread = NULL;
1467 *msg_code = msg->msg;
1468 if (msg->msg == WM_INPUT || msg->msg == WM_INPUT_DEVICE_CHANGE)
1470 if (!(win = msg->win) && input) win = input->focus;
1472 else if (is_keyboard_msg( msg ))
1474 if (input && !(win = input->focus))
1476 win = input->active;
1477 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1480 else if (!input || !(win = input->capture)) /* mouse message */
1482 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1483 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1485 *thread = window_thread_from_point( win, msg->x, msg->y );
1488 if (!*thread)
1489 *thread = get_window_thread( win );
1490 return win;
1493 static struct rawinput_device_entry *find_rawinput_device( struct process *process, unsigned short usage_page, unsigned short usage )
1495 struct rawinput_device_entry *e;
1497 LIST_FOR_EACH_ENTRY( e, &process->rawinput_devices, struct rawinput_device_entry, entry )
1499 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1500 return e;
1503 return NULL;
1506 static void update_rawinput_device(const struct rawinput_device *device)
1508 struct rawinput_device_entry *e;
1510 if (!(e = find_rawinput_device( current->process, device->usage_page, device->usage )))
1512 if (!(e = mem_alloc( sizeof(*e) ))) return;
1513 list_add_tail( &current->process->rawinput_devices, &e->entry );
1516 if (device->flags & RIDEV_REMOVE)
1518 list_remove( &e->entry );
1519 free( e );
1520 return;
1523 e->device = *device;
1524 e->device.target = get_user_full_handle( e->device.target );
1527 static void prepend_cursor_history( int x, int y, unsigned int time, lparam_t info )
1529 cursor_pos_t *pos = &cursor_history[--cursor_history_latest % ARRAY_SIZE(cursor_history)];
1531 pos->x = x;
1532 pos->y = y;
1533 pos->time = time;
1534 pos->info = info;
1537 /* queue a hardware message into a given thread input */
1538 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1540 user_handle_t win;
1541 struct thread *thread;
1542 struct thread_input *input;
1543 struct hardware_msg_data *msg_data = msg->data;
1544 unsigned int msg_code;
1546 update_input_key_state( desktop, desktop->keystate, msg->msg, msg->wparam );
1547 last_input_time = get_tick_count();
1548 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1550 if (is_keyboard_msg( msg ))
1552 if (queue_hotkey_message( desktop, msg )) return;
1553 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1554 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1555 msg->lparam &= ~(KF_EXTENDED << 16);
1557 else if (msg->msg != WM_INPUT && msg->msg != WM_INPUT_DEVICE_CHANGE)
1559 if (msg->msg == WM_MOUSEMOVE)
1561 prepend_cursor_history( msg->x, msg->y, msg->time, msg_data->info );
1562 if (update_desktop_cursor_pos( desktop, msg->x, msg->y )) always_queue = 1;
1564 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1565 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1566 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1567 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1568 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1569 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1570 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1572 msg->x = desktop->cursor.x;
1573 msg->y = desktop->cursor.y;
1575 if (msg->win && (thread = get_window_thread( msg->win )))
1577 input = thread->queue->input;
1578 release_object( thread );
1580 else input = desktop->foreground_input;
1582 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1583 if (!win || !thread)
1585 if (input) update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1586 free_message( msg );
1587 return;
1589 input = thread->queue->input;
1591 if (win != desktop->cursor.win) always_queue = 1;
1592 desktop->cursor.win = win;
1594 if (!always_queue || merge_message( input, msg )) free_message( msg );
1595 else
1597 msg->unique_id = 0; /* will be set once we return it to the app */
1598 list_add_tail( &input->msg_list, &msg->entry );
1599 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1601 release_object( thread );
1604 /* send the low-level hook message for a given hardware message */
1605 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1606 const hw_input_t *input, struct msg_queue *sender )
1608 struct thread *hook_thread;
1609 struct msg_queue *queue;
1610 struct message *msg;
1611 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1612 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1614 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1615 if (!(queue = hook_thread->queue)) return 0;
1616 if (is_queue_hung( queue )) return 0;
1618 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1620 msg->type = MSG_HOOK_LL;
1621 msg->win = 0;
1622 msg->msg = id;
1623 msg->wparam = hardware_msg->msg;
1624 msg->x = hardware_msg->x;
1625 msg->y = hardware_msg->y;
1626 msg->time = hardware_msg->time;
1627 msg->data_size = hardware_msg->data_size;
1628 msg->result = NULL;
1630 if (input->type == INPUT_KEYBOARD)
1632 unsigned short vkey = input->kbd.vkey;
1633 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1634 msg->lparam = (input->kbd.scan << 16) | vkey;
1636 else msg->lparam = input->mouse.data << 16;
1638 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1639 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1641 free_message( msg );
1642 return 0;
1644 msg->result->hardware_msg = hardware_msg;
1645 msg->result->desktop = (struct desktop *)grab_object( desktop );
1646 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1647 set_queue_bits( queue, QS_SENDMESSAGE );
1648 return 1;
1651 /* get the foreground thread for a desktop and a window receiving input */
1652 static struct thread *get_foreground_thread( struct desktop *desktop, user_handle_t window )
1654 /* if desktop has no foreground process, assume the receiving window is */
1655 if (desktop->foreground_input) return get_window_thread( desktop->foreground_input->focus );
1656 if (window) return get_window_thread( window );
1657 return NULL;
1660 struct rawinput_message
1662 struct thread *foreground;
1663 struct desktop *desktop;
1664 struct hw_msg_source source;
1665 unsigned int time;
1666 unsigned int message;
1667 struct hardware_msg_data data;
1668 const void *hid_report;
1671 /* check if process is supposed to receive a WM_INPUT message and eventually queue it */
1672 static int queue_rawinput_message( struct process* process, void *arg )
1674 const struct rawinput_message* raw_msg = arg;
1675 const struct rawinput_device_entry *entry;
1676 const struct rawinput_device *device = NULL;
1677 struct desktop *target_desktop = NULL, *desktop = NULL;
1678 struct thread *target_thread = NULL, *foreground = NULL;
1679 struct message *msg;
1680 data_size_t report_size;
1681 int wparam = RIM_INPUT;
1683 if (raw_msg->data.rawinput.type == RIM_TYPEMOUSE)
1684 device = process->rawinput_mouse;
1685 else if (raw_msg->data.rawinput.type == RIM_TYPEKEYBOARD)
1686 device = process->rawinput_kbd;
1687 else if ((entry = find_rawinput_device( process, raw_msg->data.rawinput.hid.usage_page, raw_msg->data.rawinput.hid.usage )))
1688 device = &entry->device;
1689 if (!device) return 0;
1691 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && !(device->flags & RIDEV_DEVNOTIFY)) return 0;
1693 if (raw_msg->desktop) desktop = (struct desktop *)grab_object( raw_msg->desktop );
1694 else if (!(desktop = get_desktop_obj( process, process->desktop, 0 ))) goto done;
1696 if (raw_msg->foreground) foreground = (struct thread *)grab_object( raw_msg->foreground );
1697 else if (!(foreground = get_foreground_thread( desktop, 0 ))) goto done;
1699 if (process != foreground->process)
1701 if (raw_msg->message == WM_INPUT && !(device->flags & RIDEV_INPUTSINK)) goto done;
1702 if (!(target_thread = get_window_thread( device->target ))) goto done;
1703 if (!(target_desktop = get_thread_desktop( target_thread, 0 ))) goto done;
1704 if (target_desktop != desktop) goto done;
1705 wparam = RIM_INPUTSINK;
1708 if (raw_msg->data.rawinput.type != RIM_TYPEHID || !raw_msg->hid_report) report_size = 0;
1709 else report_size = raw_msg->data.size - sizeof(raw_msg->data);
1711 if (!(msg = alloc_hardware_message( raw_msg->data.info, raw_msg->source, raw_msg->time, report_size )))
1712 goto done;
1714 msg->win = device->target;
1715 msg->msg = raw_msg->message;
1716 msg->wparam = wparam;
1717 msg->lparam = 0;
1718 memcpy( msg->data, &raw_msg->data, sizeof(raw_msg->data) );
1719 if (report_size) memcpy( (struct hardware_msg_data *)msg->data + 1, raw_msg->hid_report, report_size );
1721 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && raw_msg->data.rawinput.type == RIM_TYPEHID)
1723 msg->wparam = raw_msg->data.rawinput.hid.param;
1724 msg->lparam = raw_msg->data.rawinput.hid.device;
1727 queue_hardware_message( desktop, msg, 1 );
1729 done:
1730 if (target_thread) release_object( target_thread );
1731 if (target_desktop) release_object( target_desktop );
1732 if (foreground) release_object( foreground );
1733 if (desktop) release_object( desktop );
1734 return 0;
1737 /* queue a hardware message for a mouse event */
1738 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1739 unsigned int origin, struct msg_queue *sender )
1741 const struct rawinput_device *device;
1742 struct hardware_msg_data *msg_data;
1743 struct rawinput_message raw_msg;
1744 struct message *msg;
1745 struct thread *foreground;
1746 unsigned int i, time, flags;
1747 struct hw_msg_source source = { IMDT_MOUSE, origin };
1748 int wait = 0, x, y;
1750 static const unsigned int messages[] =
1752 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1753 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1754 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1755 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1756 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1757 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1758 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1759 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1760 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1761 0, /* 0x0200 = unused */
1762 0, /* 0x0400 = unused */
1763 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1764 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1767 desktop->cursor.last_change = get_tick_count();
1768 flags = input->mouse.flags;
1769 time = input->mouse.time;
1770 if (!time) time = desktop->cursor.last_change;
1772 if (flags & MOUSEEVENTF_MOVE)
1774 if (flags & MOUSEEVENTF_ABSOLUTE)
1776 x = input->mouse.x;
1777 y = input->mouse.y;
1778 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1779 x == desktop->cursor.x && y == desktop->cursor.y)
1780 flags &= ~MOUSEEVENTF_MOVE;
1782 else
1784 x = desktop->cursor.x + input->mouse.x;
1785 y = desktop->cursor.y + input->mouse.y;
1788 else
1790 x = desktop->cursor.x;
1791 y = desktop->cursor.y;
1794 if ((foreground = get_foreground_thread( desktop, win )))
1796 raw_msg.foreground = foreground;
1797 raw_msg.desktop = desktop;
1798 raw_msg.source = source;
1799 raw_msg.time = time;
1800 raw_msg.message = WM_INPUT;
1801 raw_msg.hid_report = NULL;
1803 msg_data = &raw_msg.data;
1804 msg_data->info = input->mouse.info;
1805 msg_data->size = sizeof(*msg_data);
1806 msg_data->flags = flags;
1807 msg_data->rawinput.type = RIM_TYPEMOUSE;
1808 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1809 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1810 msg_data->rawinput.mouse.data = input->mouse.data;
1812 enum_processes( queue_rawinput_message, &raw_msg );
1813 release_object( foreground );
1816 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
1818 update_desktop_mouse_state( desktop, flags, x, y, input->mouse.data << 16 );
1819 return 0;
1822 for (i = 0; i < ARRAY_SIZE( messages ); i++)
1824 if (!messages[i]) continue;
1825 if (!(flags & (1 << i))) continue;
1826 flags &= ~(1 << i);
1828 if (!(msg = alloc_hardware_message( input->mouse.info, source, time, 0 ))) return 0;
1829 msg_data = msg->data;
1831 msg->win = get_user_full_handle( win );
1832 msg->msg = messages[i];
1833 msg->wparam = input->mouse.data << 16;
1834 msg->lparam = 0;
1835 msg->x = x;
1836 msg->y = y;
1837 if (origin == IMO_INJECTED) msg_data->flags = LLMHF_INJECTED;
1839 /* specify a sender only when sending the last message */
1840 if (!(flags & ((1 << ARRAY_SIZE( messages )) - 1)))
1842 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1843 queue_hardware_message( desktop, msg, 0 );
1845 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1846 queue_hardware_message( desktop, msg, 0 );
1848 return wait;
1851 /* queue a hardware message for a keyboard event */
1852 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1853 unsigned int origin, struct msg_queue *sender )
1855 struct hw_msg_source source = { IMDT_KEYBOARD, origin };
1856 const struct rawinput_device *device;
1857 struct hardware_msg_data *msg_data;
1858 struct rawinput_message raw_msg;
1859 struct message *msg;
1860 struct thread *foreground;
1861 unsigned char vkey = input->kbd.vkey;
1862 unsigned int message_code, time;
1863 int wait;
1865 if (!(time = input->kbd.time)) time = get_tick_count();
1867 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1869 switch (vkey)
1871 case VK_MENU:
1872 case VK_LMENU:
1873 case VK_RMENU:
1874 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1875 break;
1876 case VK_CONTROL:
1877 case VK_LCONTROL:
1878 case VK_RCONTROL:
1879 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1880 break;
1881 case VK_SHIFT:
1882 case VK_LSHIFT:
1883 case VK_RSHIFT:
1884 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1885 break;
1889 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1890 switch (vkey)
1892 case VK_LMENU:
1893 case VK_RMENU:
1894 if (input->kbd.flags & KEYEVENTF_KEYUP)
1896 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1897 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1898 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1899 message_code = WM_SYSKEYUP;
1900 desktop->keystate[VK_MENU] &= ~0x02;
1902 else
1904 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1905 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1906 message_code = WM_SYSKEYDOWN;
1907 desktop->keystate[VK_MENU] |= 0x02;
1909 break;
1911 case VK_LCONTROL:
1912 case VK_RCONTROL:
1913 /* send WM_SYSKEYUP on release if Alt still pressed */
1914 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1915 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1916 message_code = WM_SYSKEYUP;
1917 desktop->keystate[VK_MENU] &= ~0x02;
1918 break;
1920 default:
1921 /* send WM_SYSKEY for Alt-anykey and for F10 */
1922 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1923 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1924 /* fall through */
1925 case VK_F10:
1926 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1927 desktop->keystate[VK_MENU] &= ~0x02;
1928 break;
1931 if ((foreground = get_foreground_thread( desktop, win )))
1933 raw_msg.foreground = foreground;
1934 raw_msg.desktop = desktop;
1935 raw_msg.source = source;
1936 raw_msg.time = time;
1937 raw_msg.message = WM_INPUT;
1938 raw_msg.hid_report = NULL;
1940 msg_data = &raw_msg.data;
1941 msg_data->info = input->kbd.info;
1942 msg_data->size = sizeof(*msg_data);
1943 msg_data->flags = input->kbd.flags;
1944 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1945 msg_data->rawinput.kbd.message = message_code;
1946 msg_data->rawinput.kbd.vkey = vkey;
1947 msg_data->rawinput.kbd.scan = input->kbd.scan;
1949 enum_processes( queue_rawinput_message, &raw_msg );
1950 release_object( foreground );
1953 if ((device = current->process->rawinput_kbd) && (device->flags & RIDEV_NOLEGACY))
1955 update_input_key_state( desktop, desktop->keystate, message_code, vkey );
1956 return 0;
1959 if (!(msg = alloc_hardware_message( input->kbd.info, source, time, 0 ))) return 0;
1960 msg_data = msg->data;
1962 msg->win = get_user_full_handle( win );
1963 msg->msg = message_code;
1964 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1965 if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
1967 if (input->kbd.flags & KEYEVENTF_UNICODE && !vkey)
1969 msg->wparam = VK_PACKET;
1971 else
1973 unsigned int flags = 0;
1974 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1975 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1976 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1977 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1979 msg->wparam = vkey;
1980 msg->lparam |= flags << 16;
1981 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1984 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1985 queue_hardware_message( desktop, msg, 1 );
1987 return wait;
1990 /* queue a hardware message for a custom type of event */
1991 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1992 unsigned int origin, const hw_input_t *input )
1994 struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
1995 struct hardware_msg_data *msg_data;
1996 struct rawinput_message raw_msg;
1997 struct message *msg;
1999 switch (input->hw.msg)
2001 case WM_INPUT:
2002 case WM_INPUT_DEVICE_CHANGE:
2003 raw_msg.foreground = NULL;
2004 raw_msg.desktop = NULL;
2005 raw_msg.source = source;
2006 raw_msg.time = get_tick_count();
2007 raw_msg.message = input->hw.msg;
2008 raw_msg.hid_report = NULL;
2010 msg_data = &raw_msg.data;
2011 msg_data->info = 0;
2012 msg_data->size = sizeof(*msg_data);
2013 msg_data->flags = 0;
2014 msg_data->rawinput = input->hw.rawinput;
2016 enum_processes( queue_rawinput_message, &raw_msg );
2018 if (raw_msg.foreground) release_object( raw_msg.foreground );
2019 return;
2022 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
2024 msg->win = get_user_full_handle( win );
2025 msg->msg = input->hw.msg;
2026 msg->wparam = 0;
2027 msg->lparam = input->hw.lparam;
2028 msg->x = desktop->cursor.x;
2029 msg->y = desktop->cursor.y;
2031 queue_hardware_message( desktop, msg, 1 );
2034 /* check message filter for a hardware message */
2035 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
2036 user_handle_t filter_win, unsigned int first, unsigned int last )
2038 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
2040 /* we can only test the window for a keyboard message since the
2041 * dest window for a mouse message depends on hittest */
2042 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
2043 return 0;
2044 /* the message code is final for a keyboard message, we can simply check it */
2045 return check_msg_filter( msg_code, first, last );
2047 else /* mouse message */
2049 /* we need to check all possible values that the message can have in the end */
2051 if (check_msg_filter( msg_code, first, last )) return 1;
2052 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
2054 /* all other messages can become non-client messages */
2055 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
2057 /* clicks can become double-clicks or non-client double-clicks */
2058 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
2059 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
2061 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2062 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2064 return 0;
2069 /* find a hardware message for the given queue */
2070 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
2071 unsigned int first, unsigned int last, unsigned int flags,
2072 struct get_message_reply *reply )
2074 struct thread_input *input = thread->queue->input;
2075 struct thread *win_thread;
2076 struct list *ptr;
2077 user_handle_t win;
2078 int clear_bits, got_one = 0;
2079 unsigned int msg_code;
2081 ptr = list_head( &input->msg_list );
2082 if (hw_id)
2084 while (ptr)
2086 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2087 if (msg->unique_id == hw_id) break;
2088 ptr = list_next( &input->msg_list, ptr );
2090 if (!ptr) ptr = list_head( &input->msg_list );
2091 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
2094 if (ptr == list_head( &input->msg_list ))
2095 clear_bits = QS_INPUT;
2096 else
2097 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
2099 while (ptr)
2101 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2102 struct hardware_msg_data *data = msg->data;
2104 ptr = list_next( &input->msg_list, ptr );
2105 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
2106 if (!win || !win_thread)
2108 /* no window at all, remove it */
2109 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2110 list_remove( &msg->entry );
2111 free_message( msg );
2112 continue;
2114 if (win_thread != thread)
2116 if (win_thread->queue->input == input)
2118 /* wake the other thread */
2119 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
2120 got_one = 1;
2122 else
2124 /* for another thread input, drop it */
2125 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2126 list_remove( &msg->entry );
2127 free_message( msg );
2129 release_object( win_thread );
2130 continue;
2132 release_object( win_thread );
2134 /* if we already got a message for another thread, or if it doesn't
2135 * match the filter we skip it */
2136 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
2138 clear_bits &= ~get_hardware_msg_bit( msg );
2139 continue;
2141 /* now we can return it */
2142 if (!msg->unique_id) msg->unique_id = get_unique_id();
2143 reply->type = MSG_HARDWARE;
2144 reply->win = win;
2145 reply->msg = msg_code;
2146 reply->wparam = msg->wparam;
2147 reply->lparam = msg->lparam;
2148 reply->x = msg->x;
2149 reply->y = msg->y;
2150 reply->time = msg->time;
2152 data->hw_id = msg->unique_id;
2153 set_reply_data( msg->data, msg->data_size );
2154 if ((msg->msg == WM_INPUT || msg->msg == WM_INPUT_DEVICE_CHANGE) && (flags & PM_REMOVE))
2155 release_hardware_message( current->queue, data->hw_id );
2156 return 1;
2158 /* nothing found, clear the hardware queue bits */
2159 clear_queue_bits( thread->queue, clear_bits );
2160 return 0;
2163 /* increment (or decrement if 'incr' is negative) the queue paint count */
2164 void inc_queue_paint_count( struct thread *thread, int incr )
2166 struct msg_queue *queue = thread->queue;
2168 assert( queue );
2170 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2172 if (queue->paint_count)
2173 set_queue_bits( queue, QS_PAINT );
2174 else
2175 clear_queue_bits( queue, QS_PAINT );
2179 /* remove all messages and timers belonging to a certain window */
2180 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2182 struct msg_queue *queue = thread->queue;
2183 struct list *ptr;
2184 int i;
2186 if (!queue) return;
2188 /* remove timers */
2190 ptr = list_head( &queue->pending_timers );
2191 while (ptr)
2193 struct list *next = list_next( &queue->pending_timers, ptr );
2194 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2195 if (timer->win == win) free_timer( queue, timer );
2196 ptr = next;
2198 ptr = list_head( &queue->expired_timers );
2199 while (ptr)
2201 struct list *next = list_next( &queue->expired_timers, ptr );
2202 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2203 if (timer->win == win) free_timer( queue, timer );
2204 ptr = next;
2207 /* remove messages */
2208 for (i = 0; i < NB_MSG_KINDS; i++)
2210 struct list *ptr, *next;
2212 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2214 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2215 if (msg->win == win)
2217 if (msg->msg == WM_QUIT && !queue->quit_message)
2219 queue->quit_message = 1;
2220 queue->exit_code = msg->wparam;
2222 remove_queue_message( queue, msg, i );
2227 thread_input_cleanup_window( queue, win );
2230 /* post a message to a window */
2231 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2233 struct message *msg;
2234 struct thread *thread = get_window_thread( win );
2236 if (!thread) return;
2238 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2240 msg->type = MSG_POSTED;
2241 msg->win = get_user_full_handle( win );
2242 msg->msg = message;
2243 msg->wparam = wparam;
2244 msg->lparam = lparam;
2245 msg->result = NULL;
2246 msg->data = NULL;
2247 msg->data_size = 0;
2249 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2251 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2252 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2253 if (message == WM_HOTKEY)
2255 set_queue_bits( thread->queue, QS_HOTKEY );
2256 thread->queue->hotkey_count++;
2259 release_object( thread );
2262 /* send a notify message to a window */
2263 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2265 struct message *msg;
2266 struct thread *thread = get_window_thread( win );
2268 if (!thread) return;
2270 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2272 msg->type = MSG_NOTIFY;
2273 msg->win = get_user_full_handle( win );
2274 msg->msg = message;
2275 msg->wparam = wparam;
2276 msg->lparam = lparam;
2277 msg->result = NULL;
2278 msg->data = NULL;
2279 msg->data_size = 0;
2281 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2283 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2284 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2286 release_object( thread );
2289 /* post a win event */
2290 void post_win_event( struct thread *thread, unsigned int event,
2291 user_handle_t win, unsigned int object_id,
2292 unsigned int child_id, client_ptr_t hook_proc,
2293 const WCHAR *module, data_size_t module_size,
2294 user_handle_t hook)
2296 struct message *msg;
2298 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2300 struct winevent_msg_data *data;
2302 msg->type = MSG_WINEVENT;
2303 msg->win = get_user_full_handle( win );
2304 msg->msg = event;
2305 msg->wparam = object_id;
2306 msg->lparam = child_id;
2307 msg->time = get_tick_count();
2308 msg->result = NULL;
2310 if ((data = malloc( sizeof(*data) + module_size )))
2312 data->hook = hook;
2313 data->tid = get_thread_id( current );
2314 data->hook_proc = hook_proc;
2315 memcpy( data + 1, module, module_size );
2317 msg->data = data;
2318 msg->data_size = sizeof(*data) + module_size;
2320 if (debug_level > 1)
2321 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2322 get_thread_id(thread), event, win, object_id, child_id );
2323 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2324 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2326 else
2327 free( msg );
2331 /* free all hotkeys on a desktop, optionally filtering by window */
2332 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2334 struct hotkey *hotkey, *hotkey2;
2336 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2338 if (!window || hotkey->win == window)
2340 list_remove( &hotkey->entry );
2341 free( hotkey );
2347 /* check if the thread owning the window is hung */
2348 DECL_HANDLER(is_window_hung)
2350 struct thread *thread;
2352 thread = get_window_thread( req->win );
2353 if (thread)
2355 reply->is_hung = is_queue_hung( thread->queue );
2356 release_object( thread );
2358 else reply->is_hung = 0;
2362 /* get the message queue of the current thread */
2363 DECL_HANDLER(get_msg_queue)
2365 struct msg_queue *queue = get_current_queue();
2367 reply->handle = 0;
2368 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2372 /* set the file descriptor associated to the current thread queue */
2373 DECL_HANDLER(set_queue_fd)
2375 struct msg_queue *queue = get_current_queue();
2376 struct file *file;
2377 int unix_fd;
2379 if (queue->fd) /* fd can only be set once */
2381 set_error( STATUS_ACCESS_DENIED );
2382 return;
2384 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2386 if ((unix_fd = get_file_unix_fd( file )) != -1)
2388 if ((unix_fd = dup( unix_fd )) != -1)
2389 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2390 else
2391 file_set_error();
2393 release_object( file );
2397 /* set the current message queue wakeup mask */
2398 DECL_HANDLER(set_queue_mask)
2400 struct msg_queue *queue = get_current_queue();
2402 if (queue)
2404 queue->wake_mask = req->wake_mask;
2405 queue->changed_mask = req->changed_mask;
2406 reply->wake_bits = queue->wake_bits;
2407 reply->changed_bits = queue->changed_bits;
2408 if (is_signaled( queue ))
2410 /* if skip wait is set, do what would have been done in the subsequent wait */
2411 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2412 else wake_up( &queue->obj, 0 );
2418 /* get the current message queue status */
2419 DECL_HANDLER(get_queue_status)
2421 struct msg_queue *queue = current->queue;
2422 if (queue)
2424 reply->wake_bits = queue->wake_bits;
2425 reply->changed_bits = queue->changed_bits;
2426 queue->changed_bits &= ~req->clear_bits;
2428 else reply->wake_bits = reply->changed_bits = 0;
2432 /* send a message to a thread queue */
2433 DECL_HANDLER(send_message)
2435 struct message *msg;
2436 struct msg_queue *send_queue = get_current_queue();
2437 struct msg_queue *recv_queue = NULL;
2438 struct thread *thread = NULL;
2440 if (!(thread = get_thread_from_id( req->id ))) return;
2442 if (!(recv_queue = thread->queue))
2444 set_error( STATUS_INVALID_PARAMETER );
2445 release_object( thread );
2446 return;
2448 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2450 set_error( STATUS_TIMEOUT );
2451 release_object( thread );
2452 return;
2455 if ((msg = mem_alloc( sizeof(*msg) )))
2457 msg->type = req->type;
2458 msg->win = get_user_full_handle( req->win );
2459 msg->msg = req->msg;
2460 msg->wparam = req->wparam;
2461 msg->lparam = req->lparam;
2462 msg->result = NULL;
2463 msg->data = NULL;
2464 msg->data_size = get_req_data_size();
2466 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2468 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2470 free( msg );
2471 release_object( thread );
2472 return;
2475 switch(msg->type)
2477 case MSG_OTHER_PROCESS:
2478 case MSG_ASCII:
2479 case MSG_UNICODE:
2480 case MSG_CALLBACK:
2481 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2483 free_message( msg );
2484 break;
2486 /* fall through */
2487 case MSG_NOTIFY:
2488 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2489 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2490 break;
2491 case MSG_POSTED:
2492 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2493 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2494 if (msg->msg == WM_HOTKEY)
2496 set_queue_bits( recv_queue, QS_HOTKEY );
2497 recv_queue->hotkey_count++;
2499 break;
2500 case MSG_HARDWARE: /* should use send_hardware_message instead */
2501 case MSG_CALLBACK_RESULT: /* cannot send this one */
2502 case MSG_HOOK_LL: /* generated internally */
2503 default:
2504 set_error( STATUS_INVALID_PARAMETER );
2505 free( msg );
2506 break;
2509 release_object( thread );
2512 /* send a hardware message to a thread queue */
2513 DECL_HANDLER(send_hardware_message)
2515 struct thread *thread = NULL;
2516 struct desktop *desktop;
2517 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2518 struct msg_queue *sender = get_current_queue();
2519 data_size_t size = min( 256, get_reply_max_size() );
2521 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2523 if (req->win)
2525 if (!(thread = get_window_thread( req->win ))) return;
2526 if (desktop != thread->queue->input->desktop)
2528 /* don't allow queuing events to a different desktop */
2529 release_object( desktop );
2530 return;
2534 reply->prev_x = desktop->cursor.x;
2535 reply->prev_y = desktop->cursor.y;
2537 switch (req->input.type)
2539 case INPUT_MOUSE:
2540 reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2541 break;
2542 case INPUT_KEYBOARD:
2543 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2544 break;
2545 case INPUT_HARDWARE:
2546 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2547 break;
2548 default:
2549 set_error( STATUS_INVALID_PARAMETER );
2551 if (thread) release_object( thread );
2553 reply->new_x = desktop->cursor.x;
2554 reply->new_y = desktop->cursor.y;
2555 set_reply_data( desktop->keystate, size );
2556 release_object( desktop );
2559 /* post a quit message to the current queue */
2560 DECL_HANDLER(post_quit_message)
2562 struct msg_queue *queue = get_current_queue();
2564 if (!queue)
2565 return;
2567 queue->quit_message = 1;
2568 queue->exit_code = req->exit_code;
2569 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2572 /* get a message from the current queue */
2573 DECL_HANDLER(get_message)
2575 struct timer *timer;
2576 struct list *ptr;
2577 struct msg_queue *queue = get_current_queue();
2578 user_handle_t get_win = get_user_full_handle( req->get_win );
2579 unsigned int filter = req->flags >> 16;
2581 reply->active_hooks = get_active_hooks();
2583 if (get_win && get_win != 1 && get_win != -1 && !get_user_object( get_win, USER_WINDOW ))
2585 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2586 return;
2589 if (!queue) return;
2590 queue->last_get_msg = current_time;
2591 if (!filter) filter = QS_ALLINPUT;
2593 /* first check for sent messages */
2594 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2596 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2597 receive_message( queue, msg, reply );
2598 return;
2601 /* clear changed bits so we can wait on them if we don't find a message */
2602 if (filter & QS_POSTMESSAGE)
2604 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2605 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2607 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2608 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2610 /* then check for posted messages */
2611 if ((filter & QS_POSTMESSAGE) &&
2612 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2613 return;
2615 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2616 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2617 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2618 return;
2620 /* only check for quit messages if not posted messages pending */
2621 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2622 return;
2624 /* then check for any raw hardware message */
2625 if ((filter & QS_INPUT) &&
2626 filter_contains_hw_range( req->get_first, req->get_last ) &&
2627 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2628 return;
2630 /* now check for WM_PAINT */
2631 if ((filter & QS_PAINT) &&
2632 queue->paint_count &&
2633 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2634 (reply->win = find_window_to_repaint( get_win, current )))
2636 reply->type = MSG_POSTED;
2637 reply->msg = WM_PAINT;
2638 reply->wparam = 0;
2639 reply->lparam = 0;
2640 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2641 return;
2644 /* now check for timer */
2645 if ((filter & QS_TIMER) &&
2646 (timer = find_expired_timer( queue, get_win, req->get_first,
2647 req->get_last, (req->flags & PM_REMOVE) )))
2649 reply->type = MSG_POSTED;
2650 reply->win = timer->win;
2651 reply->msg = timer->msg;
2652 reply->wparam = timer->id;
2653 reply->lparam = timer->lparam;
2654 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2655 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2656 set_event( current->process->idle_event );
2657 return;
2660 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2661 queue->wake_mask = req->wake_mask;
2662 queue->changed_mask = req->changed_mask;
2663 set_error( STATUS_PENDING ); /* FIXME */
2667 /* reply to a sent message */
2668 DECL_HANDLER(reply_message)
2670 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2671 else if (current->queue->recv_result)
2672 reply_message( current->queue, req->result, 0, req->remove,
2673 get_req_data(), get_req_data_size() );
2677 /* accept the current hardware message */
2678 DECL_HANDLER(accept_hardware_message)
2680 if (current->queue)
2681 release_hardware_message( current->queue, req->hw_id );
2682 else
2683 set_error( STATUS_ACCESS_DENIED );
2687 /* retrieve the reply for the last message sent */
2688 DECL_HANDLER(get_message_reply)
2690 struct message_result *result;
2691 struct list *entry;
2692 struct msg_queue *queue = current->queue;
2694 if (queue)
2696 set_error( STATUS_PENDING );
2697 reply->result = 0;
2699 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2701 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2702 if (result->replied || req->cancel)
2704 if (result->replied)
2706 reply->result = result->result;
2707 set_error( result->error );
2708 if (result->data)
2710 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2711 set_reply_data_ptr( result->data, data_len );
2712 result->data = NULL;
2713 result->data_size = 0;
2716 remove_result_from_sender( result );
2718 entry = list_head( &queue->send_result );
2719 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2720 else
2722 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2723 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2724 else clear_queue_bits( queue, QS_SMRESULT );
2728 else set_error( STATUS_ACCESS_DENIED );
2732 /* set a window timer */
2733 DECL_HANDLER(set_win_timer)
2735 struct timer *timer;
2736 struct msg_queue *queue;
2737 struct thread *thread = NULL;
2738 user_handle_t win = 0;
2739 lparam_t id = req->id;
2741 if (req->win)
2743 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2745 set_error( STATUS_INVALID_HANDLE );
2746 return;
2748 if (thread->process != current->process)
2750 release_object( thread );
2751 set_error( STATUS_ACCESS_DENIED );
2752 return;
2754 queue = thread->queue;
2755 /* remove it if it existed already */
2756 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2758 else
2760 queue = get_current_queue();
2761 /* look for a timer with this id */
2762 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2764 /* free and reuse id */
2765 free_timer( queue, timer );
2767 else
2769 lparam_t end_id = queue->next_timer_id;
2771 /* find a free id for it */
2772 while (1)
2774 id = queue->next_timer_id;
2775 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2777 if (!find_timer( queue, 0, req->msg, id )) break;
2778 if (queue->next_timer_id == end_id)
2780 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2781 return;
2787 if ((timer = set_timer( queue, req->rate )))
2789 timer->win = win;
2790 timer->msg = req->msg;
2791 timer->id = id;
2792 timer->lparam = req->lparam;
2793 reply->id = id;
2795 if (thread) release_object( thread );
2798 /* kill a window timer */
2799 DECL_HANDLER(kill_win_timer)
2801 struct timer *timer;
2802 struct thread *thread;
2803 user_handle_t win = 0;
2805 if (req->win)
2807 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2809 set_error( STATUS_INVALID_HANDLE );
2810 return;
2812 if (thread->process != current->process)
2814 release_object( thread );
2815 set_error( STATUS_ACCESS_DENIED );
2816 return;
2819 else thread = (struct thread *)grab_object( current );
2821 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2822 free_timer( thread->queue, timer );
2823 else
2824 set_error( STATUS_INVALID_PARAMETER );
2826 release_object( thread );
2829 DECL_HANDLER(register_hotkey)
2831 struct desktop *desktop;
2832 user_handle_t win_handle = req->window;
2833 struct hotkey *hotkey;
2834 struct hotkey *new_hotkey = NULL;
2835 struct thread *thread;
2836 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2838 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2840 if (win_handle)
2842 if (!(win_handle = get_valid_window_handle( win_handle )))
2844 release_object( desktop );
2845 return;
2848 thread = get_window_thread( win_handle );
2849 if (thread) release_object( thread );
2851 if (thread != current)
2853 release_object( desktop );
2854 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2855 return;
2859 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2861 if (req->vkey == hotkey->vkey &&
2862 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2864 release_object( desktop );
2865 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2866 return;
2868 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2869 new_hotkey = hotkey;
2872 if (new_hotkey)
2874 reply->replaced = 1;
2875 reply->flags = new_hotkey->flags;
2876 reply->vkey = new_hotkey->vkey;
2878 else
2880 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2881 if (new_hotkey)
2883 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2884 new_hotkey->queue = current->queue;
2885 new_hotkey->win = win_handle;
2886 new_hotkey->id = req->id;
2890 if (new_hotkey)
2892 new_hotkey->flags = req->flags;
2893 new_hotkey->vkey = req->vkey;
2896 release_object( desktop );
2899 DECL_HANDLER(unregister_hotkey)
2901 struct desktop *desktop;
2902 user_handle_t win_handle = req->window;
2903 struct hotkey *hotkey;
2904 struct thread *thread;
2906 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2908 if (win_handle)
2910 if (!(win_handle = get_valid_window_handle( win_handle )))
2912 release_object( desktop );
2913 return;
2916 thread = get_window_thread( win_handle );
2917 if (thread) release_object( thread );
2919 if (thread != current)
2921 release_object( desktop );
2922 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2923 return;
2927 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2929 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2930 goto found;
2933 release_object( desktop );
2934 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2935 return;
2937 found:
2938 reply->flags = hotkey->flags;
2939 reply->vkey = hotkey->vkey;
2940 list_remove( &hotkey->entry );
2941 free( hotkey );
2942 release_object( desktop );
2945 /* attach (or detach) thread inputs */
2946 DECL_HANDLER(attach_thread_input)
2948 struct thread *thread_from = get_thread_from_id( req->tid_from );
2949 struct thread *thread_to = get_thread_from_id( req->tid_to );
2951 if (!thread_from || !thread_to)
2953 if (thread_from) release_object( thread_from );
2954 if (thread_to) release_object( thread_to );
2955 return;
2957 if (thread_from != thread_to)
2959 if (req->attach)
2961 if ((thread_to->queue || thread_to == current) &&
2962 (thread_from->queue || thread_from == current))
2963 attach_thread_input( thread_from, thread_to );
2964 else
2965 set_error( STATUS_INVALID_PARAMETER );
2967 else
2969 if (thread_from->queue && thread_to->queue &&
2970 thread_from->queue->input == thread_to->queue->input)
2971 detach_thread_input( thread_from );
2972 else
2973 set_error( STATUS_ACCESS_DENIED );
2976 else set_error( STATUS_ACCESS_DENIED );
2977 release_object( thread_from );
2978 release_object( thread_to );
2982 /* get thread input data */
2983 DECL_HANDLER(get_thread_input)
2985 struct thread *thread = NULL;
2986 struct desktop *desktop;
2987 struct thread_input *input;
2989 if (req->tid)
2991 if (!(thread = get_thread_from_id( req->tid ))) return;
2992 if (!(desktop = get_thread_desktop( thread, 0 )))
2994 release_object( thread );
2995 return;
2997 input = thread->queue ? thread->queue->input : NULL;
2999 else
3001 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3002 input = desktop->foreground_input; /* get the foreground thread info */
3005 if (input)
3007 reply->focus = input->focus;
3008 reply->capture = input->capture;
3009 reply->active = input->active;
3010 reply->menu_owner = input->menu_owner;
3011 reply->move_size = input->move_size;
3012 reply->caret = input->caret;
3013 reply->cursor = input->cursor;
3014 reply->show_count = input->cursor_count;
3015 reply->rect = input->caret_rect;
3018 /* foreground window is active window of foreground thread */
3019 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
3020 if (thread) release_object( thread );
3021 release_object( desktop );
3025 /* retrieve queue keyboard state for current thread or global async state */
3026 DECL_HANDLER(get_key_state)
3028 struct desktop *desktop;
3029 data_size_t size = min( 256, get_reply_max_size() );
3031 if (req->async) /* get global async key state */
3033 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3034 if (req->key >= 0)
3036 reply->state = desktop->keystate[req->key & 0xff];
3037 desktop->keystate[req->key & 0xff] &= ~0x40;
3039 set_reply_data( desktop->keystate, size );
3040 release_object( desktop );
3042 else if (!current->queue)
3044 unsigned char *keystate;
3045 /* fallback to desktop keystate */
3046 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3047 if (req->key >= 0) reply->state = desktop->keystate[req->key & 0xff] & ~0x40;
3048 if ((keystate = set_reply_data_size( size )))
3050 unsigned int i;
3051 for (i = 0; i < size; i++) keystate[i] = desktop->keystate[i] & ~0x40;
3053 release_object( desktop );
3055 else
3057 unsigned char *keystate = current->queue->input->keystate;
3058 if (req->key >= 0) reply->state = keystate[req->key & 0xff];
3059 set_reply_data( keystate, size );
3064 /* set queue keyboard state for current thread */
3065 DECL_HANDLER(set_key_state)
3067 struct desktop *desktop;
3068 data_size_t size = min( 256, get_req_data_size() );
3070 if (current->queue) memcpy( current->queue->input->keystate, get_req_data(), size );
3071 if (req->async && (desktop = get_thread_desktop( current, 0 )))
3073 memcpy( desktop->keystate, get_req_data(), size );
3074 release_object( desktop );
3079 /* set the system foreground window */
3080 DECL_HANDLER(set_foreground_window)
3082 struct thread *thread = NULL;
3083 struct desktop *desktop;
3084 struct msg_queue *queue = get_current_queue();
3086 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3087 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
3088 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
3089 reply->send_msg_new = FALSE;
3091 if (is_valid_foreground_window( req->handle ) &&
3092 (thread = get_window_thread( req->handle )) &&
3093 thread->queue->input->desktop == desktop)
3095 set_foreground_input( desktop, thread->queue->input );
3096 reply->send_msg_new = (desktop->foreground_input != queue->input);
3098 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
3100 if (thread) release_object( thread );
3101 release_object( desktop );
3105 /* set the current thread focus window */
3106 DECL_HANDLER(set_focus_window)
3108 struct msg_queue *queue = get_current_queue();
3110 reply->previous = 0;
3111 if (queue && check_queue_input_window( queue, req->handle ))
3113 reply->previous = queue->input->focus;
3114 queue->input->focus = get_user_full_handle( req->handle );
3119 /* set the current thread active window */
3120 DECL_HANDLER(set_active_window)
3122 struct msg_queue *queue = get_current_queue();
3124 reply->previous = 0;
3125 if (queue && check_queue_input_window( queue, req->handle ))
3127 if (!req->handle || make_window_active( req->handle ))
3129 reply->previous = queue->input->active;
3130 queue->input->active = get_user_full_handle( req->handle );
3132 else set_error( STATUS_INVALID_HANDLE );
3137 /* set the current thread capture window */
3138 DECL_HANDLER(set_capture_window)
3140 struct msg_queue *queue = get_current_queue();
3142 reply->previous = reply->full_handle = 0;
3143 if (queue && check_queue_input_window( queue, req->handle ))
3145 struct thread_input *input = queue->input;
3147 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
3148 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
3150 set_error(STATUS_ACCESS_DENIED);
3151 return;
3153 reply->previous = input->capture;
3154 input->capture = get_user_full_handle( req->handle );
3155 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
3156 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
3157 reply->full_handle = input->capture;
3162 /* Set the current thread caret window */
3163 DECL_HANDLER(set_caret_window)
3165 struct msg_queue *queue = get_current_queue();
3167 reply->previous = 0;
3168 if (queue && check_queue_input_window( queue, req->handle ))
3170 struct thread_input *input = queue->input;
3172 reply->previous = input->caret;
3173 reply->old_rect = input->caret_rect;
3174 reply->old_hide = input->caret_hide;
3175 reply->old_state = input->caret_state;
3177 set_caret_window( input, get_user_full_handle(req->handle) );
3178 input->caret_rect.right = input->caret_rect.left + req->width;
3179 input->caret_rect.bottom = input->caret_rect.top + req->height;
3184 /* Set the current thread caret information */
3185 DECL_HANDLER(set_caret_info)
3187 struct msg_queue *queue = get_current_queue();
3188 struct thread_input *input;
3190 if (!queue) return;
3191 input = queue->input;
3192 reply->full_handle = input->caret;
3193 reply->old_rect = input->caret_rect;
3194 reply->old_hide = input->caret_hide;
3195 reply->old_state = input->caret_state;
3197 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3199 set_error( STATUS_ACCESS_DENIED );
3200 return;
3202 if (req->flags & SET_CARET_POS)
3204 input->caret_rect.right += req->x - input->caret_rect.left;
3205 input->caret_rect.bottom += req->y - input->caret_rect.top;
3206 input->caret_rect.left = req->x;
3207 input->caret_rect.top = req->y;
3209 if (req->flags & SET_CARET_HIDE)
3211 input->caret_hide += req->hide;
3212 if (input->caret_hide < 0) input->caret_hide = 0;
3214 if (req->flags & SET_CARET_STATE)
3216 switch (req->state)
3218 case CARET_STATE_OFF: input->caret_state = 0; break;
3219 case CARET_STATE_ON: input->caret_state = 1; break;
3220 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3221 case CARET_STATE_ON_IF_MOVED:
3222 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3223 break;
3229 /* get the time of the last input event */
3230 DECL_HANDLER(get_last_input_time)
3232 reply->time = last_input_time;
3235 /* set/get the current cursor */
3236 DECL_HANDLER(set_cursor)
3238 struct msg_queue *queue = get_current_queue();
3239 struct thread_input *input;
3241 if (!queue) return;
3242 input = queue->input;
3244 reply->prev_handle = input->cursor;
3245 reply->prev_count = input->cursor_count;
3246 reply->prev_x = input->desktop->cursor.x;
3247 reply->prev_y = input->desktop->cursor.y;
3249 if (req->flags & SET_CURSOR_HANDLE)
3251 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3253 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3254 return;
3256 input->cursor = req->handle;
3258 if (req->flags & SET_CURSOR_COUNT)
3260 queue->cursor_count += req->show_count;
3261 input->cursor_count += req->show_count;
3263 if (req->flags & SET_CURSOR_POS)
3265 set_cursor_pos( input->desktop, req->x, req->y );
3267 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3269 struct desktop *desktop = input->desktop;
3271 /* only the desktop owner can set the message */
3272 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3273 desktop->cursor.clip_msg = req->clip_msg;
3275 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip, 0 );
3278 reply->new_x = input->desktop->cursor.x;
3279 reply->new_y = input->desktop->cursor.y;
3280 reply->new_clip = input->desktop->cursor.clip;
3281 reply->last_change = input->desktop->cursor.last_change;
3284 /* Get the history of the 64 last cursor positions */
3285 DECL_HANDLER(get_cursor_history)
3287 cursor_pos_t *pos;
3288 unsigned int i, count = min( 64, get_reply_max_size() / sizeof(*pos) );
3290 if ((pos = set_reply_data_size( count * sizeof(*pos) )))
3291 for (i = 0; i < count; i++)
3292 pos[i] = cursor_history[(i + cursor_history_latest) % ARRAY_SIZE(cursor_history)];
3295 DECL_HANDLER(get_rawinput_buffer)
3297 struct thread_input *input = current->queue->input;
3298 data_size_t size = 0, next_size = 0;
3299 struct list *ptr;
3300 char *buf, *cur, *tmp;
3301 int count = 0, buf_size = 16 * sizeof(struct hardware_msg_data);
3303 if (!req->buffer_size) buf = NULL;
3304 else if (!(buf = mem_alloc( buf_size ))) return;
3306 cur = buf;
3307 ptr = list_head( &input->msg_list );
3308 while (ptr)
3310 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
3311 struct hardware_msg_data *data = msg->data;
3312 data_size_t extra_size = data->size - sizeof(*data);
3314 ptr = list_next( &input->msg_list, ptr );
3315 if (msg->msg != WM_INPUT) continue;
3317 next_size = req->rawinput_size + extra_size;
3318 if (size + next_size > req->buffer_size) break;
3319 if (cur + data->size > buf + get_reply_max_size()) break;
3320 if (cur + data->size > buf + buf_size)
3322 buf_size += buf_size / 2 + extra_size;
3323 if (!(tmp = realloc( buf, buf_size )))
3325 set_error( STATUS_NO_MEMORY );
3326 return;
3328 cur = tmp + (cur - buf);
3329 buf = tmp;
3332 memcpy( cur, data, data->size );
3333 list_remove( &msg->entry );
3334 free_message( msg );
3336 size += next_size;
3337 cur += sizeof(*data);
3338 count++;
3341 reply->next_size = next_size;
3342 reply->count = count;
3343 set_reply_data_ptr( buf, cur - buf );
3346 DECL_HANDLER(update_rawinput_devices)
3348 const struct rawinput_device *devices = get_req_data();
3349 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3350 const struct rawinput_device_entry *e;
3351 unsigned int i;
3353 for (i = 0; i < device_count; ++i)
3355 update_rawinput_device(&devices[i]);
3358 e = find_rawinput_device( current->process, 1, 2 );
3359 current->process->rawinput_mouse = e ? &e->device : NULL;
3360 e = find_rawinput_device( current->process, 1, 6 );
3361 current->process->rawinput_kbd = e ? &e->device : NULL;
3364 DECL_HANDLER(get_rawinput_devices)
3366 struct rawinput_device_entry *e;
3367 struct rawinput_device *devices;
3368 unsigned int i = 0, device_count = list_count( &current->process->rawinput_devices );
3369 unsigned int size = device_count * sizeof(*devices);
3371 reply->device_count = device_count;
3373 /* no buffer provided, nothing else to do */
3374 if (!get_reply_max_size()) return;
3376 if (size > get_reply_max_size())
3377 set_error( STATUS_BUFFER_TOO_SMALL );
3378 else if ((devices = set_reply_data_size( size )))
3380 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
3381 devices[i++] = e->device;