server: Clear the queue's QS_SMRESULT bit when sending a new message.
[wine/multimedia.git] / server / queue.c
blob57603e11ca473796598c4010e7c923183524595f
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>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "winternl.h"
37 #include "handle.h"
38 #include "file.h"
39 #include "thread.h"
40 #include "process.h"
41 #include "request.h"
42 #include "user.h"
44 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
45 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
47 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
48 #define NB_MSG_KINDS (POST_MESSAGE+1)
51 struct message_result
53 struct list sender_entry; /* entry in sender list */
54 struct message *msg; /* message the result is for */
55 struct message_result *recv_next; /* next in receiver list */
56 struct msg_queue *sender; /* sender queue */
57 struct msg_queue *receiver; /* receiver queue */
58 int replied; /* has it been replied to? */
59 unsigned int error; /* error code to pass back to sender */
60 lparam_t result; /* reply result */
61 struct message *hardware_msg; /* hardware message if low-level hook result */
62 struct desktop *desktop; /* desktop for hardware message */
63 struct message *callback_msg; /* message to queue for callback */
64 void *data; /* message reply data */
65 unsigned int data_size; /* size of message reply data */
66 struct timeout_user *timeout; /* result timeout */
69 struct message
71 struct list entry; /* entry in message list */
72 enum message_type type; /* message type */
73 user_handle_t win; /* window handle */
74 unsigned int msg; /* message code */
75 lparam_t wparam; /* parameters */
76 lparam_t lparam; /* parameters */
77 unsigned int time; /* message time */
78 void *data; /* message data for sent messages */
79 unsigned int data_size; /* size of message data */
80 unsigned int unique_id; /* unique id for nested hw message waits */
81 struct message_result *result; /* result in sender queue */
84 struct timer
86 struct list entry; /* entry in timer list */
87 timeout_t when; /* next expiration */
88 unsigned int rate; /* timer rate in ms */
89 user_handle_t win; /* window handle */
90 unsigned int msg; /* message to post */
91 lparam_t id; /* timer id */
92 lparam_t lparam; /* lparam for message */
95 struct thread_input
97 struct object obj; /* object header */
98 struct desktop *desktop; /* desktop that this thread input belongs to */
99 user_handle_t focus; /* focus window */
100 user_handle_t capture; /* capture window */
101 user_handle_t active; /* active window */
102 user_handle_t menu_owner; /* current menu owner window */
103 user_handle_t move_size; /* current moving/resizing window */
104 user_handle_t caret; /* caret window */
105 rectangle_t caret_rect; /* caret rectangle */
106 int caret_hide; /* caret hide count */
107 int caret_state; /* caret on/off state */
108 user_handle_t cursor; /* current cursor */
109 int cursor_count; /* cursor show count */
110 struct list msg_list; /* list of hardware messages */
111 unsigned char keystate[256]; /* state of each key */
114 struct msg_queue
116 struct object obj; /* object header */
117 struct fd *fd; /* optional file descriptor to poll */
118 unsigned int wake_bits; /* wakeup bits */
119 unsigned int wake_mask; /* wakeup mask */
120 unsigned int changed_bits; /* changed wakeup bits */
121 unsigned int changed_mask; /* changed wakeup mask */
122 int paint_count; /* pending paint messages count */
123 int hotkey_count; /* pending hotkey messages count */
124 int quit_message; /* is there a pending quit message? */
125 int exit_code; /* exit code of pending quit message */
126 int cursor_count; /* per-queue cursor show count */
127 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
128 struct list send_result; /* stack of sent messages waiting for result */
129 struct list callback_result; /* list of callback messages waiting for result */
130 struct message_result *recv_result; /* stack of received messages waiting for result */
131 struct list pending_timers; /* list of pending timers */
132 struct list expired_timers; /* list of expired timers */
133 lparam_t next_timer_id; /* id for the next timer with a 0 window */
134 struct timeout_user *timeout; /* timeout for next timer to expire */
135 struct thread_input *input; /* thread input descriptor */
136 struct hook_table *hooks; /* hook table */
137 timeout_t last_get_msg; /* time of last get message call */
140 struct hotkey
142 struct list entry; /* entry in desktop hotkey list */
143 struct msg_queue *queue; /* queue owning this hotkey */
144 user_handle_t win; /* window handle */
145 int id; /* hotkey id */
146 unsigned int vkey; /* virtual key code */
147 unsigned int flags; /* key modifiers */
150 static void msg_queue_dump( struct object *obj, int verbose );
151 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
152 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
153 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
154 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
155 static void msg_queue_destroy( struct object *obj );
156 static void msg_queue_poll_event( struct fd *fd, int event );
157 static void thread_input_dump( struct object *obj, int verbose );
158 static void thread_input_destroy( struct object *obj );
159 static void timer_callback( void *private );
161 static const struct object_ops msg_queue_ops =
163 sizeof(struct msg_queue), /* size */
164 msg_queue_dump, /* dump */
165 no_get_type, /* get_type */
166 msg_queue_add_queue, /* add_queue */
167 msg_queue_remove_queue, /* remove_queue */
168 msg_queue_signaled, /* signaled */
169 msg_queue_satisfied, /* satisfied */
170 no_signal, /* signal */
171 no_get_fd, /* get_fd */
172 no_map_access, /* map_access */
173 default_get_sd, /* get_sd */
174 default_set_sd, /* set_sd */
175 no_lookup_name, /* lookup_name */
176 no_open_file, /* open_file */
177 no_close_handle, /* close_handle */
178 msg_queue_destroy /* destroy */
181 static const struct fd_ops msg_queue_fd_ops =
183 NULL, /* get_poll_events */
184 msg_queue_poll_event, /* poll_event */
185 NULL, /* flush */
186 NULL, /* get_fd_type */
187 NULL, /* ioctl */
188 NULL, /* queue_async */
189 NULL, /* reselect_async */
190 NULL /* cancel async */
194 static const struct object_ops thread_input_ops =
196 sizeof(struct thread_input), /* size */
197 thread_input_dump, /* dump */
198 no_get_type, /* get_type */
199 no_add_queue, /* add_queue */
200 NULL, /* remove_queue */
201 NULL, /* signaled */
202 NULL, /* satisfied */
203 no_signal, /* signal */
204 no_get_fd, /* get_fd */
205 no_map_access, /* map_access */
206 default_get_sd, /* get_sd */
207 default_set_sd, /* set_sd */
208 no_lookup_name, /* lookup_name */
209 no_open_file, /* open_file */
210 no_close_handle, /* close_handle */
211 thread_input_destroy /* destroy */
214 /* pointer to input structure of foreground thread */
215 static unsigned int last_input_time;
217 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
218 static void free_message( struct message *msg );
220 /* set the caret window in a given thread input */
221 static void set_caret_window( struct thread_input *input, user_handle_t win )
223 if (!win || win != input->caret)
225 input->caret_rect.left = 0;
226 input->caret_rect.top = 0;
227 input->caret_rect.right = 0;
228 input->caret_rect.bottom = 0;
230 input->caret = win;
231 input->caret_hide = 1;
232 input->caret_state = 0;
235 /* create a thread input object */
236 static struct thread_input *create_thread_input( struct thread *thread )
238 struct thread_input *input;
240 if ((input = alloc_object( &thread_input_ops )))
242 input->focus = 0;
243 input->capture = 0;
244 input->active = 0;
245 input->menu_owner = 0;
246 input->move_size = 0;
247 input->cursor = 0;
248 input->cursor_count = 0;
249 list_init( &input->msg_list );
250 set_caret_window( input, 0 );
251 memset( input->keystate, 0, sizeof(input->keystate) );
253 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
255 release_object( input );
256 return NULL;
259 return input;
262 /* create a message queue object */
263 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
265 struct thread_input *new_input = NULL;
266 struct msg_queue *queue;
267 int i;
269 if (!input)
271 if (!(new_input = create_thread_input( thread ))) return NULL;
272 input = new_input;
275 if ((queue = alloc_object( &msg_queue_ops )))
277 queue->fd = NULL;
278 queue->wake_bits = 0;
279 queue->wake_mask = 0;
280 queue->changed_bits = 0;
281 queue->changed_mask = 0;
282 queue->paint_count = 0;
283 queue->hotkey_count = 0;
284 queue->quit_message = 0;
285 queue->cursor_count = 0;
286 queue->recv_result = NULL;
287 queue->next_timer_id = 0x7fff;
288 queue->timeout = NULL;
289 queue->input = (struct thread_input *)grab_object( input );
290 queue->hooks = NULL;
291 queue->last_get_msg = current_time;
292 list_init( &queue->send_result );
293 list_init( &queue->callback_result );
294 list_init( &queue->pending_timers );
295 list_init( &queue->expired_timers );
296 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
298 thread->queue = queue;
300 if (new_input) release_object( new_input );
301 return queue;
304 /* free the message queue of a thread at thread exit */
305 void free_msg_queue( struct thread *thread )
307 remove_thread_hooks( thread );
308 if (!thread->queue) return;
309 release_object( thread->queue );
310 thread->queue = NULL;
313 /* change the thread input data of a given thread */
314 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
316 struct msg_queue *queue = thread->queue;
318 if (!queue)
320 thread->queue = create_msg_queue( thread, new_input );
321 return thread->queue != NULL;
323 if (queue->input)
325 queue->input->cursor_count -= queue->cursor_count;
326 release_object( queue->input );
328 queue->input = (struct thread_input *)grab_object( new_input );
329 new_input->cursor_count += queue->cursor_count;
330 return 1;
333 /* set the cursor position and queue the corresponding mouse message */
334 static void set_cursor_pos( struct desktop *desktop, int x, int y )
336 struct hardware_msg_data *msg_data;
337 struct message *msg;
339 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
340 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
342 free( msg );
343 return;
345 memset( msg_data, 0, sizeof(*msg_data) );
347 msg->type = MSG_HARDWARE;
348 msg->win = 0;
349 msg->msg = WM_MOUSEMOVE;
350 msg->wparam = 0;
351 msg->lparam = 0;
352 msg->time = get_tick_count();
353 msg->result = NULL;
354 msg->data = msg_data;
355 msg->data_size = sizeof(*msg_data);
356 msg_data->x = x;
357 msg_data->y = y;
358 queue_hardware_message( desktop, msg, 1 );
361 /* set the cursor clip rectangle */
362 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect )
364 rectangle_t top_rect;
365 int x, y;
367 get_top_window_rectangle( desktop, &top_rect );
368 if (rect)
370 rectangle_t new_rect = *rect;
371 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
372 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
373 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
374 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
375 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
376 desktop->cursor.clip = new_rect;
378 else desktop->cursor.clip = top_rect;
380 if (desktop->cursor.clip_msg)
381 post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
383 /* warp the mouse to be inside the clip rect */
384 x = min( max( desktop->cursor.x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
385 y = min( max( desktop->cursor.y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
386 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
389 /* change the foreground input and reset the cursor clip rect */
390 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
392 if (desktop->foreground_input == input) return;
393 set_clip_rectangle( desktop, NULL );
394 desktop->foreground_input = input;
397 /* get the hook table for a given thread */
398 struct hook_table *get_queue_hooks( struct thread *thread )
400 if (!thread->queue) return NULL;
401 return thread->queue->hooks;
404 /* set the hook table for a given thread, allocating the queue if needed */
405 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
407 struct msg_queue *queue = thread->queue;
408 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
409 if (queue->hooks) release_object( queue->hooks );
410 queue->hooks = hooks;
413 /* check the queue status */
414 static inline int is_signaled( struct msg_queue *queue )
416 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
419 /* set some queue bits */
420 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
422 queue->wake_bits |= bits;
423 queue->changed_bits |= bits;
424 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
427 /* clear some queue bits */
428 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
430 queue->wake_bits &= ~bits;
431 queue->changed_bits &= ~bits;
434 /* check whether msg is a keyboard message */
435 static inline int is_keyboard_msg( struct message *msg )
437 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
440 /* check if message is matched by the filter */
441 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
443 return (msg >= first && msg <= last);
446 /* check whether a message filter contains at least one potential hardware message */
447 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
449 /* hardware message ranges are (in numerical order):
450 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
451 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
452 * WM_MOUSEFIRST .. WM_MOUSELAST
454 if (last < WM_NCMOUSEFIRST) return 0;
455 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
456 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
457 if (first > WM_MOUSELAST) return 0;
458 return 1;
461 /* get the QS_* bit corresponding to a given hardware message */
462 static inline int get_hardware_msg_bit( struct message *msg )
464 if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
465 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
466 if (is_keyboard_msg( msg )) return QS_KEY;
467 return QS_MOUSEBUTTON;
470 /* get the current thread queue, creating it if needed */
471 static inline struct msg_queue *get_current_queue(void)
473 struct msg_queue *queue = current->queue;
474 if (!queue) queue = create_msg_queue( current, NULL );
475 return queue;
478 /* get a (pseudo-)unique id to tag hardware messages */
479 static inline unsigned int get_unique_id(void)
481 static unsigned int id;
482 if (!++id) id = 1; /* avoid an id of 0 */
483 return id;
486 /* try to merge a message with the last in the list; return 1 if successful */
487 static int merge_message( struct thread_input *input, const struct message *msg )
489 struct message *prev;
490 struct list *ptr;
492 if (msg->msg != WM_MOUSEMOVE) return 0;
493 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
495 prev = LIST_ENTRY( ptr, struct message, entry );
496 if (prev->msg != WM_INPUT) break;
498 if (!ptr) return 0;
499 if (prev->result) return 0;
500 if (prev->win && msg->win && prev->win != msg->win) return 0;
501 if (prev->msg != msg->msg) return 0;
502 if (prev->type != msg->type) return 0;
503 /* now we can merge it */
504 prev->wparam = msg->wparam;
505 prev->lparam = msg->lparam;
506 prev->time = msg->time;
507 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
509 struct hardware_msg_data *prev_data = prev->data;
510 struct hardware_msg_data *msg_data = msg->data;
511 prev_data->x = msg_data->x;
512 prev_data->y = msg_data->y;
513 prev_data->info = msg_data->info;
515 list_remove( ptr );
516 list_add_tail( &input->msg_list, ptr );
517 return 1;
520 /* free a result structure */
521 static void free_result( struct message_result *result )
523 if (result->timeout) remove_timeout_user( result->timeout );
524 free( result->data );
525 if (result->callback_msg) free_message( result->callback_msg );
526 if (result->hardware_msg) free_message( result->hardware_msg );
527 if (result->desktop) release_object( result->desktop );
528 free( result );
531 /* remove the result from the sender list it is on */
532 static inline void remove_result_from_sender( struct message_result *result )
534 assert( result->sender );
536 list_remove( &result->sender_entry );
537 result->sender = NULL;
538 if (!result->receiver) free_result( result );
541 /* store the message result in the appropriate structure */
542 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
544 res->result = result;
545 res->error = error;
546 res->replied = 1;
547 if (res->timeout)
549 remove_timeout_user( res->timeout );
550 res->timeout = NULL;
553 if (res->hardware_msg)
555 if (!error && result) /* rejected by the hook */
556 free_message( res->hardware_msg );
557 else
558 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
560 res->hardware_msg = NULL;
563 if (res->sender)
565 if (res->callback_msg)
567 /* queue the callback message in the sender queue */
568 struct callback_msg_data *data = res->callback_msg->data;
569 data->result = result;
570 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
571 set_queue_bits( res->sender, QS_SENDMESSAGE );
572 res->callback_msg = NULL;
573 remove_result_from_sender( res );
575 else
577 /* wake sender queue if waiting on this result */
578 if (list_head(&res->sender->send_result) == &res->sender_entry)
579 set_queue_bits( res->sender, QS_SMRESULT );
582 else if (!res->receiver) free_result( res );
585 /* free a message when deleting a queue or window */
586 static void free_message( struct message *msg )
588 struct message_result *result = msg->result;
589 if (result)
591 result->msg = NULL;
592 result->receiver = NULL;
593 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
595 free( msg->data );
596 free( msg );
599 /* remove (and free) a message from a message list */
600 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
601 enum message_kind kind )
603 list_remove( &msg->entry );
604 switch(kind)
606 case SEND_MESSAGE:
607 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
608 break;
609 case POST_MESSAGE:
610 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
611 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
612 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
613 clear_queue_bits( queue, QS_HOTKEY );
614 break;
616 free_message( msg );
619 /* message timed out without getting a reply */
620 static void result_timeout( void *private )
622 struct message_result *result = private;
624 assert( !result->replied );
626 result->timeout = NULL;
628 if (result->msg) /* not received yet */
630 struct message *msg = result->msg;
632 result->msg = NULL;
633 msg->result = NULL;
634 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
635 result->receiver = NULL;
637 store_message_result( result, 0, STATUS_TIMEOUT );
640 /* allocate and fill a message result structure */
641 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
642 struct msg_queue *recv_queue,
643 struct message *msg, timeout_t timeout )
645 struct message_result *result = mem_alloc( sizeof(*result) );
646 if (result)
648 result->msg = msg;
649 result->sender = send_queue;
650 result->receiver = recv_queue;
651 result->replied = 0;
652 result->data = NULL;
653 result->data_size = 0;
654 result->timeout = NULL;
655 result->hardware_msg = NULL;
656 result->desktop = NULL;
657 result->callback_msg = NULL;
659 if (msg->type == MSG_CALLBACK)
661 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
663 if (!callback_msg)
665 free( result );
666 return NULL;
668 callback_msg->type = MSG_CALLBACK_RESULT;
669 callback_msg->win = msg->win;
670 callback_msg->msg = msg->msg;
671 callback_msg->wparam = 0;
672 callback_msg->lparam = 0;
673 callback_msg->time = get_tick_count();
674 callback_msg->result = NULL;
675 /* steal the data from the original message */
676 callback_msg->data = msg->data;
677 callback_msg->data_size = msg->data_size;
678 msg->data = NULL;
679 msg->data_size = 0;
681 result->callback_msg = callback_msg;
682 list_add_head( &send_queue->callback_result, &result->sender_entry );
684 else if (send_queue)
686 list_add_head( &send_queue->send_result, &result->sender_entry );
687 clear_queue_bits( send_queue, QS_SMRESULT );
690 if (timeout != TIMEOUT_INFINITE)
691 result->timeout = add_timeout_user( timeout, result_timeout, result );
693 return result;
696 /* receive a message, removing it from the sent queue */
697 static void receive_message( struct msg_queue *queue, struct message *msg,
698 struct get_message_reply *reply )
700 struct message_result *result = msg->result;
702 reply->total = msg->data_size;
703 if (msg->data_size > get_reply_max_size())
705 set_error( STATUS_BUFFER_OVERFLOW );
706 return;
708 reply->type = msg->type;
709 reply->win = msg->win;
710 reply->msg = msg->msg;
711 reply->wparam = msg->wparam;
712 reply->lparam = msg->lparam;
713 reply->time = msg->time;
715 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
717 list_remove( &msg->entry );
718 /* put the result on the receiver result stack */
719 if (result)
721 result->msg = NULL;
722 result->recv_next = queue->recv_result;
723 queue->recv_result = result;
725 free( msg );
726 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
729 /* set the result of the current received message */
730 static void reply_message( struct msg_queue *queue, lparam_t result,
731 unsigned int error, int remove, const void *data, data_size_t len )
733 struct message_result *res = queue->recv_result;
735 if (remove)
737 queue->recv_result = res->recv_next;
738 res->receiver = NULL;
739 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
741 free_result( res );
742 return;
745 if (!res->replied)
747 if (len && (res->data = memdup( data, len ))) res->data_size = len;
748 store_message_result( res, result, error );
752 static int match_window( user_handle_t win, user_handle_t msg_win )
754 if (!win) return 1;
755 if (win == -1 || win == 1) return !msg_win;
756 if (msg_win == win) return 1;
757 return is_child_window( win, msg_win );
760 /* retrieve a posted message */
761 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
762 unsigned int first, unsigned int last, unsigned int flags,
763 struct get_message_reply *reply )
765 struct message *msg;
767 /* check against the filters */
768 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
770 if (!match_window( win, msg->win )) continue;
771 if (!check_msg_filter( msg->msg, first, last )) continue;
772 goto found; /* found one */
774 return 0;
776 /* return it to the app */
777 found:
778 reply->total = msg->data_size;
779 if (msg->data_size > get_reply_max_size())
781 set_error( STATUS_BUFFER_OVERFLOW );
782 return 1;
784 reply->type = msg->type;
785 reply->win = msg->win;
786 reply->msg = msg->msg;
787 reply->wparam = msg->wparam;
788 reply->lparam = msg->lparam;
789 reply->time = msg->time;
791 if (flags & PM_REMOVE)
793 if (msg->data)
795 set_reply_data_ptr( msg->data, msg->data_size );
796 msg->data = NULL;
797 msg->data_size = 0;
799 remove_queue_message( queue, msg, POST_MESSAGE );
801 else if (msg->data) set_reply_data( msg->data, msg->data_size );
803 return 1;
806 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
807 struct get_message_reply *reply )
809 if (queue->quit_message)
811 reply->total = 0;
812 reply->type = MSG_POSTED;
813 reply->win = 0;
814 reply->msg = WM_QUIT;
815 reply->wparam = queue->exit_code;
816 reply->lparam = 0;
817 reply->time = get_tick_count();
819 if (flags & PM_REMOVE)
821 queue->quit_message = 0;
822 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
823 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
825 return 1;
827 else
828 return 0;
831 /* empty a message list and free all the messages */
832 static void empty_msg_list( struct list *list )
834 struct list *ptr;
836 while ((ptr = list_head( list )) != NULL)
838 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
839 list_remove( &msg->entry );
840 free_message( msg );
844 /* cleanup all pending results when deleting a queue */
845 static void cleanup_results( struct msg_queue *queue )
847 struct list *entry;
849 while ((entry = list_head( &queue->send_result )) != NULL)
851 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
854 while ((entry = list_head( &queue->callback_result )) != NULL)
856 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
859 while (queue->recv_result)
860 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
863 /* check if the thread owning the queue is hung (not checking for messages) */
864 static int is_queue_hung( struct msg_queue *queue )
866 struct wait_queue_entry *entry;
868 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
869 return 0; /* less than 5 seconds since last get message -> not hung */
871 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
873 if (get_wait_queue_thread(entry)->queue == queue)
874 return 0; /* thread is waiting on queue -> not hung */
876 return 1;
879 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
881 struct msg_queue *queue = (struct msg_queue *)obj;
882 struct process *process = get_wait_queue_thread(entry)->process;
884 /* a thread can only wait on its own queue */
885 if (get_wait_queue_thread(entry)->queue != queue)
887 set_error( STATUS_ACCESS_DENIED );
888 return 0;
890 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
892 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
893 set_fd_events( queue->fd, POLLIN );
894 add_queue( obj, entry );
895 return 1;
898 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
900 struct msg_queue *queue = (struct msg_queue *)obj;
902 remove_queue( obj, entry );
903 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
904 set_fd_events( queue->fd, 0 );
907 static void msg_queue_dump( struct object *obj, int verbose )
909 struct msg_queue *queue = (struct msg_queue *)obj;
910 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
911 queue->wake_bits, queue->wake_mask );
914 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
916 struct msg_queue *queue = (struct msg_queue *)obj;
917 int ret = 0;
919 if (queue->fd)
921 if ((ret = check_fd_events( queue->fd, POLLIN )))
922 /* stop waiting on select() if we are signaled */
923 set_fd_events( queue->fd, 0 );
924 else if (!list_empty( &obj->wait_queue ))
925 /* restart waiting on poll() if we are no longer signaled */
926 set_fd_events( queue->fd, POLLIN );
928 return ret || is_signaled( queue );
931 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
933 struct msg_queue *queue = (struct msg_queue *)obj;
934 queue->wake_mask = 0;
935 queue->changed_mask = 0;
938 static void msg_queue_destroy( struct object *obj )
940 struct msg_queue *queue = (struct msg_queue *)obj;
941 struct list *ptr;
942 struct hotkey *hotkey, *hotkey2;
943 int i;
945 cleanup_results( queue );
946 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
948 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
950 if (hotkey->queue == queue)
952 list_remove( &hotkey->entry );
953 free( hotkey );
957 while ((ptr = list_head( &queue->pending_timers )))
959 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
960 list_remove( &timer->entry );
961 free( timer );
963 while ((ptr = list_head( &queue->expired_timers )))
965 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
966 list_remove( &timer->entry );
967 free( timer );
969 if (queue->timeout) remove_timeout_user( queue->timeout );
970 queue->input->cursor_count -= queue->cursor_count;
971 release_object( queue->input );
972 if (queue->hooks) release_object( queue->hooks );
973 if (queue->fd) release_object( queue->fd );
976 static void msg_queue_poll_event( struct fd *fd, int event )
978 struct msg_queue *queue = get_fd_user( fd );
979 assert( queue->obj.ops == &msg_queue_ops );
981 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
982 else set_fd_events( queue->fd, 0 );
983 wake_up( &queue->obj, 0 );
986 static void thread_input_dump( struct object *obj, int verbose )
988 struct thread_input *input = (struct thread_input *)obj;
989 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
990 input->focus, input->capture, input->active );
993 static void thread_input_destroy( struct object *obj )
995 struct thread_input *input = (struct thread_input *)obj;
997 empty_msg_list( &input->msg_list );
998 if (input->desktop)
1000 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
1001 release_object( input->desktop );
1005 /* fix the thread input data when a window is destroyed */
1006 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1008 struct thread_input *input = queue->input;
1010 if (window == input->focus) input->focus = 0;
1011 if (window == input->capture) input->capture = 0;
1012 if (window == input->active) input->active = 0;
1013 if (window == input->menu_owner) input->menu_owner = 0;
1014 if (window == input->move_size) input->move_size = 0;
1015 if (window == input->caret) set_caret_window( input, 0 );
1018 /* check if the specified window can be set in the input data of a given queue */
1019 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1021 struct thread *thread;
1022 int ret = 0;
1024 if (!window) return 1; /* we can always clear the data */
1026 if ((thread = get_window_thread( window )))
1028 ret = (queue->input == thread->queue->input);
1029 if (!ret) set_error( STATUS_ACCESS_DENIED );
1030 release_object( thread );
1032 else set_error( STATUS_INVALID_HANDLE );
1034 return ret;
1037 /* make sure the specified thread has a queue */
1038 int init_thread_queue( struct thread *thread )
1040 if (thread->queue) return 1;
1041 return (create_msg_queue( thread, NULL ) != NULL);
1044 /* attach two thread input data structures */
1045 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1047 struct desktop *desktop;
1048 struct thread_input *input;
1049 int ret;
1051 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1052 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1053 input = (struct thread_input *)grab_object( thread_to->queue->input );
1054 if (input->desktop != desktop)
1056 set_error( STATUS_ACCESS_DENIED );
1057 release_object( input );
1058 release_object( desktop );
1059 return 0;
1061 release_object( desktop );
1063 ret = assign_thread_input( thread_from, input );
1064 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1065 release_object( input );
1066 return ret;
1069 /* detach two thread input data structures */
1070 void detach_thread_input( struct thread *thread_from )
1072 struct thread_input *input;
1074 if ((input = create_thread_input( thread_from )))
1076 assign_thread_input( thread_from, input );
1077 release_object( input );
1082 /* set the next timer to expire */
1083 static void set_next_timer( struct msg_queue *queue )
1085 struct list *ptr;
1087 if (queue->timeout)
1089 remove_timeout_user( queue->timeout );
1090 queue->timeout = NULL;
1092 if ((ptr = list_head( &queue->pending_timers )))
1094 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1095 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1097 /* set/clear QS_TIMER bit */
1098 if (list_empty( &queue->expired_timers ))
1099 clear_queue_bits( queue, QS_TIMER );
1100 else
1101 set_queue_bits( queue, QS_TIMER );
1104 /* find a timer from its window and id */
1105 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1106 unsigned int msg, lparam_t id )
1108 struct list *ptr;
1110 /* we need to search both lists */
1112 LIST_FOR_EACH( ptr, &queue->pending_timers )
1114 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1115 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1117 LIST_FOR_EACH( ptr, &queue->expired_timers )
1119 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1120 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1122 return NULL;
1125 /* callback for the next timer expiration */
1126 static void timer_callback( void *private )
1128 struct msg_queue *queue = private;
1129 struct list *ptr;
1131 queue->timeout = NULL;
1132 /* move on to the next timer */
1133 ptr = list_head( &queue->pending_timers );
1134 list_remove( ptr );
1135 list_add_tail( &queue->expired_timers, ptr );
1136 set_next_timer( queue );
1139 /* link a timer at its rightful place in the queue list */
1140 static void link_timer( struct msg_queue *queue, struct timer *timer )
1142 struct list *ptr;
1144 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1146 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1147 if (t->when >= timer->when) break;
1149 list_add_before( ptr, &timer->entry );
1152 /* remove a timer from the queue timer list and free it */
1153 static void free_timer( struct msg_queue *queue, struct timer *timer )
1155 list_remove( &timer->entry );
1156 free( timer );
1157 set_next_timer( queue );
1160 /* restart an expired timer */
1161 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1163 list_remove( &timer->entry );
1164 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1165 link_timer( queue, timer );
1166 set_next_timer( queue );
1169 /* find an expired timer matching the filtering parameters */
1170 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1171 unsigned int get_first, unsigned int get_last,
1172 int remove )
1174 struct list *ptr;
1176 LIST_FOR_EACH( ptr, &queue->expired_timers )
1178 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1179 if (win && timer->win != win) continue;
1180 if (check_msg_filter( timer->msg, get_first, get_last ))
1182 if (remove) restart_timer( queue, timer );
1183 return timer;
1186 return NULL;
1189 /* add a timer */
1190 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1192 struct timer *timer = mem_alloc( sizeof(*timer) );
1193 if (timer)
1195 timer->rate = max( rate, 1 );
1196 timer->when = current_time + (timeout_t)timer->rate * 10000;
1197 link_timer( queue, timer );
1198 /* check if we replaced the next timer */
1199 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1201 return timer;
1204 /* change the input key state for a given key */
1205 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1207 if (down)
1209 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1210 keystate[key] |= down;
1212 else keystate[key] &= ~0x80;
1215 /* update the input key state for a keyboard message */
1216 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1217 const struct message *msg )
1219 unsigned char key;
1220 int down = 0;
1222 switch (msg->msg)
1224 case WM_LBUTTONDOWN:
1225 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1226 /* fall through */
1227 case WM_LBUTTONUP:
1228 set_input_key_state( keystate, VK_LBUTTON, down );
1229 break;
1230 case WM_MBUTTONDOWN:
1231 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1232 /* fall through */
1233 case WM_MBUTTONUP:
1234 set_input_key_state( keystate, VK_MBUTTON, down );
1235 break;
1236 case WM_RBUTTONDOWN:
1237 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1238 /* fall through */
1239 case WM_RBUTTONUP:
1240 set_input_key_state( keystate, VK_RBUTTON, down );
1241 break;
1242 case WM_XBUTTONDOWN:
1243 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1244 /* fall through */
1245 case WM_XBUTTONUP:
1246 if (msg->wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1247 else if (msg->wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1248 break;
1249 case WM_KEYDOWN:
1250 case WM_SYSKEYDOWN:
1251 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1252 /* fall through */
1253 case WM_KEYUP:
1254 case WM_SYSKEYUP:
1255 key = (unsigned char)msg->wparam;
1256 set_input_key_state( keystate, key, down );
1257 switch(key)
1259 case VK_LCONTROL:
1260 case VK_RCONTROL:
1261 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1262 set_input_key_state( keystate, VK_CONTROL, down );
1263 break;
1264 case VK_LMENU:
1265 case VK_RMENU:
1266 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1267 set_input_key_state( keystate, VK_MENU, down );
1268 break;
1269 case VK_LSHIFT:
1270 case VK_RSHIFT:
1271 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1272 set_input_key_state( keystate, VK_SHIFT, down );
1273 break;
1275 break;
1279 /* release the hardware message currently being processed by the given thread */
1280 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1281 int remove, user_handle_t new_win )
1283 struct thread_input *input = queue->input;
1284 struct message *msg;
1286 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1288 if (msg->unique_id == hw_id) break;
1290 if (&msg->entry == &input->msg_list) return; /* not found */
1292 /* clear the queue bit for that message */
1293 if (remove || new_win)
1295 struct message *other;
1296 int clr_bit;
1298 clr_bit = get_hardware_msg_bit( msg );
1299 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1301 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1303 clr_bit = 0;
1304 break;
1307 if (clr_bit) clear_queue_bits( queue, clr_bit );
1310 if (new_win) /* set the new window */
1312 struct thread *owner = get_window_thread( new_win );
1313 if (owner)
1315 msg->win = new_win;
1316 if (owner->queue->input != input)
1318 list_remove( &msg->entry );
1319 if (merge_message( owner->queue->input, msg ))
1321 free_message( msg );
1322 release_object( owner );
1323 return;
1325 list_add_tail( &owner->queue->input->msg_list, &msg->entry );
1327 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1328 remove = 0;
1329 release_object( owner );
1332 if (remove)
1334 update_input_key_state( input->desktop, input->keystate, msg );
1335 list_remove( &msg->entry );
1336 free_message( msg );
1340 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1342 struct hotkey *hotkey;
1343 unsigned int modifiers = 0;
1345 if (msg->msg != WM_KEYDOWN) return 0;
1347 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1348 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1349 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1350 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1352 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1354 if (hotkey->vkey != msg->wparam) continue;
1355 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1358 return 0;
1360 found:
1361 msg->type = MSG_POSTED;
1362 msg->win = hotkey->win;
1363 msg->msg = WM_HOTKEY;
1364 msg->wparam = hotkey->id;
1365 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1367 free( msg->data );
1368 msg->data = NULL;
1369 msg->data_size = 0;
1371 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1372 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1373 hotkey->queue->hotkey_count++;
1374 return 1;
1377 /* find the window that should receive a given hardware message */
1378 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1379 struct message *msg, unsigned int *msg_code )
1381 struct hardware_msg_data *data = msg->data;
1382 user_handle_t win = 0;
1384 *msg_code = msg->msg;
1385 if (msg->msg == WM_INPUT)
1387 if (!(win = msg->win) && input) win = input->focus;
1389 else if (is_keyboard_msg( msg ))
1391 if (input && !(win = input->focus))
1393 win = input->active;
1394 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1397 else /* mouse message */
1399 if (!input || !(win = input->capture))
1401 if (!(win = msg->win) || !is_window_visible( win ) || is_window_transparent( win ))
1402 win = window_from_point( desktop, data->x, data->y );
1405 return win;
1408 static struct rawinput_device_entry *find_rawinput_device( unsigned short usage_page, unsigned short usage )
1410 struct rawinput_device_entry *e;
1412 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
1414 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1415 return e;
1418 return NULL;
1421 static void update_rawinput_device(const struct rawinput_device *device)
1423 struct rawinput_device_entry *e;
1425 if (!(e = find_rawinput_device( device->usage_page, device->usage )))
1427 if (!(e = mem_alloc( sizeof(*e) ))) return;
1428 list_add_tail( &current->process->rawinput_devices, &e->entry );
1431 if (device->flags & RIDEV_REMOVE)
1433 list_remove( &e->entry );
1434 free( e );
1435 return;
1438 e->device = *device;
1439 e->device.target = get_user_full_handle( e->device.target );
1442 /* queue a hardware message into a given thread input */
1443 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1445 user_handle_t win;
1446 struct thread *thread;
1447 struct thread_input *input;
1448 unsigned int msg_code;
1449 struct hardware_msg_data *data = msg->data;
1451 update_input_key_state( desktop, desktop->keystate, msg );
1452 last_input_time = get_tick_count();
1453 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1455 if (is_keyboard_msg( msg ))
1457 if (queue_hotkey_message( desktop, msg )) return;
1458 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1459 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1460 msg->lparam &= ~(KF_EXTENDED << 16);
1462 else if (msg->msg != WM_INPUT)
1464 if (msg->msg == WM_MOUSEMOVE)
1466 int x = min( max( data->x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
1467 int y = min( max( data->y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
1468 if (desktop->cursor.x != x || desktop->cursor.y != y) always_queue = 1;
1469 desktop->cursor.x = x;
1470 desktop->cursor.y = y;
1471 desktop->cursor.last_change = get_tick_count();
1473 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1474 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1475 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1476 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1477 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1478 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1479 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1481 data->x = desktop->cursor.x;
1482 data->y = desktop->cursor.y;
1484 if (msg->win && (thread = get_window_thread( msg->win )))
1486 input = thread->queue->input;
1487 release_object( thread );
1489 else input = desktop->foreground_input;
1491 win = find_hardware_message_window( desktop, input, msg, &msg_code );
1492 if (!win || !(thread = get_window_thread(win)))
1494 if (input) update_input_key_state( input->desktop, input->keystate, msg );
1495 free_message( msg );
1496 return;
1498 input = thread->queue->input;
1500 if (win != desktop->cursor.win) always_queue = 1;
1501 desktop->cursor.win = win;
1503 if (!always_queue || merge_message( input, msg )) free_message( msg );
1504 else
1506 msg->unique_id = 0; /* will be set once we return it to the app */
1507 list_add_tail( &input->msg_list, &msg->entry );
1508 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1510 release_object( thread );
1513 /* send the low-level hook message for a given hardware message */
1514 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1515 const hw_input_t *input, struct msg_queue *sender )
1517 struct thread *hook_thread;
1518 struct msg_queue *queue;
1519 struct message *msg;
1520 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1521 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1523 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1524 if (!(queue = hook_thread->queue)) return 0;
1525 if (is_queue_hung( queue )) return 0;
1527 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1529 msg->type = MSG_HOOK_LL;
1530 msg->win = 0;
1531 msg->msg = id;
1532 msg->wparam = hardware_msg->msg;
1533 msg->time = hardware_msg->time;
1534 msg->data_size = hardware_msg->data_size;
1535 msg->result = NULL;
1537 if (input->type == INPUT_KEYBOARD)
1539 unsigned short vkey = input->kbd.vkey;
1540 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1541 msg->lparam = (input->kbd.scan << 16) | vkey;
1543 else msg->lparam = input->mouse.data << 16;
1545 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1546 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1548 free_message( msg );
1549 return 0;
1551 msg->result->hardware_msg = hardware_msg;
1552 msg->result->desktop = (struct desktop *)grab_object( desktop );
1553 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1554 set_queue_bits( queue, QS_SENDMESSAGE );
1555 return 1;
1558 /* queue a hardware message for a mouse event */
1559 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1560 unsigned int hook_flags, struct msg_queue *sender )
1562 const struct rawinput_device *device;
1563 struct hardware_msg_data *msg_data;
1564 struct message *msg;
1565 unsigned int i, time, flags;
1566 int wait = 0, x, y;
1568 static const unsigned int messages[] =
1570 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1571 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1572 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1573 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1574 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1575 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1576 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1577 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1578 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1579 0, /* 0x0200 = unused */
1580 0, /* 0x0400 = unused */
1581 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1582 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1585 desktop->cursor.last_change = get_tick_count();
1586 flags = input->mouse.flags;
1587 time = input->mouse.time;
1588 if (!time) time = desktop->cursor.last_change;
1590 if (flags & MOUSEEVENTF_MOVE)
1592 if (flags & MOUSEEVENTF_ABSOLUTE)
1594 x = input->mouse.x;
1595 y = input->mouse.y;
1596 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1597 x == desktop->cursor.x && y == desktop->cursor.y)
1598 flags &= ~MOUSEEVENTF_MOVE;
1600 else
1602 x = desktop->cursor.x + input->mouse.x;
1603 y = desktop->cursor.y + input->mouse.y;
1606 else
1608 x = desktop->cursor.x;
1609 y = desktop->cursor.y;
1612 if ((device = current->process->rawinput_mouse))
1614 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1615 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1617 free( msg );
1618 return 0;
1621 msg->type = MSG_HARDWARE;
1622 msg->win = device->target;
1623 msg->msg = WM_INPUT;
1624 msg->wparam = RIM_INPUT;
1625 msg->lparam = 0;
1626 msg->time = time;
1627 msg->data = msg_data;
1628 msg->data_size = sizeof(*msg_data);
1629 msg->result = NULL;
1631 msg_data->info = input->mouse.info;
1632 msg_data->flags = flags;
1633 msg_data->rawinput.type = RIM_TYPEMOUSE;
1634 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1635 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1636 msg_data->rawinput.mouse.data = input->mouse.data;
1638 queue_hardware_message( desktop, msg, 0 );
1641 for (i = 0; i < sizeof(messages)/sizeof(messages[0]); i++)
1643 if (!messages[i]) continue;
1644 if (!(flags & (1 << i))) continue;
1645 flags &= ~(1 << i);
1647 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1648 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1650 free( msg );
1651 return 0;
1653 memset( msg_data, 0, sizeof(*msg_data) );
1655 msg->type = MSG_HARDWARE;
1656 msg->win = get_user_full_handle( win );
1657 msg->msg = messages[i];
1658 msg->wparam = input->mouse.data << 16;
1659 msg->lparam = 0;
1660 msg->time = time;
1661 msg->result = NULL;
1662 msg->data = msg_data;
1663 msg->data_size = sizeof(*msg_data);
1664 msg_data->x = x;
1665 msg_data->y = y;
1666 msg_data->info = input->mouse.info;
1667 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLMHF_INJECTED;
1669 /* specify a sender only when sending the last message */
1670 if (!(flags & ((1 << sizeof(messages)/sizeof(messages[0])) - 1)))
1672 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1673 queue_hardware_message( desktop, msg, 0 );
1675 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1676 queue_hardware_message( desktop, msg, 0 );
1678 return wait;
1681 /* queue a hardware message for a keyboard event */
1682 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1683 unsigned int hook_flags, struct msg_queue *sender )
1685 const struct rawinput_device *device;
1686 struct hardware_msg_data *msg_data;
1687 struct message *msg;
1688 unsigned char vkey = input->kbd.vkey;
1689 unsigned int message_code, time;
1690 int wait;
1692 if (!(time = input->kbd.time)) time = get_tick_count();
1694 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1696 switch (vkey)
1698 case VK_MENU:
1699 case VK_LMENU:
1700 case VK_RMENU:
1701 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1702 break;
1703 case VK_CONTROL:
1704 case VK_LCONTROL:
1705 case VK_RCONTROL:
1706 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1707 break;
1708 case VK_SHIFT:
1709 case VK_LSHIFT:
1710 case VK_RSHIFT:
1711 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1712 break;
1716 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1717 switch (vkey)
1719 case VK_LMENU:
1720 case VK_RMENU:
1721 if (input->kbd.flags & KEYEVENTF_KEYUP)
1723 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1724 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1725 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1726 message_code = WM_SYSKEYUP;
1727 desktop->keystate[VK_MENU] &= ~0x02;
1729 else
1731 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1732 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1733 message_code = WM_SYSKEYDOWN;
1734 desktop->keystate[VK_MENU] |= 0x02;
1736 break;
1738 case VK_LCONTROL:
1739 case VK_RCONTROL:
1740 /* send WM_SYSKEYUP on release if Alt still pressed */
1741 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1742 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1743 message_code = WM_SYSKEYUP;
1744 desktop->keystate[VK_MENU] &= ~0x02;
1745 break;
1747 default:
1748 /* send WM_SYSKEY for Alt-anykey and for F10 */
1749 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1750 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1751 /* fall through */
1752 case VK_F10:
1753 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1754 desktop->keystate[VK_MENU] &= ~0x02;
1755 break;
1758 if ((device = current->process->rawinput_kbd))
1760 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1761 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1763 free( msg );
1764 return 0;
1767 msg->type = MSG_HARDWARE;
1768 msg->win = device->target;
1769 msg->msg = WM_INPUT;
1770 msg->wparam = RIM_INPUT;
1771 msg->lparam = 0;
1772 msg->time = time;
1773 msg->data = msg_data;
1774 msg->data_size = sizeof(*msg_data);
1775 msg->result = NULL;
1777 msg_data->info = input->kbd.info;
1778 msg_data->flags = input->kbd.flags;
1779 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1780 msg_data->rawinput.kbd.message = message_code;
1781 msg_data->rawinput.kbd.vkey = vkey;
1782 msg_data->rawinput.kbd.scan = input->kbd.scan;
1784 queue_hardware_message( desktop, msg, 0 );
1787 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1788 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1790 free( msg );
1791 return 0;
1793 memset( msg_data, 0, sizeof(*msg_data) );
1795 msg->type = MSG_HARDWARE;
1796 msg->win = get_user_full_handle( win );
1797 msg->msg = message_code;
1798 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1799 msg->time = time;
1800 msg->result = NULL;
1801 msg->data = msg_data;
1802 msg->data_size = sizeof(*msg_data);
1803 msg_data->info = input->kbd.info;
1804 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLKHF_INJECTED;
1806 if (input->kbd.flags & KEYEVENTF_UNICODE)
1808 msg->wparam = VK_PACKET;
1810 else
1812 unsigned int flags = 0;
1813 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1814 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1815 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1816 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1818 msg->wparam = vkey;
1819 msg->lparam |= flags << 16;
1820 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1823 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1824 queue_hardware_message( desktop, msg, 1 );
1826 return wait;
1829 /* queue a hardware message for a custom type of event */
1830 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1831 const hw_input_t *input )
1833 struct hardware_msg_data *msg_data;
1834 struct message *msg;
1836 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
1837 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1839 free( msg );
1840 return;
1842 memset( msg_data, 0, sizeof(*msg_data) );
1844 msg->type = MSG_HARDWARE;
1845 msg->win = get_user_full_handle( win );
1846 msg->msg = input->hw.msg;
1847 msg->wparam = 0;
1848 msg->lparam = input->hw.lparam;
1849 msg->time = get_tick_count();
1850 msg->result = NULL;
1851 msg->data = msg_data;
1852 msg->data_size = sizeof(*msg_data);
1854 queue_hardware_message( desktop, msg, 1 );
1857 /* check message filter for a hardware message */
1858 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1859 user_handle_t filter_win, unsigned int first, unsigned int last )
1861 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1863 /* we can only test the window for a keyboard message since the
1864 * dest window for a mouse message depends on hittest */
1865 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1866 return 0;
1867 /* the message code is final for a keyboard message, we can simply check it */
1868 return check_msg_filter( msg_code, first, last );
1870 else /* mouse message */
1872 /* we need to check all possible values that the message can have in the end */
1874 if (check_msg_filter( msg_code, first, last )) return 1;
1875 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1877 /* all other messages can become non-client messages */
1878 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1880 /* clicks can become double-clicks or non-client double-clicks */
1881 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1882 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1884 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1885 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1887 return 0;
1892 /* find a hardware message for the given queue */
1893 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1894 unsigned int first, unsigned int last, unsigned int flags,
1895 struct get_message_reply *reply )
1897 struct thread_input *input = thread->queue->input;
1898 struct thread *win_thread;
1899 struct list *ptr;
1900 user_handle_t win;
1901 int clear_bits, got_one = 0;
1902 unsigned int msg_code;
1904 ptr = list_head( &input->msg_list );
1905 if (hw_id)
1907 while (ptr)
1909 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1910 if (msg->unique_id == hw_id) break;
1911 ptr = list_next( &input->msg_list, ptr );
1913 if (!ptr) ptr = list_head( &input->msg_list );
1914 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1917 if (ptr == list_head( &input->msg_list ))
1918 clear_bits = QS_INPUT;
1919 else
1920 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1922 while (ptr)
1924 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1925 struct hardware_msg_data *data = msg->data;
1927 ptr = list_next( &input->msg_list, ptr );
1928 win = find_hardware_message_window( input->desktop, input, msg, &msg_code );
1929 if (!win || !(win_thread = get_window_thread( win )))
1931 /* no window at all, remove it */
1932 update_input_key_state( input->desktop, input->keystate, msg );
1933 list_remove( &msg->entry );
1934 free_message( msg );
1935 continue;
1937 if (win_thread != thread)
1939 if (win_thread->queue->input == input)
1941 /* wake the other thread */
1942 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1943 got_one = 1;
1945 else
1947 /* for another thread input, drop it */
1948 update_input_key_state( input->desktop, input->keystate, msg );
1949 list_remove( &msg->entry );
1950 free_message( msg );
1952 release_object( win_thread );
1953 continue;
1955 release_object( win_thread );
1957 /* if we already got a message for another thread, or if it doesn't
1958 * match the filter we skip it */
1959 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1961 clear_bits &= ~get_hardware_msg_bit( msg );
1962 continue;
1964 /* now we can return it */
1965 if (!msg->unique_id) msg->unique_id = get_unique_id();
1966 reply->type = MSG_HARDWARE;
1967 reply->win = win;
1968 reply->msg = msg_code;
1969 reply->wparam = msg->wparam;
1970 reply->lparam = msg->lparam;
1971 reply->time = msg->time;
1973 data->hw_id = msg->unique_id;
1974 set_reply_data( msg->data, msg->data_size );
1975 if (msg->msg == WM_INPUT && (flags & PM_REMOVE))
1976 release_hardware_message( current->queue, data->hw_id, 1, 0 );
1977 return 1;
1979 /* nothing found, clear the hardware queue bits */
1980 clear_queue_bits( thread->queue, clear_bits );
1981 return 0;
1984 /* increment (or decrement if 'incr' is negative) the queue paint count */
1985 void inc_queue_paint_count( struct thread *thread, int incr )
1987 struct msg_queue *queue = thread->queue;
1989 assert( queue );
1991 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1993 if (queue->paint_count)
1994 set_queue_bits( queue, QS_PAINT );
1995 else
1996 clear_queue_bits( queue, QS_PAINT );
2000 /* remove all messages and timers belonging to a certain window */
2001 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2003 struct msg_queue *queue = thread->queue;
2004 struct list *ptr;
2005 int i;
2007 if (!queue) return;
2009 /* remove timers */
2011 ptr = list_head( &queue->pending_timers );
2012 while (ptr)
2014 struct list *next = list_next( &queue->pending_timers, ptr );
2015 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2016 if (timer->win == win) free_timer( queue, timer );
2017 ptr = next;
2019 ptr = list_head( &queue->expired_timers );
2020 while (ptr)
2022 struct list *next = list_next( &queue->expired_timers, ptr );
2023 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2024 if (timer->win == win) free_timer( queue, timer );
2025 ptr = next;
2028 /* remove messages */
2029 for (i = 0; i < NB_MSG_KINDS; i++)
2031 struct list *ptr, *next;
2033 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2035 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2036 if (msg->win == win)
2038 if (msg->msg == WM_QUIT && !queue->quit_message)
2040 queue->quit_message = 1;
2041 queue->exit_code = msg->wparam;
2043 remove_queue_message( queue, msg, i );
2048 thread_input_cleanup_window( queue, win );
2051 /* post a message to a window; used by socket handling */
2052 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2054 struct message *msg;
2055 struct thread *thread = get_window_thread( win );
2057 if (!thread) return;
2059 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2061 msg->type = MSG_POSTED;
2062 msg->win = get_user_full_handle( win );
2063 msg->msg = message;
2064 msg->wparam = wparam;
2065 msg->lparam = lparam;
2066 msg->time = get_tick_count();
2067 msg->result = NULL;
2068 msg->data = NULL;
2069 msg->data_size = 0;
2071 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2072 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2073 if (message == WM_HOTKEY)
2075 set_queue_bits( thread->queue, QS_HOTKEY );
2076 thread->queue->hotkey_count++;
2079 release_object( thread );
2082 /* post a win event */
2083 void post_win_event( struct thread *thread, unsigned int event,
2084 user_handle_t win, unsigned int object_id,
2085 unsigned int child_id, client_ptr_t hook_proc,
2086 const WCHAR *module, data_size_t module_size,
2087 user_handle_t hook)
2089 struct message *msg;
2091 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2093 struct winevent_msg_data *data;
2095 msg->type = MSG_WINEVENT;
2096 msg->win = get_user_full_handle( win );
2097 msg->msg = event;
2098 msg->wparam = object_id;
2099 msg->lparam = child_id;
2100 msg->time = get_tick_count();
2101 msg->result = NULL;
2103 if ((data = malloc( sizeof(*data) + module_size )))
2105 data->hook = hook;
2106 data->tid = get_thread_id( current );
2107 data->hook_proc = hook_proc;
2108 memcpy( data + 1, module, module_size );
2110 msg->data = data;
2111 msg->data_size = sizeof(*data) + module_size;
2113 if (debug_level > 1)
2114 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2115 get_thread_id(thread), event, win, object_id, child_id );
2116 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2117 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2119 else
2120 free( msg );
2124 /* free all hotkeys on a desktop, optionally filtering by window */
2125 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2127 struct hotkey *hotkey, *hotkey2;
2129 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2131 if (!window || hotkey->win == window)
2133 list_remove( &hotkey->entry );
2134 free( hotkey );
2140 /* check if the thread owning the window is hung */
2141 DECL_HANDLER(is_window_hung)
2143 struct thread *thread;
2145 thread = get_window_thread( req->win );
2146 if (thread)
2148 reply->is_hung = is_queue_hung( thread->queue );
2149 release_object( thread );
2151 else reply->is_hung = 0;
2155 /* get the message queue of the current thread */
2156 DECL_HANDLER(get_msg_queue)
2158 struct msg_queue *queue = get_current_queue();
2160 reply->handle = 0;
2161 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2165 /* set the file descriptor associated to the current thread queue */
2166 DECL_HANDLER(set_queue_fd)
2168 struct msg_queue *queue = get_current_queue();
2169 struct file *file;
2170 int unix_fd;
2172 if (queue->fd) /* fd can only be set once */
2174 set_error( STATUS_ACCESS_DENIED );
2175 return;
2177 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2179 if ((unix_fd = get_file_unix_fd( file )) != -1)
2181 if ((unix_fd = dup( unix_fd )) != -1)
2182 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2183 else
2184 file_set_error();
2186 release_object( file );
2190 /* set the current message queue wakeup mask */
2191 DECL_HANDLER(set_queue_mask)
2193 struct msg_queue *queue = get_current_queue();
2195 if (queue)
2197 queue->wake_mask = req->wake_mask;
2198 queue->changed_mask = req->changed_mask;
2199 reply->wake_bits = queue->wake_bits;
2200 reply->changed_bits = queue->changed_bits;
2201 if (is_signaled( queue ))
2203 /* if skip wait is set, do what would have been done in the subsequent wait */
2204 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2205 else wake_up( &queue->obj, 0 );
2211 /* get the current message queue status */
2212 DECL_HANDLER(get_queue_status)
2214 struct msg_queue *queue = current->queue;
2215 if (queue)
2217 reply->wake_bits = queue->wake_bits;
2218 reply->changed_bits = queue->changed_bits;
2219 if (req->clear) queue->changed_bits = 0;
2221 else reply->wake_bits = reply->changed_bits = 0;
2225 /* send a message to a thread queue */
2226 DECL_HANDLER(send_message)
2228 struct message *msg;
2229 struct msg_queue *send_queue = get_current_queue();
2230 struct msg_queue *recv_queue = NULL;
2231 struct thread *thread = NULL;
2233 if (!(thread = get_thread_from_id( req->id ))) return;
2235 if (!(recv_queue = thread->queue))
2237 set_error( STATUS_INVALID_PARAMETER );
2238 release_object( thread );
2239 return;
2241 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2243 set_error( STATUS_TIMEOUT );
2244 release_object( thread );
2245 return;
2248 if ((msg = mem_alloc( sizeof(*msg) )))
2250 msg->type = req->type;
2251 msg->win = get_user_full_handle( req->win );
2252 msg->msg = req->msg;
2253 msg->wparam = req->wparam;
2254 msg->lparam = req->lparam;
2255 msg->time = get_tick_count();
2256 msg->result = NULL;
2257 msg->data = NULL;
2258 msg->data_size = get_req_data_size();
2260 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2262 free( msg );
2263 release_object( thread );
2264 return;
2267 switch(msg->type)
2269 case MSG_OTHER_PROCESS:
2270 case MSG_ASCII:
2271 case MSG_UNICODE:
2272 case MSG_CALLBACK:
2273 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2275 free_message( msg );
2276 break;
2278 /* fall through */
2279 case MSG_NOTIFY:
2280 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2281 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2282 break;
2283 case MSG_POSTED:
2284 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2285 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2286 if (msg->msg == WM_HOTKEY)
2288 set_queue_bits( recv_queue, QS_HOTKEY );
2289 recv_queue->hotkey_count++;
2291 break;
2292 case MSG_HARDWARE: /* should use send_hardware_message instead */
2293 case MSG_CALLBACK_RESULT: /* cannot send this one */
2294 case MSG_HOOK_LL: /* generated internally */
2295 default:
2296 set_error( STATUS_INVALID_PARAMETER );
2297 free( msg );
2298 break;
2301 release_object( thread );
2304 /* send a hardware message to a thread queue */
2305 DECL_HANDLER(send_hardware_message)
2307 struct thread *thread = NULL;
2308 struct desktop *desktop;
2309 struct msg_queue *sender = get_current_queue();
2310 data_size_t size = min( 256, get_reply_max_size() );
2312 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2314 if (req->win)
2316 if (!(thread = get_window_thread( req->win ))) return;
2317 if (desktop != thread->queue->input->desktop)
2319 /* don't allow queuing events to a different desktop */
2320 release_object( desktop );
2321 return;
2325 reply->prev_x = desktop->cursor.x;
2326 reply->prev_y = desktop->cursor.y;
2328 switch (req->input.type)
2330 case INPUT_MOUSE:
2331 reply->wait = queue_mouse_message( desktop, req->win, &req->input, req->flags, sender );
2332 break;
2333 case INPUT_KEYBOARD:
2334 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, req->flags, sender );
2335 break;
2336 case INPUT_HARDWARE:
2337 queue_custom_hardware_message( desktop, req->win, &req->input );
2338 break;
2339 default:
2340 set_error( STATUS_INVALID_PARAMETER );
2342 if (thread) release_object( thread );
2344 reply->new_x = desktop->cursor.x;
2345 reply->new_y = desktop->cursor.y;
2346 set_reply_data( desktop->keystate, size );
2347 release_object( desktop );
2350 /* post a quit message to the current queue */
2351 DECL_HANDLER(post_quit_message)
2353 struct msg_queue *queue = get_current_queue();
2355 if (!queue)
2356 return;
2358 queue->quit_message = 1;
2359 queue->exit_code = req->exit_code;
2360 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2363 /* get a message from the current queue */
2364 DECL_HANDLER(get_message)
2366 struct timer *timer;
2367 struct list *ptr;
2368 struct msg_queue *queue = get_current_queue();
2369 user_handle_t get_win = get_user_full_handle( req->get_win );
2370 unsigned int filter = req->flags >> 16;
2372 reply->active_hooks = get_active_hooks();
2374 if (!queue) return;
2375 queue->last_get_msg = current_time;
2376 if (!filter) filter = QS_ALLINPUT;
2378 /* first check for sent messages */
2379 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2381 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2382 receive_message( queue, msg, reply );
2383 return;
2386 /* clear changed bits so we can wait on them if we don't find a message */
2387 if (filter & QS_POSTMESSAGE)
2389 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2390 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2392 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2393 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2395 /* then check for posted messages */
2396 if ((filter & QS_POSTMESSAGE) &&
2397 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2398 return;
2400 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2401 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2402 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2403 return;
2405 /* only check for quit messages if not posted messages pending.
2406 * note: the quit message isn't filtered */
2407 if (get_quit_message( queue, req->flags, reply ))
2408 return;
2410 /* then check for any raw hardware message */
2411 if ((filter & QS_INPUT) &&
2412 filter_contains_hw_range( req->get_first, req->get_last ) &&
2413 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2414 return;
2416 /* now check for WM_PAINT */
2417 if ((filter & QS_PAINT) &&
2418 queue->paint_count &&
2419 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2420 (reply->win = find_window_to_repaint( get_win, current )))
2422 reply->type = MSG_POSTED;
2423 reply->msg = WM_PAINT;
2424 reply->wparam = 0;
2425 reply->lparam = 0;
2426 reply->time = get_tick_count();
2427 return;
2430 /* now check for timer */
2431 if ((filter & QS_TIMER) &&
2432 (timer = find_expired_timer( queue, get_win, req->get_first,
2433 req->get_last, (req->flags & PM_REMOVE) )))
2435 reply->type = MSG_POSTED;
2436 reply->win = timer->win;
2437 reply->msg = timer->msg;
2438 reply->wparam = timer->id;
2439 reply->lparam = timer->lparam;
2440 reply->time = get_tick_count();
2441 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2442 set_event( current->process->idle_event );
2443 return;
2446 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2447 queue->wake_mask = req->wake_mask;
2448 queue->changed_mask = req->changed_mask;
2449 set_error( STATUS_PENDING ); /* FIXME */
2453 /* reply to a sent message */
2454 DECL_HANDLER(reply_message)
2456 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2457 else if (current->queue->recv_result)
2458 reply_message( current->queue, req->result, 0, req->remove,
2459 get_req_data(), get_req_data_size() );
2463 /* accept the current hardware message */
2464 DECL_HANDLER(accept_hardware_message)
2466 if (current->queue)
2467 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
2468 else
2469 set_error( STATUS_ACCESS_DENIED );
2473 /* retrieve the reply for the last message sent */
2474 DECL_HANDLER(get_message_reply)
2476 struct message_result *result;
2477 struct list *entry;
2478 struct msg_queue *queue = current->queue;
2480 if (queue)
2482 set_error( STATUS_PENDING );
2483 reply->result = 0;
2485 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2487 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2488 if (result->replied || req->cancel)
2490 if (result->replied)
2492 reply->result = result->result;
2493 set_error( result->error );
2494 if (result->data)
2496 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2497 set_reply_data_ptr( result->data, data_len );
2498 result->data = NULL;
2499 result->data_size = 0;
2502 remove_result_from_sender( result );
2504 entry = list_head( &queue->send_result );
2505 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2506 else
2508 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2509 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2510 else clear_queue_bits( queue, QS_SMRESULT );
2514 else set_error( STATUS_ACCESS_DENIED );
2518 /* set a window timer */
2519 DECL_HANDLER(set_win_timer)
2521 struct timer *timer;
2522 struct msg_queue *queue;
2523 struct thread *thread = NULL;
2524 user_handle_t win = 0;
2525 lparam_t id = req->id;
2527 if (req->win)
2529 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2531 set_error( STATUS_INVALID_HANDLE );
2532 return;
2534 if (thread->process != current->process)
2536 release_object( thread );
2537 set_error( STATUS_ACCESS_DENIED );
2538 return;
2540 queue = thread->queue;
2541 /* remove it if it existed already */
2542 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2544 else
2546 queue = get_current_queue();
2547 /* look for a timer with this id */
2548 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2550 /* free and reuse id */
2551 free_timer( queue, timer );
2553 else
2555 /* find a free id for it */
2558 id = queue->next_timer_id;
2559 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2561 while (find_timer( queue, 0, req->msg, id ));
2565 if ((timer = set_timer( queue, req->rate )))
2567 timer->win = win;
2568 timer->msg = req->msg;
2569 timer->id = id;
2570 timer->lparam = req->lparam;
2571 reply->id = id;
2573 if (thread) release_object( thread );
2576 /* kill a window timer */
2577 DECL_HANDLER(kill_win_timer)
2579 struct timer *timer;
2580 struct thread *thread;
2581 user_handle_t win = 0;
2583 if (req->win)
2585 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2587 set_error( STATUS_INVALID_HANDLE );
2588 return;
2590 if (thread->process != current->process)
2592 release_object( thread );
2593 set_error( STATUS_ACCESS_DENIED );
2594 return;
2597 else thread = (struct thread *)grab_object( current );
2599 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2600 free_timer( thread->queue, timer );
2601 else
2602 set_error( STATUS_INVALID_PARAMETER );
2604 release_object( thread );
2607 DECL_HANDLER(register_hotkey)
2609 struct desktop *desktop;
2610 user_handle_t win_handle = req->window;
2611 struct hotkey *hotkey;
2612 struct hotkey *new_hotkey = NULL;
2613 struct thread *thread;
2614 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2616 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2618 if (win_handle)
2620 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2622 release_object( desktop );
2623 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2624 return;
2627 thread = get_window_thread( win_handle );
2628 if (thread) release_object( thread );
2630 if (thread != current)
2632 release_object( desktop );
2633 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2634 return;
2638 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2640 if (req->vkey == hotkey->vkey &&
2641 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2643 release_object( desktop );
2644 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2645 return;
2647 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2648 new_hotkey = hotkey;
2651 if (new_hotkey)
2653 reply->replaced = 1;
2654 reply->flags = new_hotkey->flags;
2655 reply->vkey = new_hotkey->vkey;
2657 else
2659 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2660 if (new_hotkey)
2662 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2663 new_hotkey->queue = current->queue;
2664 new_hotkey->win = win_handle;
2665 new_hotkey->id = req->id;
2669 if (new_hotkey)
2671 new_hotkey->flags = req->flags;
2672 new_hotkey->vkey = req->vkey;
2675 release_object( desktop );
2678 DECL_HANDLER(unregister_hotkey)
2680 struct desktop *desktop;
2681 user_handle_t win_handle = req->window;
2682 struct hotkey *hotkey;
2683 struct thread *thread;
2685 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2687 if (win_handle)
2689 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2691 release_object( desktop );
2692 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2693 return;
2696 thread = get_window_thread( win_handle );
2697 if (thread) release_object( thread );
2699 if (thread != current)
2701 release_object( desktop );
2702 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2703 return;
2707 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2709 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2710 goto found;
2713 release_object( desktop );
2714 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2715 return;
2717 found:
2718 reply->flags = hotkey->flags;
2719 reply->vkey = hotkey->vkey;
2720 list_remove( &hotkey->entry );
2721 free( hotkey );
2722 release_object( desktop );
2725 /* attach (or detach) thread inputs */
2726 DECL_HANDLER(attach_thread_input)
2728 struct thread *thread_from = get_thread_from_id( req->tid_from );
2729 struct thread *thread_to = get_thread_from_id( req->tid_to );
2731 if (!thread_from || !thread_to)
2733 if (thread_from) release_object( thread_from );
2734 if (thread_to) release_object( thread_to );
2735 return;
2737 if (thread_from != thread_to)
2739 if (req->attach) attach_thread_input( thread_from, thread_to );
2740 else
2742 if (thread_from->queue && thread_to->queue &&
2743 thread_from->queue->input == thread_to->queue->input)
2744 detach_thread_input( thread_from );
2745 else
2746 set_error( STATUS_ACCESS_DENIED );
2749 else set_error( STATUS_ACCESS_DENIED );
2750 release_object( thread_from );
2751 release_object( thread_to );
2755 /* get thread input data */
2756 DECL_HANDLER(get_thread_input)
2758 struct thread *thread = NULL;
2759 struct desktop *desktop;
2760 struct thread_input *input;
2762 if (req->tid)
2764 if (!(thread = get_thread_from_id( req->tid ))) return;
2765 if (!(desktop = get_thread_desktop( thread, 0 )))
2767 release_object( thread );
2768 return;
2770 input = thread->queue ? thread->queue->input : NULL;
2772 else
2774 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2775 input = desktop->foreground_input; /* get the foreground thread info */
2778 if (input)
2780 reply->focus = input->focus;
2781 reply->capture = input->capture;
2782 reply->active = input->active;
2783 reply->menu_owner = input->menu_owner;
2784 reply->move_size = input->move_size;
2785 reply->caret = input->caret;
2786 reply->cursor = input->cursor;
2787 reply->show_count = input->cursor_count;
2788 reply->rect = input->caret_rect;
2791 /* foreground window is active window of foreground thread */
2792 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2793 if (thread) release_object( thread );
2794 release_object( desktop );
2798 /* retrieve queue keyboard state for a given thread */
2799 DECL_HANDLER(get_key_state)
2801 struct thread *thread;
2802 struct desktop *desktop;
2803 data_size_t size = min( 256, get_reply_max_size() );
2805 if (!req->tid) /* get global async key state */
2807 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2808 if (req->key >= 0)
2810 reply->state = desktop->keystate[req->key & 0xff];
2811 desktop->keystate[req->key & 0xff] &= ~0x40;
2813 set_reply_data( desktop->keystate, size );
2814 release_object( desktop );
2816 else
2818 if (!(thread = get_thread_from_id( req->tid ))) return;
2819 if (thread->queue)
2821 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2822 set_reply_data( thread->queue->input->keystate, size );
2824 release_object( thread );
2829 /* set queue keyboard state for a given thread */
2830 DECL_HANDLER(set_key_state)
2832 struct thread *thread;
2833 struct desktop *desktop;
2834 data_size_t size = min( 256, get_req_data_size() );
2836 if (!req->tid) /* set global async key state */
2838 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2839 memcpy( desktop->keystate, get_req_data(), size );
2840 release_object( desktop );
2842 else
2844 if (!(thread = get_thread_from_id( req->tid ))) return;
2845 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
2846 if (req->async && (desktop = get_thread_desktop( thread, 0 )))
2848 memcpy( desktop->keystate, get_req_data(), size );
2849 release_object( desktop );
2851 release_object( thread );
2856 /* set the system foreground window */
2857 DECL_HANDLER(set_foreground_window)
2859 struct thread *thread = NULL;
2860 struct desktop *desktop;
2861 struct msg_queue *queue = get_current_queue();
2863 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2864 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
2865 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
2866 reply->send_msg_new = FALSE;
2868 if (is_valid_foreground_window( req->handle ) &&
2869 (thread = get_window_thread( req->handle )) &&
2870 thread->queue->input->desktop == desktop)
2872 set_foreground_input( desktop, thread->queue->input );
2873 reply->send_msg_new = (desktop->foreground_input != queue->input);
2875 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2877 if (thread) release_object( thread );
2878 release_object( desktop );
2882 /* set the current thread focus window */
2883 DECL_HANDLER(set_focus_window)
2885 struct msg_queue *queue = get_current_queue();
2887 reply->previous = 0;
2888 if (queue && check_queue_input_window( queue, req->handle ))
2890 reply->previous = queue->input->focus;
2891 queue->input->focus = get_user_full_handle( req->handle );
2896 /* set the current thread active window */
2897 DECL_HANDLER(set_active_window)
2899 struct msg_queue *queue = get_current_queue();
2901 reply->previous = 0;
2902 if (queue && check_queue_input_window( queue, req->handle ))
2904 if (!req->handle || make_window_active( req->handle ))
2906 reply->previous = queue->input->active;
2907 queue->input->active = get_user_full_handle( req->handle );
2909 else set_error( STATUS_INVALID_HANDLE );
2914 /* set the current thread capture window */
2915 DECL_HANDLER(set_capture_window)
2917 struct msg_queue *queue = get_current_queue();
2919 reply->previous = reply->full_handle = 0;
2920 if (queue && check_queue_input_window( queue, req->handle ))
2922 struct thread_input *input = queue->input;
2924 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2925 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2927 set_error(STATUS_ACCESS_DENIED);
2928 return;
2930 reply->previous = input->capture;
2931 input->capture = get_user_full_handle( req->handle );
2932 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2933 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2934 reply->full_handle = input->capture;
2939 /* Set the current thread caret window */
2940 DECL_HANDLER(set_caret_window)
2942 struct msg_queue *queue = get_current_queue();
2944 reply->previous = 0;
2945 if (queue && check_queue_input_window( queue, req->handle ))
2947 struct thread_input *input = queue->input;
2949 reply->previous = input->caret;
2950 reply->old_rect = input->caret_rect;
2951 reply->old_hide = input->caret_hide;
2952 reply->old_state = input->caret_state;
2954 set_caret_window( input, get_user_full_handle(req->handle) );
2955 input->caret_rect.right = input->caret_rect.left + req->width;
2956 input->caret_rect.bottom = input->caret_rect.top + req->height;
2961 /* Set the current thread caret information */
2962 DECL_HANDLER(set_caret_info)
2964 struct msg_queue *queue = get_current_queue();
2965 struct thread_input *input;
2967 if (!queue) return;
2968 input = queue->input;
2969 reply->full_handle = input->caret;
2970 reply->old_rect = input->caret_rect;
2971 reply->old_hide = input->caret_hide;
2972 reply->old_state = input->caret_state;
2974 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2976 set_error( STATUS_ACCESS_DENIED );
2977 return;
2979 if (req->flags & SET_CARET_POS)
2981 input->caret_rect.right += req->x - input->caret_rect.left;
2982 input->caret_rect.bottom += req->y - input->caret_rect.top;
2983 input->caret_rect.left = req->x;
2984 input->caret_rect.top = req->y;
2986 if (req->flags & SET_CARET_HIDE)
2988 input->caret_hide += req->hide;
2989 if (input->caret_hide < 0) input->caret_hide = 0;
2991 if (req->flags & SET_CARET_STATE)
2993 if (req->state == -1) input->caret_state = !input->caret_state;
2994 else input->caret_state = !!req->state;
2999 /* get the time of the last input event */
3000 DECL_HANDLER(get_last_input_time)
3002 reply->time = last_input_time;
3005 /* set/get the current cursor */
3006 DECL_HANDLER(set_cursor)
3008 struct msg_queue *queue = get_current_queue();
3009 struct thread_input *input;
3011 if (!queue) return;
3012 input = queue->input;
3014 reply->prev_handle = input->cursor;
3015 reply->prev_count = input->cursor_count;
3016 reply->prev_x = input->desktop->cursor.x;
3017 reply->prev_y = input->desktop->cursor.y;
3019 if (req->flags & SET_CURSOR_HANDLE)
3021 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3023 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3024 return;
3026 input->cursor = req->handle;
3028 if (req->flags & SET_CURSOR_COUNT)
3030 queue->cursor_count += req->show_count;
3031 input->cursor_count += req->show_count;
3033 if (req->flags & SET_CURSOR_POS)
3035 set_cursor_pos( input->desktop, req->x, req->y );
3037 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3039 struct desktop *desktop = input->desktop;
3041 /* only the desktop owner can set the message */
3042 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3043 desktop->cursor.clip_msg = req->clip_msg;
3045 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip );
3048 reply->new_x = input->desktop->cursor.x;
3049 reply->new_y = input->desktop->cursor.y;
3050 reply->new_clip = input->desktop->cursor.clip;
3051 reply->last_change = input->desktop->cursor.last_change;
3054 DECL_HANDLER(update_rawinput_devices)
3056 const struct rawinput_device *devices = get_req_data();
3057 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3058 const struct rawinput_device_entry *e;
3059 unsigned int i;
3061 for (i = 0; i < device_count; ++i)
3063 update_rawinput_device(&devices[i]);
3066 e = find_rawinput_device( 1, 2 );
3067 current->process->rawinput_mouse = e ? &e->device : NULL;
3068 e = find_rawinput_device( 1, 6 );
3069 current->process->rawinput_kbd = e ? &e->device : NULL;