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 abstime_t when
; /* next expiration */
93 unsigned int rate
; /* timer rate in ms */
94 user_handle_t win
; /* window handle */
95 unsigned int msg
; /* message to post */
96 lparam_t id
; /* timer id */
97 lparam_t lparam
; /* lparam for message */
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_link_name
, /* link_name */
182 NULL
, /* unlink_name */
183 no_open_file
, /* open_file */
184 no_kernel_obj_list
, /* get_kernel_obj_list */
185 no_close_handle
, /* close_handle */
186 msg_queue_destroy
/* destroy */
189 static const struct fd_ops msg_queue_fd_ops
=
191 NULL
, /* get_poll_events */
192 msg_queue_poll_event
, /* poll_event */
194 NULL
, /* get_fd_type */
196 NULL
, /* queue_async */
197 NULL
, /* reselect_async */
198 NULL
/* cancel async */
202 static const struct object_ops thread_input_ops
=
204 sizeof(struct thread_input
), /* size */
205 thread_input_dump
, /* dump */
206 no_get_type
, /* get_type */
207 no_add_queue
, /* add_queue */
208 NULL
, /* remove_queue */
210 NULL
, /* satisfied */
211 no_signal
, /* signal */
212 no_get_fd
, /* get_fd */
213 no_map_access
, /* map_access */
214 default_get_sd
, /* get_sd */
215 default_set_sd
, /* set_sd */
216 no_lookup_name
, /* lookup_name */
217 no_link_name
, /* link_name */
218 NULL
, /* unlink_name */
219 no_open_file
, /* open_file */
220 no_kernel_obj_list
, /* get_kernel_obj_list */
221 no_close_handle
, /* close_handle */
222 thread_input_destroy
/* destroy */
225 /* pointer to input structure of foreground thread */
226 static unsigned int last_input_time
;
228 static void queue_hardware_message( struct desktop
*desktop
, struct message
*msg
, int always_queue
);
229 static void free_message( struct message
*msg
);
231 /* set the caret window in a given thread input */
232 static void set_caret_window( struct thread_input
*input
, user_handle_t win
)
234 if (!win
|| win
!= input
->caret
)
236 input
->caret_rect
.left
= 0;
237 input
->caret_rect
.top
= 0;
238 input
->caret_rect
.right
= 0;
239 input
->caret_rect
.bottom
= 0;
242 input
->caret_hide
= 1;
243 input
->caret_state
= 0;
246 /* create a thread input object */
247 static struct thread_input
*create_thread_input( struct thread
*thread
)
249 struct thread_input
*input
;
251 if ((input
= alloc_object( &thread_input_ops
)))
256 input
->menu_owner
= 0;
257 input
->move_size
= 0;
259 input
->cursor_count
= 0;
260 list_init( &input
->msg_list
);
261 set_caret_window( input
, 0 );
262 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
264 if (!(input
->desktop
= get_thread_desktop( thread
, 0 /* FIXME: access rights */ )))
266 release_object( input
);
273 /* create a message queue object */
274 static struct msg_queue
*create_msg_queue( struct thread
*thread
, struct thread_input
*input
)
276 struct thread_input
*new_input
= NULL
;
277 struct msg_queue
*queue
;
282 if (!(new_input
= create_thread_input( thread
))) return NULL
;
286 if ((queue
= alloc_object( &msg_queue_ops
)))
289 queue
->wake_bits
= 0;
290 queue
->wake_mask
= 0;
291 queue
->changed_bits
= 0;
292 queue
->changed_mask
= 0;
293 queue
->paint_count
= 0;
294 queue
->hotkey_count
= 0;
295 queue
->quit_message
= 0;
296 queue
->cursor_count
= 0;
297 queue
->recv_result
= NULL
;
298 queue
->next_timer_id
= 0x7fff;
299 queue
->timeout
= NULL
;
300 queue
->input
= (struct thread_input
*)grab_object( input
);
302 queue
->last_get_msg
= current_time
;
303 list_init( &queue
->send_result
);
304 list_init( &queue
->callback_result
);
305 list_init( &queue
->pending_timers
);
306 list_init( &queue
->expired_timers
);
307 for (i
= 0; i
< NB_MSG_KINDS
; i
++) list_init( &queue
->msg_list
[i
] );
309 thread
->queue
= queue
;
311 if (new_input
) release_object( new_input
);
315 /* free the message queue of a thread at thread exit */
316 void free_msg_queue( struct thread
*thread
)
318 remove_thread_hooks( thread
);
319 if (!thread
->queue
) return;
320 release_object( thread
->queue
);
321 thread
->queue
= NULL
;
324 /* change the thread input data of a given thread */
325 static int assign_thread_input( struct thread
*thread
, struct thread_input
*new_input
)
327 struct msg_queue
*queue
= thread
->queue
;
331 thread
->queue
= create_msg_queue( thread
, new_input
);
332 return thread
->queue
!= NULL
;
336 queue
->input
->cursor_count
-= queue
->cursor_count
;
337 release_object( queue
->input
);
339 queue
->input
= (struct thread_input
*)grab_object( new_input
);
340 new_input
->cursor_count
+= queue
->cursor_count
;
344 /* allocate a hardware message and its data */
345 static struct message
*alloc_hardware_message( lparam_t info
, struct hw_msg_source source
,
348 struct hardware_msg_data
*msg_data
;
351 if (!(msg
= mem_alloc( sizeof(*msg
) ))) return NULL
;
352 if (!(msg_data
= mem_alloc( sizeof(*msg_data
) )))
357 memset( msg
, 0, sizeof(*msg
) );
358 msg
->type
= MSG_HARDWARE
;
360 msg
->data
= msg_data
;
361 msg
->data_size
= sizeof(*msg_data
);
363 memset( msg_data
, 0, sizeof(*msg_data
) );
364 msg_data
->info
= info
;
365 msg_data
->source
= source
;
369 static int update_desktop_cursor_pos( struct desktop
*desktop
, int x
, int y
)
373 x
= max( min( x
, desktop
->cursor
.clip
.right
- 1 ), desktop
->cursor
.clip
.left
);
374 y
= max( min( y
, desktop
->cursor
.clip
.bottom
- 1 ), desktop
->cursor
.clip
.top
);
375 updated
= (desktop
->cursor
.x
!= x
|| desktop
->cursor
.y
!= y
);
376 desktop
->cursor
.x
= x
;
377 desktop
->cursor
.y
= y
;
378 desktop
->cursor
.last_change
= get_tick_count();
383 /* set the cursor position and queue the corresponding mouse message */
384 static void set_cursor_pos( struct desktop
*desktop
, int x
, int y
)
386 static const struct hw_msg_source source
= { IMDT_UNAVAILABLE
, IMO_SYSTEM
};
387 const struct rawinput_device
*device
;
390 if ((device
= current
->process
->rawinput_mouse
) && (device
->flags
& RIDEV_NOLEGACY
))
392 update_desktop_cursor_pos( desktop
, x
, y
);
396 if (!(msg
= alloc_hardware_message( 0, source
, get_tick_count() ))) return;
398 msg
->msg
= WM_MOUSEMOVE
;
401 queue_hardware_message( desktop
, msg
, 1 );
404 /* retrieve default position and time for synthesized messages */
405 static void get_message_defaults( struct msg_queue
*queue
, int *x
, int *y
, unsigned int *time
)
407 struct desktop
*desktop
= queue
->input
->desktop
;
409 *x
= desktop
->cursor
.x
;
410 *y
= desktop
->cursor
.y
;
411 *time
= get_tick_count();
414 /* set the cursor clip rectangle */
415 static void set_clip_rectangle( struct desktop
*desktop
, const rectangle_t
*rect
, int send_clip_msg
)
417 rectangle_t top_rect
;
420 get_top_window_rectangle( desktop
, &top_rect
);
423 rectangle_t new_rect
= *rect
;
424 if (new_rect
.left
< top_rect
.left
) new_rect
.left
= top_rect
.left
;
425 if (new_rect
.right
> top_rect
.right
) new_rect
.right
= top_rect
.right
;
426 if (new_rect
.top
< top_rect
.top
) new_rect
.top
= top_rect
.top
;
427 if (new_rect
.bottom
> top_rect
.bottom
) new_rect
.bottom
= top_rect
.bottom
;
428 if (new_rect
.left
> new_rect
.right
|| new_rect
.top
> new_rect
.bottom
) new_rect
= top_rect
;
429 desktop
->cursor
.clip
= new_rect
;
431 else desktop
->cursor
.clip
= top_rect
;
433 if (desktop
->cursor
.clip_msg
&& send_clip_msg
)
434 post_desktop_message( desktop
, desktop
->cursor
.clip_msg
, rect
!= NULL
, 0 );
436 /* warp the mouse to be inside the clip rect */
437 x
= max( min( desktop
->cursor
.x
, desktop
->cursor
.clip
.right
- 1 ), desktop
->cursor
.clip
.left
);
438 y
= max( min( desktop
->cursor
.y
, desktop
->cursor
.clip
.bottom
- 1 ), desktop
->cursor
.clip
.top
);
439 if (x
!= desktop
->cursor
.x
|| y
!= desktop
->cursor
.y
) set_cursor_pos( desktop
, x
, y
);
442 /* change the foreground input and reset the cursor clip rect */
443 static void set_foreground_input( struct desktop
*desktop
, struct thread_input
*input
)
445 if (desktop
->foreground_input
== input
) return;
446 set_clip_rectangle( desktop
, NULL
, 1 );
447 desktop
->foreground_input
= input
;
450 /* get the hook table for a given thread */
451 struct hook_table
*get_queue_hooks( struct thread
*thread
)
453 if (!thread
->queue
) return NULL
;
454 return thread
->queue
->hooks
;
457 /* set the hook table for a given thread, allocating the queue if needed */
458 void set_queue_hooks( struct thread
*thread
, struct hook_table
*hooks
)
460 struct msg_queue
*queue
= thread
->queue
;
461 if (!queue
&& !(queue
= create_msg_queue( thread
, NULL
))) return;
462 if (queue
->hooks
) release_object( queue
->hooks
);
463 queue
->hooks
= hooks
;
466 /* check the queue status */
467 static inline int is_signaled( struct msg_queue
*queue
)
469 return ((queue
->wake_bits
& queue
->wake_mask
) || (queue
->changed_bits
& queue
->changed_mask
));
472 /* set some queue bits */
473 static inline void set_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
475 queue
->wake_bits
|= bits
;
476 queue
->changed_bits
|= bits
;
477 if (is_signaled( queue
)) wake_up( &queue
->obj
, 0 );
480 /* clear some queue bits */
481 static inline void clear_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
483 queue
->wake_bits
&= ~bits
;
484 queue
->changed_bits
&= ~bits
;
487 /* check whether msg is a keyboard message */
488 static inline int is_keyboard_msg( struct message
*msg
)
490 return (msg
->msg
>= WM_KEYFIRST
&& msg
->msg
<= WM_KEYLAST
);
493 /* check if message is matched by the filter */
494 static inline int check_msg_filter( unsigned int msg
, unsigned int first
, unsigned int last
)
496 return (msg
>= first
&& msg
<= last
);
499 /* check whether a message filter contains at least one potential hardware message */
500 static inline int filter_contains_hw_range( unsigned int first
, unsigned int last
)
502 /* hardware message ranges are (in numerical order):
503 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
504 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
505 * WM_MOUSEFIRST .. WM_MOUSELAST
507 if (last
< WM_NCMOUSEFIRST
) return 0;
508 if (first
> WM_NCMOUSELAST
&& last
< WM_INPUT_DEVICE_CHANGE
) return 0;
509 if (first
> WM_KEYLAST
&& last
< WM_MOUSEFIRST
) return 0;
510 if (first
> WM_MOUSELAST
) return 0;
514 /* get the QS_* bit corresponding to a given hardware message */
515 static inline int get_hardware_msg_bit( struct message
*msg
)
517 if (msg
->msg
== WM_INPUT_DEVICE_CHANGE
|| msg
->msg
== WM_INPUT
) return QS_RAWINPUT
;
518 if (msg
->msg
== WM_MOUSEMOVE
|| msg
->msg
== WM_NCMOUSEMOVE
) return QS_MOUSEMOVE
;
519 if (is_keyboard_msg( msg
)) return QS_KEY
;
520 return QS_MOUSEBUTTON
;
523 /* get the current thread queue, creating it if needed */
524 static inline struct msg_queue
*get_current_queue(void)
526 struct msg_queue
*queue
= current
->queue
;
527 if (!queue
) queue
= create_msg_queue( current
, NULL
);
531 /* get a (pseudo-)unique id to tag hardware messages */
532 static inline unsigned int get_unique_id(void)
534 static unsigned int id
;
535 if (!++id
) id
= 1; /* avoid an id of 0 */
539 /* try to merge a message with the last in the list; return 1 if successful */
540 static int merge_message( struct thread_input
*input
, const struct message
*msg
)
542 struct message
*prev
;
545 if (msg
->msg
!= WM_MOUSEMOVE
) return 0;
546 for (ptr
= list_tail( &input
->msg_list
); ptr
; ptr
= list_prev( &input
->msg_list
, ptr
))
548 prev
= LIST_ENTRY( ptr
, struct message
, entry
);
549 if (prev
->msg
!= WM_INPUT
) break;
552 if (prev
->result
) return 0;
553 if (prev
->win
&& msg
->win
&& prev
->win
!= msg
->win
) return 0;
554 if (prev
->msg
!= msg
->msg
) return 0;
555 if (prev
->type
!= msg
->type
) return 0;
556 /* now we can merge it */
557 prev
->wparam
= msg
->wparam
;
558 prev
->lparam
= msg
->lparam
;
561 prev
->time
= msg
->time
;
562 if (msg
->type
== MSG_HARDWARE
&& prev
->data
&& msg
->data
)
564 struct hardware_msg_data
*prev_data
= prev
->data
;
565 struct hardware_msg_data
*msg_data
= msg
->data
;
566 prev_data
->info
= msg_data
->info
;
569 list_add_tail( &input
->msg_list
, ptr
);
573 /* free a result structure */
574 static void free_result( struct message_result
*result
)
576 if (result
->timeout
) remove_timeout_user( result
->timeout
);
577 free( result
->data
);
578 if (result
->callback_msg
) free_message( result
->callback_msg
);
579 if (result
->hardware_msg
) free_message( result
->hardware_msg
);
580 if (result
->desktop
) release_object( result
->desktop
);
584 /* remove the result from the sender list it is on */
585 static inline void remove_result_from_sender( struct message_result
*result
)
587 assert( result
->sender
);
589 list_remove( &result
->sender_entry
);
590 result
->sender
= NULL
;
591 if (!result
->receiver
) free_result( result
);
594 /* store the message result in the appropriate structure */
595 static void store_message_result( struct message_result
*res
, lparam_t result
, unsigned int error
)
597 res
->result
= result
;
602 remove_timeout_user( res
->timeout
);
606 if (res
->hardware_msg
)
608 if (!error
&& result
) /* rejected by the hook */
609 free_message( res
->hardware_msg
);
611 queue_hardware_message( res
->desktop
, res
->hardware_msg
, 0 );
613 res
->hardware_msg
= NULL
;
618 if (res
->callback_msg
)
620 /* queue the callback message in the sender queue */
621 struct callback_msg_data
*data
= res
->callback_msg
->data
;
622 data
->result
= result
;
623 list_add_tail( &res
->sender
->msg_list
[SEND_MESSAGE
], &res
->callback_msg
->entry
);
624 set_queue_bits( res
->sender
, QS_SENDMESSAGE
);
625 res
->callback_msg
= NULL
;
626 remove_result_from_sender( res
);
630 /* wake sender queue if waiting on this result */
631 if (list_head(&res
->sender
->send_result
) == &res
->sender_entry
)
632 set_queue_bits( res
->sender
, QS_SMRESULT
);
635 else if (!res
->receiver
) free_result( res
);
638 /* free a message when deleting a queue or window */
639 static void free_message( struct message
*msg
)
641 struct message_result
*result
= msg
->result
;
645 result
->receiver
= NULL
;
646 store_message_result( result
, 0, STATUS_ACCESS_DENIED
/*FIXME*/ );
652 /* remove (and free) a message from a message list */
653 static void remove_queue_message( struct msg_queue
*queue
, struct message
*msg
,
654 enum message_kind kind
)
656 list_remove( &msg
->entry
);
660 if (list_empty( &queue
->msg_list
[kind
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
663 if (list_empty( &queue
->msg_list
[kind
] ) && !queue
->quit_message
)
664 clear_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
665 if (msg
->msg
== WM_HOTKEY
&& --queue
->hotkey_count
== 0)
666 clear_queue_bits( queue
, QS_HOTKEY
);
672 /* message timed out without getting a reply */
673 static void result_timeout( void *private )
675 struct message_result
*result
= private;
677 assert( !result
->replied
);
679 result
->timeout
= NULL
;
681 if (result
->msg
) /* not received yet */
683 struct message
*msg
= result
->msg
;
687 remove_queue_message( result
->receiver
, msg
, SEND_MESSAGE
);
688 result
->receiver
= NULL
;
690 store_message_result( result
, 0, STATUS_TIMEOUT
);
693 /* allocate and fill a message result structure */
694 static struct message_result
*alloc_message_result( struct msg_queue
*send_queue
,
695 struct msg_queue
*recv_queue
,
696 struct message
*msg
, timeout_t timeout
)
698 struct message_result
*result
= mem_alloc( sizeof(*result
) );
702 result
->sender
= send_queue
;
703 result
->receiver
= recv_queue
;
706 result
->data_size
= 0;
707 result
->timeout
= NULL
;
708 result
->hardware_msg
= NULL
;
709 result
->desktop
= NULL
;
710 result
->callback_msg
= NULL
;
712 if (msg
->type
== MSG_CALLBACK
)
714 struct message
*callback_msg
= mem_alloc( sizeof(*callback_msg
) );
721 callback_msg
->type
= MSG_CALLBACK_RESULT
;
722 callback_msg
->win
= msg
->win
;
723 callback_msg
->msg
= msg
->msg
;
724 callback_msg
->wparam
= 0;
725 callback_msg
->lparam
= 0;
726 callback_msg
->time
= get_tick_count();
727 callback_msg
->result
= NULL
;
728 /* steal the data from the original message */
729 callback_msg
->data
= msg
->data
;
730 callback_msg
->data_size
= msg
->data_size
;
734 result
->callback_msg
= callback_msg
;
735 list_add_head( &send_queue
->callback_result
, &result
->sender_entry
);
739 list_add_head( &send_queue
->send_result
, &result
->sender_entry
);
740 clear_queue_bits( send_queue
, QS_SMRESULT
);
743 if (timeout
!= TIMEOUT_INFINITE
)
744 result
->timeout
= add_timeout_user( timeout
, result_timeout
, result
);
749 /* receive a message, removing it from the sent queue */
750 static void receive_message( struct msg_queue
*queue
, struct message
*msg
,
751 struct get_message_reply
*reply
)
753 struct message_result
*result
= msg
->result
;
755 reply
->total
= msg
->data_size
;
756 if (msg
->data_size
> get_reply_max_size())
758 set_error( STATUS_BUFFER_OVERFLOW
);
761 reply
->type
= msg
->type
;
762 reply
->win
= msg
->win
;
763 reply
->msg
= msg
->msg
;
764 reply
->wparam
= msg
->wparam
;
765 reply
->lparam
= msg
->lparam
;
768 reply
->time
= msg
->time
;
770 if (msg
->data
) set_reply_data_ptr( msg
->data
, msg
->data_size
);
772 list_remove( &msg
->entry
);
773 /* put the result on the receiver result stack */
777 result
->recv_next
= queue
->recv_result
;
778 queue
->recv_result
= result
;
781 if (list_empty( &queue
->msg_list
[SEND_MESSAGE
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
784 /* set the result of the current received message */
785 static void reply_message( struct msg_queue
*queue
, lparam_t result
,
786 unsigned int error
, int remove
, const void *data
, data_size_t len
)
788 struct message_result
*res
= queue
->recv_result
;
792 queue
->recv_result
= res
->recv_next
;
793 res
->receiver
= NULL
;
794 if (!res
->sender
&& !res
->hardware_msg
) /* no one waiting for it */
802 if (len
&& (res
->data
= memdup( data
, len
))) res
->data_size
= len
;
803 store_message_result( res
, result
, error
);
807 static int match_window( user_handle_t win
, user_handle_t msg_win
)
810 if (win
== -1 || win
== 1) return !msg_win
;
811 if (msg_win
== win
) return 1;
812 return is_child_window( win
, msg_win
);
815 /* retrieve a posted message */
816 static int get_posted_message( struct msg_queue
*queue
, user_handle_t win
,
817 unsigned int first
, unsigned int last
, unsigned int flags
,
818 struct get_message_reply
*reply
)
822 /* check against the filters */
823 LIST_FOR_EACH_ENTRY( msg
, &queue
->msg_list
[POST_MESSAGE
], struct message
, entry
)
825 if (!match_window( win
, msg
->win
)) continue;
826 if (!check_msg_filter( msg
->msg
, first
, last
)) continue;
827 goto found
; /* found one */
831 /* return it to the app */
833 reply
->total
= msg
->data_size
;
834 if (msg
->data_size
> get_reply_max_size())
836 set_error( STATUS_BUFFER_OVERFLOW
);
839 reply
->type
= msg
->type
;
840 reply
->win
= msg
->win
;
841 reply
->msg
= msg
->msg
;
842 reply
->wparam
= msg
->wparam
;
843 reply
->lparam
= msg
->lparam
;
846 reply
->time
= msg
->time
;
848 if (flags
& PM_REMOVE
)
852 set_reply_data_ptr( msg
->data
, msg
->data_size
);
856 remove_queue_message( queue
, msg
, POST_MESSAGE
);
858 else if (msg
->data
) set_reply_data( msg
->data
, msg
->data_size
);
863 static int get_quit_message( struct msg_queue
*queue
, unsigned int flags
,
864 struct get_message_reply
*reply
)
866 if (queue
->quit_message
)
869 reply
->type
= MSG_POSTED
;
871 reply
->msg
= WM_QUIT
;
872 reply
->wparam
= queue
->exit_code
;
875 get_message_defaults( queue
, &reply
->x
, &reply
->y
, &reply
->time
);
877 if (flags
& PM_REMOVE
)
879 queue
->quit_message
= 0;
880 if (list_empty( &queue
->msg_list
[POST_MESSAGE
] ))
881 clear_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
889 /* empty a message list and free all the messages */
890 static void empty_msg_list( struct list
*list
)
894 while ((ptr
= list_head( list
)) != NULL
)
896 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
897 list_remove( &msg
->entry
);
902 /* cleanup all pending results when deleting a queue */
903 static void cleanup_results( struct msg_queue
*queue
)
907 while ((entry
= list_head( &queue
->send_result
)) != NULL
)
909 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
912 while ((entry
= list_head( &queue
->callback_result
)) != NULL
)
914 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
917 while (queue
->recv_result
)
918 reply_message( queue
, 0, STATUS_ACCESS_DENIED
/*FIXME*/, 1, NULL
, 0 );
921 /* check if the thread owning the queue is hung (not checking for messages) */
922 static int is_queue_hung( struct msg_queue
*queue
)
924 struct wait_queue_entry
*entry
;
926 if (current_time
- queue
->last_get_msg
<= 5 * TICKS_PER_SEC
)
927 return 0; /* less than 5 seconds since last get message -> not hung */
929 LIST_FOR_EACH_ENTRY( entry
, &queue
->obj
.wait_queue
, struct wait_queue_entry
, entry
)
931 if (get_wait_queue_thread(entry
)->queue
== queue
)
932 return 0; /* thread is waiting on queue -> not hung */
937 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
939 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
940 struct process
*process
= get_wait_queue_thread(entry
)->process
;
942 /* a thread can only wait on its own queue */
943 if (get_wait_queue_thread(entry
)->queue
!= queue
)
945 set_error( STATUS_ACCESS_DENIED
);
948 if (process
->idle_event
&& !(queue
->wake_mask
& QS_SMRESULT
)) set_event( process
->idle_event
);
950 if (queue
->fd
&& list_empty( &obj
->wait_queue
)) /* first on the queue */
951 set_fd_events( queue
->fd
, POLLIN
);
952 add_queue( obj
, entry
);
956 static void msg_queue_remove_queue(struct object
*obj
, struct wait_queue_entry
*entry
)
958 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
960 remove_queue( obj
, entry
);
961 if (queue
->fd
&& list_empty( &obj
->wait_queue
)) /* last on the queue is gone */
962 set_fd_events( queue
->fd
, 0 );
965 static void msg_queue_dump( struct object
*obj
, int verbose
)
967 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
968 fprintf( stderr
, "Msg queue bits=%x mask=%x\n",
969 queue
->wake_bits
, queue
->wake_mask
);
972 static int msg_queue_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
974 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
979 if ((ret
= check_fd_events( queue
->fd
, POLLIN
)))
980 /* stop waiting on select() if we are signaled */
981 set_fd_events( queue
->fd
, 0 );
982 else if (!list_empty( &obj
->wait_queue
))
983 /* restart waiting on poll() if we are no longer signaled */
984 set_fd_events( queue
->fd
, POLLIN
);
986 return ret
|| is_signaled( queue
);
989 static void msg_queue_satisfied( struct object
*obj
, struct wait_queue_entry
*entry
)
991 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
992 queue
->wake_mask
= 0;
993 queue
->changed_mask
= 0;
996 static void msg_queue_destroy( struct object
*obj
)
998 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
1000 struct hotkey
*hotkey
, *hotkey2
;
1003 cleanup_results( queue
);
1004 for (i
= 0; i
< NB_MSG_KINDS
; i
++) empty_msg_list( &queue
->msg_list
[i
] );
1006 LIST_FOR_EACH_ENTRY_SAFE( hotkey
, hotkey2
, &queue
->input
->desktop
->hotkeys
, struct hotkey
, entry
)
1008 if (hotkey
->queue
== queue
)
1010 list_remove( &hotkey
->entry
);
1015 while ((ptr
= list_head( &queue
->pending_timers
)))
1017 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1018 list_remove( &timer
->entry
);
1021 while ((ptr
= list_head( &queue
->expired_timers
)))
1023 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1024 list_remove( &timer
->entry
);
1027 if (queue
->timeout
) remove_timeout_user( queue
->timeout
);
1028 queue
->input
->cursor_count
-= queue
->cursor_count
;
1029 release_object( queue
->input
);
1030 if (queue
->hooks
) release_object( queue
->hooks
);
1031 if (queue
->fd
) release_object( queue
->fd
);
1034 static void msg_queue_poll_event( struct fd
*fd
, int event
)
1036 struct msg_queue
*queue
= get_fd_user( fd
);
1037 assert( queue
->obj
.ops
== &msg_queue_ops
);
1039 if (event
& (POLLERR
| POLLHUP
)) set_fd_events( fd
, -1 );
1040 else set_fd_events( queue
->fd
, 0 );
1041 wake_up( &queue
->obj
, 0 );
1044 static void thread_input_dump( struct object
*obj
, int verbose
)
1046 struct thread_input
*input
= (struct thread_input
*)obj
;
1047 fprintf( stderr
, "Thread input focus=%08x capture=%08x active=%08x\n",
1048 input
->focus
, input
->capture
, input
->active
);
1051 static void thread_input_destroy( struct object
*obj
)
1053 struct thread_input
*input
= (struct thread_input
*)obj
;
1055 empty_msg_list( &input
->msg_list
);
1058 if (input
->desktop
->foreground_input
== input
) set_foreground_input( input
->desktop
, NULL
);
1059 release_object( input
->desktop
);
1063 /* fix the thread input data when a window is destroyed */
1064 static inline void thread_input_cleanup_window( struct msg_queue
*queue
, user_handle_t window
)
1066 struct thread_input
*input
= queue
->input
;
1068 if (window
== input
->focus
) input
->focus
= 0;
1069 if (window
== input
->capture
) input
->capture
= 0;
1070 if (window
== input
->active
) input
->active
= 0;
1071 if (window
== input
->menu_owner
) input
->menu_owner
= 0;
1072 if (window
== input
->move_size
) input
->move_size
= 0;
1073 if (window
== input
->caret
) set_caret_window( input
, 0 );
1076 /* check if the specified window can be set in the input data of a given queue */
1077 static int check_queue_input_window( struct msg_queue
*queue
, user_handle_t window
)
1079 struct thread
*thread
;
1082 if (!window
) return 1; /* we can always clear the data */
1084 if ((thread
= get_window_thread( window
)))
1086 ret
= (queue
->input
== thread
->queue
->input
);
1087 if (!ret
) set_error( STATUS_ACCESS_DENIED
);
1088 release_object( thread
);
1090 else set_error( STATUS_INVALID_HANDLE
);
1095 /* make sure the specified thread has a queue */
1096 int init_thread_queue( struct thread
*thread
)
1098 if (thread
->queue
) return 1;
1099 return (create_msg_queue( thread
, NULL
) != NULL
);
1102 /* attach two thread input data structures */
1103 int attach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
1105 struct desktop
*desktop
;
1106 struct thread_input
*input
;
1109 if (!thread_to
->queue
&& !(thread_to
->queue
= create_msg_queue( thread_to
, NULL
))) return 0;
1110 if (!(desktop
= get_thread_desktop( thread_from
, 0 ))) return 0;
1111 input
= (struct thread_input
*)grab_object( thread_to
->queue
->input
);
1112 if (input
->desktop
!= desktop
)
1114 set_error( STATUS_ACCESS_DENIED
);
1115 release_object( input
);
1116 release_object( desktop
);
1119 release_object( desktop
);
1121 if (thread_from
->queue
)
1123 if (!input
->focus
) input
->focus
= thread_from
->queue
->input
->focus
;
1124 if (!input
->active
) input
->active
= thread_from
->queue
->input
->active
;
1127 ret
= assign_thread_input( thread_from
, input
);
1128 if (ret
) memset( input
->keystate
, 0, sizeof(input
->keystate
) );
1129 release_object( input
);
1133 /* detach two thread input data structures */
1134 void detach_thread_input( struct thread
*thread_from
)
1136 struct thread
*thread
;
1137 struct thread_input
*input
, *old_input
= thread_from
->queue
->input
;
1139 if ((input
= create_thread_input( thread_from
)))
1141 if (old_input
->focus
&& (thread
= get_window_thread( old_input
->focus
)))
1143 if (thread
== thread_from
)
1145 input
->focus
= old_input
->focus
;
1146 old_input
->focus
= 0;
1148 release_object( thread
);
1150 if (old_input
->active
&& (thread
= get_window_thread( old_input
->active
)))
1152 if (thread
== thread_from
)
1154 input
->active
= old_input
->active
;
1155 old_input
->active
= 0;
1157 release_object( thread
);
1159 assign_thread_input( thread_from
, input
);
1160 release_object( input
);
1165 /* set the next timer to expire */
1166 static void set_next_timer( struct msg_queue
*queue
)
1172 remove_timeout_user( queue
->timeout
);
1173 queue
->timeout
= NULL
;
1175 if ((ptr
= list_head( &queue
->pending_timers
)))
1177 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1178 queue
->timeout
= add_timeout_user( abstime_to_timeout(timer
->when
), timer_callback
, queue
);
1180 /* set/clear QS_TIMER bit */
1181 if (list_empty( &queue
->expired_timers
))
1182 clear_queue_bits( queue
, QS_TIMER
);
1184 set_queue_bits( queue
, QS_TIMER
);
1187 /* find a timer from its window and id */
1188 static struct timer
*find_timer( struct msg_queue
*queue
, user_handle_t win
,
1189 unsigned int msg
, lparam_t id
)
1193 /* we need to search both lists */
1195 LIST_FOR_EACH( ptr
, &queue
->pending_timers
)
1197 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1198 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
1200 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
1202 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1203 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
1208 /* callback for the next timer expiration */
1209 static void timer_callback( void *private )
1211 struct msg_queue
*queue
= private;
1214 queue
->timeout
= NULL
;
1215 /* move on to the next timer */
1216 ptr
= list_head( &queue
->pending_timers
);
1218 list_add_tail( &queue
->expired_timers
, ptr
);
1219 set_next_timer( queue
);
1222 /* link a timer at its rightful place in the queue list */
1223 static void link_timer( struct msg_queue
*queue
, struct timer
*timer
)
1227 for (ptr
= queue
->pending_timers
.next
; ptr
!= &queue
->pending_timers
; ptr
= ptr
->next
)
1229 struct timer
*t
= LIST_ENTRY( ptr
, struct timer
, entry
);
1230 if (t
->when
<= timer
->when
) break;
1232 list_add_before( ptr
, &timer
->entry
);
1235 /* remove a timer from the queue timer list and free it */
1236 static void free_timer( struct msg_queue
*queue
, struct timer
*timer
)
1238 list_remove( &timer
->entry
);
1240 set_next_timer( queue
);
1243 /* restart an expired timer */
1244 static void restart_timer( struct msg_queue
*queue
, struct timer
*timer
)
1246 list_remove( &timer
->entry
);
1247 while (-timer
->when
<= monotonic_time
) timer
->when
-= (timeout_t
)timer
->rate
* 10000;
1248 link_timer( queue
, timer
);
1249 set_next_timer( queue
);
1252 /* find an expired timer matching the filtering parameters */
1253 static struct timer
*find_expired_timer( struct msg_queue
*queue
, user_handle_t win
,
1254 unsigned int get_first
, unsigned int get_last
,
1259 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
1261 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1262 if (win
&& timer
->win
!= win
) continue;
1263 if (check_msg_filter( timer
->msg
, get_first
, get_last
))
1265 if (remove
) restart_timer( queue
, timer
);
1273 static struct timer
*set_timer( struct msg_queue
*queue
, unsigned int rate
)
1275 struct timer
*timer
= mem_alloc( sizeof(*timer
) );
1278 timer
->rate
= max( rate
, 1 );
1279 timer
->when
= -monotonic_time
- (timeout_t
)timer
->rate
* 10000;
1280 link_timer( queue
, timer
);
1281 /* check if we replaced the next timer */
1282 if (list_head( &queue
->pending_timers
) == &timer
->entry
) set_next_timer( queue
);
1287 /* change the input key state for a given key */
1288 static void set_input_key_state( unsigned char *keystate
, unsigned char key
, int down
)
1292 if (!(keystate
[key
] & 0x80)) keystate
[key
] ^= 0x01;
1293 keystate
[key
] |= down
;
1295 else keystate
[key
] &= ~0x80;
1298 /* update the input key state for a keyboard message */
1299 static void update_input_key_state( struct desktop
*desktop
, unsigned char *keystate
,
1300 unsigned int msg
, lparam_t wparam
)
1307 case WM_LBUTTONDOWN
:
1308 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1311 set_input_key_state( keystate
, VK_LBUTTON
, down
);
1313 case WM_MBUTTONDOWN
:
1314 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1317 set_input_key_state( keystate
, VK_MBUTTON
, down
);
1319 case WM_RBUTTONDOWN
:
1320 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1323 set_input_key_state( keystate
, VK_RBUTTON
, down
);
1325 case WM_XBUTTONDOWN
:
1326 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1329 if (wparam
>> 16 == XBUTTON1
) set_input_key_state( keystate
, VK_XBUTTON1
, down
);
1330 else if (wparam
>> 16 == XBUTTON2
) set_input_key_state( keystate
, VK_XBUTTON2
, down
);
1334 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1338 key
= (unsigned char)wparam
;
1339 set_input_key_state( keystate
, key
, down
);
1344 down
= (keystate
[VK_LCONTROL
] | keystate
[VK_RCONTROL
]) & 0x80;
1345 set_input_key_state( keystate
, VK_CONTROL
, down
);
1349 down
= (keystate
[VK_LMENU
] | keystate
[VK_RMENU
]) & 0x80;
1350 set_input_key_state( keystate
, VK_MENU
, down
);
1354 down
= (keystate
[VK_LSHIFT
] | keystate
[VK_RSHIFT
]) & 0x80;
1355 set_input_key_state( keystate
, VK_SHIFT
, down
);
1362 /* update the desktop key state according to a mouse message flags */
1363 static void update_desktop_mouse_state( struct desktop
*desktop
, unsigned int flags
,
1364 int x
, int y
, lparam_t wparam
)
1366 if (flags
& MOUSEEVENTF_MOVE
)
1367 update_desktop_cursor_pos( desktop
, x
, y
);
1368 if (flags
& MOUSEEVENTF_LEFTDOWN
)
1369 update_input_key_state( desktop
, desktop
->keystate
, WM_LBUTTONDOWN
, wparam
);
1370 if (flags
& MOUSEEVENTF_LEFTUP
)
1371 update_input_key_state( desktop
, desktop
->keystate
, WM_LBUTTONUP
, wparam
);
1372 if (flags
& MOUSEEVENTF_RIGHTDOWN
)
1373 update_input_key_state( desktop
, desktop
->keystate
, WM_RBUTTONDOWN
, wparam
);
1374 if (flags
& MOUSEEVENTF_RIGHTUP
)
1375 update_input_key_state( desktop
, desktop
->keystate
, WM_RBUTTONUP
, wparam
);
1376 if (flags
& MOUSEEVENTF_MIDDLEDOWN
)
1377 update_input_key_state( desktop
, desktop
->keystate
, WM_MBUTTONDOWN
, wparam
);
1378 if (flags
& MOUSEEVENTF_MIDDLEUP
)
1379 update_input_key_state( desktop
, desktop
->keystate
, WM_MBUTTONUP
, wparam
);
1380 if (flags
& MOUSEEVENTF_XDOWN
)
1381 update_input_key_state( desktop
, desktop
->keystate
, WM_XBUTTONDOWN
, wparam
);
1382 if (flags
& MOUSEEVENTF_XUP
)
1383 update_input_key_state( desktop
, desktop
->keystate
, WM_XBUTTONUP
, wparam
);
1386 /* release the hardware message currently being processed by the given thread */
1387 static void release_hardware_message( struct msg_queue
*queue
, unsigned int hw_id
)
1389 struct thread_input
*input
= queue
->input
;
1390 struct message
*msg
, *other
;
1393 LIST_FOR_EACH_ENTRY( msg
, &input
->msg_list
, struct message
, entry
)
1395 if (msg
->unique_id
== hw_id
) break;
1397 if (&msg
->entry
== &input
->msg_list
) return; /* not found */
1399 /* clear the queue bit for that message */
1400 clr_bit
= get_hardware_msg_bit( msg
);
1401 LIST_FOR_EACH_ENTRY( other
, &input
->msg_list
, struct message
, entry
)
1403 if (other
!= msg
&& get_hardware_msg_bit( other
) == clr_bit
)
1409 if (clr_bit
) clear_queue_bits( queue
, clr_bit
);
1411 update_input_key_state( input
->desktop
, input
->keystate
, msg
->msg
, msg
->wparam
);
1412 list_remove( &msg
->entry
);
1413 free_message( msg
);
1416 static int queue_hotkey_message( struct desktop
*desktop
, struct message
*msg
)
1418 struct hotkey
*hotkey
;
1419 unsigned int modifiers
= 0;
1421 if (msg
->msg
!= WM_KEYDOWN
) return 0;
1423 if (desktop
->keystate
[VK_MENU
] & 0x80) modifiers
|= MOD_ALT
;
1424 if (desktop
->keystate
[VK_CONTROL
] & 0x80) modifiers
|= MOD_CONTROL
;
1425 if (desktop
->keystate
[VK_SHIFT
] & 0x80) modifiers
|= MOD_SHIFT
;
1426 if ((desktop
->keystate
[VK_LWIN
] & 0x80) || (desktop
->keystate
[VK_RWIN
] & 0x80)) modifiers
|= MOD_WIN
;
1428 LIST_FOR_EACH_ENTRY( hotkey
, &desktop
->hotkeys
, struct hotkey
, entry
)
1430 if (hotkey
->vkey
!= msg
->wparam
) continue;
1431 if ((hotkey
->flags
& (MOD_ALT
|MOD_CONTROL
|MOD_SHIFT
|MOD_WIN
)) == modifiers
) goto found
;
1437 msg
->type
= MSG_POSTED
;
1438 msg
->win
= hotkey
->win
;
1439 msg
->msg
= WM_HOTKEY
;
1440 msg
->wparam
= hotkey
->id
;
1441 msg
->lparam
= ((hotkey
->vkey
& 0xffff) << 16) | modifiers
;
1447 list_add_tail( &hotkey
->queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1448 set_queue_bits( hotkey
->queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
|QS_HOTKEY
);
1449 hotkey
->queue
->hotkey_count
++;
1453 /* find the window that should receive a given hardware message */
1454 static user_handle_t
find_hardware_message_window( struct desktop
*desktop
, struct thread_input
*input
,
1455 struct message
*msg
, unsigned int *msg_code
,
1456 struct thread
**thread
)
1458 user_handle_t win
= 0;
1461 *msg_code
= msg
->msg
;
1462 if (msg
->msg
== WM_INPUT
)
1464 if (!(win
= msg
->win
) && input
) win
= input
->focus
;
1466 else if (is_keyboard_msg( msg
))
1468 if (input
&& !(win
= input
->focus
))
1470 win
= input
->active
;
1471 if (*msg_code
< WM_SYSKEYDOWN
) *msg_code
+= WM_SYSKEYDOWN
- WM_KEYDOWN
;
1474 else if (!input
|| !(win
= input
->capture
)) /* mouse message */
1476 if (is_window_visible( msg
->win
) && !is_window_transparent( msg
->win
)) win
= msg
->win
;
1477 else win
= shallow_window_from_point( desktop
, msg
->x
, msg
->y
);
1479 *thread
= window_thread_from_point( win
, msg
->x
, msg
->y
);
1483 *thread
= get_window_thread( win
);
1487 static struct rawinput_device_entry
*find_rawinput_device( unsigned short usage_page
, unsigned short usage
)
1489 struct rawinput_device_entry
*e
;
1491 LIST_FOR_EACH_ENTRY( e
, ¤t
->process
->rawinput_devices
, struct rawinput_device_entry
, entry
)
1493 if (e
->device
.usage_page
!= usage_page
|| e
->device
.usage
!= usage
) continue;
1500 static void update_rawinput_device(const struct rawinput_device
*device
)
1502 struct rawinput_device_entry
*e
;
1504 if (!(e
= find_rawinput_device( device
->usage_page
, device
->usage
)))
1506 if (!(e
= mem_alloc( sizeof(*e
) ))) return;
1507 list_add_tail( ¤t
->process
->rawinput_devices
, &e
->entry
);
1510 if (device
->flags
& RIDEV_REMOVE
)
1512 list_remove( &e
->entry
);
1517 e
->device
= *device
;
1518 e
->device
.target
= get_user_full_handle( e
->device
.target
);
1521 /* queue a hardware message into a given thread input */
1522 static void queue_hardware_message( struct desktop
*desktop
, struct message
*msg
, int always_queue
)
1525 struct thread
*thread
;
1526 struct thread_input
*input
;
1527 unsigned int msg_code
;
1529 update_input_key_state( desktop
, desktop
->keystate
, msg
->msg
, msg
->wparam
);
1530 last_input_time
= get_tick_count();
1531 if (msg
->msg
!= WM_MOUSEMOVE
) always_queue
= 1;
1533 if (is_keyboard_msg( msg
))
1535 if (queue_hotkey_message( desktop
, msg
)) return;
1536 if (desktop
->keystate
[VK_MENU
] & 0x80) msg
->lparam
|= KF_ALTDOWN
<< 16;
1537 if (msg
->wparam
== VK_SHIFT
|| msg
->wparam
== VK_LSHIFT
|| msg
->wparam
== VK_RSHIFT
)
1538 msg
->lparam
&= ~(KF_EXTENDED
<< 16);
1540 else if (msg
->msg
!= WM_INPUT
)
1542 if (msg
->msg
== WM_MOUSEMOVE
&& update_desktop_cursor_pos( desktop
, msg
->x
, msg
->y
)) always_queue
= 1;
1543 if (desktop
->keystate
[VK_LBUTTON
] & 0x80) msg
->wparam
|= MK_LBUTTON
;
1544 if (desktop
->keystate
[VK_MBUTTON
] & 0x80) msg
->wparam
|= MK_MBUTTON
;
1545 if (desktop
->keystate
[VK_RBUTTON
] & 0x80) msg
->wparam
|= MK_RBUTTON
;
1546 if (desktop
->keystate
[VK_SHIFT
] & 0x80) msg
->wparam
|= MK_SHIFT
;
1547 if (desktop
->keystate
[VK_CONTROL
] & 0x80) msg
->wparam
|= MK_CONTROL
;
1548 if (desktop
->keystate
[VK_XBUTTON1
] & 0x80) msg
->wparam
|= MK_XBUTTON1
;
1549 if (desktop
->keystate
[VK_XBUTTON2
] & 0x80) msg
->wparam
|= MK_XBUTTON2
;
1551 msg
->x
= desktop
->cursor
.x
;
1552 msg
->y
= desktop
->cursor
.y
;
1554 if (msg
->win
&& (thread
= get_window_thread( msg
->win
)))
1556 input
= thread
->queue
->input
;
1557 release_object( thread
);
1559 else input
= desktop
->foreground_input
;
1561 win
= find_hardware_message_window( desktop
, input
, msg
, &msg_code
, &thread
);
1562 if (!win
|| !thread
)
1564 if (input
) update_input_key_state( input
->desktop
, input
->keystate
, msg
->msg
, msg
->wparam
);
1565 free_message( msg
);
1568 input
= thread
->queue
->input
;
1570 if (win
!= desktop
->cursor
.win
) always_queue
= 1;
1571 desktop
->cursor
.win
= win
;
1573 if (!always_queue
|| merge_message( input
, msg
)) free_message( msg
);
1576 msg
->unique_id
= 0; /* will be set once we return it to the app */
1577 list_add_tail( &input
->msg_list
, &msg
->entry
);
1578 set_queue_bits( thread
->queue
, get_hardware_msg_bit(msg
) );
1580 release_object( thread
);
1583 /* send the low-level hook message for a given hardware message */
1584 static int send_hook_ll_message( struct desktop
*desktop
, struct message
*hardware_msg
,
1585 const hw_input_t
*input
, struct msg_queue
*sender
)
1587 struct thread
*hook_thread
;
1588 struct msg_queue
*queue
;
1589 struct message
*msg
;
1590 timeout_t timeout
= 2000 * -10000; /* FIXME: load from registry */
1591 int id
= (input
->type
== INPUT_MOUSE
) ? WH_MOUSE_LL
: WH_KEYBOARD_LL
;
1593 if (!(hook_thread
= get_first_global_hook( id
))) return 0;
1594 if (!(queue
= hook_thread
->queue
)) return 0;
1595 if (is_queue_hung( queue
)) return 0;
1597 if (!(msg
= mem_alloc( sizeof(*msg
) ))) return 0;
1599 msg
->type
= MSG_HOOK_LL
;
1602 msg
->wparam
= hardware_msg
->msg
;
1603 msg
->x
= hardware_msg
->x
;
1604 msg
->y
= hardware_msg
->y
;
1605 msg
->time
= hardware_msg
->time
;
1606 msg
->data_size
= hardware_msg
->data_size
;
1609 if (input
->type
== INPUT_KEYBOARD
)
1611 unsigned short vkey
= input
->kbd
.vkey
;
1612 if (input
->kbd
.flags
& KEYEVENTF_UNICODE
) vkey
= VK_PACKET
;
1613 msg
->lparam
= (input
->kbd
.scan
<< 16) | vkey
;
1615 else msg
->lparam
= input
->mouse
.data
<< 16;
1617 if (!(msg
->data
= memdup( hardware_msg
->data
, hardware_msg
->data_size
)) ||
1618 !(msg
->result
= alloc_message_result( sender
, queue
, msg
, timeout
)))
1620 free_message( msg
);
1623 msg
->result
->hardware_msg
= hardware_msg
;
1624 msg
->result
->desktop
= (struct desktop
*)grab_object( desktop
);
1625 list_add_tail( &queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1626 set_queue_bits( queue
, QS_SENDMESSAGE
);
1630 /* get the foreground thread for a desktop and a window receiving input */
1631 static struct thread
*get_foreground_thread( struct desktop
*desktop
, user_handle_t window
)
1633 /* if desktop has no foreground process, assume the receiving window is */
1634 if (desktop
->foreground_input
) return get_window_thread( desktop
->foreground_input
->focus
);
1635 if (window
) return get_window_thread( window
);
1639 struct rawinput_message
1641 struct thread
*foreground
;
1642 struct desktop
*desktop
;
1643 struct hw_msg_source source
;
1645 struct hardware_msg_data data
;
1648 /* check if process is supposed to receive a WM_INPUT message and eventually queue it */
1649 static int queue_rawinput_message( struct process
* process
, void *arg
)
1651 const struct rawinput_message
* raw_msg
= arg
;
1652 const struct rawinput_device
*device
= NULL
;
1653 struct desktop
*target_desktop
= NULL
;
1654 struct thread
*target_thread
= NULL
;
1655 struct message
*msg
;
1656 int wparam
= RIM_INPUT
;
1658 if (raw_msg
->data
.rawinput
.type
== RIM_TYPEMOUSE
)
1659 device
= process
->rawinput_mouse
;
1660 else if (raw_msg
->data
.rawinput
.type
== RIM_TYPEKEYBOARD
)
1661 device
= process
->rawinput_kbd
;
1662 if (!device
) return 0;
1664 if (process
!= raw_msg
->foreground
->process
)
1666 if (!(device
->flags
& RIDEV_INPUTSINK
)) goto done
;
1667 if (!(target_thread
= get_window_thread( device
->target
))) goto done
;
1668 if (!(target_desktop
= get_thread_desktop( target_thread
, 0 ))) goto done
;
1669 if (target_desktop
!= raw_msg
->desktop
) goto done
;
1670 wparam
= RIM_INPUTSINK
;
1673 if (!(msg
= alloc_hardware_message( raw_msg
->data
.info
, raw_msg
->source
, raw_msg
->time
)))
1676 msg
->win
= device
->target
;
1677 msg
->msg
= WM_INPUT
;
1678 msg
->wparam
= wparam
;
1680 memcpy( msg
->data
, &raw_msg
->data
, sizeof(raw_msg
->data
) );
1682 queue_hardware_message( raw_msg
->desktop
, msg
, 1 );
1685 if (target_thread
) release_object( target_thread
);
1686 if (target_desktop
) release_object( target_desktop
);
1690 /* queue a hardware message for a mouse event */
1691 static int queue_mouse_message( struct desktop
*desktop
, user_handle_t win
, const hw_input_t
*input
,
1692 unsigned int origin
, struct msg_queue
*sender
)
1694 const struct rawinput_device
*device
;
1695 struct hardware_msg_data
*msg_data
;
1696 struct rawinput_message raw_msg
;
1697 struct message
*msg
;
1698 struct thread
*foreground
;
1699 unsigned int i
, time
, flags
;
1700 struct hw_msg_source source
= { IMDT_MOUSE
, origin
};
1703 static const unsigned int messages
[] =
1705 WM_MOUSEMOVE
, /* 0x0001 = MOUSEEVENTF_MOVE */
1706 WM_LBUTTONDOWN
, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1707 WM_LBUTTONUP
, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1708 WM_RBUTTONDOWN
, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1709 WM_RBUTTONUP
, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1710 WM_MBUTTONDOWN
, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1711 WM_MBUTTONUP
, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1712 WM_XBUTTONDOWN
, /* 0x0080 = MOUSEEVENTF_XDOWN */
1713 WM_XBUTTONUP
, /* 0x0100 = MOUSEEVENTF_XUP */
1714 0, /* 0x0200 = unused */
1715 0, /* 0x0400 = unused */
1716 WM_MOUSEWHEEL
, /* 0x0800 = MOUSEEVENTF_WHEEL */
1717 WM_MOUSEHWHEEL
/* 0x1000 = MOUSEEVENTF_HWHEEL */
1720 desktop
->cursor
.last_change
= get_tick_count();
1721 flags
= input
->mouse
.flags
;
1722 time
= input
->mouse
.time
;
1723 if (!time
) time
= desktop
->cursor
.last_change
;
1725 if (flags
& MOUSEEVENTF_MOVE
)
1727 if (flags
& MOUSEEVENTF_ABSOLUTE
)
1731 if (flags
& ~(MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
) &&
1732 x
== desktop
->cursor
.x
&& y
== desktop
->cursor
.y
)
1733 flags
&= ~MOUSEEVENTF_MOVE
;
1737 x
= desktop
->cursor
.x
+ input
->mouse
.x
;
1738 y
= desktop
->cursor
.y
+ input
->mouse
.y
;
1743 x
= desktop
->cursor
.x
;
1744 y
= desktop
->cursor
.y
;
1747 if ((foreground
= get_foreground_thread( desktop
, win
)))
1749 raw_msg
.foreground
= foreground
;
1750 raw_msg
.desktop
= desktop
;
1751 raw_msg
.source
= source
;
1752 raw_msg
.time
= time
;
1754 msg_data
= &raw_msg
.data
;
1755 msg_data
->info
= input
->mouse
.info
;
1756 msg_data
->flags
= flags
;
1757 msg_data
->rawinput
.type
= RIM_TYPEMOUSE
;
1758 msg_data
->rawinput
.mouse
.x
= x
- desktop
->cursor
.x
;
1759 msg_data
->rawinput
.mouse
.y
= y
- desktop
->cursor
.y
;
1760 msg_data
->rawinput
.mouse
.data
= input
->mouse
.data
;
1762 enum_processes( queue_rawinput_message
, &raw_msg
);
1763 release_object( foreground
);
1766 if ((device
= current
->process
->rawinput_mouse
) && (device
->flags
& RIDEV_NOLEGACY
))
1768 update_desktop_mouse_state( desktop
, flags
, x
, y
, input
->mouse
.data
<< 16 );
1772 for (i
= 0; i
< ARRAY_SIZE( messages
); i
++)
1774 if (!messages
[i
]) continue;
1775 if (!(flags
& (1 << i
))) continue;
1778 if (!(msg
= alloc_hardware_message( input
->mouse
.info
, source
, time
))) return 0;
1779 msg_data
= msg
->data
;
1781 msg
->win
= get_user_full_handle( win
);
1782 msg
->msg
= messages
[i
];
1783 msg
->wparam
= input
->mouse
.data
<< 16;
1787 if (origin
== IMO_INJECTED
) msg_data
->flags
= LLMHF_INJECTED
;
1789 /* specify a sender only when sending the last message */
1790 if (!(flags
& ((1 << ARRAY_SIZE( messages
)) - 1)))
1792 if (!(wait
= send_hook_ll_message( desktop
, msg
, input
, sender
)))
1793 queue_hardware_message( desktop
, msg
, 0 );
1795 else if (!send_hook_ll_message( desktop
, msg
, input
, NULL
))
1796 queue_hardware_message( desktop
, msg
, 0 );
1801 /* queue a hardware message for a keyboard event */
1802 static int queue_keyboard_message( struct desktop
*desktop
, user_handle_t win
, const hw_input_t
*input
,
1803 unsigned int origin
, struct msg_queue
*sender
)
1805 struct hw_msg_source source
= { IMDT_KEYBOARD
, origin
};
1806 const struct rawinput_device
*device
;
1807 struct hardware_msg_data
*msg_data
;
1808 struct rawinput_message raw_msg
;
1809 struct message
*msg
;
1810 struct thread
*foreground
;
1811 unsigned char vkey
= input
->kbd
.vkey
;
1812 unsigned int message_code
, time
;
1815 if (!(time
= input
->kbd
.time
)) time
= get_tick_count();
1817 if (!(input
->kbd
.flags
& KEYEVENTF_UNICODE
))
1824 vkey
= (input
->kbd
.flags
& KEYEVENTF_EXTENDEDKEY
) ? VK_RMENU
: VK_LMENU
;
1829 vkey
= (input
->kbd
.flags
& KEYEVENTF_EXTENDEDKEY
) ? VK_RCONTROL
: VK_LCONTROL
;
1834 vkey
= (input
->kbd
.flags
& KEYEVENTF_EXTENDEDKEY
) ? VK_RSHIFT
: VK_LSHIFT
;
1839 message_code
= (input
->kbd
.flags
& KEYEVENTF_KEYUP
) ? WM_KEYUP
: WM_KEYDOWN
;
1844 if (input
->kbd
.flags
& KEYEVENTF_KEYUP
)
1846 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1847 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1848 if ((desktop
->keystate
[VK_MENU
] & 0x82) != 0x82) break;
1849 message_code
= WM_SYSKEYUP
;
1850 desktop
->keystate
[VK_MENU
] &= ~0x02;
1854 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1855 if (desktop
->keystate
[VK_CONTROL
] & 0x80) break;
1856 message_code
= WM_SYSKEYDOWN
;
1857 desktop
->keystate
[VK_MENU
] |= 0x02;
1863 /* send WM_SYSKEYUP on release if Alt still pressed */
1864 if (!(input
->kbd
.flags
& KEYEVENTF_KEYUP
)) break;
1865 if (!(desktop
->keystate
[VK_MENU
] & 0x80)) break;
1866 message_code
= WM_SYSKEYUP
;
1867 desktop
->keystate
[VK_MENU
] &= ~0x02;
1871 /* send WM_SYSKEY for Alt-anykey and for F10 */
1872 if (desktop
->keystate
[VK_CONTROL
] & 0x80) break;
1873 if (!(desktop
->keystate
[VK_MENU
] & 0x80)) break;
1876 message_code
= (input
->kbd
.flags
& KEYEVENTF_KEYUP
) ? WM_SYSKEYUP
: WM_SYSKEYDOWN
;
1877 desktop
->keystate
[VK_MENU
] &= ~0x02;
1881 if ((foreground
= get_foreground_thread( desktop
, win
)))
1883 raw_msg
.foreground
= foreground
;
1884 raw_msg
.desktop
= desktop
;
1885 raw_msg
.source
= source
;
1886 raw_msg
.time
= time
;
1888 msg_data
= &raw_msg
.data
;
1889 msg_data
->info
= input
->kbd
.info
;
1890 msg_data
->flags
= input
->kbd
.flags
;
1891 msg_data
->rawinput
.type
= RIM_TYPEKEYBOARD
;
1892 msg_data
->rawinput
.kbd
.message
= message_code
;
1893 msg_data
->rawinput
.kbd
.vkey
= vkey
;
1894 msg_data
->rawinput
.kbd
.scan
= input
->kbd
.scan
;
1896 enum_processes( queue_rawinput_message
, &raw_msg
);
1897 release_object( foreground
);
1900 if ((device
= current
->process
->rawinput_kbd
) && (device
->flags
& RIDEV_NOLEGACY
))
1902 update_input_key_state( desktop
, desktop
->keystate
, message_code
, vkey
);
1906 if (!(msg
= alloc_hardware_message( input
->kbd
.info
, source
, time
))) return 0;
1907 msg_data
= msg
->data
;
1909 msg
->win
= get_user_full_handle( win
);
1910 msg
->msg
= message_code
;
1911 msg
->lparam
= (input
->kbd
.scan
<< 16) | 1u; /* repeat count */
1912 if (origin
== IMO_INJECTED
) msg_data
->flags
= LLKHF_INJECTED
;
1914 if (input
->kbd
.flags
& KEYEVENTF_UNICODE
&& !vkey
)
1916 msg
->wparam
= VK_PACKET
;
1920 unsigned int flags
= 0;
1921 if (input
->kbd
.flags
& KEYEVENTF_EXTENDEDKEY
) flags
|= KF_EXTENDED
;
1922 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1923 if (input
->kbd
.flags
& KEYEVENTF_KEYUP
) flags
|= KF_REPEAT
| KF_UP
;
1924 else if (desktop
->keystate
[vkey
] & 0x80) flags
|= KF_REPEAT
;
1927 msg
->lparam
|= flags
<< 16;
1928 msg_data
->flags
|= (flags
& (KF_EXTENDED
| KF_ALTDOWN
| KF_UP
)) >> 8;
1931 if (!(wait
= send_hook_ll_message( desktop
, msg
, input
, sender
)))
1932 queue_hardware_message( desktop
, msg
, 1 );
1937 /* queue a hardware message for a custom type of event */
1938 static void queue_custom_hardware_message( struct desktop
*desktop
, user_handle_t win
,
1939 unsigned int origin
, const hw_input_t
*input
)
1941 struct hw_msg_source source
= { IMDT_UNAVAILABLE
, origin
};
1942 struct message
*msg
;
1944 if (!(msg
= alloc_hardware_message( 0, source
, get_tick_count() ))) return;
1946 msg
->win
= get_user_full_handle( win
);
1947 msg
->msg
= input
->hw
.msg
;
1949 msg
->lparam
= input
->hw
.lparam
;
1950 msg
->x
= desktop
->cursor
.x
;
1951 msg
->y
= desktop
->cursor
.y
;
1953 queue_hardware_message( desktop
, msg
, 1 );
1956 /* check message filter for a hardware message */
1957 static int check_hw_message_filter( user_handle_t win
, unsigned int msg_code
,
1958 user_handle_t filter_win
, unsigned int first
, unsigned int last
)
1960 if (msg_code
>= WM_KEYFIRST
&& msg_code
<= WM_KEYLAST
)
1962 /* we can only test the window for a keyboard message since the
1963 * dest window for a mouse message depends on hittest */
1964 if (filter_win
&& win
!= filter_win
&& !is_child_window( filter_win
, win
))
1966 /* the message code is final for a keyboard message, we can simply check it */
1967 return check_msg_filter( msg_code
, first
, last
);
1969 else /* mouse message */
1971 /* we need to check all possible values that the message can have in the end */
1973 if (check_msg_filter( msg_code
, first
, last
)) return 1;
1974 if (msg_code
== WM_MOUSEWHEEL
) return 0; /* no other possible value for this one */
1976 /* all other messages can become non-client messages */
1977 if (check_msg_filter( msg_code
+ (WM_NCMOUSEFIRST
- WM_MOUSEFIRST
), first
, last
)) return 1;
1979 /* clicks can become double-clicks or non-client double-clicks */
1980 if (msg_code
== WM_LBUTTONDOWN
|| msg_code
== WM_MBUTTONDOWN
||
1981 msg_code
== WM_RBUTTONDOWN
|| msg_code
== WM_XBUTTONDOWN
)
1983 if (check_msg_filter( msg_code
+ (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
1984 if (check_msg_filter( msg_code
+ (WM_NCLBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
1991 /* find a hardware message for the given queue */
1992 static int get_hardware_message( struct thread
*thread
, unsigned int hw_id
, user_handle_t filter_win
,
1993 unsigned int first
, unsigned int last
, unsigned int flags
,
1994 struct get_message_reply
*reply
)
1996 struct thread_input
*input
= thread
->queue
->input
;
1997 struct thread
*win_thread
;
2000 int clear_bits
, got_one
= 0;
2001 unsigned int msg_code
;
2003 ptr
= list_head( &input
->msg_list
);
2008 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
2009 if (msg
->unique_id
== hw_id
) break;
2010 ptr
= list_next( &input
->msg_list
, ptr
);
2012 if (!ptr
) ptr
= list_head( &input
->msg_list
);
2013 else ptr
= list_next( &input
->msg_list
, ptr
); /* start from the next one */
2016 if (ptr
== list_head( &input
->msg_list
))
2017 clear_bits
= QS_INPUT
;
2019 clear_bits
= 0; /* don't clear bits if we don't go through the whole list */
2023 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
2024 struct hardware_msg_data
*data
= msg
->data
;
2026 ptr
= list_next( &input
->msg_list
, ptr
);
2027 win
= find_hardware_message_window( input
->desktop
, input
, msg
, &msg_code
, &win_thread
);
2028 if (!win
|| !win_thread
)
2030 /* no window at all, remove it */
2031 update_input_key_state( input
->desktop
, input
->keystate
, msg
->msg
, msg
->wparam
);
2032 list_remove( &msg
->entry
);
2033 free_message( msg
);
2036 if (win_thread
!= thread
)
2038 if (win_thread
->queue
->input
== input
)
2040 /* wake the other thread */
2041 set_queue_bits( win_thread
->queue
, get_hardware_msg_bit(msg
) );
2046 /* for another thread input, drop it */
2047 update_input_key_state( input
->desktop
, input
->keystate
, msg
->msg
, msg
->wparam
);
2048 list_remove( &msg
->entry
);
2049 free_message( msg
);
2051 release_object( win_thread
);
2054 release_object( win_thread
);
2056 /* if we already got a message for another thread, or if it doesn't
2057 * match the filter we skip it */
2058 if (got_one
|| !check_hw_message_filter( win
, msg_code
, filter_win
, first
, last
))
2060 clear_bits
&= ~get_hardware_msg_bit( msg
);
2063 /* now we can return it */
2064 if (!msg
->unique_id
) msg
->unique_id
= get_unique_id();
2065 reply
->type
= MSG_HARDWARE
;
2067 reply
->msg
= msg_code
;
2068 reply
->wparam
= msg
->wparam
;
2069 reply
->lparam
= msg
->lparam
;
2072 reply
->time
= msg
->time
;
2074 data
->hw_id
= msg
->unique_id
;
2075 set_reply_data( msg
->data
, msg
->data_size
);
2076 if (msg
->msg
== WM_INPUT
&& (flags
& PM_REMOVE
))
2077 release_hardware_message( current
->queue
, data
->hw_id
);
2080 /* nothing found, clear the hardware queue bits */
2081 clear_queue_bits( thread
->queue
, clear_bits
);
2085 /* increment (or decrement if 'incr' is negative) the queue paint count */
2086 void inc_queue_paint_count( struct thread
*thread
, int incr
)
2088 struct msg_queue
*queue
= thread
->queue
;
2092 if ((queue
->paint_count
+= incr
) < 0) queue
->paint_count
= 0;
2094 if (queue
->paint_count
)
2095 set_queue_bits( queue
, QS_PAINT
);
2097 clear_queue_bits( queue
, QS_PAINT
);
2101 /* remove all messages and timers belonging to a certain window */
2102 void queue_cleanup_window( struct thread
*thread
, user_handle_t win
)
2104 struct msg_queue
*queue
= thread
->queue
;
2112 ptr
= list_head( &queue
->pending_timers
);
2115 struct list
*next
= list_next( &queue
->pending_timers
, ptr
);
2116 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
2117 if (timer
->win
== win
) free_timer( queue
, timer
);
2120 ptr
= list_head( &queue
->expired_timers
);
2123 struct list
*next
= list_next( &queue
->expired_timers
, ptr
);
2124 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
2125 if (timer
->win
== win
) free_timer( queue
, timer
);
2129 /* remove messages */
2130 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
2132 struct list
*ptr
, *next
;
2134 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->msg_list
[i
] )
2136 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
2137 if (msg
->win
== win
)
2139 if (msg
->msg
== WM_QUIT
&& !queue
->quit_message
)
2141 queue
->quit_message
= 1;
2142 queue
->exit_code
= msg
->wparam
;
2144 remove_queue_message( queue
, msg
, i
);
2149 thread_input_cleanup_window( queue
, win
);
2152 /* post a message to a window */
2153 void post_message( user_handle_t win
, unsigned int message
, lparam_t wparam
, lparam_t lparam
)
2155 struct message
*msg
;
2156 struct thread
*thread
= get_window_thread( win
);
2158 if (!thread
) return;
2160 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
2162 msg
->type
= MSG_POSTED
;
2163 msg
->win
= get_user_full_handle( win
);
2165 msg
->wparam
= wparam
;
2166 msg
->lparam
= lparam
;
2171 get_message_defaults( thread
->queue
, &msg
->x
, &msg
->y
, &msg
->time
);
2173 list_add_tail( &thread
->queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
2174 set_queue_bits( thread
->queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
2175 if (message
== WM_HOTKEY
)
2177 set_queue_bits( thread
->queue
, QS_HOTKEY
);
2178 thread
->queue
->hotkey_count
++;
2181 release_object( thread
);
2184 /* send a notify message to a window */
2185 void send_notify_message( user_handle_t win
, unsigned int message
, lparam_t wparam
, lparam_t lparam
)
2187 struct message
*msg
;
2188 struct thread
*thread
= get_window_thread( win
);
2190 if (!thread
) return;
2192 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
2194 msg
->type
= MSG_NOTIFY
;
2195 msg
->win
= get_user_full_handle( win
);
2197 msg
->wparam
= wparam
;
2198 msg
->lparam
= lparam
;
2203 get_message_defaults( thread
->queue
, &msg
->x
, &msg
->y
, &msg
->time
);
2205 list_add_tail( &thread
->queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
2206 set_queue_bits( thread
->queue
, QS_SENDMESSAGE
);
2208 release_object( thread
);
2211 /* post a win event */
2212 void post_win_event( struct thread
*thread
, unsigned int event
,
2213 user_handle_t win
, unsigned int object_id
,
2214 unsigned int child_id
, client_ptr_t hook_proc
,
2215 const WCHAR
*module
, data_size_t module_size
,
2218 struct message
*msg
;
2220 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
2222 struct winevent_msg_data
*data
;
2224 msg
->type
= MSG_WINEVENT
;
2225 msg
->win
= get_user_full_handle( win
);
2227 msg
->wparam
= object_id
;
2228 msg
->lparam
= child_id
;
2229 msg
->time
= get_tick_count();
2232 if ((data
= malloc( sizeof(*data
) + module_size
)))
2235 data
->tid
= get_thread_id( current
);
2236 data
->hook_proc
= hook_proc
;
2237 memcpy( data
+ 1, module
, module_size
);
2240 msg
->data_size
= sizeof(*data
) + module_size
;
2242 if (debug_level
> 1)
2243 fprintf( stderr
, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2244 get_thread_id(thread
), event
, win
, object_id
, child_id
);
2245 list_add_tail( &thread
->queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
2246 set_queue_bits( thread
->queue
, QS_SENDMESSAGE
);
2253 /* free all hotkeys on a desktop, optionally filtering by window */
2254 void free_hotkeys( struct desktop
*desktop
, user_handle_t window
)
2256 struct hotkey
*hotkey
, *hotkey2
;
2258 LIST_FOR_EACH_ENTRY_SAFE( hotkey
, hotkey2
, &desktop
->hotkeys
, struct hotkey
, entry
)
2260 if (!window
|| hotkey
->win
== window
)
2262 list_remove( &hotkey
->entry
);
2269 /* check if the thread owning the window is hung */
2270 DECL_HANDLER(is_window_hung
)
2272 struct thread
*thread
;
2274 thread
= get_window_thread( req
->win
);
2277 reply
->is_hung
= is_queue_hung( thread
->queue
);
2278 release_object( thread
);
2280 else reply
->is_hung
= 0;
2284 /* get the message queue of the current thread */
2285 DECL_HANDLER(get_msg_queue
)
2287 struct msg_queue
*queue
= get_current_queue();
2290 if (queue
) reply
->handle
= alloc_handle( current
->process
, queue
, SYNCHRONIZE
, 0 );
2294 /* set the file descriptor associated to the current thread queue */
2295 DECL_HANDLER(set_queue_fd
)
2297 struct msg_queue
*queue
= get_current_queue();
2301 if (queue
->fd
) /* fd can only be set once */
2303 set_error( STATUS_ACCESS_DENIED
);
2306 if (!(file
= get_file_obj( current
->process
, req
->handle
, SYNCHRONIZE
))) return;
2308 if ((unix_fd
= get_file_unix_fd( file
)) != -1)
2310 if ((unix_fd
= dup( unix_fd
)) != -1)
2311 queue
->fd
= create_anonymous_fd( &msg_queue_fd_ops
, unix_fd
, &queue
->obj
, 0 );
2315 release_object( file
);
2319 /* set the current message queue wakeup mask */
2320 DECL_HANDLER(set_queue_mask
)
2322 struct msg_queue
*queue
= get_current_queue();
2326 queue
->wake_mask
= req
->wake_mask
;
2327 queue
->changed_mask
= req
->changed_mask
;
2328 reply
->wake_bits
= queue
->wake_bits
;
2329 reply
->changed_bits
= queue
->changed_bits
;
2330 if (is_signaled( queue
))
2332 /* if skip wait is set, do what would have been done in the subsequent wait */
2333 if (req
->skip_wait
) queue
->wake_mask
= queue
->changed_mask
= 0;
2334 else wake_up( &queue
->obj
, 0 );
2340 /* get the current message queue status */
2341 DECL_HANDLER(get_queue_status
)
2343 struct msg_queue
*queue
= current
->queue
;
2346 reply
->wake_bits
= queue
->wake_bits
;
2347 reply
->changed_bits
= queue
->changed_bits
;
2348 queue
->changed_bits
&= ~req
->clear_bits
;
2350 else reply
->wake_bits
= reply
->changed_bits
= 0;
2354 /* send a message to a thread queue */
2355 DECL_HANDLER(send_message
)
2357 struct message
*msg
;
2358 struct msg_queue
*send_queue
= get_current_queue();
2359 struct msg_queue
*recv_queue
= NULL
;
2360 struct thread
*thread
= NULL
;
2362 if (!(thread
= get_thread_from_id( req
->id
))) return;
2364 if (!(recv_queue
= thread
->queue
))
2366 set_error( STATUS_INVALID_PARAMETER
);
2367 release_object( thread
);
2370 if ((req
->flags
& SEND_MSG_ABORT_IF_HUNG
) && is_queue_hung(recv_queue
))
2372 set_error( STATUS_TIMEOUT
);
2373 release_object( thread
);
2377 if ((msg
= mem_alloc( sizeof(*msg
) )))
2379 msg
->type
= req
->type
;
2380 msg
->win
= get_user_full_handle( req
->win
);
2381 msg
->msg
= req
->msg
;
2382 msg
->wparam
= req
->wparam
;
2383 msg
->lparam
= req
->lparam
;
2386 msg
->data_size
= get_req_data_size();
2388 get_message_defaults( recv_queue
, &msg
->x
, &msg
->y
, &msg
->time
);
2390 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
2393 release_object( thread
);
2399 case MSG_OTHER_PROCESS
:
2403 if (!(msg
->result
= alloc_message_result( send_queue
, recv_queue
, msg
, req
->timeout
)))
2405 free_message( msg
);
2410 list_add_tail( &recv_queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
2411 set_queue_bits( recv_queue
, QS_SENDMESSAGE
);
2414 list_add_tail( &recv_queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
2415 set_queue_bits( recv_queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
2416 if (msg
->msg
== WM_HOTKEY
)
2418 set_queue_bits( recv_queue
, QS_HOTKEY
);
2419 recv_queue
->hotkey_count
++;
2422 case MSG_HARDWARE
: /* should use send_hardware_message instead */
2423 case MSG_CALLBACK_RESULT
: /* cannot send this one */
2424 case MSG_HOOK_LL
: /* generated internally */
2426 set_error( STATUS_INVALID_PARAMETER
);
2431 release_object( thread
);
2434 /* send a hardware message to a thread queue */
2435 DECL_HANDLER(send_hardware_message
)
2437 struct thread
*thread
= NULL
;
2438 struct desktop
*desktop
;
2439 unsigned int origin
= (req
->flags
& SEND_HWMSG_INJECTED
? IMO_INJECTED
: IMO_HARDWARE
);
2440 struct msg_queue
*sender
= get_current_queue();
2441 data_size_t size
= min( 256, get_reply_max_size() );
2443 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2447 if (!(thread
= get_window_thread( req
->win
))) return;
2448 if (desktop
!= thread
->queue
->input
->desktop
)
2450 /* don't allow queuing events to a different desktop */
2451 release_object( desktop
);
2456 reply
->prev_x
= desktop
->cursor
.x
;
2457 reply
->prev_y
= desktop
->cursor
.y
;
2459 switch (req
->input
.type
)
2462 reply
->wait
= queue_mouse_message( desktop
, req
->win
, &req
->input
, origin
, sender
);
2464 case INPUT_KEYBOARD
:
2465 reply
->wait
= queue_keyboard_message( desktop
, req
->win
, &req
->input
, origin
, sender
);
2467 case INPUT_HARDWARE
:
2468 queue_custom_hardware_message( desktop
, req
->win
, origin
, &req
->input
);
2471 set_error( STATUS_INVALID_PARAMETER
);
2473 if (thread
) release_object( thread
);
2475 reply
->new_x
= desktop
->cursor
.x
;
2476 reply
->new_y
= desktop
->cursor
.y
;
2477 set_reply_data( desktop
->keystate
, size
);
2478 release_object( desktop
);
2481 /* post a quit message to the current queue */
2482 DECL_HANDLER(post_quit_message
)
2484 struct msg_queue
*queue
= get_current_queue();
2489 queue
->quit_message
= 1;
2490 queue
->exit_code
= req
->exit_code
;
2491 set_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
2494 /* get a message from the current queue */
2495 DECL_HANDLER(get_message
)
2497 struct timer
*timer
;
2499 struct msg_queue
*queue
= get_current_queue();
2500 user_handle_t get_win
= get_user_full_handle( req
->get_win
);
2501 unsigned int filter
= req
->flags
>> 16;
2503 reply
->active_hooks
= get_active_hooks();
2505 if (get_win
&& get_win
!= 1 && get_win
!= -1 && !get_user_object( get_win
, USER_WINDOW
))
2507 set_win32_error( ERROR_INVALID_WINDOW_HANDLE
);
2512 queue
->last_get_msg
= current_time
;
2513 if (!filter
) filter
= QS_ALLINPUT
;
2515 /* first check for sent messages */
2516 if ((ptr
= list_head( &queue
->msg_list
[SEND_MESSAGE
] )))
2518 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
2519 receive_message( queue
, msg
, reply
);
2523 /* clear changed bits so we can wait on them if we don't find a message */
2524 if (filter
& QS_POSTMESSAGE
)
2526 queue
->changed_bits
&= ~(QS_POSTMESSAGE
| QS_HOTKEY
| QS_TIMER
);
2527 if (req
->get_first
== 0 && req
->get_last
== ~0U) queue
->changed_bits
&= ~QS_ALLPOSTMESSAGE
;
2529 if (filter
& QS_INPUT
) queue
->changed_bits
&= ~QS_INPUT
;
2530 if (filter
& QS_PAINT
) queue
->changed_bits
&= ~QS_PAINT
;
2532 /* then check for posted messages */
2533 if ((filter
& QS_POSTMESSAGE
) &&
2534 get_posted_message( queue
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
2537 if ((filter
& QS_HOTKEY
) && queue
->hotkey_count
&&
2538 req
->get_first
<= WM_HOTKEY
&& req
->get_last
>= WM_HOTKEY
&&
2539 get_posted_message( queue
, get_win
, WM_HOTKEY
, WM_HOTKEY
, req
->flags
, reply
))
2542 /* only check for quit messages if not posted messages pending */
2543 if ((filter
& QS_POSTMESSAGE
) && get_quit_message( queue
, req
->flags
, reply
))
2546 /* then check for any raw hardware message */
2547 if ((filter
& QS_INPUT
) &&
2548 filter_contains_hw_range( req
->get_first
, req
->get_last
) &&
2549 get_hardware_message( current
, req
->hw_id
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
2552 /* now check for WM_PAINT */
2553 if ((filter
& QS_PAINT
) &&
2554 queue
->paint_count
&&
2555 check_msg_filter( WM_PAINT
, req
->get_first
, req
->get_last
) &&
2556 (reply
->win
= find_window_to_repaint( get_win
, current
)))
2558 reply
->type
= MSG_POSTED
;
2559 reply
->msg
= WM_PAINT
;
2562 get_message_defaults( queue
, &reply
->x
, &reply
->y
, &reply
->time
);
2566 /* now check for timer */
2567 if ((filter
& QS_TIMER
) &&
2568 (timer
= find_expired_timer( queue
, get_win
, req
->get_first
,
2569 req
->get_last
, (req
->flags
& PM_REMOVE
) )))
2571 reply
->type
= MSG_POSTED
;
2572 reply
->win
= timer
->win
;
2573 reply
->msg
= timer
->msg
;
2574 reply
->wparam
= timer
->id
;
2575 reply
->lparam
= timer
->lparam
;
2576 get_message_defaults( queue
, &reply
->x
, &reply
->y
, &reply
->time
);
2577 if (!(req
->flags
& PM_NOYIELD
) && current
->process
->idle_event
)
2578 set_event( current
->process
->idle_event
);
2582 if (get_win
== -1 && current
->process
->idle_event
) set_event( current
->process
->idle_event
);
2583 queue
->wake_mask
= req
->wake_mask
;
2584 queue
->changed_mask
= req
->changed_mask
;
2585 set_error( STATUS_PENDING
); /* FIXME */
2589 /* reply to a sent message */
2590 DECL_HANDLER(reply_message
)
2592 if (!current
->queue
) set_error( STATUS_ACCESS_DENIED
);
2593 else if (current
->queue
->recv_result
)
2594 reply_message( current
->queue
, req
->result
, 0, req
->remove
,
2595 get_req_data(), get_req_data_size() );
2599 /* accept the current hardware message */
2600 DECL_HANDLER(accept_hardware_message
)
2603 release_hardware_message( current
->queue
, req
->hw_id
);
2605 set_error( STATUS_ACCESS_DENIED
);
2609 /* retrieve the reply for the last message sent */
2610 DECL_HANDLER(get_message_reply
)
2612 struct message_result
*result
;
2614 struct msg_queue
*queue
= current
->queue
;
2618 set_error( STATUS_PENDING
);
2621 if (!(entry
= list_head( &queue
->send_result
))) return; /* no reply ready */
2623 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
2624 if (result
->replied
|| req
->cancel
)
2626 if (result
->replied
)
2628 reply
->result
= result
->result
;
2629 set_error( result
->error
);
2632 data_size_t data_len
= min( result
->data_size
, get_reply_max_size() );
2633 set_reply_data_ptr( result
->data
, data_len
);
2634 result
->data
= NULL
;
2635 result
->data_size
= 0;
2638 remove_result_from_sender( result
);
2640 entry
= list_head( &queue
->send_result
);
2641 if (!entry
) clear_queue_bits( queue
, QS_SMRESULT
);
2644 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
2645 if (result
->replied
) set_queue_bits( queue
, QS_SMRESULT
);
2646 else clear_queue_bits( queue
, QS_SMRESULT
);
2650 else set_error( STATUS_ACCESS_DENIED
);
2654 /* set a window timer */
2655 DECL_HANDLER(set_win_timer
)
2657 struct timer
*timer
;
2658 struct msg_queue
*queue
;
2659 struct thread
*thread
= NULL
;
2660 user_handle_t win
= 0;
2661 lparam_t id
= req
->id
;
2665 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
2667 set_error( STATUS_INVALID_HANDLE
);
2670 if (thread
->process
!= current
->process
)
2672 release_object( thread
);
2673 set_error( STATUS_ACCESS_DENIED
);
2676 queue
= thread
->queue
;
2677 /* remove it if it existed already */
2678 if ((timer
= find_timer( queue
, win
, req
->msg
, id
))) free_timer( queue
, timer
);
2682 queue
= get_current_queue();
2683 /* look for a timer with this id */
2684 if (id
&& (timer
= find_timer( queue
, 0, req
->msg
, id
)))
2686 /* free and reuse id */
2687 free_timer( queue
, timer
);
2691 lparam_t end_id
= queue
->next_timer_id
;
2693 /* find a free id for it */
2696 id
= queue
->next_timer_id
;
2697 if (--queue
->next_timer_id
<= 0x100) queue
->next_timer_id
= 0x7fff;
2699 if (!find_timer( queue
, 0, req
->msg
, id
)) break;
2700 if (queue
->next_timer_id
== end_id
)
2702 set_win32_error( ERROR_NO_MORE_USER_HANDLES
);
2709 if ((timer
= set_timer( queue
, req
->rate
)))
2712 timer
->msg
= req
->msg
;
2714 timer
->lparam
= req
->lparam
;
2717 if (thread
) release_object( thread
);
2720 /* kill a window timer */
2721 DECL_HANDLER(kill_win_timer
)
2723 struct timer
*timer
;
2724 struct thread
*thread
;
2725 user_handle_t win
= 0;
2729 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
2731 set_error( STATUS_INVALID_HANDLE
);
2734 if (thread
->process
!= current
->process
)
2736 release_object( thread
);
2737 set_error( STATUS_ACCESS_DENIED
);
2741 else thread
= (struct thread
*)grab_object( current
);
2743 if (thread
->queue
&& (timer
= find_timer( thread
->queue
, win
, req
->msg
, req
->id
)))
2744 free_timer( thread
->queue
, timer
);
2746 set_error( STATUS_INVALID_PARAMETER
);
2748 release_object( thread
);
2751 DECL_HANDLER(register_hotkey
)
2753 struct desktop
*desktop
;
2754 user_handle_t win_handle
= req
->window
;
2755 struct hotkey
*hotkey
;
2756 struct hotkey
*new_hotkey
= NULL
;
2757 struct thread
*thread
;
2758 const int modifier_flags
= MOD_ALT
|MOD_CONTROL
|MOD_SHIFT
|MOD_WIN
;
2760 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2764 if (!(win_handle
= get_valid_window_handle( win_handle
)))
2766 release_object( desktop
);
2770 thread
= get_window_thread( win_handle
);
2771 if (thread
) release_object( thread
);
2773 if (thread
!= current
)
2775 release_object( desktop
);
2776 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD
);
2781 LIST_FOR_EACH_ENTRY( hotkey
, &desktop
->hotkeys
, struct hotkey
, entry
)
2783 if (req
->vkey
== hotkey
->vkey
&&
2784 (req
->flags
& modifier_flags
) == (hotkey
->flags
& modifier_flags
))
2786 release_object( desktop
);
2787 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED
);
2790 if (current
->queue
== hotkey
->queue
&& win_handle
== hotkey
->win
&& req
->id
== hotkey
->id
)
2791 new_hotkey
= hotkey
;
2796 reply
->replaced
= 1;
2797 reply
->flags
= new_hotkey
->flags
;
2798 reply
->vkey
= new_hotkey
->vkey
;
2802 new_hotkey
= mem_alloc( sizeof(*new_hotkey
) );
2805 list_add_tail( &desktop
->hotkeys
, &new_hotkey
->entry
);
2806 new_hotkey
->queue
= current
->queue
;
2807 new_hotkey
->win
= win_handle
;
2808 new_hotkey
->id
= req
->id
;
2814 new_hotkey
->flags
= req
->flags
;
2815 new_hotkey
->vkey
= req
->vkey
;
2818 release_object( desktop
);
2821 DECL_HANDLER(unregister_hotkey
)
2823 struct desktop
*desktop
;
2824 user_handle_t win_handle
= req
->window
;
2825 struct hotkey
*hotkey
;
2826 struct thread
*thread
;
2828 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2832 if (!(win_handle
= get_valid_window_handle( win_handle
)))
2834 release_object( desktop
);
2838 thread
= get_window_thread( win_handle
);
2839 if (thread
) release_object( thread
);
2841 if (thread
!= current
)
2843 release_object( desktop
);
2844 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD
);
2849 LIST_FOR_EACH_ENTRY( hotkey
, &desktop
->hotkeys
, struct hotkey
, entry
)
2851 if (current
->queue
== hotkey
->queue
&& win_handle
== hotkey
->win
&& req
->id
== hotkey
->id
)
2855 release_object( desktop
);
2856 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED
);
2860 reply
->flags
= hotkey
->flags
;
2861 reply
->vkey
= hotkey
->vkey
;
2862 list_remove( &hotkey
->entry
);
2864 release_object( desktop
);
2867 /* attach (or detach) thread inputs */
2868 DECL_HANDLER(attach_thread_input
)
2870 struct thread
*thread_from
= get_thread_from_id( req
->tid_from
);
2871 struct thread
*thread_to
= get_thread_from_id( req
->tid_to
);
2873 if (!thread_from
|| !thread_to
)
2875 if (thread_from
) release_object( thread_from
);
2876 if (thread_to
) release_object( thread_to
);
2879 if (thread_from
!= thread_to
)
2883 if ((thread_to
->queue
|| thread_to
== current
) &&
2884 (thread_from
->queue
|| thread_from
== current
))
2885 attach_thread_input( thread_from
, thread_to
);
2887 set_error( STATUS_INVALID_PARAMETER
);
2891 if (thread_from
->queue
&& thread_to
->queue
&&
2892 thread_from
->queue
->input
== thread_to
->queue
->input
)
2893 detach_thread_input( thread_from
);
2895 set_error( STATUS_ACCESS_DENIED
);
2898 else set_error( STATUS_ACCESS_DENIED
);
2899 release_object( thread_from
);
2900 release_object( thread_to
);
2904 /* get thread input data */
2905 DECL_HANDLER(get_thread_input
)
2907 struct thread
*thread
= NULL
;
2908 struct desktop
*desktop
;
2909 struct thread_input
*input
;
2913 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2914 if (!(desktop
= get_thread_desktop( thread
, 0 )))
2916 release_object( thread
);
2919 input
= thread
->queue
? thread
->queue
->input
: NULL
;
2923 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2924 input
= desktop
->foreground_input
; /* get the foreground thread info */
2929 reply
->focus
= input
->focus
;
2930 reply
->capture
= input
->capture
;
2931 reply
->active
= input
->active
;
2932 reply
->menu_owner
= input
->menu_owner
;
2933 reply
->move_size
= input
->move_size
;
2934 reply
->caret
= input
->caret
;
2935 reply
->cursor
= input
->cursor
;
2936 reply
->show_count
= input
->cursor_count
;
2937 reply
->rect
= input
->caret_rect
;
2940 /* foreground window is active window of foreground thread */
2941 reply
->foreground
= desktop
->foreground_input
? desktop
->foreground_input
->active
: 0;
2942 if (thread
) release_object( thread
);
2943 release_object( desktop
);
2947 /* retrieve queue keyboard state for a given thread */
2948 DECL_HANDLER(get_key_state
)
2950 struct thread
*thread
;
2951 struct desktop
*desktop
;
2952 data_size_t size
= min( 256, get_reply_max_size() );
2954 if (!req
->tid
) /* get global async key state */
2956 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2959 reply
->state
= desktop
->keystate
[req
->key
& 0xff];
2960 desktop
->keystate
[req
->key
& 0xff] &= ~0x40;
2962 set_reply_data( desktop
->keystate
, size
);
2963 release_object( desktop
);
2967 unsigned char *keystate
;
2968 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2971 if (req
->key
>= 0) reply
->state
= thread
->queue
->input
->keystate
[req
->key
& 0xff];
2972 set_reply_data( thread
->queue
->input
->keystate
, size
);
2973 release_object( thread
);
2976 release_object( thread
);
2978 /* fallback to desktop keystate */
2979 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2980 if (req
->key
>= 0) reply
->state
= desktop
->keystate
[req
->key
& 0xff] & ~0x40;
2981 if ((keystate
= set_reply_data_size( size
)))
2984 for (i
= 0; i
< size
; i
++) keystate
[i
] = desktop
->keystate
[i
] & ~0x40;
2986 release_object( desktop
);
2991 /* set queue keyboard state for a given thread */
2992 DECL_HANDLER(set_key_state
)
2994 struct thread
*thread
;
2995 struct desktop
*desktop
;
2996 data_size_t size
= min( 256, get_req_data_size() );
2998 if (!req
->tid
) /* set global async key state */
3000 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
3001 memcpy( desktop
->keystate
, get_req_data(), size
);
3002 release_object( desktop
);
3006 if (!(thread
= get_thread_from_id( req
->tid
))) return;
3007 if (thread
->queue
) memcpy( thread
->queue
->input
->keystate
, get_req_data(), size
);
3008 if (req
->async
&& (desktop
= get_thread_desktop( thread
, 0 )))
3010 memcpy( desktop
->keystate
, get_req_data(), size
);
3011 release_object( desktop
);
3013 release_object( thread
);
3018 /* set the system foreground window */
3019 DECL_HANDLER(set_foreground_window
)
3021 struct thread
*thread
= NULL
;
3022 struct desktop
*desktop
;
3023 struct msg_queue
*queue
= get_current_queue();
3025 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
3026 reply
->previous
= desktop
->foreground_input
? desktop
->foreground_input
->active
: 0;
3027 reply
->send_msg_old
= (reply
->previous
&& desktop
->foreground_input
!= queue
->input
);
3028 reply
->send_msg_new
= FALSE
;
3030 if (is_valid_foreground_window( req
->handle
) &&
3031 (thread
= get_window_thread( req
->handle
)) &&
3032 thread
->queue
->input
->desktop
== desktop
)
3034 set_foreground_input( desktop
, thread
->queue
->input
);
3035 reply
->send_msg_new
= (desktop
->foreground_input
!= queue
->input
);
3037 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE
);
3039 if (thread
) release_object( thread
);
3040 release_object( desktop
);
3044 /* set the current thread focus window */
3045 DECL_HANDLER(set_focus_window
)
3047 struct msg_queue
*queue
= get_current_queue();
3049 reply
->previous
= 0;
3050 if (queue
&& check_queue_input_window( queue
, req
->handle
))
3052 reply
->previous
= queue
->input
->focus
;
3053 queue
->input
->focus
= get_user_full_handle( req
->handle
);
3058 /* set the current thread active window */
3059 DECL_HANDLER(set_active_window
)
3061 struct msg_queue
*queue
= get_current_queue();
3063 reply
->previous
= 0;
3064 if (queue
&& check_queue_input_window( queue
, req
->handle
))
3066 if (!req
->handle
|| make_window_active( req
->handle
))
3068 reply
->previous
= queue
->input
->active
;
3069 queue
->input
->active
= get_user_full_handle( req
->handle
);
3071 else set_error( STATUS_INVALID_HANDLE
);
3076 /* set the current thread capture window */
3077 DECL_HANDLER(set_capture_window
)
3079 struct msg_queue
*queue
= get_current_queue();
3081 reply
->previous
= reply
->full_handle
= 0;
3082 if (queue
&& check_queue_input_window( queue
, req
->handle
))
3084 struct thread_input
*input
= queue
->input
;
3086 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
3087 if (input
->menu_owner
&& !(req
->flags
& CAPTURE_MENU
))
3089 set_error(STATUS_ACCESS_DENIED
);
3092 reply
->previous
= input
->capture
;
3093 input
->capture
= get_user_full_handle( req
->handle
);
3094 input
->menu_owner
= (req
->flags
& CAPTURE_MENU
) ? input
->capture
: 0;
3095 input
->move_size
= (req
->flags
& CAPTURE_MOVESIZE
) ? input
->capture
: 0;
3096 reply
->full_handle
= input
->capture
;
3101 /* Set the current thread caret window */
3102 DECL_HANDLER(set_caret_window
)
3104 struct msg_queue
*queue
= get_current_queue();
3106 reply
->previous
= 0;
3107 if (queue
&& check_queue_input_window( queue
, req
->handle
))
3109 struct thread_input
*input
= queue
->input
;
3111 reply
->previous
= input
->caret
;
3112 reply
->old_rect
= input
->caret_rect
;
3113 reply
->old_hide
= input
->caret_hide
;
3114 reply
->old_state
= input
->caret_state
;
3116 set_caret_window( input
, get_user_full_handle(req
->handle
) );
3117 input
->caret_rect
.right
= input
->caret_rect
.left
+ req
->width
;
3118 input
->caret_rect
.bottom
= input
->caret_rect
.top
+ req
->height
;
3123 /* Set the current thread caret information */
3124 DECL_HANDLER(set_caret_info
)
3126 struct msg_queue
*queue
= get_current_queue();
3127 struct thread_input
*input
;
3130 input
= queue
->input
;
3131 reply
->full_handle
= input
->caret
;
3132 reply
->old_rect
= input
->caret_rect
;
3133 reply
->old_hide
= input
->caret_hide
;
3134 reply
->old_state
= input
->caret_state
;
3136 if (req
->handle
&& get_user_full_handle(req
->handle
) != input
->caret
)
3138 set_error( STATUS_ACCESS_DENIED
);
3141 if (req
->flags
& SET_CARET_POS
)
3143 input
->caret_rect
.right
+= req
->x
- input
->caret_rect
.left
;
3144 input
->caret_rect
.bottom
+= req
->y
- input
->caret_rect
.top
;
3145 input
->caret_rect
.left
= req
->x
;
3146 input
->caret_rect
.top
= req
->y
;
3148 if (req
->flags
& SET_CARET_HIDE
)
3150 input
->caret_hide
+= req
->hide
;
3151 if (input
->caret_hide
< 0) input
->caret_hide
= 0;
3153 if (req
->flags
& SET_CARET_STATE
)
3157 case CARET_STATE_OFF
: input
->caret_state
= 0; break;
3158 case CARET_STATE_ON
: input
->caret_state
= 1; break;
3159 case CARET_STATE_TOGGLE
: input
->caret_state
= !input
->caret_state
; break;
3160 case CARET_STATE_ON_IF_MOVED
:
3161 if (req
->x
!= reply
->old_rect
.left
|| req
->y
!= reply
->old_rect
.top
) input
->caret_state
= 1;
3168 /* get the time of the last input event */
3169 DECL_HANDLER(get_last_input_time
)
3171 reply
->time
= last_input_time
;
3174 /* set/get the current cursor */
3175 DECL_HANDLER(set_cursor
)
3177 struct msg_queue
*queue
= get_current_queue();
3178 struct thread_input
*input
;
3181 input
= queue
->input
;
3183 reply
->prev_handle
= input
->cursor
;
3184 reply
->prev_count
= input
->cursor_count
;
3185 reply
->prev_x
= input
->desktop
->cursor
.x
;
3186 reply
->prev_y
= input
->desktop
->cursor
.y
;
3188 if (req
->flags
& SET_CURSOR_HANDLE
)
3190 if (req
->handle
&& !get_user_object( req
->handle
, USER_CLIENT
))
3192 set_win32_error( ERROR_INVALID_CURSOR_HANDLE
);
3195 input
->cursor
= req
->handle
;
3197 if (req
->flags
& SET_CURSOR_COUNT
)
3199 queue
->cursor_count
+= req
->show_count
;
3200 input
->cursor_count
+= req
->show_count
;
3202 if (req
->flags
& SET_CURSOR_POS
)
3204 set_cursor_pos( input
->desktop
, req
->x
, req
->y
);
3206 if (req
->flags
& (SET_CURSOR_CLIP
| SET_CURSOR_NOCLIP
))
3208 struct desktop
*desktop
= input
->desktop
;
3210 /* only the desktop owner can set the message */
3211 if (req
->clip_msg
&& get_top_window_owner(desktop
) == current
->process
)
3212 desktop
->cursor
.clip_msg
= req
->clip_msg
;
3214 set_clip_rectangle( desktop
, (req
->flags
& SET_CURSOR_NOCLIP
) ? NULL
: &req
->clip
, 0 );
3217 reply
->new_x
= input
->desktop
->cursor
.x
;
3218 reply
->new_y
= input
->desktop
->cursor
.y
;
3219 reply
->new_clip
= input
->desktop
->cursor
.clip
;
3220 reply
->last_change
= input
->desktop
->cursor
.last_change
;
3223 DECL_HANDLER(get_rawinput_buffer
)
3225 struct thread_input
*input
= current
->queue
->input
;
3226 data_size_t size
= 0, next_size
= 0;
3231 if (!req
->buffer_size
) buf
= NULL
;
3232 else if (!(buf
= mem_alloc( get_reply_max_size() )))
3236 ptr
= list_head( &input
->msg_list
);
3239 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
3240 struct hardware_msg_data
*data
= msg
->data
;
3242 ptr
= list_next( &input
->msg_list
, ptr
);
3243 if (msg
->msg
!= WM_INPUT
) continue;
3245 next_size
= req
->rawinput_size
;
3246 if (size
+ next_size
> req
->buffer_size
) break;
3247 if (cur
+ sizeof(*data
) > buf
+ get_reply_max_size()) break;
3249 memcpy(cur
, data
, sizeof(*data
));
3250 list_remove( &msg
->entry
);
3251 free_message( msg
);
3254 cur
+= sizeof(*data
);
3258 reply
->next_size
= next_size
;
3259 reply
->count
= count
;
3260 set_reply_data_ptr( buf
, cur
- buf
);
3263 DECL_HANDLER(update_rawinput_devices
)
3265 const struct rawinput_device
*devices
= get_req_data();
3266 unsigned int device_count
= get_req_data_size() / sizeof (*devices
);
3267 const struct rawinput_device_entry
*e
;
3270 for (i
= 0; i
< device_count
; ++i
)
3272 update_rawinput_device(&devices
[i
]);
3275 e
= find_rawinput_device( 1, 2 );
3276 current
->process
->rawinput_mouse
= e
? &e
->device
: NULL
;
3277 e
= find_rawinput_device( 1, 6 );
3278 current
->process
->rawinput_kbd
= e
? &e
->device
: NULL
;