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
22 #include "wine/port.h"
33 #define WIN32_NO_STATUS
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)
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 */
74 struct list entry
; /* entry in message list */
75 enum message_type type
; /* message type */
76 user_handle_t win
; /* window handle */
77 unsigned int msg
; /* message code */
78 lparam_t wparam
; /* parameters */
79 lparam_t lparam
; /* parameters */
80 int x
; /* message position */
82 unsigned int time
; /* message time */
83 void *data
; /* message data for sent messages */
84 unsigned int data_size
; /* size of message data */
85 unsigned int unique_id
; /* unique id for nested hw message waits */
86 struct message_result
*result
; /* result in sender queue */
91 struct list entry
; /* entry in timer list */
92 timeout_t when
; /* next expiration */
93 unsigned int rate
; /* timer rate in ms */
94 user_handle_t win
; /* window handle */
95 unsigned int msg
; /* message to post */
96 lparam_t id
; /* timer id */
97 lparam_t lparam
; /* lparam for message */
102 struct object obj
; /* object header */
103 struct desktop
*desktop
; /* desktop that this thread input belongs to */
104 user_handle_t focus
; /* focus window */
105 user_handle_t capture
; /* capture window */
106 user_handle_t active
; /* active window */
107 user_handle_t menu_owner
; /* current menu owner window */
108 user_handle_t move_size
; /* current moving/resizing window */
109 user_handle_t caret
; /* caret window */
110 rectangle_t caret_rect
; /* caret rectangle */
111 int caret_hide
; /* caret hide count */
112 int caret_state
; /* caret on/off state */
113 user_handle_t cursor
; /* current cursor */
114 int cursor_count
; /* cursor show count */
115 struct list msg_list
; /* list of hardware messages */
116 unsigned char keystate
[256]; /* state of each key */
121 struct object obj
; /* object header */
122 struct fd
*fd
; /* optional file descriptor to poll */
123 unsigned int wake_bits
; /* wakeup bits */
124 unsigned int wake_mask
; /* wakeup mask */
125 unsigned int changed_bits
; /* changed wakeup bits */
126 unsigned int changed_mask
; /* changed wakeup mask */
127 int paint_count
; /* pending paint messages count */
128 int hotkey_count
; /* pending hotkey messages count */
129 int quit_message
; /* is there a pending quit message? */
130 int exit_code
; /* exit code of pending quit message */
131 int cursor_count
; /* per-queue cursor show count */
132 struct list msg_list
[NB_MSG_KINDS
]; /* lists of messages */
133 struct list send_result
; /* stack of sent messages waiting for result */
134 struct list callback_result
; /* list of callback messages waiting for result */
135 struct message_result
*recv_result
; /* stack of received messages waiting for result */
136 struct list pending_timers
; /* list of pending timers */
137 struct list expired_timers
; /* list of expired timers */
138 lparam_t next_timer_id
; /* id for the next timer with a 0 window */
139 struct timeout_user
*timeout
; /* timeout for next timer to expire */
140 struct thread_input
*input
; /* thread input descriptor */
141 struct hook_table
*hooks
; /* hook table */
142 timeout_t last_get_msg
; /* time of last get message call */
147 struct list entry
; /* entry in desktop hotkey list */
148 struct msg_queue
*queue
; /* queue owning this hotkey */
149 user_handle_t win
; /* window handle */
150 int id
; /* hotkey id */
151 unsigned int vkey
; /* virtual key code */
152 unsigned int flags
; /* key modifiers */
155 static void msg_queue_dump( struct object
*obj
, int verbose
);
156 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
157 static void msg_queue_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
158 static int msg_queue_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
159 static void msg_queue_satisfied( struct object
*obj
, struct wait_queue_entry
*entry
);
160 static void msg_queue_destroy( struct object
*obj
);
161 static void msg_queue_poll_event( struct fd
*fd
, int event
);
162 static void thread_input_dump( struct object
*obj
, int verbose
);
163 static void thread_input_destroy( struct object
*obj
);
164 static void timer_callback( void *private );
166 static const struct object_ops msg_queue_ops
=
168 sizeof(struct msg_queue
), /* size */
169 msg_queue_dump
, /* dump */
170 no_get_type
, /* get_type */
171 msg_queue_add_queue
, /* add_queue */
172 msg_queue_remove_queue
, /* remove_queue */
173 msg_queue_signaled
, /* signaled */
174 msg_queue_satisfied
, /* satisfied */
175 no_signal
, /* signal */
176 no_get_fd
, /* get_fd */
177 no_map_access
, /* map_access */
178 default_get_sd
, /* get_sd */
179 default_set_sd
, /* set_sd */
180 no_lookup_name
, /* lookup_name */
181 no_open_file
, /* open_file */
182 no_close_handle
, /* close_handle */
183 msg_queue_destroy
/* destroy */
186 static const struct fd_ops msg_queue_fd_ops
=
188 NULL
, /* get_poll_events */
189 msg_queue_poll_event
, /* poll_event */
191 NULL
, /* get_fd_type */
193 NULL
, /* queue_async */
194 NULL
, /* reselect_async */
195 NULL
/* cancel async */
199 static const struct object_ops thread_input_ops
=
201 sizeof(struct thread_input
), /* size */
202 thread_input_dump
, /* dump */
203 no_get_type
, /* get_type */
204 no_add_queue
, /* add_queue */
205 NULL
, /* remove_queue */
207 NULL
, /* satisfied */
208 no_signal
, /* signal */
209 no_get_fd
, /* get_fd */
210 no_map_access
, /* map_access */
211 default_get_sd
, /* get_sd */
212 default_set_sd
, /* set_sd */
213 no_lookup_name
, /* lookup_name */
214 no_open_file
, /* open_file */
215 no_close_handle
, /* close_handle */
216 thread_input_destroy
/* destroy */
219 /* pointer to input structure of foreground thread */
220 static unsigned int last_input_time
;
222 static void queue_hardware_message( struct desktop
*desktop
, struct message
*msg
, int always_queue
);
223 static void free_message( struct message
*msg
);
225 /* set the caret window in a given thread input */
226 static void set_caret_window( struct thread_input
*input
, user_handle_t win
)
228 if (!win
|| win
!= input
->caret
)
230 input
->caret_rect
.left
= 0;
231 input
->caret_rect
.top
= 0;
232 input
->caret_rect
.right
= 0;
233 input
->caret_rect
.bottom
= 0;
236 input
->caret_hide
= 1;
237 input
->caret_state
= 0;
240 /* create a thread input object */
241 static struct thread_input
*create_thread_input( struct thread
*thread
)
243 struct thread_input
*input
;
245 if ((input
= alloc_object( &thread_input_ops
)))
250 input
->menu_owner
= 0;
251 input
->move_size
= 0;
253 input
->cursor_count
= 0;
254 list_init( &input
->msg_list
);
255 set_caret_window( input
, 0 );
256 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
258 if (!(input
->desktop
= get_thread_desktop( thread
, 0 /* FIXME: access rights */ )))
260 release_object( input
);
267 /* create a message queue object */
268 static struct msg_queue
*create_msg_queue( struct thread
*thread
, struct thread_input
*input
)
270 struct thread_input
*new_input
= NULL
;
271 struct msg_queue
*queue
;
276 if (!(new_input
= create_thread_input( thread
))) return NULL
;
280 if ((queue
= alloc_object( &msg_queue_ops
)))
283 queue
->wake_bits
= 0;
284 queue
->wake_mask
= 0;
285 queue
->changed_bits
= 0;
286 queue
->changed_mask
= 0;
287 queue
->paint_count
= 0;
288 queue
->hotkey_count
= 0;
289 queue
->quit_message
= 0;
290 queue
->cursor_count
= 0;
291 queue
->recv_result
= NULL
;
292 queue
->next_timer_id
= 0x7fff;
293 queue
->timeout
= NULL
;
294 queue
->input
= (struct thread_input
*)grab_object( input
);
296 queue
->last_get_msg
= current_time
;
297 list_init( &queue
->send_result
);
298 list_init( &queue
->callback_result
);
299 list_init( &queue
->pending_timers
);
300 list_init( &queue
->expired_timers
);
301 for (i
= 0; i
< NB_MSG_KINDS
; i
++) list_init( &queue
->msg_list
[i
] );
303 thread
->queue
= queue
;
305 if (new_input
) release_object( new_input
);
309 /* free the message queue of a thread at thread exit */
310 void free_msg_queue( struct thread
*thread
)
312 remove_thread_hooks( thread
);
313 if (!thread
->queue
) return;
314 release_object( thread
->queue
);
315 thread
->queue
= NULL
;
318 /* change the thread input data of a given thread */
319 static int assign_thread_input( struct thread
*thread
, struct thread_input
*new_input
)
321 struct msg_queue
*queue
= thread
->queue
;
325 thread
->queue
= create_msg_queue( thread
, new_input
);
326 return thread
->queue
!= NULL
;
330 queue
->input
->cursor_count
-= queue
->cursor_count
;
331 release_object( queue
->input
);
333 queue
->input
= (struct thread_input
*)grab_object( new_input
);
334 new_input
->cursor_count
+= queue
->cursor_count
;
338 /* set the cursor position and queue the corresponding mouse message */
339 static void set_cursor_pos( struct desktop
*desktop
, int x
, int y
)
341 struct hardware_msg_data
*msg_data
;
344 if (!(msg
= mem_alloc( sizeof(*msg
) ))) return;
345 if (!(msg_data
= mem_alloc( sizeof(*msg_data
) )))
350 memset( msg_data
, 0, sizeof(*msg_data
) );
352 msg
->type
= MSG_HARDWARE
;
354 msg
->msg
= WM_MOUSEMOVE
;
359 msg
->time
= get_tick_count();
361 msg
->data
= msg_data
;
362 msg
->data_size
= sizeof(*msg_data
);
363 queue_hardware_message( desktop
, msg
, 1 );
366 /* retrieve default position and time for synthesized messages */
367 static void get_message_defaults( struct msg_queue
*queue
, int *x
, int *y
, unsigned int *time
)
369 struct desktop
*desktop
= queue
->input
->desktop
;
371 *x
= desktop
->cursor
.x
;
372 *y
= desktop
->cursor
.y
;
373 *time
= get_tick_count();
376 /* set the cursor clip rectangle */
377 static void set_clip_rectangle( struct desktop
*desktop
, const rectangle_t
*rect
)
379 rectangle_t top_rect
;
382 get_top_window_rectangle( desktop
, &top_rect
);
385 rectangle_t new_rect
= *rect
;
386 if (new_rect
.left
< top_rect
.left
) new_rect
.left
= top_rect
.left
;
387 if (new_rect
.right
> top_rect
.right
) new_rect
.right
= top_rect
.right
;
388 if (new_rect
.top
< top_rect
.top
) new_rect
.top
= top_rect
.top
;
389 if (new_rect
.bottom
> top_rect
.bottom
) new_rect
.bottom
= top_rect
.bottom
;
390 if (new_rect
.left
> new_rect
.right
|| new_rect
.top
> new_rect
.bottom
) new_rect
= top_rect
;
391 desktop
->cursor
.clip
= new_rect
;
393 else desktop
->cursor
.clip
= top_rect
;
395 if (desktop
->cursor
.clip_msg
)
396 post_desktop_message( desktop
, desktop
->cursor
.clip_msg
, rect
!= NULL
, 0 );
398 /* warp the mouse to be inside the clip rect */
399 x
= min( max( desktop
->cursor
.x
, desktop
->cursor
.clip
.left
), desktop
->cursor
.clip
.right
-1 );
400 y
= min( max( desktop
->cursor
.y
, desktop
->cursor
.clip
.top
), desktop
->cursor
.clip
.bottom
-1 );
401 if (x
!= desktop
->cursor
.x
|| y
!= desktop
->cursor
.y
) set_cursor_pos( desktop
, x
, y
);
404 /* change the foreground input and reset the cursor clip rect */
405 static void set_foreground_input( struct desktop
*desktop
, struct thread_input
*input
)
407 if (desktop
->foreground_input
== input
) return;
408 set_clip_rectangle( desktop
, NULL
);
409 desktop
->foreground_input
= input
;
412 /* get the hook table for a given thread */
413 struct hook_table
*get_queue_hooks( struct thread
*thread
)
415 if (!thread
->queue
) return NULL
;
416 return thread
->queue
->hooks
;
419 /* set the hook table for a given thread, allocating the queue if needed */
420 void set_queue_hooks( struct thread
*thread
, struct hook_table
*hooks
)
422 struct msg_queue
*queue
= thread
->queue
;
423 if (!queue
&& !(queue
= create_msg_queue( thread
, NULL
))) return;
424 if (queue
->hooks
) release_object( queue
->hooks
);
425 queue
->hooks
= hooks
;
428 /* check the queue status */
429 static inline int is_signaled( struct msg_queue
*queue
)
431 return ((queue
->wake_bits
& queue
->wake_mask
) || (queue
->changed_bits
& queue
->changed_mask
));
434 /* set some queue bits */
435 static inline void set_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
437 queue
->wake_bits
|= bits
;
438 queue
->changed_bits
|= bits
;
439 if (is_signaled( queue
)) wake_up( &queue
->obj
, 0 );
442 /* clear some queue bits */
443 static inline void clear_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
445 queue
->wake_bits
&= ~bits
;
446 queue
->changed_bits
&= ~bits
;
449 /* check whether msg is a keyboard message */
450 static inline int is_keyboard_msg( struct message
*msg
)
452 return (msg
->msg
>= WM_KEYFIRST
&& msg
->msg
<= WM_KEYLAST
);
455 /* check if message is matched by the filter */
456 static inline int check_msg_filter( unsigned int msg
, unsigned int first
, unsigned int last
)
458 return (msg
>= first
&& msg
<= last
);
461 /* check whether a message filter contains at least one potential hardware message */
462 static inline int filter_contains_hw_range( unsigned int first
, unsigned int last
)
464 /* hardware message ranges are (in numerical order):
465 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
466 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
467 * WM_MOUSEFIRST .. WM_MOUSELAST
469 if (last
< WM_NCMOUSEFIRST
) return 0;
470 if (first
> WM_NCMOUSELAST
&& last
< WM_INPUT_DEVICE_CHANGE
) return 0;
471 if (first
> WM_KEYLAST
&& last
< WM_MOUSEFIRST
) return 0;
472 if (first
> WM_MOUSELAST
) return 0;
476 /* get the QS_* bit corresponding to a given hardware message */
477 static inline int get_hardware_msg_bit( struct message
*msg
)
479 if (msg
->msg
== WM_INPUT_DEVICE_CHANGE
|| msg
->msg
== WM_INPUT
) return QS_RAWINPUT
;
480 if (msg
->msg
== WM_MOUSEMOVE
|| msg
->msg
== WM_NCMOUSEMOVE
) return QS_MOUSEMOVE
;
481 if (is_keyboard_msg( msg
)) return QS_KEY
;
482 return QS_MOUSEBUTTON
;
485 /* get the current thread queue, creating it if needed */
486 static inline struct msg_queue
*get_current_queue(void)
488 struct msg_queue
*queue
= current
->queue
;
489 if (!queue
) queue
= create_msg_queue( current
, NULL
);
493 /* get a (pseudo-)unique id to tag hardware messages */
494 static inline unsigned int get_unique_id(void)
496 static unsigned int id
;
497 if (!++id
) id
= 1; /* avoid an id of 0 */
501 /* try to merge a message with the last in the list; return 1 if successful */
502 static int merge_message( struct thread_input
*input
, const struct message
*msg
)
504 struct message
*prev
;
507 if (msg
->msg
!= WM_MOUSEMOVE
) return 0;
508 for (ptr
= list_tail( &input
->msg_list
); ptr
; ptr
= list_prev( &input
->msg_list
, ptr
))
510 prev
= LIST_ENTRY( ptr
, struct message
, entry
);
511 if (prev
->msg
!= WM_INPUT
) break;
514 if (prev
->result
) return 0;
515 if (prev
->win
&& msg
->win
&& prev
->win
!= msg
->win
) return 0;
516 if (prev
->msg
!= msg
->msg
) return 0;
517 if (prev
->type
!= msg
->type
) return 0;
518 /* now we can merge it */
519 prev
->wparam
= msg
->wparam
;
520 prev
->lparam
= msg
->lparam
;
523 prev
->time
= msg
->time
;
524 if (msg
->type
== MSG_HARDWARE
&& prev
->data
&& msg
->data
)
526 struct hardware_msg_data
*prev_data
= prev
->data
;
527 struct hardware_msg_data
*msg_data
= msg
->data
;
528 prev_data
->info
= msg_data
->info
;
531 list_add_tail( &input
->msg_list
, ptr
);
535 /* free a result structure */
536 static void free_result( struct message_result
*result
)
538 if (result
->timeout
) remove_timeout_user( result
->timeout
);
539 free( result
->data
);
540 if (result
->callback_msg
) free_message( result
->callback_msg
);
541 if (result
->hardware_msg
) free_message( result
->hardware_msg
);
542 if (result
->desktop
) release_object( result
->desktop
);
546 /* remove the result from the sender list it is on */
547 static inline void remove_result_from_sender( struct message_result
*result
)
549 assert( result
->sender
);
551 list_remove( &result
->sender_entry
);
552 result
->sender
= NULL
;
553 if (!result
->receiver
) free_result( result
);
556 /* store the message result in the appropriate structure */
557 static void store_message_result( struct message_result
*res
, lparam_t result
, unsigned int error
)
559 res
->result
= result
;
564 remove_timeout_user( res
->timeout
);
568 if (res
->hardware_msg
)
570 if (!error
&& result
) /* rejected by the hook */
571 free_message( res
->hardware_msg
);
573 queue_hardware_message( res
->desktop
, res
->hardware_msg
, 0 );
575 res
->hardware_msg
= NULL
;
580 if (res
->callback_msg
)
582 /* queue the callback message in the sender queue */
583 struct callback_msg_data
*data
= res
->callback_msg
->data
;
584 data
->result
= result
;
585 list_add_tail( &res
->sender
->msg_list
[SEND_MESSAGE
], &res
->callback_msg
->entry
);
586 set_queue_bits( res
->sender
, QS_SENDMESSAGE
);
587 res
->callback_msg
= NULL
;
588 remove_result_from_sender( res
);
592 /* wake sender queue if waiting on this result */
593 if (list_head(&res
->sender
->send_result
) == &res
->sender_entry
)
594 set_queue_bits( res
->sender
, QS_SMRESULT
);
597 else if (!res
->receiver
) free_result( res
);
600 /* free a message when deleting a queue or window */
601 static void free_message( struct message
*msg
)
603 struct message_result
*result
= msg
->result
;
607 result
->receiver
= NULL
;
608 store_message_result( result
, 0, STATUS_ACCESS_DENIED
/*FIXME*/ );
614 /* remove (and free) a message from a message list */
615 static void remove_queue_message( struct msg_queue
*queue
, struct message
*msg
,
616 enum message_kind kind
)
618 list_remove( &msg
->entry
);
622 if (list_empty( &queue
->msg_list
[kind
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
625 if (list_empty( &queue
->msg_list
[kind
] ) && !queue
->quit_message
)
626 clear_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
627 if (msg
->msg
== WM_HOTKEY
&& --queue
->hotkey_count
== 0)
628 clear_queue_bits( queue
, QS_HOTKEY
);
634 /* message timed out without getting a reply */
635 static void result_timeout( void *private )
637 struct message_result
*result
= private;
639 assert( !result
->replied
);
641 result
->timeout
= NULL
;
643 if (result
->msg
) /* not received yet */
645 struct message
*msg
= result
->msg
;
649 remove_queue_message( result
->receiver
, msg
, SEND_MESSAGE
);
650 result
->receiver
= NULL
;
652 store_message_result( result
, 0, STATUS_TIMEOUT
);
655 /* allocate and fill a message result structure */
656 static struct message_result
*alloc_message_result( struct msg_queue
*send_queue
,
657 struct msg_queue
*recv_queue
,
658 struct message
*msg
, timeout_t timeout
)
660 struct message_result
*result
= mem_alloc( sizeof(*result
) );
664 result
->sender
= send_queue
;
665 result
->receiver
= recv_queue
;
668 result
->data_size
= 0;
669 result
->timeout
= NULL
;
670 result
->hardware_msg
= NULL
;
671 result
->desktop
= NULL
;
672 result
->callback_msg
= NULL
;
674 if (msg
->type
== MSG_CALLBACK
)
676 struct message
*callback_msg
= mem_alloc( sizeof(*callback_msg
) );
683 callback_msg
->type
= MSG_CALLBACK_RESULT
;
684 callback_msg
->win
= msg
->win
;
685 callback_msg
->msg
= msg
->msg
;
686 callback_msg
->wparam
= 0;
687 callback_msg
->lparam
= 0;
688 callback_msg
->time
= get_tick_count();
689 callback_msg
->result
= NULL
;
690 /* steal the data from the original message */
691 callback_msg
->data
= msg
->data
;
692 callback_msg
->data_size
= msg
->data_size
;
696 result
->callback_msg
= callback_msg
;
697 list_add_head( &send_queue
->callback_result
, &result
->sender_entry
);
701 list_add_head( &send_queue
->send_result
, &result
->sender_entry
);
702 clear_queue_bits( send_queue
, QS_SMRESULT
);
705 if (timeout
!= TIMEOUT_INFINITE
)
706 result
->timeout
= add_timeout_user( timeout
, result_timeout
, result
);
711 /* receive a message, removing it from the sent queue */
712 static void receive_message( struct msg_queue
*queue
, struct message
*msg
,
713 struct get_message_reply
*reply
)
715 struct message_result
*result
= msg
->result
;
717 reply
->total
= msg
->data_size
;
718 if (msg
->data_size
> get_reply_max_size())
720 set_error( STATUS_BUFFER_OVERFLOW
);
723 reply
->type
= msg
->type
;
724 reply
->win
= msg
->win
;
725 reply
->msg
= msg
->msg
;
726 reply
->wparam
= msg
->wparam
;
727 reply
->lparam
= msg
->lparam
;
730 reply
->time
= msg
->time
;
732 if (msg
->data
) set_reply_data_ptr( msg
->data
, msg
->data_size
);
734 list_remove( &msg
->entry
);
735 /* put the result on the receiver result stack */
739 result
->recv_next
= queue
->recv_result
;
740 queue
->recv_result
= result
;
743 if (list_empty( &queue
->msg_list
[SEND_MESSAGE
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
746 /* set the result of the current received message */
747 static void reply_message( struct msg_queue
*queue
, lparam_t result
,
748 unsigned int error
, int remove
, const void *data
, data_size_t len
)
750 struct message_result
*res
= queue
->recv_result
;
754 queue
->recv_result
= res
->recv_next
;
755 res
->receiver
= NULL
;
756 if (!res
->sender
&& !res
->hardware_msg
) /* no one waiting for it */
764 if (len
&& (res
->data
= memdup( data
, len
))) res
->data_size
= len
;
765 store_message_result( res
, result
, error
);
769 static int match_window( user_handle_t win
, user_handle_t msg_win
)
772 if (win
== -1 || win
== 1) return !msg_win
;
773 if (msg_win
== win
) return 1;
774 return is_child_window( win
, msg_win
);
777 /* retrieve a posted message */
778 static int get_posted_message( struct msg_queue
*queue
, user_handle_t win
,
779 unsigned int first
, unsigned int last
, unsigned int flags
,
780 struct get_message_reply
*reply
)
784 /* check against the filters */
785 LIST_FOR_EACH_ENTRY( msg
, &queue
->msg_list
[POST_MESSAGE
], struct message
, entry
)
787 if (!match_window( win
, msg
->win
)) continue;
788 if (!check_msg_filter( msg
->msg
, first
, last
)) continue;
789 goto found
; /* found one */
793 /* return it to the app */
795 reply
->total
= msg
->data_size
;
796 if (msg
->data_size
> get_reply_max_size())
798 set_error( STATUS_BUFFER_OVERFLOW
);
801 reply
->type
= msg
->type
;
802 reply
->win
= msg
->win
;
803 reply
->msg
= msg
->msg
;
804 reply
->wparam
= msg
->wparam
;
805 reply
->lparam
= msg
->lparam
;
808 reply
->time
= msg
->time
;
810 if (flags
& PM_REMOVE
)
814 set_reply_data_ptr( msg
->data
, msg
->data_size
);
818 remove_queue_message( queue
, msg
, POST_MESSAGE
);
820 else if (msg
->data
) set_reply_data( msg
->data
, msg
->data_size
);
825 static int get_quit_message( struct msg_queue
*queue
, unsigned int flags
,
826 struct get_message_reply
*reply
)
828 if (queue
->quit_message
)
831 reply
->type
= MSG_POSTED
;
833 reply
->msg
= WM_QUIT
;
834 reply
->wparam
= queue
->exit_code
;
837 get_message_defaults( queue
, &reply
->x
, &reply
->y
, &reply
->time
);
839 if (flags
& PM_REMOVE
)
841 queue
->quit_message
= 0;
842 if (list_empty( &queue
->msg_list
[POST_MESSAGE
] ))
843 clear_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
851 /* empty a message list and free all the messages */
852 static void empty_msg_list( struct list
*list
)
856 while ((ptr
= list_head( list
)) != NULL
)
858 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
859 list_remove( &msg
->entry
);
864 /* cleanup all pending results when deleting a queue */
865 static void cleanup_results( struct msg_queue
*queue
)
869 while ((entry
= list_head( &queue
->send_result
)) != NULL
)
871 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
874 while ((entry
= list_head( &queue
->callback_result
)) != NULL
)
876 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
879 while (queue
->recv_result
)
880 reply_message( queue
, 0, STATUS_ACCESS_DENIED
/*FIXME*/, 1, NULL
, 0 );
883 /* check if the thread owning the queue is hung (not checking for messages) */
884 static int is_queue_hung( struct msg_queue
*queue
)
886 struct wait_queue_entry
*entry
;
888 if (current_time
- queue
->last_get_msg
<= 5 * TICKS_PER_SEC
)
889 return 0; /* less than 5 seconds since last get message -> not hung */
891 LIST_FOR_EACH_ENTRY( entry
, &queue
->obj
.wait_queue
, struct wait_queue_entry
, entry
)
893 if (get_wait_queue_thread(entry
)->queue
== queue
)
894 return 0; /* thread is waiting on queue -> not hung */
899 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
901 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
902 struct process
*process
= get_wait_queue_thread(entry
)->process
;
904 /* a thread can only wait on its own queue */
905 if (get_wait_queue_thread(entry
)->queue
!= queue
)
907 set_error( STATUS_ACCESS_DENIED
);
910 if (process
->idle_event
&& !(queue
->wake_mask
& QS_SMRESULT
)) set_event( process
->idle_event
);
912 if (queue
->fd
&& list_empty( &obj
->wait_queue
)) /* first on the queue */
913 set_fd_events( queue
->fd
, POLLIN
);
914 add_queue( obj
, entry
);
918 static void msg_queue_remove_queue(struct object
*obj
, struct wait_queue_entry
*entry
)
920 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
922 remove_queue( obj
, entry
);
923 if (queue
->fd
&& list_empty( &obj
->wait_queue
)) /* last on the queue is gone */
924 set_fd_events( queue
->fd
, 0 );
927 static void msg_queue_dump( struct object
*obj
, int verbose
)
929 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
930 fprintf( stderr
, "Msg queue bits=%x mask=%x\n",
931 queue
->wake_bits
, queue
->wake_mask
);
934 static int msg_queue_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
936 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
941 if ((ret
= check_fd_events( queue
->fd
, POLLIN
)))
942 /* stop waiting on select() if we are signaled */
943 set_fd_events( queue
->fd
, 0 );
944 else if (!list_empty( &obj
->wait_queue
))
945 /* restart waiting on poll() if we are no longer signaled */
946 set_fd_events( queue
->fd
, POLLIN
);
948 return ret
|| is_signaled( queue
);
951 static void msg_queue_satisfied( struct object
*obj
, struct wait_queue_entry
*entry
)
953 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
954 queue
->wake_mask
= 0;
955 queue
->changed_mask
= 0;
958 static void msg_queue_destroy( struct object
*obj
)
960 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
962 struct hotkey
*hotkey
, *hotkey2
;
965 cleanup_results( queue
);
966 for (i
= 0; i
< NB_MSG_KINDS
; i
++) empty_msg_list( &queue
->msg_list
[i
] );
968 LIST_FOR_EACH_ENTRY_SAFE( hotkey
, hotkey2
, &queue
->input
->desktop
->hotkeys
, struct hotkey
, entry
)
970 if (hotkey
->queue
== queue
)
972 list_remove( &hotkey
->entry
);
977 while ((ptr
= list_head( &queue
->pending_timers
)))
979 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
980 list_remove( &timer
->entry
);
983 while ((ptr
= list_head( &queue
->expired_timers
)))
985 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
986 list_remove( &timer
->entry
);
989 if (queue
->timeout
) remove_timeout_user( queue
->timeout
);
990 queue
->input
->cursor_count
-= queue
->cursor_count
;
991 release_object( queue
->input
);
992 if (queue
->hooks
) release_object( queue
->hooks
);
993 if (queue
->fd
) release_object( queue
->fd
);
996 static void msg_queue_poll_event( struct fd
*fd
, int event
)
998 struct msg_queue
*queue
= get_fd_user( fd
);
999 assert( queue
->obj
.ops
== &msg_queue_ops
);
1001 if (event
& (POLLERR
| POLLHUP
)) set_fd_events( fd
, -1 );
1002 else set_fd_events( queue
->fd
, 0 );
1003 wake_up( &queue
->obj
, 0 );
1006 static void thread_input_dump( struct object
*obj
, int verbose
)
1008 struct thread_input
*input
= (struct thread_input
*)obj
;
1009 fprintf( stderr
, "Thread input focus=%08x capture=%08x active=%08x\n",
1010 input
->focus
, input
->capture
, input
->active
);
1013 static void thread_input_destroy( struct object
*obj
)
1015 struct thread_input
*input
= (struct thread_input
*)obj
;
1017 empty_msg_list( &input
->msg_list
);
1020 if (input
->desktop
->foreground_input
== input
) set_foreground_input( input
->desktop
, NULL
);
1021 release_object( input
->desktop
);
1025 /* fix the thread input data when a window is destroyed */
1026 static inline void thread_input_cleanup_window( struct msg_queue
*queue
, user_handle_t window
)
1028 struct thread_input
*input
= queue
->input
;
1030 if (window
== input
->focus
) input
->focus
= 0;
1031 if (window
== input
->capture
) input
->capture
= 0;
1032 if (window
== input
->active
) input
->active
= 0;
1033 if (window
== input
->menu_owner
) input
->menu_owner
= 0;
1034 if (window
== input
->move_size
) input
->move_size
= 0;
1035 if (window
== input
->caret
) set_caret_window( input
, 0 );
1038 /* check if the specified window can be set in the input data of a given queue */
1039 static int check_queue_input_window( struct msg_queue
*queue
, user_handle_t window
)
1041 struct thread
*thread
;
1044 if (!window
) return 1; /* we can always clear the data */
1046 if ((thread
= get_window_thread( window
)))
1048 ret
= (queue
->input
== thread
->queue
->input
);
1049 if (!ret
) set_error( STATUS_ACCESS_DENIED
);
1050 release_object( thread
);
1052 else set_error( STATUS_INVALID_HANDLE
);
1057 /* make sure the specified thread has a queue */
1058 int init_thread_queue( struct thread
*thread
)
1060 if (thread
->queue
) return 1;
1061 return (create_msg_queue( thread
, NULL
) != NULL
);
1064 /* attach two thread input data structures */
1065 int attach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
1067 struct desktop
*desktop
;
1068 struct thread_input
*input
;
1071 if (!thread_to
->queue
&& !(thread_to
->queue
= create_msg_queue( thread_to
, NULL
))) return 0;
1072 if (!(desktop
= get_thread_desktop( thread_from
, 0 ))) return 0;
1073 input
= (struct thread_input
*)grab_object( thread_to
->queue
->input
);
1074 if (input
->desktop
!= desktop
)
1076 set_error( STATUS_ACCESS_DENIED
);
1077 release_object( input
);
1078 release_object( desktop
);
1081 release_object( desktop
);
1083 if (thread_from
->queue
)
1085 if (!input
->focus
) input
->focus
= thread_from
->queue
->input
->focus
;
1086 if (!input
->active
) input
->active
= thread_from
->queue
->input
->active
;
1089 ret
= assign_thread_input( thread_from
, input
);
1090 if (ret
) memset( input
->keystate
, 0, sizeof(input
->keystate
) );
1091 release_object( input
);
1095 /* detach two thread input data structures */
1096 void detach_thread_input( struct thread
*thread_from
)
1098 struct thread
*thread
;
1099 struct thread_input
*input
, *old_input
= thread_from
->queue
->input
;
1101 if ((input
= create_thread_input( thread_from
)))
1103 if (old_input
->focus
&& (thread
= get_window_thread( old_input
->focus
)))
1105 if (thread
== thread_from
)
1107 input
->focus
= old_input
->focus
;
1108 old_input
->focus
= 0;
1110 release_object( thread
);
1112 if (old_input
->active
&& (thread
= get_window_thread( old_input
->active
)))
1114 if (thread
== thread_from
)
1116 input
->active
= old_input
->active
;
1117 old_input
->active
= 0;
1119 release_object( thread
);
1121 assign_thread_input( thread_from
, input
);
1122 release_object( input
);
1127 /* set the next timer to expire */
1128 static void set_next_timer( struct msg_queue
*queue
)
1134 remove_timeout_user( queue
->timeout
);
1135 queue
->timeout
= NULL
;
1137 if ((ptr
= list_head( &queue
->pending_timers
)))
1139 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1140 queue
->timeout
= add_timeout_user( timer
->when
, timer_callback
, queue
);
1142 /* set/clear QS_TIMER bit */
1143 if (list_empty( &queue
->expired_timers
))
1144 clear_queue_bits( queue
, QS_TIMER
);
1146 set_queue_bits( queue
, QS_TIMER
);
1149 /* find a timer from its window and id */
1150 static struct timer
*find_timer( struct msg_queue
*queue
, user_handle_t win
,
1151 unsigned int msg
, lparam_t id
)
1155 /* we need to search both lists */
1157 LIST_FOR_EACH( ptr
, &queue
->pending_timers
)
1159 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1160 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
1162 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
1164 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1165 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
1170 /* callback for the next timer expiration */
1171 static void timer_callback( void *private )
1173 struct msg_queue
*queue
= private;
1176 queue
->timeout
= NULL
;
1177 /* move on to the next timer */
1178 ptr
= list_head( &queue
->pending_timers
);
1180 list_add_tail( &queue
->expired_timers
, ptr
);
1181 set_next_timer( queue
);
1184 /* link a timer at its rightful place in the queue list */
1185 static void link_timer( struct msg_queue
*queue
, struct timer
*timer
)
1189 for (ptr
= queue
->pending_timers
.next
; ptr
!= &queue
->pending_timers
; ptr
= ptr
->next
)
1191 struct timer
*t
= LIST_ENTRY( ptr
, struct timer
, entry
);
1192 if (t
->when
>= timer
->when
) break;
1194 list_add_before( ptr
, &timer
->entry
);
1197 /* remove a timer from the queue timer list and free it */
1198 static void free_timer( struct msg_queue
*queue
, struct timer
*timer
)
1200 list_remove( &timer
->entry
);
1202 set_next_timer( queue
);
1205 /* restart an expired timer */
1206 static void restart_timer( struct msg_queue
*queue
, struct timer
*timer
)
1208 list_remove( &timer
->entry
);
1209 while (timer
->when
<= current_time
) timer
->when
+= (timeout_t
)timer
->rate
* 10000;
1210 link_timer( queue
, timer
);
1211 set_next_timer( queue
);
1214 /* find an expired timer matching the filtering parameters */
1215 static struct timer
*find_expired_timer( struct msg_queue
*queue
, user_handle_t win
,
1216 unsigned int get_first
, unsigned int get_last
,
1221 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
1223 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1224 if (win
&& timer
->win
!= win
) continue;
1225 if (check_msg_filter( timer
->msg
, get_first
, get_last
))
1227 if (remove
) restart_timer( queue
, timer
);
1235 static struct timer
*set_timer( struct msg_queue
*queue
, unsigned int rate
)
1237 struct timer
*timer
= mem_alloc( sizeof(*timer
) );
1240 timer
->rate
= max( rate
, 1 );
1241 timer
->when
= current_time
+ (timeout_t
)timer
->rate
* 10000;
1242 link_timer( queue
, timer
);
1243 /* check if we replaced the next timer */
1244 if (list_head( &queue
->pending_timers
) == &timer
->entry
) set_next_timer( queue
);
1249 /* change the input key state for a given key */
1250 static void set_input_key_state( unsigned char *keystate
, unsigned char key
, int down
)
1254 if (!(keystate
[key
] & 0x80)) keystate
[key
] ^= 0x01;
1255 keystate
[key
] |= down
;
1257 else keystate
[key
] &= ~0x80;
1260 /* update the input key state for a keyboard message */
1261 static void update_input_key_state( struct desktop
*desktop
, unsigned char *keystate
,
1262 const struct message
*msg
)
1269 case WM_LBUTTONDOWN
:
1270 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1273 set_input_key_state( keystate
, VK_LBUTTON
, down
);
1275 case WM_MBUTTONDOWN
:
1276 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1279 set_input_key_state( keystate
, VK_MBUTTON
, down
);
1281 case WM_RBUTTONDOWN
:
1282 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1285 set_input_key_state( keystate
, VK_RBUTTON
, down
);
1287 case WM_XBUTTONDOWN
:
1288 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1291 if (msg
->wparam
>> 16 == XBUTTON1
) set_input_key_state( keystate
, VK_XBUTTON1
, down
);
1292 else if (msg
->wparam
>> 16 == XBUTTON2
) set_input_key_state( keystate
, VK_XBUTTON2
, down
);
1296 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1300 key
= (unsigned char)msg
->wparam
;
1301 set_input_key_state( keystate
, key
, down
);
1306 down
= (keystate
[VK_LCONTROL
] | keystate
[VK_RCONTROL
]) & 0x80;
1307 set_input_key_state( keystate
, VK_CONTROL
, down
);
1311 down
= (keystate
[VK_LMENU
] | keystate
[VK_RMENU
]) & 0x80;
1312 set_input_key_state( keystate
, VK_MENU
, down
);
1316 down
= (keystate
[VK_LSHIFT
] | keystate
[VK_RSHIFT
]) & 0x80;
1317 set_input_key_state( keystate
, VK_SHIFT
, down
);
1324 /* release the hardware message currently being processed by the given thread */
1325 static void release_hardware_message( struct msg_queue
*queue
, unsigned int hw_id
,
1328 struct thread_input
*input
= queue
->input
;
1329 struct message
*msg
;
1331 LIST_FOR_EACH_ENTRY( msg
, &input
->msg_list
, struct message
, entry
)
1333 if (msg
->unique_id
== hw_id
) break;
1335 if (&msg
->entry
== &input
->msg_list
) return; /* not found */
1337 /* clear the queue bit for that message */
1340 struct message
*other
;
1343 clr_bit
= get_hardware_msg_bit( msg
);
1344 LIST_FOR_EACH_ENTRY( other
, &input
->msg_list
, struct message
, entry
)
1346 if (other
!= msg
&& get_hardware_msg_bit( other
) == clr_bit
)
1352 if (clr_bit
) clear_queue_bits( queue
, clr_bit
);
1354 update_input_key_state( input
->desktop
, input
->keystate
, msg
);
1355 list_remove( &msg
->entry
);
1356 free_message( msg
);
1360 static int queue_hotkey_message( struct desktop
*desktop
, struct message
*msg
)
1362 struct hotkey
*hotkey
;
1363 unsigned int modifiers
= 0;
1365 if (msg
->msg
!= WM_KEYDOWN
) return 0;
1367 if (desktop
->keystate
[VK_MENU
] & 0x80) modifiers
|= MOD_ALT
;
1368 if (desktop
->keystate
[VK_CONTROL
] & 0x80) modifiers
|= MOD_CONTROL
;
1369 if (desktop
->keystate
[VK_SHIFT
] & 0x80) modifiers
|= MOD_SHIFT
;
1370 if ((desktop
->keystate
[VK_LWIN
] & 0x80) || (desktop
->keystate
[VK_RWIN
] & 0x80)) modifiers
|= MOD_WIN
;
1372 LIST_FOR_EACH_ENTRY( hotkey
, &desktop
->hotkeys
, struct hotkey
, entry
)
1374 if (hotkey
->vkey
!= msg
->wparam
) continue;
1375 if ((hotkey
->flags
& (MOD_ALT
|MOD_CONTROL
|MOD_SHIFT
|MOD_WIN
)) == modifiers
) goto found
;
1381 msg
->type
= MSG_POSTED
;
1382 msg
->win
= hotkey
->win
;
1383 msg
->msg
= WM_HOTKEY
;
1384 msg
->wparam
= hotkey
->id
;
1385 msg
->lparam
= ((hotkey
->vkey
& 0xffff) << 16) | modifiers
;
1391 list_add_tail( &hotkey
->queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1392 set_queue_bits( hotkey
->queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
|QS_HOTKEY
);
1393 hotkey
->queue
->hotkey_count
++;
1397 /* find the window that should receive a given hardware message */
1398 static user_handle_t
find_hardware_message_window( struct desktop
*desktop
, struct thread_input
*input
,
1399 struct message
*msg
, unsigned int *msg_code
,
1400 struct thread
**thread
)
1402 user_handle_t win
= 0;
1405 *msg_code
= msg
->msg
;
1406 if (msg
->msg
== WM_INPUT
)
1408 if (!(win
= msg
->win
) && input
) win
= input
->focus
;
1410 else if (is_keyboard_msg( msg
))
1412 if (input
&& !(win
= input
->focus
))
1414 win
= input
->active
;
1415 if (*msg_code
< WM_SYSKEYDOWN
) *msg_code
+= WM_SYSKEYDOWN
- WM_KEYDOWN
;
1418 else if (!input
|| !(win
= input
->capture
)) /* mouse message */
1420 if (is_window_visible( msg
->win
) && !is_window_transparent( msg
->win
)) win
= msg
->win
;
1421 else win
= shallow_window_from_point( desktop
, msg
->x
, msg
->y
);
1423 *thread
= window_thread_from_point( win
, msg
->x
, msg
->y
);
1427 *thread
= get_window_thread( win
);
1431 static struct rawinput_device_entry
*find_rawinput_device( unsigned short usage_page
, unsigned short usage
)
1433 struct rawinput_device_entry
*e
;
1435 LIST_FOR_EACH_ENTRY( e
, ¤t
->process
->rawinput_devices
, struct rawinput_device_entry
, entry
)
1437 if (e
->device
.usage_page
!= usage_page
|| e
->device
.usage
!= usage
) continue;
1444 static void update_rawinput_device(const struct rawinput_device
*device
)
1446 struct rawinput_device_entry
*e
;
1448 if (!(e
= find_rawinput_device( device
->usage_page
, device
->usage
)))
1450 if (!(e
= mem_alloc( sizeof(*e
) ))) return;
1451 list_add_tail( ¤t
->process
->rawinput_devices
, &e
->entry
);
1454 if (device
->flags
& RIDEV_REMOVE
)
1456 list_remove( &e
->entry
);
1461 e
->device
= *device
;
1462 e
->device
.target
= get_user_full_handle( e
->device
.target
);
1465 /* queue a hardware message into a given thread input */
1466 static void queue_hardware_message( struct desktop
*desktop
, struct message
*msg
, int always_queue
)
1469 struct thread
*thread
;
1470 struct thread_input
*input
;
1471 unsigned int msg_code
;
1473 update_input_key_state( desktop
, desktop
->keystate
, msg
);
1474 last_input_time
= get_tick_count();
1475 if (msg
->msg
!= WM_MOUSEMOVE
) always_queue
= 1;
1477 if (is_keyboard_msg( msg
))
1479 if (queue_hotkey_message( desktop
, msg
)) return;
1480 if (desktop
->keystate
[VK_MENU
] & 0x80) msg
->lparam
|= KF_ALTDOWN
<< 16;
1481 if (msg
->wparam
== VK_SHIFT
|| msg
->wparam
== VK_LSHIFT
|| msg
->wparam
== VK_RSHIFT
)
1482 msg
->lparam
&= ~(KF_EXTENDED
<< 16);
1484 else if (msg
->msg
!= WM_INPUT
)
1486 if (msg
->msg
== WM_MOUSEMOVE
)
1488 int x
= min( max( msg
->x
, desktop
->cursor
.clip
.left
), desktop
->cursor
.clip
.right
-1 );
1489 int y
= min( max( msg
->y
, desktop
->cursor
.clip
.top
), desktop
->cursor
.clip
.bottom
-1 );
1490 if (desktop
->cursor
.x
!= x
|| desktop
->cursor
.y
!= y
) always_queue
= 1;
1491 desktop
->cursor
.x
= x
;
1492 desktop
->cursor
.y
= y
;
1493 desktop
->cursor
.last_change
= get_tick_count();
1495 if (desktop
->keystate
[VK_LBUTTON
] & 0x80) msg
->wparam
|= MK_LBUTTON
;
1496 if (desktop
->keystate
[VK_MBUTTON
] & 0x80) msg
->wparam
|= MK_MBUTTON
;
1497 if (desktop
->keystate
[VK_RBUTTON
] & 0x80) msg
->wparam
|= MK_RBUTTON
;
1498 if (desktop
->keystate
[VK_SHIFT
] & 0x80) msg
->wparam
|= MK_SHIFT
;
1499 if (desktop
->keystate
[VK_CONTROL
] & 0x80) msg
->wparam
|= MK_CONTROL
;
1500 if (desktop
->keystate
[VK_XBUTTON1
] & 0x80) msg
->wparam
|= MK_XBUTTON1
;
1501 if (desktop
->keystate
[VK_XBUTTON2
] & 0x80) msg
->wparam
|= MK_XBUTTON2
;
1503 msg
->x
= desktop
->cursor
.x
;
1504 msg
->y
= desktop
->cursor
.y
;
1506 if (msg
->win
&& (thread
= get_window_thread( msg
->win
)))
1508 input
= thread
->queue
->input
;
1509 release_object( thread
);
1511 else input
= desktop
->foreground_input
;
1513 win
= find_hardware_message_window( desktop
, input
, msg
, &msg_code
, &thread
);
1514 if (!win
|| !thread
)
1516 if (input
) update_input_key_state( input
->desktop
, input
->keystate
, msg
);
1517 free_message( msg
);
1520 input
= thread
->queue
->input
;
1522 if (win
!= desktop
->cursor
.win
) always_queue
= 1;
1523 desktop
->cursor
.win
= win
;
1525 if (!always_queue
|| merge_message( input
, msg
)) free_message( msg
);
1528 msg
->unique_id
= 0; /* will be set once we return it to the app */
1529 list_add_tail( &input
->msg_list
, &msg
->entry
);
1530 set_queue_bits( thread
->queue
, get_hardware_msg_bit(msg
) );
1532 release_object( thread
);
1535 /* send the low-level hook message for a given hardware message */
1536 static int send_hook_ll_message( struct desktop
*desktop
, struct message
*hardware_msg
,
1537 const hw_input_t
*input
, struct msg_queue
*sender
)
1539 struct thread
*hook_thread
;
1540 struct msg_queue
*queue
;
1541 struct message
*msg
;
1542 timeout_t timeout
= 2000 * -10000; /* FIXME: load from registry */
1543 int id
= (input
->type
== INPUT_MOUSE
) ? WH_MOUSE_LL
: WH_KEYBOARD_LL
;
1545 if (!(hook_thread
= get_first_global_hook( id
))) return 0;
1546 if (!(queue
= hook_thread
->queue
)) return 0;
1547 if (is_queue_hung( queue
)) return 0;
1549 if (!(msg
= mem_alloc( sizeof(*msg
) ))) return 0;
1551 msg
->type
= MSG_HOOK_LL
;
1554 msg
->wparam
= hardware_msg
->msg
;
1555 msg
->x
= hardware_msg
->x
;
1556 msg
->y
= hardware_msg
->y
;
1557 msg
->time
= hardware_msg
->time
;
1558 msg
->data_size
= hardware_msg
->data_size
;
1561 if (input
->type
== INPUT_KEYBOARD
)
1563 unsigned short vkey
= input
->kbd
.vkey
;
1564 if (input
->kbd
.flags
& KEYEVENTF_UNICODE
) vkey
= VK_PACKET
;
1565 msg
->lparam
= (input
->kbd
.scan
<< 16) | vkey
;
1567 else msg
->lparam
= input
->mouse
.data
<< 16;
1569 if (!(msg
->data
= memdup( hardware_msg
->data
, hardware_msg
->data_size
)) ||
1570 !(msg
->result
= alloc_message_result( sender
, queue
, msg
, timeout
)))
1572 free_message( msg
);
1575 msg
->result
->hardware_msg
= hardware_msg
;
1576 msg
->result
->desktop
= (struct desktop
*)grab_object( desktop
);
1577 list_add_tail( &queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1578 set_queue_bits( queue
, QS_SENDMESSAGE
);
1582 /* queue a hardware message for a mouse event */
1583 static int queue_mouse_message( struct desktop
*desktop
, user_handle_t win
, const hw_input_t
*input
,
1584 unsigned int hook_flags
, struct msg_queue
*sender
)
1586 const struct rawinput_device
*device
;
1587 struct hardware_msg_data
*msg_data
;
1588 struct message
*msg
;
1589 unsigned int i
, time
, flags
;
1592 static const unsigned int messages
[] =
1594 WM_MOUSEMOVE
, /* 0x0001 = MOUSEEVENTF_MOVE */
1595 WM_LBUTTONDOWN
, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1596 WM_LBUTTONUP
, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1597 WM_RBUTTONDOWN
, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1598 WM_RBUTTONUP
, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1599 WM_MBUTTONDOWN
, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1600 WM_MBUTTONUP
, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1601 WM_XBUTTONDOWN
, /* 0x0080 = MOUSEEVENTF_XDOWN */
1602 WM_XBUTTONUP
, /* 0x0100 = MOUSEEVENTF_XUP */
1603 0, /* 0x0200 = unused */
1604 0, /* 0x0400 = unused */
1605 WM_MOUSEWHEEL
, /* 0x0800 = MOUSEEVENTF_WHEEL */
1606 WM_MOUSEHWHEEL
/* 0x1000 = MOUSEEVENTF_HWHEEL */
1609 desktop
->cursor
.last_change
= get_tick_count();
1610 flags
= input
->mouse
.flags
;
1611 time
= input
->mouse
.time
;
1612 if (!time
) time
= desktop
->cursor
.last_change
;
1614 if (flags
& MOUSEEVENTF_MOVE
)
1616 if (flags
& MOUSEEVENTF_ABSOLUTE
)
1620 if (flags
& ~(MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
) &&
1621 x
== desktop
->cursor
.x
&& y
== desktop
->cursor
.y
)
1622 flags
&= ~MOUSEEVENTF_MOVE
;
1626 x
= desktop
->cursor
.x
+ input
->mouse
.x
;
1627 y
= desktop
->cursor
.y
+ input
->mouse
.y
;
1632 x
= desktop
->cursor
.x
;
1633 y
= desktop
->cursor
.y
;
1636 if ((device
= current
->process
->rawinput_mouse
))
1638 if (!(msg
= mem_alloc( sizeof(*msg
) ))) return 0;
1639 if (!(msg_data
= mem_alloc( sizeof(*msg_data
) )))
1645 msg
->type
= MSG_HARDWARE
;
1646 msg
->win
= device
->target
;
1647 msg
->msg
= WM_INPUT
;
1648 msg
->wparam
= RIM_INPUT
;
1651 msg
->data
= msg_data
;
1652 msg
->data_size
= sizeof(*msg_data
);
1655 msg_data
->info
= input
->mouse
.info
;
1656 msg_data
->flags
= flags
;
1657 msg_data
->rawinput
.type
= RIM_TYPEMOUSE
;
1658 msg_data
->rawinput
.mouse
.x
= x
- desktop
->cursor
.x
;
1659 msg_data
->rawinput
.mouse
.y
= y
- desktop
->cursor
.y
;
1660 msg_data
->rawinput
.mouse
.data
= input
->mouse
.data
;
1662 queue_hardware_message( desktop
, msg
, 0 );
1665 for (i
= 0; i
< sizeof(messages
)/sizeof(messages
[0]); i
++)
1667 if (!messages
[i
]) continue;
1668 if (!(flags
& (1 << i
))) continue;
1671 if (!(msg
= mem_alloc( sizeof(*msg
) ))) return 0;
1672 if (!(msg_data
= mem_alloc( sizeof(*msg_data
) )))
1677 memset( msg_data
, 0, sizeof(*msg_data
) );
1679 msg
->type
= MSG_HARDWARE
;
1680 msg
->win
= get_user_full_handle( win
);
1681 msg
->msg
= messages
[i
];
1682 msg
->wparam
= input
->mouse
.data
<< 16;
1688 msg
->data
= msg_data
;
1689 msg
->data_size
= sizeof(*msg_data
);
1690 msg_data
->info
= input
->mouse
.info
;
1691 if (hook_flags
& SEND_HWMSG_INJECTED
) msg_data
->flags
= LLMHF_INJECTED
;
1693 /* specify a sender only when sending the last message */
1694 if (!(flags
& ((1 << sizeof(messages
)/sizeof(messages
[0])) - 1)))
1696 if (!(wait
= send_hook_ll_message( desktop
, msg
, input
, sender
)))
1697 queue_hardware_message( desktop
, msg
, 0 );
1699 else if (!send_hook_ll_message( desktop
, msg
, input
, NULL
))
1700 queue_hardware_message( desktop
, msg
, 0 );
1705 /* queue a hardware message for a keyboard event */
1706 static int queue_keyboard_message( struct desktop
*desktop
, user_handle_t win
, const hw_input_t
*input
,
1707 unsigned int hook_flags
, struct msg_queue
*sender
)
1709 const struct rawinput_device
*device
;
1710 struct hardware_msg_data
*msg_data
;
1711 struct message
*msg
;
1712 unsigned char vkey
= input
->kbd
.vkey
;
1713 unsigned int message_code
, time
;
1716 if (!(time
= input
->kbd
.time
)) time
= get_tick_count();
1718 if (!(input
->kbd
.flags
& KEYEVENTF_UNICODE
))
1725 vkey
= (input
->kbd
.flags
& KEYEVENTF_EXTENDEDKEY
) ? VK_RMENU
: VK_LMENU
;
1730 vkey
= (input
->kbd
.flags
& KEYEVENTF_EXTENDEDKEY
) ? VK_RCONTROL
: VK_LCONTROL
;
1735 vkey
= (input
->kbd
.flags
& KEYEVENTF_EXTENDEDKEY
) ? VK_RSHIFT
: VK_LSHIFT
;
1740 message_code
= (input
->kbd
.flags
& KEYEVENTF_KEYUP
) ? WM_KEYUP
: WM_KEYDOWN
;
1745 if (input
->kbd
.flags
& KEYEVENTF_KEYUP
)
1747 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1748 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1749 if ((desktop
->keystate
[VK_MENU
] & 0x82) != 0x82) break;
1750 message_code
= WM_SYSKEYUP
;
1751 desktop
->keystate
[VK_MENU
] &= ~0x02;
1755 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1756 if (desktop
->keystate
[VK_CONTROL
] & 0x80) break;
1757 message_code
= WM_SYSKEYDOWN
;
1758 desktop
->keystate
[VK_MENU
] |= 0x02;
1764 /* send WM_SYSKEYUP on release if Alt still pressed */
1765 if (!(input
->kbd
.flags
& KEYEVENTF_KEYUP
)) break;
1766 if (!(desktop
->keystate
[VK_MENU
] & 0x80)) break;
1767 message_code
= WM_SYSKEYUP
;
1768 desktop
->keystate
[VK_MENU
] &= ~0x02;
1772 /* send WM_SYSKEY for Alt-anykey and for F10 */
1773 if (desktop
->keystate
[VK_CONTROL
] & 0x80) break;
1774 if (!(desktop
->keystate
[VK_MENU
] & 0x80)) break;
1777 message_code
= (input
->kbd
.flags
& KEYEVENTF_KEYUP
) ? WM_SYSKEYUP
: WM_SYSKEYDOWN
;
1778 desktop
->keystate
[VK_MENU
] &= ~0x02;
1782 if ((device
= current
->process
->rawinput_kbd
))
1784 if (!(msg
= mem_alloc( sizeof(*msg
) ))) return 0;
1785 if (!(msg_data
= mem_alloc( sizeof(*msg_data
) )))
1791 msg
->type
= MSG_HARDWARE
;
1792 msg
->win
= device
->target
;
1793 msg
->msg
= WM_INPUT
;
1794 msg
->wparam
= RIM_INPUT
;
1797 msg
->data
= msg_data
;
1798 msg
->data_size
= sizeof(*msg_data
);
1801 msg_data
->info
= input
->kbd
.info
;
1802 msg_data
->flags
= input
->kbd
.flags
;
1803 msg_data
->rawinput
.type
= RIM_TYPEKEYBOARD
;
1804 msg_data
->rawinput
.kbd
.message
= message_code
;
1805 msg_data
->rawinput
.kbd
.vkey
= vkey
;
1806 msg_data
->rawinput
.kbd
.scan
= input
->kbd
.scan
;
1808 queue_hardware_message( desktop
, msg
, 0 );
1811 if (!(msg
= mem_alloc( sizeof(*msg
) ))) return 0;
1812 if (!(msg_data
= mem_alloc( sizeof(*msg_data
) )))
1817 memset( msg_data
, 0, sizeof(*msg_data
) );
1819 msg
->type
= MSG_HARDWARE
;
1820 msg
->win
= get_user_full_handle( win
);
1821 msg
->msg
= message_code
;
1822 msg
->lparam
= (input
->kbd
.scan
<< 16) | 1u; /* repeat count */
1825 msg
->data
= msg_data
;
1826 msg
->data_size
= sizeof(*msg_data
);
1827 msg_data
->info
= input
->kbd
.info
;
1828 if (hook_flags
& SEND_HWMSG_INJECTED
) msg_data
->flags
= LLKHF_INJECTED
;
1830 if (input
->kbd
.flags
& KEYEVENTF_UNICODE
)
1832 msg
->wparam
= VK_PACKET
;
1836 unsigned int flags
= 0;
1837 if (input
->kbd
.flags
& KEYEVENTF_EXTENDEDKEY
) flags
|= KF_EXTENDED
;
1838 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1839 if (input
->kbd
.flags
& KEYEVENTF_KEYUP
) flags
|= KF_REPEAT
| KF_UP
;
1840 else if (desktop
->keystate
[vkey
] & 0x80) flags
|= KF_REPEAT
;
1843 msg
->lparam
|= flags
<< 16;
1844 msg_data
->flags
|= (flags
& (KF_EXTENDED
| KF_ALTDOWN
| KF_UP
)) >> 8;
1847 if (!(wait
= send_hook_ll_message( desktop
, msg
, input
, sender
)))
1848 queue_hardware_message( desktop
, msg
, 1 );
1853 /* queue a hardware message for a custom type of event */
1854 static void queue_custom_hardware_message( struct desktop
*desktop
, user_handle_t win
,
1855 const hw_input_t
*input
)
1857 struct hardware_msg_data
*msg_data
;
1858 struct message
*msg
;
1860 if (!(msg
= mem_alloc( sizeof(*msg
) ))) return;
1861 if (!(msg_data
= mem_alloc( sizeof(*msg_data
) )))
1866 memset( msg_data
, 0, sizeof(*msg_data
) );
1868 msg
->type
= MSG_HARDWARE
;
1869 msg
->win
= get_user_full_handle( win
);
1870 msg
->msg
= input
->hw
.msg
;
1872 msg
->lparam
= input
->hw
.lparam
;
1873 msg
->x
= desktop
->cursor
.x
;
1874 msg
->y
= desktop
->cursor
.y
;
1875 msg
->time
= get_tick_count();
1877 msg
->data
= msg_data
;
1878 msg
->data_size
= sizeof(*msg_data
);
1880 queue_hardware_message( desktop
, msg
, 1 );
1883 /* check message filter for a hardware message */
1884 static int check_hw_message_filter( user_handle_t win
, unsigned int msg_code
,
1885 user_handle_t filter_win
, unsigned int first
, unsigned int last
)
1887 if (msg_code
>= WM_KEYFIRST
&& msg_code
<= WM_KEYLAST
)
1889 /* we can only test the window for a keyboard message since the
1890 * dest window for a mouse message depends on hittest */
1891 if (filter_win
&& win
!= filter_win
&& !is_child_window( filter_win
, win
))
1893 /* the message code is final for a keyboard message, we can simply check it */
1894 return check_msg_filter( msg_code
, first
, last
);
1896 else /* mouse message */
1898 /* we need to check all possible values that the message can have in the end */
1900 if (check_msg_filter( msg_code
, first
, last
)) return 1;
1901 if (msg_code
== WM_MOUSEWHEEL
) return 0; /* no other possible value for this one */
1903 /* all other messages can become non-client messages */
1904 if (check_msg_filter( msg_code
+ (WM_NCMOUSEFIRST
- WM_MOUSEFIRST
), first
, last
)) return 1;
1906 /* clicks can become double-clicks or non-client double-clicks */
1907 if (msg_code
== WM_LBUTTONDOWN
|| msg_code
== WM_MBUTTONDOWN
||
1908 msg_code
== WM_RBUTTONDOWN
|| msg_code
== WM_XBUTTONDOWN
)
1910 if (check_msg_filter( msg_code
+ (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
1911 if (check_msg_filter( msg_code
+ (WM_NCLBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
1918 /* find a hardware message for the given queue */
1919 static int get_hardware_message( struct thread
*thread
, unsigned int hw_id
, user_handle_t filter_win
,
1920 unsigned int first
, unsigned int last
, unsigned int flags
,
1921 struct get_message_reply
*reply
)
1923 struct thread_input
*input
= thread
->queue
->input
;
1924 struct thread
*win_thread
;
1927 int clear_bits
, got_one
= 0;
1928 unsigned int msg_code
;
1930 ptr
= list_head( &input
->msg_list
);
1935 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1936 if (msg
->unique_id
== hw_id
) break;
1937 ptr
= list_next( &input
->msg_list
, ptr
);
1939 if (!ptr
) ptr
= list_head( &input
->msg_list
);
1940 else ptr
= list_next( &input
->msg_list
, ptr
); /* start from the next one */
1943 if (ptr
== list_head( &input
->msg_list
))
1944 clear_bits
= QS_INPUT
;
1946 clear_bits
= 0; /* don't clear bits if we don't go through the whole list */
1950 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1951 struct hardware_msg_data
*data
= msg
->data
;
1953 ptr
= list_next( &input
->msg_list
, ptr
);
1954 win
= find_hardware_message_window( input
->desktop
, input
, msg
, &msg_code
, &win_thread
);
1955 if (!win
|| !win_thread
)
1957 /* no window at all, remove it */
1958 update_input_key_state( input
->desktop
, input
->keystate
, msg
);
1959 list_remove( &msg
->entry
);
1960 free_message( msg
);
1963 if (win_thread
!= thread
)
1965 if (win_thread
->queue
->input
== input
)
1967 /* wake the other thread */
1968 set_queue_bits( win_thread
->queue
, get_hardware_msg_bit(msg
) );
1973 /* for another thread input, drop it */
1974 update_input_key_state( input
->desktop
, input
->keystate
, msg
);
1975 list_remove( &msg
->entry
);
1976 free_message( msg
);
1978 release_object( win_thread
);
1981 release_object( win_thread
);
1983 /* if we already got a message for another thread, or if it doesn't
1984 * match the filter we skip it */
1985 if (got_one
|| !check_hw_message_filter( win
, msg_code
, filter_win
, first
, last
))
1987 clear_bits
&= ~get_hardware_msg_bit( msg
);
1990 /* now we can return it */
1991 if (!msg
->unique_id
) msg
->unique_id
= get_unique_id();
1992 reply
->type
= MSG_HARDWARE
;
1994 reply
->msg
= msg_code
;
1995 reply
->wparam
= msg
->wparam
;
1996 reply
->lparam
= msg
->lparam
;
1999 reply
->time
= msg
->time
;
2001 data
->hw_id
= msg
->unique_id
;
2002 set_reply_data( msg
->data
, msg
->data_size
);
2003 if (msg
->msg
== WM_INPUT
&& (flags
& PM_REMOVE
))
2004 release_hardware_message( current
->queue
, data
->hw_id
, 1 );
2007 /* nothing found, clear the hardware queue bits */
2008 clear_queue_bits( thread
->queue
, clear_bits
);
2012 /* increment (or decrement if 'incr' is negative) the queue paint count */
2013 void inc_queue_paint_count( struct thread
*thread
, int incr
)
2015 struct msg_queue
*queue
= thread
->queue
;
2019 if ((queue
->paint_count
+= incr
) < 0) queue
->paint_count
= 0;
2021 if (queue
->paint_count
)
2022 set_queue_bits( queue
, QS_PAINT
);
2024 clear_queue_bits( queue
, QS_PAINT
);
2028 /* remove all messages and timers belonging to a certain window */
2029 void queue_cleanup_window( struct thread
*thread
, user_handle_t win
)
2031 struct msg_queue
*queue
= thread
->queue
;
2039 ptr
= list_head( &queue
->pending_timers
);
2042 struct list
*next
= list_next( &queue
->pending_timers
, ptr
);
2043 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
2044 if (timer
->win
== win
) free_timer( queue
, timer
);
2047 ptr
= list_head( &queue
->expired_timers
);
2050 struct list
*next
= list_next( &queue
->expired_timers
, ptr
);
2051 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
2052 if (timer
->win
== win
) free_timer( queue
, timer
);
2056 /* remove messages */
2057 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
2059 struct list
*ptr
, *next
;
2061 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->msg_list
[i
] )
2063 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
2064 if (msg
->win
== win
)
2066 if (msg
->msg
== WM_QUIT
&& !queue
->quit_message
)
2068 queue
->quit_message
= 1;
2069 queue
->exit_code
= msg
->wparam
;
2071 remove_queue_message( queue
, msg
, i
);
2076 thread_input_cleanup_window( queue
, win
);
2079 /* post a message to a window; used by socket handling */
2080 void post_message( user_handle_t win
, unsigned int message
, lparam_t wparam
, lparam_t lparam
)
2082 struct message
*msg
;
2083 struct thread
*thread
= get_window_thread( win
);
2085 if (!thread
) return;
2087 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
2089 msg
->type
= MSG_POSTED
;
2090 msg
->win
= get_user_full_handle( win
);
2092 msg
->wparam
= wparam
;
2093 msg
->lparam
= lparam
;
2098 get_message_defaults( thread
->queue
, &msg
->x
, &msg
->y
, &msg
->time
);
2100 list_add_tail( &thread
->queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
2101 set_queue_bits( thread
->queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
2102 if (message
== WM_HOTKEY
)
2104 set_queue_bits( thread
->queue
, QS_HOTKEY
);
2105 thread
->queue
->hotkey_count
++;
2108 release_object( thread
);
2111 /* post a win event */
2112 void post_win_event( struct thread
*thread
, unsigned int event
,
2113 user_handle_t win
, unsigned int object_id
,
2114 unsigned int child_id
, client_ptr_t hook_proc
,
2115 const WCHAR
*module
, data_size_t module_size
,
2118 struct message
*msg
;
2120 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
2122 struct winevent_msg_data
*data
;
2124 msg
->type
= MSG_WINEVENT
;
2125 msg
->win
= get_user_full_handle( win
);
2127 msg
->wparam
= object_id
;
2128 msg
->lparam
= child_id
;
2129 msg
->time
= get_tick_count();
2132 if ((data
= malloc( sizeof(*data
) + module_size
)))
2135 data
->tid
= get_thread_id( current
);
2136 data
->hook_proc
= hook_proc
;
2137 memcpy( data
+ 1, module
, module_size
);
2140 msg
->data_size
= sizeof(*data
) + module_size
;
2142 if (debug_level
> 1)
2143 fprintf( stderr
, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2144 get_thread_id(thread
), event
, win
, object_id
, child_id
);
2145 list_add_tail( &thread
->queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
2146 set_queue_bits( thread
->queue
, QS_SENDMESSAGE
);
2153 /* free all hotkeys on a desktop, optionally filtering by window */
2154 void free_hotkeys( struct desktop
*desktop
, user_handle_t window
)
2156 struct hotkey
*hotkey
, *hotkey2
;
2158 LIST_FOR_EACH_ENTRY_SAFE( hotkey
, hotkey2
, &desktop
->hotkeys
, struct hotkey
, entry
)
2160 if (!window
|| hotkey
->win
== window
)
2162 list_remove( &hotkey
->entry
);
2169 /* check if the thread owning the window is hung */
2170 DECL_HANDLER(is_window_hung
)
2172 struct thread
*thread
;
2174 thread
= get_window_thread( req
->win
);
2177 reply
->is_hung
= is_queue_hung( thread
->queue
);
2178 release_object( thread
);
2180 else reply
->is_hung
= 0;
2184 /* get the message queue of the current thread */
2185 DECL_HANDLER(get_msg_queue
)
2187 struct msg_queue
*queue
= get_current_queue();
2190 if (queue
) reply
->handle
= alloc_handle( current
->process
, queue
, SYNCHRONIZE
, 0 );
2194 /* set the file descriptor associated to the current thread queue */
2195 DECL_HANDLER(set_queue_fd
)
2197 struct msg_queue
*queue
= get_current_queue();
2201 if (queue
->fd
) /* fd can only be set once */
2203 set_error( STATUS_ACCESS_DENIED
);
2206 if (!(file
= get_file_obj( current
->process
, req
->handle
, SYNCHRONIZE
))) return;
2208 if ((unix_fd
= get_file_unix_fd( file
)) != -1)
2210 if ((unix_fd
= dup( unix_fd
)) != -1)
2211 queue
->fd
= create_anonymous_fd( &msg_queue_fd_ops
, unix_fd
, &queue
->obj
, 0 );
2215 release_object( file
);
2219 /* set the current message queue wakeup mask */
2220 DECL_HANDLER(set_queue_mask
)
2222 struct msg_queue
*queue
= get_current_queue();
2226 queue
->wake_mask
= req
->wake_mask
;
2227 queue
->changed_mask
= req
->changed_mask
;
2228 reply
->wake_bits
= queue
->wake_bits
;
2229 reply
->changed_bits
= queue
->changed_bits
;
2230 if (is_signaled( queue
))
2232 /* if skip wait is set, do what would have been done in the subsequent wait */
2233 if (req
->skip_wait
) queue
->wake_mask
= queue
->changed_mask
= 0;
2234 else wake_up( &queue
->obj
, 0 );
2240 /* get the current message queue status */
2241 DECL_HANDLER(get_queue_status
)
2243 struct msg_queue
*queue
= current
->queue
;
2246 reply
->wake_bits
= queue
->wake_bits
;
2247 reply
->changed_bits
= queue
->changed_bits
;
2248 queue
->changed_bits
&= ~req
->clear_bits
;
2250 else reply
->wake_bits
= reply
->changed_bits
= 0;
2254 /* send a message to a thread queue */
2255 DECL_HANDLER(send_message
)
2257 struct message
*msg
;
2258 struct msg_queue
*send_queue
= get_current_queue();
2259 struct msg_queue
*recv_queue
= NULL
;
2260 struct thread
*thread
= NULL
;
2262 if (!(thread
= get_thread_from_id( req
->id
))) return;
2264 if (!(recv_queue
= thread
->queue
))
2266 set_error( STATUS_INVALID_PARAMETER
);
2267 release_object( thread
);
2270 if ((req
->flags
& SEND_MSG_ABORT_IF_HUNG
) && is_queue_hung(recv_queue
))
2272 set_error( STATUS_TIMEOUT
);
2273 release_object( thread
);
2277 if ((msg
= mem_alloc( sizeof(*msg
) )))
2279 msg
->type
= req
->type
;
2280 msg
->win
= get_user_full_handle( req
->win
);
2281 msg
->msg
= req
->msg
;
2282 msg
->wparam
= req
->wparam
;
2283 msg
->lparam
= req
->lparam
;
2286 msg
->data_size
= get_req_data_size();
2288 get_message_defaults( recv_queue
, &msg
->x
, &msg
->y
, &msg
->time
);
2290 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
2293 release_object( thread
);
2299 case MSG_OTHER_PROCESS
:
2303 if (!(msg
->result
= alloc_message_result( send_queue
, recv_queue
, msg
, req
->timeout
)))
2305 free_message( msg
);
2310 list_add_tail( &recv_queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
2311 set_queue_bits( recv_queue
, QS_SENDMESSAGE
);
2314 list_add_tail( &recv_queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
2315 set_queue_bits( recv_queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
2316 if (msg
->msg
== WM_HOTKEY
)
2318 set_queue_bits( recv_queue
, QS_HOTKEY
);
2319 recv_queue
->hotkey_count
++;
2322 case MSG_HARDWARE
: /* should use send_hardware_message instead */
2323 case MSG_CALLBACK_RESULT
: /* cannot send this one */
2324 case MSG_HOOK_LL
: /* generated internally */
2326 set_error( STATUS_INVALID_PARAMETER
);
2331 release_object( thread
);
2334 /* send a hardware message to a thread queue */
2335 DECL_HANDLER(send_hardware_message
)
2337 struct thread
*thread
= NULL
;
2338 struct desktop
*desktop
;
2339 struct msg_queue
*sender
= get_current_queue();
2340 data_size_t size
= min( 256, get_reply_max_size() );
2342 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2346 if (!(thread
= get_window_thread( req
->win
))) return;
2347 if (desktop
!= thread
->queue
->input
->desktop
)
2349 /* don't allow queuing events to a different desktop */
2350 release_object( desktop
);
2355 reply
->prev_x
= desktop
->cursor
.x
;
2356 reply
->prev_y
= desktop
->cursor
.y
;
2358 switch (req
->input
.type
)
2361 reply
->wait
= queue_mouse_message( desktop
, req
->win
, &req
->input
, req
->flags
, sender
);
2363 case INPUT_KEYBOARD
:
2364 reply
->wait
= queue_keyboard_message( desktop
, req
->win
, &req
->input
, req
->flags
, sender
);
2366 case INPUT_HARDWARE
:
2367 queue_custom_hardware_message( desktop
, req
->win
, &req
->input
);
2370 set_error( STATUS_INVALID_PARAMETER
);
2372 if (thread
) release_object( thread
);
2374 reply
->new_x
= desktop
->cursor
.x
;
2375 reply
->new_y
= desktop
->cursor
.y
;
2376 set_reply_data( desktop
->keystate
, size
);
2377 release_object( desktop
);
2380 /* post a quit message to the current queue */
2381 DECL_HANDLER(post_quit_message
)
2383 struct msg_queue
*queue
= get_current_queue();
2388 queue
->quit_message
= 1;
2389 queue
->exit_code
= req
->exit_code
;
2390 set_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
2393 /* get a message from the current queue */
2394 DECL_HANDLER(get_message
)
2396 struct timer
*timer
;
2398 struct msg_queue
*queue
= get_current_queue();
2399 user_handle_t get_win
= get_user_full_handle( req
->get_win
);
2400 unsigned int filter
= req
->flags
>> 16;
2402 reply
->active_hooks
= get_active_hooks();
2405 queue
->last_get_msg
= current_time
;
2406 if (!filter
) filter
= QS_ALLINPUT
;
2408 /* first check for sent messages */
2409 if ((ptr
= list_head( &queue
->msg_list
[SEND_MESSAGE
] )))
2411 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
2412 receive_message( queue
, msg
, reply
);
2416 /* clear changed bits so we can wait on them if we don't find a message */
2417 if (filter
& QS_POSTMESSAGE
)
2419 queue
->changed_bits
&= ~(QS_POSTMESSAGE
| QS_HOTKEY
| QS_TIMER
);
2420 if (req
->get_first
== 0 && req
->get_last
== ~0U) queue
->changed_bits
&= ~QS_ALLPOSTMESSAGE
;
2422 if (filter
& QS_INPUT
) queue
->changed_bits
&= ~QS_INPUT
;
2423 if (filter
& QS_PAINT
) queue
->changed_bits
&= ~QS_PAINT
;
2425 /* then check for posted messages */
2426 if ((filter
& QS_POSTMESSAGE
) &&
2427 get_posted_message( queue
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
2430 if ((filter
& QS_HOTKEY
) && queue
->hotkey_count
&&
2431 req
->get_first
<= WM_HOTKEY
&& req
->get_last
>= WM_HOTKEY
&&
2432 get_posted_message( queue
, get_win
, WM_HOTKEY
, WM_HOTKEY
, req
->flags
, reply
))
2435 /* only check for quit messages if not posted messages pending */
2436 if ((filter
& QS_POSTMESSAGE
) && get_quit_message( queue
, req
->flags
, reply
))
2439 /* then check for any raw hardware message */
2440 if ((filter
& QS_INPUT
) &&
2441 filter_contains_hw_range( req
->get_first
, req
->get_last
) &&
2442 get_hardware_message( current
, req
->hw_id
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
2445 /* now check for WM_PAINT */
2446 if ((filter
& QS_PAINT
) &&
2447 queue
->paint_count
&&
2448 check_msg_filter( WM_PAINT
, req
->get_first
, req
->get_last
) &&
2449 (reply
->win
= find_window_to_repaint( get_win
, current
)))
2451 reply
->type
= MSG_POSTED
;
2452 reply
->msg
= WM_PAINT
;
2455 get_message_defaults( queue
, &reply
->x
, &reply
->y
, &reply
->time
);
2459 /* now check for timer */
2460 if ((filter
& QS_TIMER
) &&
2461 (timer
= find_expired_timer( queue
, get_win
, req
->get_first
,
2462 req
->get_last
, (req
->flags
& PM_REMOVE
) )))
2464 reply
->type
= MSG_POSTED
;
2465 reply
->win
= timer
->win
;
2466 reply
->msg
= timer
->msg
;
2467 reply
->wparam
= timer
->id
;
2468 reply
->lparam
= timer
->lparam
;
2469 get_message_defaults( queue
, &reply
->x
, &reply
->y
, &reply
->time
);
2470 if (!(req
->flags
& PM_NOYIELD
) && current
->process
->idle_event
)
2471 set_event( current
->process
->idle_event
);
2475 if (get_win
== -1 && current
->process
->idle_event
) set_event( current
->process
->idle_event
);
2476 queue
->wake_mask
= req
->wake_mask
;
2477 queue
->changed_mask
= req
->changed_mask
;
2478 set_error( STATUS_PENDING
); /* FIXME */
2482 /* reply to a sent message */
2483 DECL_HANDLER(reply_message
)
2485 if (!current
->queue
) set_error( STATUS_ACCESS_DENIED
);
2486 else if (current
->queue
->recv_result
)
2487 reply_message( current
->queue
, req
->result
, 0, req
->remove
,
2488 get_req_data(), get_req_data_size() );
2492 /* accept the current hardware message */
2493 DECL_HANDLER(accept_hardware_message
)
2496 release_hardware_message( current
->queue
, req
->hw_id
, req
->remove
);
2498 set_error( STATUS_ACCESS_DENIED
);
2502 /* retrieve the reply for the last message sent */
2503 DECL_HANDLER(get_message_reply
)
2505 struct message_result
*result
;
2507 struct msg_queue
*queue
= current
->queue
;
2511 set_error( STATUS_PENDING
);
2514 if (!(entry
= list_head( &queue
->send_result
))) return; /* no reply ready */
2516 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
2517 if (result
->replied
|| req
->cancel
)
2519 if (result
->replied
)
2521 reply
->result
= result
->result
;
2522 set_error( result
->error
);
2525 data_size_t data_len
= min( result
->data_size
, get_reply_max_size() );
2526 set_reply_data_ptr( result
->data
, data_len
);
2527 result
->data
= NULL
;
2528 result
->data_size
= 0;
2531 remove_result_from_sender( result
);
2533 entry
= list_head( &queue
->send_result
);
2534 if (!entry
) clear_queue_bits( queue
, QS_SMRESULT
);
2537 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
2538 if (result
->replied
) set_queue_bits( queue
, QS_SMRESULT
);
2539 else clear_queue_bits( queue
, QS_SMRESULT
);
2543 else set_error( STATUS_ACCESS_DENIED
);
2547 /* set a window timer */
2548 DECL_HANDLER(set_win_timer
)
2550 struct timer
*timer
;
2551 struct msg_queue
*queue
;
2552 struct thread
*thread
= NULL
;
2553 user_handle_t win
= 0;
2554 lparam_t id
= req
->id
;
2558 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
2560 set_error( STATUS_INVALID_HANDLE
);
2563 if (thread
->process
!= current
->process
)
2565 release_object( thread
);
2566 set_error( STATUS_ACCESS_DENIED
);
2569 queue
= thread
->queue
;
2570 /* remove it if it existed already */
2571 if ((timer
= find_timer( queue
, win
, req
->msg
, id
))) free_timer( queue
, timer
);
2575 queue
= get_current_queue();
2576 /* look for a timer with this id */
2577 if (id
&& (timer
= find_timer( queue
, 0, req
->msg
, id
)))
2579 /* free and reuse id */
2580 free_timer( queue
, timer
);
2584 /* find a free id for it */
2587 id
= queue
->next_timer_id
;
2588 if (--queue
->next_timer_id
<= 0x100) queue
->next_timer_id
= 0x7fff;
2590 while (find_timer( queue
, 0, req
->msg
, id
));
2594 if ((timer
= set_timer( queue
, req
->rate
)))
2597 timer
->msg
= req
->msg
;
2599 timer
->lparam
= req
->lparam
;
2602 if (thread
) release_object( thread
);
2605 /* kill a window timer */
2606 DECL_HANDLER(kill_win_timer
)
2608 struct timer
*timer
;
2609 struct thread
*thread
;
2610 user_handle_t win
= 0;
2614 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
2616 set_error( STATUS_INVALID_HANDLE
);
2619 if (thread
->process
!= current
->process
)
2621 release_object( thread
);
2622 set_error( STATUS_ACCESS_DENIED
);
2626 else thread
= (struct thread
*)grab_object( current
);
2628 if (thread
->queue
&& (timer
= find_timer( thread
->queue
, win
, req
->msg
, req
->id
)))
2629 free_timer( thread
->queue
, timer
);
2631 set_error( STATUS_INVALID_PARAMETER
);
2633 release_object( thread
);
2636 DECL_HANDLER(register_hotkey
)
2638 struct desktop
*desktop
;
2639 user_handle_t win_handle
= req
->window
;
2640 struct hotkey
*hotkey
;
2641 struct hotkey
*new_hotkey
= NULL
;
2642 struct thread
*thread
;
2643 const int modifier_flags
= MOD_ALT
|MOD_CONTROL
|MOD_SHIFT
|MOD_WIN
;
2645 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2649 if (!get_user_object_handle( &win_handle
, USER_WINDOW
))
2651 release_object( desktop
);
2652 set_win32_error( ERROR_INVALID_WINDOW_HANDLE
);
2656 thread
= get_window_thread( win_handle
);
2657 if (thread
) release_object( thread
);
2659 if (thread
!= current
)
2661 release_object( desktop
);
2662 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD
);
2667 LIST_FOR_EACH_ENTRY( hotkey
, &desktop
->hotkeys
, struct hotkey
, entry
)
2669 if (req
->vkey
== hotkey
->vkey
&&
2670 (req
->flags
& modifier_flags
) == (hotkey
->flags
& modifier_flags
))
2672 release_object( desktop
);
2673 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED
);
2676 if (current
->queue
== hotkey
->queue
&& win_handle
== hotkey
->win
&& req
->id
== hotkey
->id
)
2677 new_hotkey
= hotkey
;
2682 reply
->replaced
= 1;
2683 reply
->flags
= new_hotkey
->flags
;
2684 reply
->vkey
= new_hotkey
->vkey
;
2688 new_hotkey
= mem_alloc( sizeof(*new_hotkey
) );
2691 list_add_tail( &desktop
->hotkeys
, &new_hotkey
->entry
);
2692 new_hotkey
->queue
= current
->queue
;
2693 new_hotkey
->win
= win_handle
;
2694 new_hotkey
->id
= req
->id
;
2700 new_hotkey
->flags
= req
->flags
;
2701 new_hotkey
->vkey
= req
->vkey
;
2704 release_object( desktop
);
2707 DECL_HANDLER(unregister_hotkey
)
2709 struct desktop
*desktop
;
2710 user_handle_t win_handle
= req
->window
;
2711 struct hotkey
*hotkey
;
2712 struct thread
*thread
;
2714 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2718 if (!get_user_object_handle( &win_handle
, USER_WINDOW
))
2720 release_object( desktop
);
2721 set_win32_error( ERROR_INVALID_WINDOW_HANDLE
);
2725 thread
= get_window_thread( win_handle
);
2726 if (thread
) release_object( thread
);
2728 if (thread
!= current
)
2730 release_object( desktop
);
2731 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD
);
2736 LIST_FOR_EACH_ENTRY( hotkey
, &desktop
->hotkeys
, struct hotkey
, entry
)
2738 if (current
->queue
== hotkey
->queue
&& win_handle
== hotkey
->win
&& req
->id
== hotkey
->id
)
2742 release_object( desktop
);
2743 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED
);
2747 reply
->flags
= hotkey
->flags
;
2748 reply
->vkey
= hotkey
->vkey
;
2749 list_remove( &hotkey
->entry
);
2751 release_object( desktop
);
2754 /* attach (or detach) thread inputs */
2755 DECL_HANDLER(attach_thread_input
)
2757 struct thread
*thread_from
= get_thread_from_id( req
->tid_from
);
2758 struct thread
*thread_to
= get_thread_from_id( req
->tid_to
);
2760 if (!thread_from
|| !thread_to
)
2762 if (thread_from
) release_object( thread_from
);
2763 if (thread_to
) release_object( thread_to
);
2766 if (thread_from
!= thread_to
)
2770 if ((thread_to
->queue
|| thread_to
== current
) &&
2771 (thread_from
->queue
|| thread_from
== current
))
2772 attach_thread_input( thread_from
, thread_to
);
2774 set_error( STATUS_INVALID_PARAMETER
);
2778 if (thread_from
->queue
&& thread_to
->queue
&&
2779 thread_from
->queue
->input
== thread_to
->queue
->input
)
2780 detach_thread_input( thread_from
);
2782 set_error( STATUS_ACCESS_DENIED
);
2785 else set_error( STATUS_ACCESS_DENIED
);
2786 release_object( thread_from
);
2787 release_object( thread_to
);
2791 /* get thread input data */
2792 DECL_HANDLER(get_thread_input
)
2794 struct thread
*thread
= NULL
;
2795 struct desktop
*desktop
;
2796 struct thread_input
*input
;
2800 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2801 if (!(desktop
= get_thread_desktop( thread
, 0 )))
2803 release_object( thread
);
2806 input
= thread
->queue
? thread
->queue
->input
: NULL
;
2810 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2811 input
= desktop
->foreground_input
; /* get the foreground thread info */
2816 reply
->focus
= input
->focus
;
2817 reply
->capture
= input
->capture
;
2818 reply
->active
= input
->active
;
2819 reply
->menu_owner
= input
->menu_owner
;
2820 reply
->move_size
= input
->move_size
;
2821 reply
->caret
= input
->caret
;
2822 reply
->cursor
= input
->cursor
;
2823 reply
->show_count
= input
->cursor_count
;
2824 reply
->rect
= input
->caret_rect
;
2827 /* foreground window is active window of foreground thread */
2828 reply
->foreground
= desktop
->foreground_input
? desktop
->foreground_input
->active
: 0;
2829 if (thread
) release_object( thread
);
2830 release_object( desktop
);
2834 /* retrieve queue keyboard state for a given thread */
2835 DECL_HANDLER(get_key_state
)
2837 struct thread
*thread
;
2838 struct desktop
*desktop
;
2839 data_size_t size
= min( 256, get_reply_max_size() );
2841 if (!req
->tid
) /* get global async key state */
2843 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2846 reply
->state
= desktop
->keystate
[req
->key
& 0xff];
2847 desktop
->keystate
[req
->key
& 0xff] &= ~0x40;
2849 set_reply_data( desktop
->keystate
, size
);
2850 release_object( desktop
);
2854 unsigned char *keystate
;
2855 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2858 if (req
->key
>= 0) reply
->state
= thread
->queue
->input
->keystate
[req
->key
& 0xff];
2859 set_reply_data( thread
->queue
->input
->keystate
, size
);
2860 release_object( thread
);
2863 release_object( thread
);
2865 /* fallback to desktop keystate */
2866 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2867 if (req
->key
>= 0) reply
->state
= desktop
->keystate
[req
->key
& 0xff] & ~0x40;
2868 if ((keystate
= set_reply_data_size( size
)))
2871 for (i
= 0; i
< size
; i
++) keystate
[i
] = desktop
->keystate
[i
] & ~0x40;
2873 release_object( desktop
);
2878 /* set queue keyboard state for a given thread */
2879 DECL_HANDLER(set_key_state
)
2881 struct thread
*thread
;
2882 struct desktop
*desktop
;
2883 data_size_t size
= min( 256, get_req_data_size() );
2885 if (!req
->tid
) /* set global async key state */
2887 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2888 memcpy( desktop
->keystate
, get_req_data(), size
);
2889 release_object( desktop
);
2893 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2894 if (thread
->queue
) memcpy( thread
->queue
->input
->keystate
, get_req_data(), size
);
2895 if (req
->async
&& (desktop
= get_thread_desktop( thread
, 0 )))
2897 memcpy( desktop
->keystate
, get_req_data(), size
);
2898 release_object( desktop
);
2900 release_object( thread
);
2905 /* set the system foreground window */
2906 DECL_HANDLER(set_foreground_window
)
2908 struct thread
*thread
= NULL
;
2909 struct desktop
*desktop
;
2910 struct msg_queue
*queue
= get_current_queue();
2912 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2913 reply
->previous
= desktop
->foreground_input
? desktop
->foreground_input
->active
: 0;
2914 reply
->send_msg_old
= (reply
->previous
&& desktop
->foreground_input
!= queue
->input
);
2915 reply
->send_msg_new
= FALSE
;
2917 if (is_valid_foreground_window( req
->handle
) &&
2918 (thread
= get_window_thread( req
->handle
)) &&
2919 thread
->queue
->input
->desktop
== desktop
)
2921 set_foreground_input( desktop
, thread
->queue
->input
);
2922 reply
->send_msg_new
= (desktop
->foreground_input
!= queue
->input
);
2924 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE
);
2926 if (thread
) release_object( thread
);
2927 release_object( desktop
);
2931 /* set the current thread focus window */
2932 DECL_HANDLER(set_focus_window
)
2934 struct msg_queue
*queue
= get_current_queue();
2936 reply
->previous
= 0;
2937 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2939 reply
->previous
= queue
->input
->focus
;
2940 queue
->input
->focus
= get_user_full_handle( req
->handle
);
2945 /* set the current thread active window */
2946 DECL_HANDLER(set_active_window
)
2948 struct msg_queue
*queue
= get_current_queue();
2950 reply
->previous
= 0;
2951 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2953 if (!req
->handle
|| make_window_active( req
->handle
))
2955 reply
->previous
= queue
->input
->active
;
2956 queue
->input
->active
= get_user_full_handle( req
->handle
);
2958 else set_error( STATUS_INVALID_HANDLE
);
2963 /* set the current thread capture window */
2964 DECL_HANDLER(set_capture_window
)
2966 struct msg_queue
*queue
= get_current_queue();
2968 reply
->previous
= reply
->full_handle
= 0;
2969 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2971 struct thread_input
*input
= queue
->input
;
2973 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2974 if (input
->menu_owner
&& !(req
->flags
& CAPTURE_MENU
))
2976 set_error(STATUS_ACCESS_DENIED
);
2979 reply
->previous
= input
->capture
;
2980 input
->capture
= get_user_full_handle( req
->handle
);
2981 input
->menu_owner
= (req
->flags
& CAPTURE_MENU
) ? input
->capture
: 0;
2982 input
->move_size
= (req
->flags
& CAPTURE_MOVESIZE
) ? input
->capture
: 0;
2983 reply
->full_handle
= input
->capture
;
2988 /* Set the current thread caret window */
2989 DECL_HANDLER(set_caret_window
)
2991 struct msg_queue
*queue
= get_current_queue();
2993 reply
->previous
= 0;
2994 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2996 struct thread_input
*input
= queue
->input
;
2998 reply
->previous
= input
->caret
;
2999 reply
->old_rect
= input
->caret_rect
;
3000 reply
->old_hide
= input
->caret_hide
;
3001 reply
->old_state
= input
->caret_state
;
3003 set_caret_window( input
, get_user_full_handle(req
->handle
) );
3004 input
->caret_rect
.right
= input
->caret_rect
.left
+ req
->width
;
3005 input
->caret_rect
.bottom
= input
->caret_rect
.top
+ req
->height
;
3010 /* Set the current thread caret information */
3011 DECL_HANDLER(set_caret_info
)
3013 struct msg_queue
*queue
= get_current_queue();
3014 struct thread_input
*input
;
3017 input
= queue
->input
;
3018 reply
->full_handle
= input
->caret
;
3019 reply
->old_rect
= input
->caret_rect
;
3020 reply
->old_hide
= input
->caret_hide
;
3021 reply
->old_state
= input
->caret_state
;
3023 if (req
->handle
&& get_user_full_handle(req
->handle
) != input
->caret
)
3025 set_error( STATUS_ACCESS_DENIED
);
3028 if (req
->flags
& SET_CARET_POS
)
3030 input
->caret_rect
.right
+= req
->x
- input
->caret_rect
.left
;
3031 input
->caret_rect
.bottom
+= req
->y
- input
->caret_rect
.top
;
3032 input
->caret_rect
.left
= req
->x
;
3033 input
->caret_rect
.top
= req
->y
;
3035 if (req
->flags
& SET_CARET_HIDE
)
3037 input
->caret_hide
+= req
->hide
;
3038 if (input
->caret_hide
< 0) input
->caret_hide
= 0;
3040 if (req
->flags
& SET_CARET_STATE
)
3042 if (req
->state
== -1) input
->caret_state
= !input
->caret_state
;
3043 else input
->caret_state
= !!req
->state
;
3048 /* get the time of the last input event */
3049 DECL_HANDLER(get_last_input_time
)
3051 reply
->time
= last_input_time
;
3054 /* set/get the current cursor */
3055 DECL_HANDLER(set_cursor
)
3057 struct msg_queue
*queue
= get_current_queue();
3058 struct thread_input
*input
;
3061 input
= queue
->input
;
3063 reply
->prev_handle
= input
->cursor
;
3064 reply
->prev_count
= input
->cursor_count
;
3065 reply
->prev_x
= input
->desktop
->cursor
.x
;
3066 reply
->prev_y
= input
->desktop
->cursor
.y
;
3068 if (req
->flags
& SET_CURSOR_HANDLE
)
3070 if (req
->handle
&& !get_user_object( req
->handle
, USER_CLIENT
))
3072 set_win32_error( ERROR_INVALID_CURSOR_HANDLE
);
3075 input
->cursor
= req
->handle
;
3077 if (req
->flags
& SET_CURSOR_COUNT
)
3079 queue
->cursor_count
+= req
->show_count
;
3080 input
->cursor_count
+= req
->show_count
;
3082 if (req
->flags
& SET_CURSOR_POS
)
3084 set_cursor_pos( input
->desktop
, req
->x
, req
->y
);
3086 if (req
->flags
& (SET_CURSOR_CLIP
| SET_CURSOR_NOCLIP
))
3088 struct desktop
*desktop
= input
->desktop
;
3090 /* only the desktop owner can set the message */
3091 if (req
->clip_msg
&& get_top_window_owner(desktop
) == current
->process
)
3092 desktop
->cursor
.clip_msg
= req
->clip_msg
;
3094 set_clip_rectangle( desktop
, (req
->flags
& SET_CURSOR_NOCLIP
) ? NULL
: &req
->clip
);
3097 reply
->new_x
= input
->desktop
->cursor
.x
;
3098 reply
->new_y
= input
->desktop
->cursor
.y
;
3099 reply
->new_clip
= input
->desktop
->cursor
.clip
;
3100 reply
->last_change
= input
->desktop
->cursor
.last_change
;
3103 DECL_HANDLER(update_rawinput_devices
)
3105 const struct rawinput_device
*devices
= get_req_data();
3106 unsigned int device_count
= get_req_data_size() / sizeof (*devices
);
3107 const struct rawinput_device_entry
*e
;
3110 for (i
= 0; i
< device_count
; ++i
)
3112 update_rawinput_device(&devices
[i
]);
3115 e
= find_rawinput_device( 1, 2 );
3116 current
->process
->rawinput_mouse
= e
? &e
->device
: NULL
;
3117 e
= find_rawinput_device( 1, 6 );
3118 current
->process
->rawinput_kbd
= e
? &e
->device
: NULL
;