server: Send mouse hardware messages to thread owning top-most window.
[wine.git] / server / queue.c
blob71dfc11d43484255333b007cd33b4252ebf90d1e
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 unsigned int time; /* message time */
81 void *data; /* message data for sent messages */
82 unsigned int data_size; /* size of message data */
83 unsigned int unique_id; /* unique id for nested hw message waits */
84 struct message_result *result; /* result in sender queue */
87 struct timer
89 struct list entry; /* entry in timer list */
90 timeout_t when; /* next expiration */
91 unsigned int rate; /* timer rate in ms */
92 user_handle_t win; /* window handle */
93 unsigned int msg; /* message to post */
94 lparam_t id; /* timer id */
95 lparam_t lparam; /* lparam for message */
98 struct thread_input
100 struct object obj; /* object header */
101 struct desktop *desktop; /* desktop that this thread input belongs to */
102 user_handle_t focus; /* focus window */
103 user_handle_t capture; /* capture window */
104 user_handle_t active; /* active window */
105 user_handle_t menu_owner; /* current menu owner window */
106 user_handle_t move_size; /* current moving/resizing window */
107 user_handle_t caret; /* caret window */
108 rectangle_t caret_rect; /* caret rectangle */
109 int caret_hide; /* caret hide count */
110 int caret_state; /* caret on/off state */
111 user_handle_t cursor; /* current cursor */
112 int cursor_count; /* cursor show count */
113 struct list msg_list; /* list of hardware messages */
114 unsigned char keystate[256]; /* state of each key */
117 struct msg_queue
119 struct object obj; /* object header */
120 struct fd *fd; /* optional file descriptor to poll */
121 unsigned int wake_bits; /* wakeup bits */
122 unsigned int wake_mask; /* wakeup mask */
123 unsigned int changed_bits; /* changed wakeup bits */
124 unsigned int changed_mask; /* changed wakeup mask */
125 int paint_count; /* pending paint messages count */
126 int hotkey_count; /* pending hotkey messages count */
127 int quit_message; /* is there a pending quit message? */
128 int exit_code; /* exit code of pending quit message */
129 int cursor_count; /* per-queue cursor show count */
130 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
131 struct list send_result; /* stack of sent messages waiting for result */
132 struct list callback_result; /* list of callback messages waiting for result */
133 struct message_result *recv_result; /* stack of received messages waiting for result */
134 struct list pending_timers; /* list of pending timers */
135 struct list expired_timers; /* list of expired timers */
136 lparam_t next_timer_id; /* id for the next timer with a 0 window */
137 struct timeout_user *timeout; /* timeout for next timer to expire */
138 struct thread_input *input; /* thread input descriptor */
139 struct hook_table *hooks; /* hook table */
140 timeout_t last_get_msg; /* time of last get message call */
143 struct hotkey
145 struct list entry; /* entry in desktop hotkey list */
146 struct msg_queue *queue; /* queue owning this hotkey */
147 user_handle_t win; /* window handle */
148 int id; /* hotkey id */
149 unsigned int vkey; /* virtual key code */
150 unsigned int flags; /* key modifiers */
153 static void msg_queue_dump( struct object *obj, int verbose );
154 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
155 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
156 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
157 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
158 static void msg_queue_destroy( struct object *obj );
159 static void msg_queue_poll_event( struct fd *fd, int event );
160 static void thread_input_dump( struct object *obj, int verbose );
161 static void thread_input_destroy( struct object *obj );
162 static void timer_callback( void *private );
164 static const struct object_ops msg_queue_ops =
166 sizeof(struct msg_queue), /* size */
167 msg_queue_dump, /* dump */
168 no_get_type, /* get_type */
169 msg_queue_add_queue, /* add_queue */
170 msg_queue_remove_queue, /* remove_queue */
171 msg_queue_signaled, /* signaled */
172 msg_queue_satisfied, /* satisfied */
173 no_signal, /* signal */
174 no_get_fd, /* get_fd */
175 no_map_access, /* map_access */
176 default_get_sd, /* get_sd */
177 default_set_sd, /* set_sd */
178 no_lookup_name, /* lookup_name */
179 no_open_file, /* open_file */
180 no_close_handle, /* close_handle */
181 msg_queue_destroy /* destroy */
184 static const struct fd_ops msg_queue_fd_ops =
186 NULL, /* get_poll_events */
187 msg_queue_poll_event, /* poll_event */
188 NULL, /* flush */
189 NULL, /* get_fd_type */
190 NULL, /* ioctl */
191 NULL, /* queue_async */
192 NULL, /* reselect_async */
193 NULL /* cancel async */
197 static const struct object_ops thread_input_ops =
199 sizeof(struct thread_input), /* size */
200 thread_input_dump, /* dump */
201 no_get_type, /* get_type */
202 no_add_queue, /* add_queue */
203 NULL, /* remove_queue */
204 NULL, /* signaled */
205 NULL, /* satisfied */
206 no_signal, /* signal */
207 no_get_fd, /* get_fd */
208 no_map_access, /* map_access */
209 default_get_sd, /* get_sd */
210 default_set_sd, /* set_sd */
211 no_lookup_name, /* lookup_name */
212 no_open_file, /* open_file */
213 no_close_handle, /* close_handle */
214 thread_input_destroy /* destroy */
217 /* pointer to input structure of foreground thread */
218 static unsigned int last_input_time;
220 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
221 static void free_message( struct message *msg );
223 /* set the caret window in a given thread input */
224 static void set_caret_window( struct thread_input *input, user_handle_t win )
226 if (!win || win != input->caret)
228 input->caret_rect.left = 0;
229 input->caret_rect.top = 0;
230 input->caret_rect.right = 0;
231 input->caret_rect.bottom = 0;
233 input->caret = win;
234 input->caret_hide = 1;
235 input->caret_state = 0;
238 /* create a thread input object */
239 static struct thread_input *create_thread_input( struct thread *thread )
241 struct thread_input *input;
243 if ((input = alloc_object( &thread_input_ops )))
245 input->focus = 0;
246 input->capture = 0;
247 input->active = 0;
248 input->menu_owner = 0;
249 input->move_size = 0;
250 input->cursor = 0;
251 input->cursor_count = 0;
252 list_init( &input->msg_list );
253 set_caret_window( input, 0 );
254 memset( input->keystate, 0, sizeof(input->keystate) );
256 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
258 release_object( input );
259 return NULL;
262 return input;
265 /* create a message queue object */
266 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
268 struct thread_input *new_input = NULL;
269 struct msg_queue *queue;
270 int i;
272 if (!input)
274 if (!(new_input = create_thread_input( thread ))) return NULL;
275 input = new_input;
278 if ((queue = alloc_object( &msg_queue_ops )))
280 queue->fd = NULL;
281 queue->wake_bits = 0;
282 queue->wake_mask = 0;
283 queue->changed_bits = 0;
284 queue->changed_mask = 0;
285 queue->paint_count = 0;
286 queue->hotkey_count = 0;
287 queue->quit_message = 0;
288 queue->cursor_count = 0;
289 queue->recv_result = NULL;
290 queue->next_timer_id = 0x7fff;
291 queue->timeout = NULL;
292 queue->input = (struct thread_input *)grab_object( input );
293 queue->hooks = NULL;
294 queue->last_get_msg = current_time;
295 list_init( &queue->send_result );
296 list_init( &queue->callback_result );
297 list_init( &queue->pending_timers );
298 list_init( &queue->expired_timers );
299 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
301 thread->queue = queue;
303 if (new_input) release_object( new_input );
304 return queue;
307 /* free the message queue of a thread at thread exit */
308 void free_msg_queue( struct thread *thread )
310 remove_thread_hooks( thread );
311 if (!thread->queue) return;
312 release_object( thread->queue );
313 thread->queue = NULL;
316 /* change the thread input data of a given thread */
317 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
319 struct msg_queue *queue = thread->queue;
321 if (!queue)
323 thread->queue = create_msg_queue( thread, new_input );
324 return thread->queue != NULL;
326 if (queue->input)
328 queue->input->cursor_count -= queue->cursor_count;
329 release_object( queue->input );
331 queue->input = (struct thread_input *)grab_object( new_input );
332 new_input->cursor_count += queue->cursor_count;
333 return 1;
336 /* set the cursor position and queue the corresponding mouse message */
337 static void set_cursor_pos( struct desktop *desktop, int x, int y )
339 struct hardware_msg_data *msg_data;
340 struct message *msg;
342 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
343 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
345 free( msg );
346 return;
348 memset( msg_data, 0, sizeof(*msg_data) );
350 msg->type = MSG_HARDWARE;
351 msg->win = 0;
352 msg->msg = WM_MOUSEMOVE;
353 msg->wparam = 0;
354 msg->lparam = 0;
355 msg->time = get_tick_count();
356 msg->result = NULL;
357 msg->data = msg_data;
358 msg->data_size = sizeof(*msg_data);
359 msg_data->x = x;
360 msg_data->y = y;
361 queue_hardware_message( desktop, msg, 1 );
364 /* set the cursor clip rectangle */
365 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect )
367 rectangle_t top_rect;
368 int x, y;
370 get_top_window_rectangle( desktop, &top_rect );
371 if (rect)
373 rectangle_t new_rect = *rect;
374 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
375 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
376 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
377 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
378 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
379 desktop->cursor.clip = new_rect;
381 else desktop->cursor.clip = top_rect;
383 if (desktop->cursor.clip_msg)
384 post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
386 /* warp the mouse to be inside the clip rect */
387 x = min( max( desktop->cursor.x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
388 y = min( max( desktop->cursor.y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
389 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
392 /* change the foreground input and reset the cursor clip rect */
393 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
395 if (desktop->foreground_input == input) return;
396 set_clip_rectangle( desktop, NULL );
397 desktop->foreground_input = input;
400 /* get the hook table for a given thread */
401 struct hook_table *get_queue_hooks( struct thread *thread )
403 if (!thread->queue) return NULL;
404 return thread->queue->hooks;
407 /* set the hook table for a given thread, allocating the queue if needed */
408 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
410 struct msg_queue *queue = thread->queue;
411 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
412 if (queue->hooks) release_object( queue->hooks );
413 queue->hooks = hooks;
416 /* check the queue status */
417 static inline int is_signaled( struct msg_queue *queue )
419 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
422 /* set some queue bits */
423 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
425 queue->wake_bits |= bits;
426 queue->changed_bits |= bits;
427 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
430 /* clear some queue bits */
431 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
433 queue->wake_bits &= ~bits;
434 queue->changed_bits &= ~bits;
437 /* check whether msg is a keyboard message */
438 static inline int is_keyboard_msg( struct message *msg )
440 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
443 /* check if message is matched by the filter */
444 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
446 return (msg >= first && msg <= last);
449 /* check whether a message filter contains at least one potential hardware message */
450 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
452 /* hardware message ranges are (in numerical order):
453 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
454 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
455 * WM_MOUSEFIRST .. WM_MOUSELAST
457 if (last < WM_NCMOUSEFIRST) return 0;
458 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
459 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
460 if (first > WM_MOUSELAST) return 0;
461 return 1;
464 /* get the QS_* bit corresponding to a given hardware message */
465 static inline int get_hardware_msg_bit( struct message *msg )
467 if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
468 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
469 if (is_keyboard_msg( msg )) return QS_KEY;
470 return QS_MOUSEBUTTON;
473 /* get the current thread queue, creating it if needed */
474 static inline struct msg_queue *get_current_queue(void)
476 struct msg_queue *queue = current->queue;
477 if (!queue) queue = create_msg_queue( current, NULL );
478 return queue;
481 /* get a (pseudo-)unique id to tag hardware messages */
482 static inline unsigned int get_unique_id(void)
484 static unsigned int id;
485 if (!++id) id = 1; /* avoid an id of 0 */
486 return id;
489 /* try to merge a message with the last in the list; return 1 if successful */
490 static int merge_message( struct thread_input *input, const struct message *msg )
492 struct message *prev;
493 struct list *ptr;
495 if (msg->msg != WM_MOUSEMOVE) return 0;
496 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
498 prev = LIST_ENTRY( ptr, struct message, entry );
499 if (prev->msg != WM_INPUT) break;
501 if (!ptr) return 0;
502 if (prev->result) return 0;
503 if (prev->win && msg->win && prev->win != msg->win) return 0;
504 if (prev->msg != msg->msg) return 0;
505 if (prev->type != msg->type) return 0;
506 /* now we can merge it */
507 prev->wparam = msg->wparam;
508 prev->lparam = msg->lparam;
509 prev->time = msg->time;
510 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
512 struct hardware_msg_data *prev_data = prev->data;
513 struct hardware_msg_data *msg_data = msg->data;
514 prev_data->x = msg_data->x;
515 prev_data->y = msg_data->y;
516 prev_data->info = msg_data->info;
518 list_remove( ptr );
519 list_add_tail( &input->msg_list, ptr );
520 return 1;
523 /* free a result structure */
524 static void free_result( struct message_result *result )
526 if (result->timeout) remove_timeout_user( result->timeout );
527 free( result->data );
528 if (result->callback_msg) free_message( result->callback_msg );
529 if (result->hardware_msg) free_message( result->hardware_msg );
530 if (result->desktop) release_object( result->desktop );
531 free( result );
534 /* remove the result from the sender list it is on */
535 static inline void remove_result_from_sender( struct message_result *result )
537 assert( result->sender );
539 list_remove( &result->sender_entry );
540 result->sender = NULL;
541 if (!result->receiver) free_result( result );
544 /* store the message result in the appropriate structure */
545 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
547 res->result = result;
548 res->error = error;
549 res->replied = 1;
550 if (res->timeout)
552 remove_timeout_user( res->timeout );
553 res->timeout = NULL;
556 if (res->hardware_msg)
558 if (!error && result) /* rejected by the hook */
559 free_message( res->hardware_msg );
560 else
561 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
563 res->hardware_msg = NULL;
566 if (res->sender)
568 if (res->callback_msg)
570 /* queue the callback message in the sender queue */
571 struct callback_msg_data *data = res->callback_msg->data;
572 data->result = result;
573 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
574 set_queue_bits( res->sender, QS_SENDMESSAGE );
575 res->callback_msg = NULL;
576 remove_result_from_sender( res );
578 else
580 /* wake sender queue if waiting on this result */
581 if (list_head(&res->sender->send_result) == &res->sender_entry)
582 set_queue_bits( res->sender, QS_SMRESULT );
585 else if (!res->receiver) free_result( res );
588 /* free a message when deleting a queue or window */
589 static void free_message( struct message *msg )
591 struct message_result *result = msg->result;
592 if (result)
594 result->msg = NULL;
595 result->receiver = NULL;
596 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
598 free( msg->data );
599 free( msg );
602 /* remove (and free) a message from a message list */
603 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
604 enum message_kind kind )
606 list_remove( &msg->entry );
607 switch(kind)
609 case SEND_MESSAGE:
610 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
611 break;
612 case POST_MESSAGE:
613 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
614 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
615 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
616 clear_queue_bits( queue, QS_HOTKEY );
617 break;
619 free_message( msg );
622 /* message timed out without getting a reply */
623 static void result_timeout( void *private )
625 struct message_result *result = private;
627 assert( !result->replied );
629 result->timeout = NULL;
631 if (result->msg) /* not received yet */
633 struct message *msg = result->msg;
635 result->msg = NULL;
636 msg->result = NULL;
637 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
638 result->receiver = NULL;
640 store_message_result( result, 0, STATUS_TIMEOUT );
643 /* allocate and fill a message result structure */
644 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
645 struct msg_queue *recv_queue,
646 struct message *msg, timeout_t timeout )
648 struct message_result *result = mem_alloc( sizeof(*result) );
649 if (result)
651 result->msg = msg;
652 result->sender = send_queue;
653 result->receiver = recv_queue;
654 result->replied = 0;
655 result->data = NULL;
656 result->data_size = 0;
657 result->timeout = NULL;
658 result->hardware_msg = NULL;
659 result->desktop = NULL;
660 result->callback_msg = NULL;
662 if (msg->type == MSG_CALLBACK)
664 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
666 if (!callback_msg)
668 free( result );
669 return NULL;
671 callback_msg->type = MSG_CALLBACK_RESULT;
672 callback_msg->win = msg->win;
673 callback_msg->msg = msg->msg;
674 callback_msg->wparam = 0;
675 callback_msg->lparam = 0;
676 callback_msg->time = get_tick_count();
677 callback_msg->result = NULL;
678 /* steal the data from the original message */
679 callback_msg->data = msg->data;
680 callback_msg->data_size = msg->data_size;
681 msg->data = NULL;
682 msg->data_size = 0;
684 result->callback_msg = callback_msg;
685 list_add_head( &send_queue->callback_result, &result->sender_entry );
687 else if (send_queue)
689 list_add_head( &send_queue->send_result, &result->sender_entry );
690 clear_queue_bits( send_queue, QS_SMRESULT );
693 if (timeout != TIMEOUT_INFINITE)
694 result->timeout = add_timeout_user( timeout, result_timeout, result );
696 return result;
699 /* receive a message, removing it from the sent queue */
700 static void receive_message( struct msg_queue *queue, struct message *msg,
701 struct get_message_reply *reply )
703 struct message_result *result = msg->result;
705 reply->total = msg->data_size;
706 if (msg->data_size > get_reply_max_size())
708 set_error( STATUS_BUFFER_OVERFLOW );
709 return;
711 reply->type = msg->type;
712 reply->win = msg->win;
713 reply->msg = msg->msg;
714 reply->wparam = msg->wparam;
715 reply->lparam = msg->lparam;
716 reply->time = msg->time;
718 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
720 list_remove( &msg->entry );
721 /* put the result on the receiver result stack */
722 if (result)
724 result->msg = NULL;
725 result->recv_next = queue->recv_result;
726 queue->recv_result = result;
728 free( msg );
729 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
732 /* set the result of the current received message */
733 static void reply_message( struct msg_queue *queue, lparam_t result,
734 unsigned int error, int remove, const void *data, data_size_t len )
736 struct message_result *res = queue->recv_result;
738 if (remove)
740 queue->recv_result = res->recv_next;
741 res->receiver = NULL;
742 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
744 free_result( res );
745 return;
748 if (!res->replied)
750 if (len && (res->data = memdup( data, len ))) res->data_size = len;
751 store_message_result( res, result, error );
755 static int match_window( user_handle_t win, user_handle_t msg_win )
757 if (!win) return 1;
758 if (win == -1 || win == 1) return !msg_win;
759 if (msg_win == win) return 1;
760 return is_child_window( win, msg_win );
763 /* retrieve a posted message */
764 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
765 unsigned int first, unsigned int last, unsigned int flags,
766 struct get_message_reply *reply )
768 struct message *msg;
770 /* check against the filters */
771 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
773 if (!match_window( win, msg->win )) continue;
774 if (!check_msg_filter( msg->msg, first, last )) continue;
775 goto found; /* found one */
777 return 0;
779 /* return it to the app */
780 found:
781 reply->total = msg->data_size;
782 if (msg->data_size > get_reply_max_size())
784 set_error( STATUS_BUFFER_OVERFLOW );
785 return 1;
787 reply->type = msg->type;
788 reply->win = msg->win;
789 reply->msg = msg->msg;
790 reply->wparam = msg->wparam;
791 reply->lparam = msg->lparam;
792 reply->time = msg->time;
794 if (flags & PM_REMOVE)
796 if (msg->data)
798 set_reply_data_ptr( msg->data, msg->data_size );
799 msg->data = NULL;
800 msg->data_size = 0;
802 remove_queue_message( queue, msg, POST_MESSAGE );
804 else if (msg->data) set_reply_data( msg->data, msg->data_size );
806 return 1;
809 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
810 struct get_message_reply *reply )
812 if (queue->quit_message)
814 reply->total = 0;
815 reply->type = MSG_POSTED;
816 reply->win = 0;
817 reply->msg = WM_QUIT;
818 reply->wparam = queue->exit_code;
819 reply->lparam = 0;
820 reply->time = get_tick_count();
822 if (flags & PM_REMOVE)
824 queue->quit_message = 0;
825 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
826 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
828 return 1;
830 else
831 return 0;
834 /* empty a message list and free all the messages */
835 static void empty_msg_list( struct list *list )
837 struct list *ptr;
839 while ((ptr = list_head( list )) != NULL)
841 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
842 list_remove( &msg->entry );
843 free_message( msg );
847 /* cleanup all pending results when deleting a queue */
848 static void cleanup_results( struct msg_queue *queue )
850 struct list *entry;
852 while ((entry = list_head( &queue->send_result )) != NULL)
854 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
857 while ((entry = list_head( &queue->callback_result )) != NULL)
859 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
862 while (queue->recv_result)
863 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
866 /* check if the thread owning the queue is hung (not checking for messages) */
867 static int is_queue_hung( struct msg_queue *queue )
869 struct wait_queue_entry *entry;
871 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
872 return 0; /* less than 5 seconds since last get message -> not hung */
874 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
876 if (get_wait_queue_thread(entry)->queue == queue)
877 return 0; /* thread is waiting on queue -> not hung */
879 return 1;
882 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
884 struct msg_queue *queue = (struct msg_queue *)obj;
885 struct process *process = get_wait_queue_thread(entry)->process;
887 /* a thread can only wait on its own queue */
888 if (get_wait_queue_thread(entry)->queue != queue)
890 set_error( STATUS_ACCESS_DENIED );
891 return 0;
893 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
895 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
896 set_fd_events( queue->fd, POLLIN );
897 add_queue( obj, entry );
898 return 1;
901 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
903 struct msg_queue *queue = (struct msg_queue *)obj;
905 remove_queue( obj, entry );
906 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
907 set_fd_events( queue->fd, 0 );
910 static void msg_queue_dump( struct object *obj, int verbose )
912 struct msg_queue *queue = (struct msg_queue *)obj;
913 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
914 queue->wake_bits, queue->wake_mask );
917 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
919 struct msg_queue *queue = (struct msg_queue *)obj;
920 int ret = 0;
922 if (queue->fd)
924 if ((ret = check_fd_events( queue->fd, POLLIN )))
925 /* stop waiting on select() if we are signaled */
926 set_fd_events( queue->fd, 0 );
927 else if (!list_empty( &obj->wait_queue ))
928 /* restart waiting on poll() if we are no longer signaled */
929 set_fd_events( queue->fd, POLLIN );
931 return ret || is_signaled( queue );
934 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
936 struct msg_queue *queue = (struct msg_queue *)obj;
937 queue->wake_mask = 0;
938 queue->changed_mask = 0;
941 static void msg_queue_destroy( struct object *obj )
943 struct msg_queue *queue = (struct msg_queue *)obj;
944 struct list *ptr;
945 struct hotkey *hotkey, *hotkey2;
946 int i;
948 cleanup_results( queue );
949 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
951 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
953 if (hotkey->queue == queue)
955 list_remove( &hotkey->entry );
956 free( hotkey );
960 while ((ptr = list_head( &queue->pending_timers )))
962 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
963 list_remove( &timer->entry );
964 free( timer );
966 while ((ptr = list_head( &queue->expired_timers )))
968 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
969 list_remove( &timer->entry );
970 free( timer );
972 if (queue->timeout) remove_timeout_user( queue->timeout );
973 queue->input->cursor_count -= queue->cursor_count;
974 release_object( queue->input );
975 if (queue->hooks) release_object( queue->hooks );
976 if (queue->fd) release_object( queue->fd );
979 static void msg_queue_poll_event( struct fd *fd, int event )
981 struct msg_queue *queue = get_fd_user( fd );
982 assert( queue->obj.ops == &msg_queue_ops );
984 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
985 else set_fd_events( queue->fd, 0 );
986 wake_up( &queue->obj, 0 );
989 static void thread_input_dump( struct object *obj, int verbose )
991 struct thread_input *input = (struct thread_input *)obj;
992 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
993 input->focus, input->capture, input->active );
996 static void thread_input_destroy( struct object *obj )
998 struct thread_input *input = (struct thread_input *)obj;
1000 empty_msg_list( &input->msg_list );
1001 if (input->desktop)
1003 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
1004 release_object( input->desktop );
1008 /* fix the thread input data when a window is destroyed */
1009 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1011 struct thread_input *input = queue->input;
1013 if (window == input->focus) input->focus = 0;
1014 if (window == input->capture) input->capture = 0;
1015 if (window == input->active) input->active = 0;
1016 if (window == input->menu_owner) input->menu_owner = 0;
1017 if (window == input->move_size) input->move_size = 0;
1018 if (window == input->caret) set_caret_window( input, 0 );
1021 /* check if the specified window can be set in the input data of a given queue */
1022 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1024 struct thread *thread;
1025 int ret = 0;
1027 if (!window) return 1; /* we can always clear the data */
1029 if ((thread = get_window_thread( window )))
1031 ret = (queue->input == thread->queue->input);
1032 if (!ret) set_error( STATUS_ACCESS_DENIED );
1033 release_object( thread );
1035 else set_error( STATUS_INVALID_HANDLE );
1037 return ret;
1040 /* make sure the specified thread has a queue */
1041 int init_thread_queue( struct thread *thread )
1043 if (thread->queue) return 1;
1044 return (create_msg_queue( thread, NULL ) != NULL);
1047 /* attach two thread input data structures */
1048 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1050 struct desktop *desktop;
1051 struct thread_input *input;
1052 int ret;
1054 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1055 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1056 input = (struct thread_input *)grab_object( thread_to->queue->input );
1057 if (input->desktop != desktop)
1059 set_error( STATUS_ACCESS_DENIED );
1060 release_object( input );
1061 release_object( desktop );
1062 return 0;
1064 release_object( desktop );
1066 ret = assign_thread_input( thread_from, input );
1067 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1068 release_object( input );
1069 return ret;
1072 /* detach two thread input data structures */
1073 void detach_thread_input( struct thread *thread_from )
1075 struct thread_input *input;
1077 if ((input = create_thread_input( thread_from )))
1079 assign_thread_input( thread_from, input );
1080 release_object( input );
1085 /* set the next timer to expire */
1086 static void set_next_timer( struct msg_queue *queue )
1088 struct list *ptr;
1090 if (queue->timeout)
1092 remove_timeout_user( queue->timeout );
1093 queue->timeout = NULL;
1095 if ((ptr = list_head( &queue->pending_timers )))
1097 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1098 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1100 /* set/clear QS_TIMER bit */
1101 if (list_empty( &queue->expired_timers ))
1102 clear_queue_bits( queue, QS_TIMER );
1103 else
1104 set_queue_bits( queue, QS_TIMER );
1107 /* find a timer from its window and id */
1108 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1109 unsigned int msg, lparam_t id )
1111 struct list *ptr;
1113 /* we need to search both lists */
1115 LIST_FOR_EACH( ptr, &queue->pending_timers )
1117 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1118 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1120 LIST_FOR_EACH( ptr, &queue->expired_timers )
1122 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1123 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1125 return NULL;
1128 /* callback for the next timer expiration */
1129 static void timer_callback( void *private )
1131 struct msg_queue *queue = private;
1132 struct list *ptr;
1134 queue->timeout = NULL;
1135 /* move on to the next timer */
1136 ptr = list_head( &queue->pending_timers );
1137 list_remove( ptr );
1138 list_add_tail( &queue->expired_timers, ptr );
1139 set_next_timer( queue );
1142 /* link a timer at its rightful place in the queue list */
1143 static void link_timer( struct msg_queue *queue, struct timer *timer )
1145 struct list *ptr;
1147 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1149 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1150 if (t->when >= timer->when) break;
1152 list_add_before( ptr, &timer->entry );
1155 /* remove a timer from the queue timer list and free it */
1156 static void free_timer( struct msg_queue *queue, struct timer *timer )
1158 list_remove( &timer->entry );
1159 free( timer );
1160 set_next_timer( queue );
1163 /* restart an expired timer */
1164 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1166 list_remove( &timer->entry );
1167 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1168 link_timer( queue, timer );
1169 set_next_timer( queue );
1172 /* find an expired timer matching the filtering parameters */
1173 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1174 unsigned int get_first, unsigned int get_last,
1175 int remove )
1177 struct list *ptr;
1179 LIST_FOR_EACH( ptr, &queue->expired_timers )
1181 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1182 if (win && timer->win != win) continue;
1183 if (check_msg_filter( timer->msg, get_first, get_last ))
1185 if (remove) restart_timer( queue, timer );
1186 return timer;
1189 return NULL;
1192 /* add a timer */
1193 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1195 struct timer *timer = mem_alloc( sizeof(*timer) );
1196 if (timer)
1198 timer->rate = max( rate, 1 );
1199 timer->when = current_time + (timeout_t)timer->rate * 10000;
1200 link_timer( queue, timer );
1201 /* check if we replaced the next timer */
1202 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1204 return timer;
1207 /* change the input key state for a given key */
1208 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1210 if (down)
1212 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1213 keystate[key] |= down;
1215 else keystate[key] &= ~0x80;
1218 /* update the input key state for a keyboard message */
1219 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1220 const struct message *msg )
1222 unsigned char key;
1223 int down = 0;
1225 switch (msg->msg)
1227 case WM_LBUTTONDOWN:
1228 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1229 /* fall through */
1230 case WM_LBUTTONUP:
1231 set_input_key_state( keystate, VK_LBUTTON, down );
1232 break;
1233 case WM_MBUTTONDOWN:
1234 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1235 /* fall through */
1236 case WM_MBUTTONUP:
1237 set_input_key_state( keystate, VK_MBUTTON, down );
1238 break;
1239 case WM_RBUTTONDOWN:
1240 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1241 /* fall through */
1242 case WM_RBUTTONUP:
1243 set_input_key_state( keystate, VK_RBUTTON, down );
1244 break;
1245 case WM_XBUTTONDOWN:
1246 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1247 /* fall through */
1248 case WM_XBUTTONUP:
1249 if (msg->wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1250 else if (msg->wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1251 break;
1252 case WM_KEYDOWN:
1253 case WM_SYSKEYDOWN:
1254 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1255 /* fall through */
1256 case WM_KEYUP:
1257 case WM_SYSKEYUP:
1258 key = (unsigned char)msg->wparam;
1259 set_input_key_state( keystate, key, down );
1260 switch(key)
1262 case VK_LCONTROL:
1263 case VK_RCONTROL:
1264 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1265 set_input_key_state( keystate, VK_CONTROL, down );
1266 break;
1267 case VK_LMENU:
1268 case VK_RMENU:
1269 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1270 set_input_key_state( keystate, VK_MENU, down );
1271 break;
1272 case VK_LSHIFT:
1273 case VK_RSHIFT:
1274 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1275 set_input_key_state( keystate, VK_SHIFT, down );
1276 break;
1278 break;
1282 /* release the hardware message currently being processed by the given thread */
1283 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1284 int remove, user_handle_t new_win )
1286 struct thread_input *input = queue->input;
1287 struct message *msg;
1289 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1291 if (msg->unique_id == hw_id) break;
1293 if (&msg->entry == &input->msg_list) return; /* not found */
1295 /* clear the queue bit for that message */
1296 if (remove || new_win)
1298 struct message *other;
1299 int clr_bit;
1301 clr_bit = get_hardware_msg_bit( msg );
1302 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1304 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1306 clr_bit = 0;
1307 break;
1310 if (clr_bit) clear_queue_bits( queue, clr_bit );
1313 if (new_win) /* set the new window */
1315 struct thread *owner = get_window_thread( new_win );
1316 if (owner)
1318 msg->win = new_win;
1319 if (owner->queue->input != input)
1321 list_remove( &msg->entry );
1322 if (merge_message( owner->queue->input, msg ))
1324 free_message( msg );
1325 release_object( owner );
1326 return;
1328 list_add_tail( &owner->queue->input->msg_list, &msg->entry );
1330 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1331 remove = 0;
1332 release_object( owner );
1335 if (remove)
1337 update_input_key_state( input->desktop, input->keystate, msg );
1338 list_remove( &msg->entry );
1339 free_message( msg );
1343 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1345 struct hotkey *hotkey;
1346 unsigned int modifiers = 0;
1348 if (msg->msg != WM_KEYDOWN) return 0;
1350 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1351 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1352 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1353 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1355 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1357 if (hotkey->vkey != msg->wparam) continue;
1358 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1361 return 0;
1363 found:
1364 msg->type = MSG_POSTED;
1365 msg->win = hotkey->win;
1366 msg->msg = WM_HOTKEY;
1367 msg->wparam = hotkey->id;
1368 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1370 free( msg->data );
1371 msg->data = NULL;
1372 msg->data_size = 0;
1374 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1375 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1376 hotkey->queue->hotkey_count++;
1377 return 1;
1380 /* find the window that should receive a given hardware message */
1381 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1382 struct message *msg, unsigned int *msg_code,
1383 struct thread **thread )
1385 struct hardware_msg_data *data = msg->data;
1386 user_handle_t win = 0;
1388 *thread = NULL;
1389 *msg_code = msg->msg;
1390 if (msg->msg == WM_INPUT)
1392 if (!(win = msg->win) && input) win = input->focus;
1394 else if (is_keyboard_msg( msg ))
1396 if (input && !(win = input->focus))
1398 win = input->active;
1399 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1402 else if (!input || !(win = input->capture)) /* mouse message */
1404 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1405 else win = shallow_window_from_point( desktop, data->x, data->y );
1407 *thread = window_thread_from_point( win, data->x, data->y );
1410 if (!*thread)
1411 *thread = get_window_thread( win );
1412 return win;
1415 static struct rawinput_device_entry *find_rawinput_device( unsigned short usage_page, unsigned short usage )
1417 struct rawinput_device_entry *e;
1419 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
1421 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1422 return e;
1425 return NULL;
1428 static void update_rawinput_device(const struct rawinput_device *device)
1430 struct rawinput_device_entry *e;
1432 if (!(e = find_rawinput_device( device->usage_page, device->usage )))
1434 if (!(e = mem_alloc( sizeof(*e) ))) return;
1435 list_add_tail( &current->process->rawinput_devices, &e->entry );
1438 if (device->flags & RIDEV_REMOVE)
1440 list_remove( &e->entry );
1441 free( e );
1442 return;
1445 e->device = *device;
1446 e->device.target = get_user_full_handle( e->device.target );
1449 /* queue a hardware message into a given thread input */
1450 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1452 user_handle_t win;
1453 struct thread *thread;
1454 struct thread_input *input;
1455 unsigned int msg_code;
1456 struct hardware_msg_data *data = msg->data;
1458 update_input_key_state( desktop, desktop->keystate, msg );
1459 last_input_time = get_tick_count();
1460 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1462 if (is_keyboard_msg( msg ))
1464 if (queue_hotkey_message( desktop, msg )) return;
1465 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1466 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1467 msg->lparam &= ~(KF_EXTENDED << 16);
1469 else if (msg->msg != WM_INPUT)
1471 if (msg->msg == WM_MOUSEMOVE)
1473 int x = min( max( data->x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
1474 int y = min( max( data->y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
1475 if (desktop->cursor.x != x || desktop->cursor.y != y) always_queue = 1;
1476 desktop->cursor.x = x;
1477 desktop->cursor.y = y;
1478 desktop->cursor.last_change = get_tick_count();
1480 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1481 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1482 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1483 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1484 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1485 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1486 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1488 data->x = desktop->cursor.x;
1489 data->y = desktop->cursor.y;
1491 if (msg->win && (thread = get_window_thread( msg->win )))
1493 input = thread->queue->input;
1494 release_object( thread );
1496 else input = desktop->foreground_input;
1498 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1499 if (!win || !thread)
1501 if (input) update_input_key_state( input->desktop, input->keystate, msg );
1502 free_message( msg );
1503 return;
1505 input = thread->queue->input;
1507 if (win != desktop->cursor.win) always_queue = 1;
1508 desktop->cursor.win = win;
1510 if (!always_queue || merge_message( input, msg )) free_message( msg );
1511 else
1513 msg->unique_id = 0; /* will be set once we return it to the app */
1514 list_add_tail( &input->msg_list, &msg->entry );
1515 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1517 release_object( thread );
1520 /* send the low-level hook message for a given hardware message */
1521 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1522 const hw_input_t *input, struct msg_queue *sender )
1524 struct thread *hook_thread;
1525 struct msg_queue *queue;
1526 struct message *msg;
1527 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1528 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1530 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1531 if (!(queue = hook_thread->queue)) return 0;
1532 if (is_queue_hung( queue )) return 0;
1534 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1536 msg->type = MSG_HOOK_LL;
1537 msg->win = 0;
1538 msg->msg = id;
1539 msg->wparam = hardware_msg->msg;
1540 msg->time = hardware_msg->time;
1541 msg->data_size = hardware_msg->data_size;
1542 msg->result = NULL;
1544 if (input->type == INPUT_KEYBOARD)
1546 unsigned short vkey = input->kbd.vkey;
1547 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1548 msg->lparam = (input->kbd.scan << 16) | vkey;
1550 else msg->lparam = input->mouse.data << 16;
1552 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1553 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1555 free_message( msg );
1556 return 0;
1558 msg->result->hardware_msg = hardware_msg;
1559 msg->result->desktop = (struct desktop *)grab_object( desktop );
1560 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1561 set_queue_bits( queue, QS_SENDMESSAGE );
1562 return 1;
1565 /* queue a hardware message for a mouse event */
1566 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1567 unsigned int hook_flags, struct msg_queue *sender )
1569 const struct rawinput_device *device;
1570 struct hardware_msg_data *msg_data;
1571 struct message *msg;
1572 unsigned int i, time, flags;
1573 int wait = 0, x, y;
1575 static const unsigned int messages[] =
1577 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1578 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1579 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1580 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1581 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1582 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1583 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1584 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1585 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1586 0, /* 0x0200 = unused */
1587 0, /* 0x0400 = unused */
1588 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1589 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1592 desktop->cursor.last_change = get_tick_count();
1593 flags = input->mouse.flags;
1594 time = input->mouse.time;
1595 if (!time) time = desktop->cursor.last_change;
1597 if (flags & MOUSEEVENTF_MOVE)
1599 if (flags & MOUSEEVENTF_ABSOLUTE)
1601 x = input->mouse.x;
1602 y = input->mouse.y;
1603 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1604 x == desktop->cursor.x && y == desktop->cursor.y)
1605 flags &= ~MOUSEEVENTF_MOVE;
1607 else
1609 x = desktop->cursor.x + input->mouse.x;
1610 y = desktop->cursor.y + input->mouse.y;
1613 else
1615 x = desktop->cursor.x;
1616 y = desktop->cursor.y;
1619 if ((device = current->process->rawinput_mouse))
1621 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1622 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1624 free( msg );
1625 return 0;
1628 msg->type = MSG_HARDWARE;
1629 msg->win = device->target;
1630 msg->msg = WM_INPUT;
1631 msg->wparam = RIM_INPUT;
1632 msg->lparam = 0;
1633 msg->time = time;
1634 msg->data = msg_data;
1635 msg->data_size = sizeof(*msg_data);
1636 msg->result = NULL;
1638 msg_data->info = input->mouse.info;
1639 msg_data->flags = flags;
1640 msg_data->rawinput.type = RIM_TYPEMOUSE;
1641 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1642 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1643 msg_data->rawinput.mouse.data = input->mouse.data;
1645 queue_hardware_message( desktop, msg, 0 );
1648 for (i = 0; i < sizeof(messages)/sizeof(messages[0]); i++)
1650 if (!messages[i]) continue;
1651 if (!(flags & (1 << i))) continue;
1652 flags &= ~(1 << i);
1654 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1655 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1657 free( msg );
1658 return 0;
1660 memset( msg_data, 0, sizeof(*msg_data) );
1662 msg->type = MSG_HARDWARE;
1663 msg->win = get_user_full_handle( win );
1664 msg->msg = messages[i];
1665 msg->wparam = input->mouse.data << 16;
1666 msg->lparam = 0;
1667 msg->time = time;
1668 msg->result = NULL;
1669 msg->data = msg_data;
1670 msg->data_size = sizeof(*msg_data);
1671 msg_data->x = x;
1672 msg_data->y = y;
1673 msg_data->info = input->mouse.info;
1674 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLMHF_INJECTED;
1676 /* specify a sender only when sending the last message */
1677 if (!(flags & ((1 << sizeof(messages)/sizeof(messages[0])) - 1)))
1679 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1680 queue_hardware_message( desktop, msg, 0 );
1682 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1683 queue_hardware_message( desktop, msg, 0 );
1685 return wait;
1688 /* queue a hardware message for a keyboard event */
1689 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1690 unsigned int hook_flags, struct msg_queue *sender )
1692 const struct rawinput_device *device;
1693 struct hardware_msg_data *msg_data;
1694 struct message *msg;
1695 unsigned char vkey = input->kbd.vkey;
1696 unsigned int message_code, time;
1697 int wait;
1699 if (!(time = input->kbd.time)) time = get_tick_count();
1701 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1703 switch (vkey)
1705 case VK_MENU:
1706 case VK_LMENU:
1707 case VK_RMENU:
1708 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1709 break;
1710 case VK_CONTROL:
1711 case VK_LCONTROL:
1712 case VK_RCONTROL:
1713 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1714 break;
1715 case VK_SHIFT:
1716 case VK_LSHIFT:
1717 case VK_RSHIFT:
1718 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1719 break;
1723 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1724 switch (vkey)
1726 case VK_LMENU:
1727 case VK_RMENU:
1728 if (input->kbd.flags & KEYEVENTF_KEYUP)
1730 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1731 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1732 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1733 message_code = WM_SYSKEYUP;
1734 desktop->keystate[VK_MENU] &= ~0x02;
1736 else
1738 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1739 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1740 message_code = WM_SYSKEYDOWN;
1741 desktop->keystate[VK_MENU] |= 0x02;
1743 break;
1745 case VK_LCONTROL:
1746 case VK_RCONTROL:
1747 /* send WM_SYSKEYUP on release if Alt still pressed */
1748 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1749 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1750 message_code = WM_SYSKEYUP;
1751 desktop->keystate[VK_MENU] &= ~0x02;
1752 break;
1754 default:
1755 /* send WM_SYSKEY for Alt-anykey and for F10 */
1756 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1757 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1758 /* fall through */
1759 case VK_F10:
1760 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1761 desktop->keystate[VK_MENU] &= ~0x02;
1762 break;
1765 if ((device = current->process->rawinput_kbd))
1767 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1768 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1770 free( msg );
1771 return 0;
1774 msg->type = MSG_HARDWARE;
1775 msg->win = device->target;
1776 msg->msg = WM_INPUT;
1777 msg->wparam = RIM_INPUT;
1778 msg->lparam = 0;
1779 msg->time = time;
1780 msg->data = msg_data;
1781 msg->data_size = sizeof(*msg_data);
1782 msg->result = NULL;
1784 msg_data->info = input->kbd.info;
1785 msg_data->flags = input->kbd.flags;
1786 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1787 msg_data->rawinput.kbd.message = message_code;
1788 msg_data->rawinput.kbd.vkey = vkey;
1789 msg_data->rawinput.kbd.scan = input->kbd.scan;
1791 queue_hardware_message( desktop, msg, 0 );
1794 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1795 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1797 free( msg );
1798 return 0;
1800 memset( msg_data, 0, sizeof(*msg_data) );
1802 msg->type = MSG_HARDWARE;
1803 msg->win = get_user_full_handle( win );
1804 msg->msg = message_code;
1805 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1806 msg->time = time;
1807 msg->result = NULL;
1808 msg->data = msg_data;
1809 msg->data_size = sizeof(*msg_data);
1810 msg_data->info = input->kbd.info;
1811 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLKHF_INJECTED;
1813 if (input->kbd.flags & KEYEVENTF_UNICODE)
1815 msg->wparam = VK_PACKET;
1817 else
1819 unsigned int flags = 0;
1820 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1821 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1822 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1823 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1825 msg->wparam = vkey;
1826 msg->lparam |= flags << 16;
1827 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1830 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1831 queue_hardware_message( desktop, msg, 1 );
1833 return wait;
1836 /* queue a hardware message for a custom type of event */
1837 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1838 const hw_input_t *input )
1840 struct hardware_msg_data *msg_data;
1841 struct message *msg;
1843 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
1844 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1846 free( msg );
1847 return;
1849 memset( msg_data, 0, sizeof(*msg_data) );
1851 msg->type = MSG_HARDWARE;
1852 msg->win = get_user_full_handle( win );
1853 msg->msg = input->hw.msg;
1854 msg->wparam = 0;
1855 msg->lparam = input->hw.lparam;
1856 msg->time = get_tick_count();
1857 msg->result = NULL;
1858 msg->data = msg_data;
1859 msg->data_size = sizeof(*msg_data);
1861 queue_hardware_message( desktop, msg, 1 );
1864 /* check message filter for a hardware message */
1865 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1866 user_handle_t filter_win, unsigned int first, unsigned int last )
1868 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1870 /* we can only test the window for a keyboard message since the
1871 * dest window for a mouse message depends on hittest */
1872 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1873 return 0;
1874 /* the message code is final for a keyboard message, we can simply check it */
1875 return check_msg_filter( msg_code, first, last );
1877 else /* mouse message */
1879 /* we need to check all possible values that the message can have in the end */
1881 if (check_msg_filter( msg_code, first, last )) return 1;
1882 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1884 /* all other messages can become non-client messages */
1885 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1887 /* clicks can become double-clicks or non-client double-clicks */
1888 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1889 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1891 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1892 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1894 return 0;
1899 /* find a hardware message for the given queue */
1900 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1901 unsigned int first, unsigned int last, unsigned int flags,
1902 struct get_message_reply *reply )
1904 struct thread_input *input = thread->queue->input;
1905 struct thread *win_thread;
1906 struct list *ptr;
1907 user_handle_t win;
1908 int clear_bits, got_one = 0;
1909 unsigned int msg_code;
1911 ptr = list_head( &input->msg_list );
1912 if (hw_id)
1914 while (ptr)
1916 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1917 if (msg->unique_id == hw_id) break;
1918 ptr = list_next( &input->msg_list, ptr );
1920 if (!ptr) ptr = list_head( &input->msg_list );
1921 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1924 if (ptr == list_head( &input->msg_list ))
1925 clear_bits = QS_INPUT;
1926 else
1927 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1929 while (ptr)
1931 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1932 struct hardware_msg_data *data = msg->data;
1934 ptr = list_next( &input->msg_list, ptr );
1935 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
1936 if (!win || !win_thread)
1938 /* no window at all, remove it */
1939 update_input_key_state( input->desktop, input->keystate, msg );
1940 list_remove( &msg->entry );
1941 free_message( msg );
1942 continue;
1944 if (win_thread != thread)
1946 if (win_thread->queue->input == input)
1948 /* wake the other thread */
1949 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1950 got_one = 1;
1952 else
1954 /* for another thread input, drop it */
1955 update_input_key_state( input->desktop, input->keystate, msg );
1956 list_remove( &msg->entry );
1957 free_message( msg );
1959 release_object( win_thread );
1960 continue;
1962 release_object( win_thread );
1964 /* if we already got a message for another thread, or if it doesn't
1965 * match the filter we skip it */
1966 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1968 clear_bits &= ~get_hardware_msg_bit( msg );
1969 continue;
1971 /* now we can return it */
1972 if (!msg->unique_id) msg->unique_id = get_unique_id();
1973 reply->type = MSG_HARDWARE;
1974 reply->win = win;
1975 reply->msg = msg_code;
1976 reply->wparam = msg->wparam;
1977 reply->lparam = msg->lparam;
1978 reply->time = msg->time;
1980 data->hw_id = msg->unique_id;
1981 set_reply_data( msg->data, msg->data_size );
1982 if (msg->msg == WM_INPUT && (flags & PM_REMOVE))
1983 release_hardware_message( current->queue, data->hw_id, 1, 0 );
1984 return 1;
1986 /* nothing found, clear the hardware queue bits */
1987 clear_queue_bits( thread->queue, clear_bits );
1988 return 0;
1991 /* increment (or decrement if 'incr' is negative) the queue paint count */
1992 void inc_queue_paint_count( struct thread *thread, int incr )
1994 struct msg_queue *queue = thread->queue;
1996 assert( queue );
1998 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2000 if (queue->paint_count)
2001 set_queue_bits( queue, QS_PAINT );
2002 else
2003 clear_queue_bits( queue, QS_PAINT );
2007 /* remove all messages and timers belonging to a certain window */
2008 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2010 struct msg_queue *queue = thread->queue;
2011 struct list *ptr;
2012 int i;
2014 if (!queue) return;
2016 /* remove timers */
2018 ptr = list_head( &queue->pending_timers );
2019 while (ptr)
2021 struct list *next = list_next( &queue->pending_timers, ptr );
2022 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2023 if (timer->win == win) free_timer( queue, timer );
2024 ptr = next;
2026 ptr = list_head( &queue->expired_timers );
2027 while (ptr)
2029 struct list *next = list_next( &queue->expired_timers, ptr );
2030 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2031 if (timer->win == win) free_timer( queue, timer );
2032 ptr = next;
2035 /* remove messages */
2036 for (i = 0; i < NB_MSG_KINDS; i++)
2038 struct list *ptr, *next;
2040 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2042 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2043 if (msg->win == win)
2045 if (msg->msg == WM_QUIT && !queue->quit_message)
2047 queue->quit_message = 1;
2048 queue->exit_code = msg->wparam;
2050 remove_queue_message( queue, msg, i );
2055 thread_input_cleanup_window( queue, win );
2058 /* post a message to a window; used by socket handling */
2059 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2061 struct message *msg;
2062 struct thread *thread = get_window_thread( win );
2064 if (!thread) return;
2066 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2068 msg->type = MSG_POSTED;
2069 msg->win = get_user_full_handle( win );
2070 msg->msg = message;
2071 msg->wparam = wparam;
2072 msg->lparam = lparam;
2073 msg->time = get_tick_count();
2074 msg->result = NULL;
2075 msg->data = NULL;
2076 msg->data_size = 0;
2078 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2079 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2080 if (message == WM_HOTKEY)
2082 set_queue_bits( thread->queue, QS_HOTKEY );
2083 thread->queue->hotkey_count++;
2086 release_object( thread );
2089 /* post a win event */
2090 void post_win_event( struct thread *thread, unsigned int event,
2091 user_handle_t win, unsigned int object_id,
2092 unsigned int child_id, client_ptr_t hook_proc,
2093 const WCHAR *module, data_size_t module_size,
2094 user_handle_t hook)
2096 struct message *msg;
2098 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2100 struct winevent_msg_data *data;
2102 msg->type = MSG_WINEVENT;
2103 msg->win = get_user_full_handle( win );
2104 msg->msg = event;
2105 msg->wparam = object_id;
2106 msg->lparam = child_id;
2107 msg->time = get_tick_count();
2108 msg->result = NULL;
2110 if ((data = malloc( sizeof(*data) + module_size )))
2112 data->hook = hook;
2113 data->tid = get_thread_id( current );
2114 data->hook_proc = hook_proc;
2115 memcpy( data + 1, module, module_size );
2117 msg->data = data;
2118 msg->data_size = sizeof(*data) + module_size;
2120 if (debug_level > 1)
2121 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2122 get_thread_id(thread), event, win, object_id, child_id );
2123 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2124 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2126 else
2127 free( msg );
2131 /* free all hotkeys on a desktop, optionally filtering by window */
2132 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2134 struct hotkey *hotkey, *hotkey2;
2136 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2138 if (!window || hotkey->win == window)
2140 list_remove( &hotkey->entry );
2141 free( hotkey );
2147 /* check if the thread owning the window is hung */
2148 DECL_HANDLER(is_window_hung)
2150 struct thread *thread;
2152 thread = get_window_thread( req->win );
2153 if (thread)
2155 reply->is_hung = is_queue_hung( thread->queue );
2156 release_object( thread );
2158 else reply->is_hung = 0;
2162 /* get the message queue of the current thread */
2163 DECL_HANDLER(get_msg_queue)
2165 struct msg_queue *queue = get_current_queue();
2167 reply->handle = 0;
2168 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2172 /* set the file descriptor associated to the current thread queue */
2173 DECL_HANDLER(set_queue_fd)
2175 struct msg_queue *queue = get_current_queue();
2176 struct file *file;
2177 int unix_fd;
2179 if (queue->fd) /* fd can only be set once */
2181 set_error( STATUS_ACCESS_DENIED );
2182 return;
2184 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2186 if ((unix_fd = get_file_unix_fd( file )) != -1)
2188 if ((unix_fd = dup( unix_fd )) != -1)
2189 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2190 else
2191 file_set_error();
2193 release_object( file );
2197 /* set the current message queue wakeup mask */
2198 DECL_HANDLER(set_queue_mask)
2200 struct msg_queue *queue = get_current_queue();
2202 if (queue)
2204 queue->wake_mask = req->wake_mask;
2205 queue->changed_mask = req->changed_mask;
2206 reply->wake_bits = queue->wake_bits;
2207 reply->changed_bits = queue->changed_bits;
2208 if (is_signaled( queue ))
2210 /* if skip wait is set, do what would have been done in the subsequent wait */
2211 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2212 else wake_up( &queue->obj, 0 );
2218 /* get the current message queue status */
2219 DECL_HANDLER(get_queue_status)
2221 struct msg_queue *queue = current->queue;
2222 if (queue)
2224 reply->wake_bits = queue->wake_bits;
2225 reply->changed_bits = queue->changed_bits;
2226 if (req->clear) queue->changed_bits = 0;
2228 else reply->wake_bits = reply->changed_bits = 0;
2232 /* send a message to a thread queue */
2233 DECL_HANDLER(send_message)
2235 struct message *msg;
2236 struct msg_queue *send_queue = get_current_queue();
2237 struct msg_queue *recv_queue = NULL;
2238 struct thread *thread = NULL;
2240 if (!(thread = get_thread_from_id( req->id ))) return;
2242 if (!(recv_queue = thread->queue))
2244 set_error( STATUS_INVALID_PARAMETER );
2245 release_object( thread );
2246 return;
2248 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2250 set_error( STATUS_TIMEOUT );
2251 release_object( thread );
2252 return;
2255 if ((msg = mem_alloc( sizeof(*msg) )))
2257 msg->type = req->type;
2258 msg->win = get_user_full_handle( req->win );
2259 msg->msg = req->msg;
2260 msg->wparam = req->wparam;
2261 msg->lparam = req->lparam;
2262 msg->time = get_tick_count();
2263 msg->result = NULL;
2264 msg->data = NULL;
2265 msg->data_size = get_req_data_size();
2267 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2269 free( msg );
2270 release_object( thread );
2271 return;
2274 switch(msg->type)
2276 case MSG_OTHER_PROCESS:
2277 case MSG_ASCII:
2278 case MSG_UNICODE:
2279 case MSG_CALLBACK:
2280 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2282 free_message( msg );
2283 break;
2285 /* fall through */
2286 case MSG_NOTIFY:
2287 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2288 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2289 break;
2290 case MSG_POSTED:
2291 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2292 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2293 if (msg->msg == WM_HOTKEY)
2295 set_queue_bits( recv_queue, QS_HOTKEY );
2296 recv_queue->hotkey_count++;
2298 break;
2299 case MSG_HARDWARE: /* should use send_hardware_message instead */
2300 case MSG_CALLBACK_RESULT: /* cannot send this one */
2301 case MSG_HOOK_LL: /* generated internally */
2302 default:
2303 set_error( STATUS_INVALID_PARAMETER );
2304 free( msg );
2305 break;
2308 release_object( thread );
2311 /* send a hardware message to a thread queue */
2312 DECL_HANDLER(send_hardware_message)
2314 struct thread *thread = NULL;
2315 struct desktop *desktop;
2316 struct msg_queue *sender = get_current_queue();
2317 data_size_t size = min( 256, get_reply_max_size() );
2319 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2321 if (req->win)
2323 if (!(thread = get_window_thread( req->win ))) return;
2324 if (desktop != thread->queue->input->desktop)
2326 /* don't allow queuing events to a different desktop */
2327 release_object( desktop );
2328 return;
2332 reply->prev_x = desktop->cursor.x;
2333 reply->prev_y = desktop->cursor.y;
2335 switch (req->input.type)
2337 case INPUT_MOUSE:
2338 reply->wait = queue_mouse_message( desktop, req->win, &req->input, req->flags, sender );
2339 break;
2340 case INPUT_KEYBOARD:
2341 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, req->flags, sender );
2342 break;
2343 case INPUT_HARDWARE:
2344 queue_custom_hardware_message( desktop, req->win, &req->input );
2345 break;
2346 default:
2347 set_error( STATUS_INVALID_PARAMETER );
2349 if (thread) release_object( thread );
2351 reply->new_x = desktop->cursor.x;
2352 reply->new_y = desktop->cursor.y;
2353 set_reply_data( desktop->keystate, size );
2354 release_object( desktop );
2357 /* post a quit message to the current queue */
2358 DECL_HANDLER(post_quit_message)
2360 struct msg_queue *queue = get_current_queue();
2362 if (!queue)
2363 return;
2365 queue->quit_message = 1;
2366 queue->exit_code = req->exit_code;
2367 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2370 /* get a message from the current queue */
2371 DECL_HANDLER(get_message)
2373 struct timer *timer;
2374 struct list *ptr;
2375 struct msg_queue *queue = get_current_queue();
2376 user_handle_t get_win = get_user_full_handle( req->get_win );
2377 unsigned int filter = req->flags >> 16;
2379 reply->active_hooks = get_active_hooks();
2381 if (!queue) return;
2382 queue->last_get_msg = current_time;
2383 if (!filter) filter = QS_ALLINPUT;
2385 /* first check for sent messages */
2386 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2388 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2389 receive_message( queue, msg, reply );
2390 return;
2393 /* clear changed bits so we can wait on them if we don't find a message */
2394 if (filter & QS_POSTMESSAGE)
2396 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2397 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2399 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2400 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2402 /* then check for posted messages */
2403 if ((filter & QS_POSTMESSAGE) &&
2404 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2405 return;
2407 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2408 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2409 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2410 return;
2412 /* only check for quit messages if not posted messages pending.
2413 * note: the quit message isn't filtered */
2414 if (get_quit_message( queue, req->flags, reply ))
2415 return;
2417 /* then check for any raw hardware message */
2418 if ((filter & QS_INPUT) &&
2419 filter_contains_hw_range( req->get_first, req->get_last ) &&
2420 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2421 return;
2423 /* now check for WM_PAINT */
2424 if ((filter & QS_PAINT) &&
2425 queue->paint_count &&
2426 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2427 (reply->win = find_window_to_repaint( get_win, current )))
2429 reply->type = MSG_POSTED;
2430 reply->msg = WM_PAINT;
2431 reply->wparam = 0;
2432 reply->lparam = 0;
2433 reply->time = get_tick_count();
2434 return;
2437 /* now check for timer */
2438 if ((filter & QS_TIMER) &&
2439 (timer = find_expired_timer( queue, get_win, req->get_first,
2440 req->get_last, (req->flags & PM_REMOVE) )))
2442 reply->type = MSG_POSTED;
2443 reply->win = timer->win;
2444 reply->msg = timer->msg;
2445 reply->wparam = timer->id;
2446 reply->lparam = timer->lparam;
2447 reply->time = get_tick_count();
2448 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2449 set_event( current->process->idle_event );
2450 return;
2453 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2454 queue->wake_mask = req->wake_mask;
2455 queue->changed_mask = req->changed_mask;
2456 set_error( STATUS_PENDING ); /* FIXME */
2460 /* reply to a sent message */
2461 DECL_HANDLER(reply_message)
2463 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2464 else if (current->queue->recv_result)
2465 reply_message( current->queue, req->result, 0, req->remove,
2466 get_req_data(), get_req_data_size() );
2470 /* accept the current hardware message */
2471 DECL_HANDLER(accept_hardware_message)
2473 if (current->queue)
2474 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
2475 else
2476 set_error( STATUS_ACCESS_DENIED );
2480 /* retrieve the reply for the last message sent */
2481 DECL_HANDLER(get_message_reply)
2483 struct message_result *result;
2484 struct list *entry;
2485 struct msg_queue *queue = current->queue;
2487 if (queue)
2489 set_error( STATUS_PENDING );
2490 reply->result = 0;
2492 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2494 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2495 if (result->replied || req->cancel)
2497 if (result->replied)
2499 reply->result = result->result;
2500 set_error( result->error );
2501 if (result->data)
2503 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2504 set_reply_data_ptr( result->data, data_len );
2505 result->data = NULL;
2506 result->data_size = 0;
2509 remove_result_from_sender( result );
2511 entry = list_head( &queue->send_result );
2512 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2513 else
2515 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2516 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2517 else clear_queue_bits( queue, QS_SMRESULT );
2521 else set_error( STATUS_ACCESS_DENIED );
2525 /* set a window timer */
2526 DECL_HANDLER(set_win_timer)
2528 struct timer *timer;
2529 struct msg_queue *queue;
2530 struct thread *thread = NULL;
2531 user_handle_t win = 0;
2532 lparam_t id = req->id;
2534 if (req->win)
2536 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2538 set_error( STATUS_INVALID_HANDLE );
2539 return;
2541 if (thread->process != current->process)
2543 release_object( thread );
2544 set_error( STATUS_ACCESS_DENIED );
2545 return;
2547 queue = thread->queue;
2548 /* remove it if it existed already */
2549 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2551 else
2553 queue = get_current_queue();
2554 /* look for a timer with this id */
2555 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2557 /* free and reuse id */
2558 free_timer( queue, timer );
2560 else
2562 /* find a free id for it */
2565 id = queue->next_timer_id;
2566 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2568 while (find_timer( queue, 0, req->msg, id ));
2572 if ((timer = set_timer( queue, req->rate )))
2574 timer->win = win;
2575 timer->msg = req->msg;
2576 timer->id = id;
2577 timer->lparam = req->lparam;
2578 reply->id = id;
2580 if (thread) release_object( thread );
2583 /* kill a window timer */
2584 DECL_HANDLER(kill_win_timer)
2586 struct timer *timer;
2587 struct thread *thread;
2588 user_handle_t win = 0;
2590 if (req->win)
2592 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2594 set_error( STATUS_INVALID_HANDLE );
2595 return;
2597 if (thread->process != current->process)
2599 release_object( thread );
2600 set_error( STATUS_ACCESS_DENIED );
2601 return;
2604 else thread = (struct thread *)grab_object( current );
2606 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2607 free_timer( thread->queue, timer );
2608 else
2609 set_error( STATUS_INVALID_PARAMETER );
2611 release_object( thread );
2614 DECL_HANDLER(register_hotkey)
2616 struct desktop *desktop;
2617 user_handle_t win_handle = req->window;
2618 struct hotkey *hotkey;
2619 struct hotkey *new_hotkey = NULL;
2620 struct thread *thread;
2621 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2623 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2625 if (win_handle)
2627 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2629 release_object( desktop );
2630 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2631 return;
2634 thread = get_window_thread( win_handle );
2635 if (thread) release_object( thread );
2637 if (thread != current)
2639 release_object( desktop );
2640 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2641 return;
2645 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2647 if (req->vkey == hotkey->vkey &&
2648 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2650 release_object( desktop );
2651 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2652 return;
2654 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2655 new_hotkey = hotkey;
2658 if (new_hotkey)
2660 reply->replaced = 1;
2661 reply->flags = new_hotkey->flags;
2662 reply->vkey = new_hotkey->vkey;
2664 else
2666 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2667 if (new_hotkey)
2669 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2670 new_hotkey->queue = current->queue;
2671 new_hotkey->win = win_handle;
2672 new_hotkey->id = req->id;
2676 if (new_hotkey)
2678 new_hotkey->flags = req->flags;
2679 new_hotkey->vkey = req->vkey;
2682 release_object( desktop );
2685 DECL_HANDLER(unregister_hotkey)
2687 struct desktop *desktop;
2688 user_handle_t win_handle = req->window;
2689 struct hotkey *hotkey;
2690 struct thread *thread;
2692 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2694 if (win_handle)
2696 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2698 release_object( desktop );
2699 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2700 return;
2703 thread = get_window_thread( win_handle );
2704 if (thread) release_object( thread );
2706 if (thread != current)
2708 release_object( desktop );
2709 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2710 return;
2714 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2716 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2717 goto found;
2720 release_object( desktop );
2721 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2722 return;
2724 found:
2725 reply->flags = hotkey->flags;
2726 reply->vkey = hotkey->vkey;
2727 list_remove( &hotkey->entry );
2728 free( hotkey );
2729 release_object( desktop );
2732 /* attach (or detach) thread inputs */
2733 DECL_HANDLER(attach_thread_input)
2735 struct thread *thread_from = get_thread_from_id( req->tid_from );
2736 struct thread *thread_to = get_thread_from_id( req->tid_to );
2738 if (!thread_from || !thread_to)
2740 if (thread_from) release_object( thread_from );
2741 if (thread_to) release_object( thread_to );
2742 return;
2744 if (thread_from != thread_to)
2746 if (req->attach) attach_thread_input( thread_from, thread_to );
2747 else
2749 if (thread_from->queue && thread_to->queue &&
2750 thread_from->queue->input == thread_to->queue->input)
2751 detach_thread_input( thread_from );
2752 else
2753 set_error( STATUS_ACCESS_DENIED );
2756 else set_error( STATUS_ACCESS_DENIED );
2757 release_object( thread_from );
2758 release_object( thread_to );
2762 /* get thread input data */
2763 DECL_HANDLER(get_thread_input)
2765 struct thread *thread = NULL;
2766 struct desktop *desktop;
2767 struct thread_input *input;
2769 if (req->tid)
2771 if (!(thread = get_thread_from_id( req->tid ))) return;
2772 if (!(desktop = get_thread_desktop( thread, 0 )))
2774 release_object( thread );
2775 return;
2777 input = thread->queue ? thread->queue->input : NULL;
2779 else
2781 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2782 input = desktop->foreground_input; /* get the foreground thread info */
2785 if (input)
2787 reply->focus = input->focus;
2788 reply->capture = input->capture;
2789 reply->active = input->active;
2790 reply->menu_owner = input->menu_owner;
2791 reply->move_size = input->move_size;
2792 reply->caret = input->caret;
2793 reply->cursor = input->cursor;
2794 reply->show_count = input->cursor_count;
2795 reply->rect = input->caret_rect;
2798 /* foreground window is active window of foreground thread */
2799 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2800 if (thread) release_object( thread );
2801 release_object( desktop );
2805 /* retrieve queue keyboard state for a given thread */
2806 DECL_HANDLER(get_key_state)
2808 struct thread *thread;
2809 struct desktop *desktop;
2810 data_size_t size = min( 256, get_reply_max_size() );
2812 if (!req->tid) /* get global async key state */
2814 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2815 if (req->key >= 0)
2817 reply->state = desktop->keystate[req->key & 0xff];
2818 desktop->keystate[req->key & 0xff] &= ~0x40;
2820 set_reply_data( desktop->keystate, size );
2821 release_object( desktop );
2823 else
2825 if (!(thread = get_thread_from_id( req->tid ))) return;
2826 if (thread->queue)
2828 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2829 set_reply_data( thread->queue->input->keystate, size );
2831 release_object( thread );
2836 /* set queue keyboard state for a given thread */
2837 DECL_HANDLER(set_key_state)
2839 struct thread *thread;
2840 struct desktop *desktop;
2841 data_size_t size = min( 256, get_req_data_size() );
2843 if (!req->tid) /* set global async key state */
2845 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2846 memcpy( desktop->keystate, get_req_data(), size );
2847 release_object( desktop );
2849 else
2851 if (!(thread = get_thread_from_id( req->tid ))) return;
2852 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
2853 if (req->async && (desktop = get_thread_desktop( thread, 0 )))
2855 memcpy( desktop->keystate, get_req_data(), size );
2856 release_object( desktop );
2858 release_object( thread );
2863 /* set the system foreground window */
2864 DECL_HANDLER(set_foreground_window)
2866 struct thread *thread = NULL;
2867 struct desktop *desktop;
2868 struct msg_queue *queue = get_current_queue();
2870 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2871 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
2872 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
2873 reply->send_msg_new = FALSE;
2875 if (is_valid_foreground_window( req->handle ) &&
2876 (thread = get_window_thread( req->handle )) &&
2877 thread->queue->input->desktop == desktop)
2879 set_foreground_input( desktop, thread->queue->input );
2880 reply->send_msg_new = (desktop->foreground_input != queue->input);
2882 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2884 if (thread) release_object( thread );
2885 release_object( desktop );
2889 /* set the current thread focus window */
2890 DECL_HANDLER(set_focus_window)
2892 struct msg_queue *queue = get_current_queue();
2894 reply->previous = 0;
2895 if (queue && check_queue_input_window( queue, req->handle ))
2897 reply->previous = queue->input->focus;
2898 queue->input->focus = get_user_full_handle( req->handle );
2903 /* set the current thread active window */
2904 DECL_HANDLER(set_active_window)
2906 struct msg_queue *queue = get_current_queue();
2908 reply->previous = 0;
2909 if (queue && check_queue_input_window( queue, req->handle ))
2911 if (!req->handle || make_window_active( req->handle ))
2913 reply->previous = queue->input->active;
2914 queue->input->active = get_user_full_handle( req->handle );
2916 else set_error( STATUS_INVALID_HANDLE );
2921 /* set the current thread capture window */
2922 DECL_HANDLER(set_capture_window)
2924 struct msg_queue *queue = get_current_queue();
2926 reply->previous = reply->full_handle = 0;
2927 if (queue && check_queue_input_window( queue, req->handle ))
2929 struct thread_input *input = queue->input;
2931 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2932 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2934 set_error(STATUS_ACCESS_DENIED);
2935 return;
2937 reply->previous = input->capture;
2938 input->capture = get_user_full_handle( req->handle );
2939 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2940 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2941 reply->full_handle = input->capture;
2946 /* Set the current thread caret window */
2947 DECL_HANDLER(set_caret_window)
2949 struct msg_queue *queue = get_current_queue();
2951 reply->previous = 0;
2952 if (queue && check_queue_input_window( queue, req->handle ))
2954 struct thread_input *input = queue->input;
2956 reply->previous = input->caret;
2957 reply->old_rect = input->caret_rect;
2958 reply->old_hide = input->caret_hide;
2959 reply->old_state = input->caret_state;
2961 set_caret_window( input, get_user_full_handle(req->handle) );
2962 input->caret_rect.right = input->caret_rect.left + req->width;
2963 input->caret_rect.bottom = input->caret_rect.top + req->height;
2968 /* Set the current thread caret information */
2969 DECL_HANDLER(set_caret_info)
2971 struct msg_queue *queue = get_current_queue();
2972 struct thread_input *input;
2974 if (!queue) return;
2975 input = queue->input;
2976 reply->full_handle = input->caret;
2977 reply->old_rect = input->caret_rect;
2978 reply->old_hide = input->caret_hide;
2979 reply->old_state = input->caret_state;
2981 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2983 set_error( STATUS_ACCESS_DENIED );
2984 return;
2986 if (req->flags & SET_CARET_POS)
2988 input->caret_rect.right += req->x - input->caret_rect.left;
2989 input->caret_rect.bottom += req->y - input->caret_rect.top;
2990 input->caret_rect.left = req->x;
2991 input->caret_rect.top = req->y;
2993 if (req->flags & SET_CARET_HIDE)
2995 input->caret_hide += req->hide;
2996 if (input->caret_hide < 0) input->caret_hide = 0;
2998 if (req->flags & SET_CARET_STATE)
3000 if (req->state == -1) input->caret_state = !input->caret_state;
3001 else input->caret_state = !!req->state;
3006 /* get the time of the last input event */
3007 DECL_HANDLER(get_last_input_time)
3009 reply->time = last_input_time;
3012 /* set/get the current cursor */
3013 DECL_HANDLER(set_cursor)
3015 struct msg_queue *queue = get_current_queue();
3016 struct thread_input *input;
3018 if (!queue) return;
3019 input = queue->input;
3021 reply->prev_handle = input->cursor;
3022 reply->prev_count = input->cursor_count;
3023 reply->prev_x = input->desktop->cursor.x;
3024 reply->prev_y = input->desktop->cursor.y;
3026 if (req->flags & SET_CURSOR_HANDLE)
3028 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3030 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3031 return;
3033 input->cursor = req->handle;
3035 if (req->flags & SET_CURSOR_COUNT)
3037 queue->cursor_count += req->show_count;
3038 input->cursor_count += req->show_count;
3040 if (req->flags & SET_CURSOR_POS)
3042 set_cursor_pos( input->desktop, req->x, req->y );
3044 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3046 struct desktop *desktop = input->desktop;
3048 /* only the desktop owner can set the message */
3049 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3050 desktop->cursor.clip_msg = req->clip_msg;
3052 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip );
3055 reply->new_x = input->desktop->cursor.x;
3056 reply->new_y = input->desktop->cursor.y;
3057 reply->new_clip = input->desktop->cursor.clip;
3058 reply->last_change = input->desktop->cursor.last_change;
3061 DECL_HANDLER(update_rawinput_devices)
3063 const struct rawinput_device *devices = get_req_data();
3064 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3065 const struct rawinput_device_entry *e;
3066 unsigned int i;
3068 for (i = 0; i < device_count; ++i)
3070 update_rawinput_device(&devices[i]);
3073 e = find_rawinput_device( 1, 2 );
3074 current->process->rawinput_mouse = e ? &e->device : NULL;
3075 e = find_rawinput_device( 1, 6 );
3076 current->process->rawinput_kbd = e ? &e->device : NULL;