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
31 #define WIN32_NO_STATUS
45 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
46 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
48 enum message_kind
{ SEND_MESSAGE
, POST_MESSAGE
};
49 #define NB_MSG_KINDS (POST_MESSAGE+1)
54 struct list sender_entry
; /* entry in sender list */
55 struct message
*msg
; /* message the result is for */
56 struct message_result
*recv_next
; /* next in receiver list */
57 struct msg_queue
*sender
; /* sender queue */
58 struct msg_queue
*receiver
; /* receiver queue */
59 int replied
; /* has it been replied to? */
60 unsigned int error
; /* error code to pass back to sender */
61 lparam_t result
; /* reply result */
62 struct message
*hardware_msg
; /* hardware message if low-level hook result */
63 struct desktop
*desktop
; /* desktop for hardware message */
64 struct message
*callback_msg
; /* message to queue for callback */
65 void *data
; /* message reply data */
66 unsigned int data_size
; /* size of message reply data */
67 struct timeout_user
*timeout
; /* result timeout */
72 struct list entry
; /* entry in message list */
73 enum message_type type
; /* message type */
74 user_handle_t win
; /* window handle */
75 unsigned int msg
; /* message code */
76 lparam_t wparam
; /* parameters */
77 lparam_t lparam
; /* parameters */
78 int x
; /* message position */
80 unsigned int time
; /* message time */
81 void *data
; /* message data for sent messages */
82 unsigned int data_size
; /* size of message data */
83 unsigned int unique_id
; /* unique id for nested hw message waits */
84 struct message_result
*result
; /* result in sender queue */
89 struct list entry
; /* entry in timer list */
90 abstime_t when
; /* next expiration */
91 unsigned int rate
; /* timer rate in ms */
92 user_handle_t win
; /* window handle */
93 unsigned int msg
; /* message to post */
94 lparam_t id
; /* timer id */
95 lparam_t lparam
; /* lparam for message */
100 struct object obj
; /* object header */
101 struct desktop
*desktop
; /* desktop that this thread input belongs to */
102 user_handle_t focus
; /* focus window */
103 user_handle_t capture
; /* capture window */
104 user_handle_t active
; /* active window */
105 user_handle_t menu_owner
; /* current menu owner window */
106 user_handle_t move_size
; /* current moving/resizing window */
107 user_handle_t caret
; /* caret window */
108 rectangle_t caret_rect
; /* caret rectangle */
109 int caret_hide
; /* caret hide count */
110 int caret_state
; /* caret on/off state */
111 user_handle_t cursor
; /* current cursor */
112 int cursor_count
; /* cursor show count */
113 struct list msg_list
; /* list of hardware messages */
114 unsigned char keystate
[256]; /* state of each key */
119 struct object obj
; /* object header */
120 struct fd
*fd
; /* optional file descriptor to poll */
121 unsigned int wake_bits
; /* wakeup bits */
122 unsigned int wake_mask
; /* wakeup mask */
123 unsigned int changed_bits
; /* changed wakeup bits */
124 unsigned int changed_mask
; /* changed wakeup mask */
125 int paint_count
; /* pending paint messages count */
126 int hotkey_count
; /* pending hotkey messages count */
127 int quit_message
; /* is there a pending quit message? */
128 int exit_code
; /* exit code of pending quit message */
129 int cursor_count
; /* per-queue cursor show count */
130 struct list msg_list
[NB_MSG_KINDS
]; /* lists of messages */
131 struct list send_result
; /* stack of sent messages waiting for result */
132 struct list callback_result
; /* list of callback messages waiting for result */
133 struct message_result
*recv_result
; /* stack of received messages waiting for result */
134 struct list pending_timers
; /* list of pending timers */
135 struct list expired_timers
; /* list of expired timers */
136 lparam_t next_timer_id
; /* id for the next timer with a 0 window */
137 struct timeout_user
*timeout
; /* timeout for next timer to expire */
138 struct thread_input
*input
; /* thread input descriptor */
139 struct hook_table
*hooks
; /* hook table */
140 timeout_t last_get_msg
; /* time of last get message call */
145 struct list entry
; /* entry in desktop hotkey list */
146 struct msg_queue
*queue
; /* queue owning this hotkey */
147 user_handle_t win
; /* window handle */
148 int id
; /* hotkey id */
149 unsigned int vkey
; /* virtual key code */
150 unsigned int flags
; /* key modifiers */
153 static void msg_queue_dump( struct object
*obj
, int verbose
);
154 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
155 static void msg_queue_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
156 static int msg_queue_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
157 static void msg_queue_satisfied( struct object
*obj
, struct wait_queue_entry
*entry
);
158 static void msg_queue_destroy( struct object
*obj
);
159 static void msg_queue_poll_event( struct fd
*fd
, int event
);
160 static void thread_input_dump( struct object
*obj
, int verbose
);
161 static void thread_input_destroy( struct object
*obj
);
162 static void timer_callback( void *private );
164 static const struct object_ops msg_queue_ops
=
166 sizeof(struct msg_queue
), /* size */
168 msg_queue_dump
, /* dump */
169 msg_queue_add_queue
, /* add_queue */
170 msg_queue_remove_queue
, /* remove_queue */
171 msg_queue_signaled
, /* signaled */
172 msg_queue_satisfied
, /* satisfied */
173 no_signal
, /* signal */
174 no_get_fd
, /* get_fd */
175 default_map_access
, /* map_access */
176 default_get_sd
, /* get_sd */
177 default_set_sd
, /* set_sd */
178 no_get_full_name
, /* get_full_name */
179 no_lookup_name
, /* lookup_name */
180 no_link_name
, /* link_name */
181 NULL
, /* unlink_name */
182 no_open_file
, /* open_file */
183 no_kernel_obj_list
, /* get_kernel_obj_list */
184 no_close_handle
, /* close_handle */
185 msg_queue_destroy
/* destroy */
188 static const struct fd_ops msg_queue_fd_ops
=
190 NULL
, /* get_poll_events */
191 msg_queue_poll_event
, /* poll_event */
193 NULL
, /* get_fd_type */
195 NULL
, /* queue_async */
196 NULL
, /* reselect_async */
197 NULL
/* cancel async */
201 static const struct object_ops thread_input_ops
=
203 sizeof(struct thread_input
), /* size */
205 thread_input_dump
, /* dump */
206 no_add_queue
, /* add_queue */
207 NULL
, /* remove_queue */
209 NULL
, /* satisfied */
210 no_signal
, /* signal */
211 no_get_fd
, /* get_fd */
212 default_map_access
, /* map_access */
213 default_get_sd
, /* get_sd */
214 default_set_sd
, /* set_sd */
215 no_get_full_name
, /* get_full_name */
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 cursor_pos_t cursor_history
[64];
229 static unsigned int cursor_history_latest
;
231 static void queue_hardware_message( struct desktop
*desktop
, struct message
*msg
, int always_queue
);
232 static void free_message( struct message
*msg
);
234 /* set the caret window in a given thread input */
235 static void set_caret_window( struct thread_input
*input
, user_handle_t win
)
237 if (!win
|| win
!= input
->caret
)
239 input
->caret_rect
.left
= 0;
240 input
->caret_rect
.top
= 0;
241 input
->caret_rect
.right
= 0;
242 input
->caret_rect
.bottom
= 0;
245 input
->caret_hide
= 1;
246 input
->caret_state
= 0;
249 /* create a thread input object */
250 static struct thread_input
*create_thread_input( struct thread
*thread
)
252 struct thread_input
*input
;
254 if ((input
= alloc_object( &thread_input_ops
)))
259 input
->menu_owner
= 0;
260 input
->move_size
= 0;
262 input
->cursor_count
= 0;
263 list_init( &input
->msg_list
);
264 set_caret_window( input
, 0 );
265 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
267 if (!(input
->desktop
= get_thread_desktop( thread
, 0 /* FIXME: access rights */ )))
269 release_object( input
);
276 /* create a message queue object */
277 static struct msg_queue
*create_msg_queue( struct thread
*thread
, struct thread_input
*input
)
279 struct thread_input
*new_input
= NULL
;
280 struct msg_queue
*queue
;
285 if (!(new_input
= create_thread_input( thread
))) return NULL
;
289 if ((queue
= alloc_object( &msg_queue_ops
)))
292 queue
->wake_bits
= 0;
293 queue
->wake_mask
= 0;
294 queue
->changed_bits
= 0;
295 queue
->changed_mask
= 0;
296 queue
->paint_count
= 0;
297 queue
->hotkey_count
= 0;
298 queue
->quit_message
= 0;
299 queue
->cursor_count
= 0;
300 queue
->recv_result
= NULL
;
301 queue
->next_timer_id
= 0x7fff;
302 queue
->timeout
= NULL
;
303 queue
->input
= (struct thread_input
*)grab_object( input
);
305 queue
->last_get_msg
= current_time
;
306 list_init( &queue
->send_result
);
307 list_init( &queue
->callback_result
);
308 list_init( &queue
->pending_timers
);
309 list_init( &queue
->expired_timers
);
310 for (i
= 0; i
< NB_MSG_KINDS
; i
++) list_init( &queue
->msg_list
[i
] );
312 thread
->queue
= queue
;
314 if (new_input
) release_object( new_input
);
318 /* free the message queue of a thread at thread exit */
319 void free_msg_queue( struct thread
*thread
)
321 remove_thread_hooks( thread
);
322 if (!thread
->queue
) return;
323 release_object( thread
->queue
);
324 thread
->queue
= NULL
;
327 /* change the thread input data of a given thread */
328 static int assign_thread_input( struct thread
*thread
, struct thread_input
*new_input
)
330 struct msg_queue
*queue
= thread
->queue
;
334 thread
->queue
= create_msg_queue( thread
, new_input
);
335 return thread
->queue
!= NULL
;
339 queue
->input
->cursor_count
-= queue
->cursor_count
;
340 release_object( queue
->input
);
342 queue
->input
= (struct thread_input
*)grab_object( new_input
);
343 new_input
->cursor_count
+= queue
->cursor_count
;
347 /* allocate a hardware message and its data */
348 static struct message
*alloc_hardware_message( lparam_t info
, struct hw_msg_source source
,
349 unsigned int time
, data_size_t extra_size
)
351 struct hardware_msg_data
*msg_data
;
354 if (!(msg
= mem_alloc( sizeof(*msg
) ))) return NULL
;
355 if (!(msg_data
= mem_alloc( sizeof(*msg_data
) + extra_size
)))
360 memset( msg
, 0, sizeof(*msg
) );
361 msg
->type
= MSG_HARDWARE
;
363 msg
->data
= msg_data
;
364 msg
->data_size
= sizeof(*msg_data
) + extra_size
;
366 memset( msg_data
, 0, sizeof(*msg_data
) + extra_size
);
367 msg_data
->info
= info
;
368 msg_data
->size
= msg
->data_size
;
369 msg_data
->source
= source
;
373 static int update_desktop_cursor_pos( struct desktop
*desktop
, int x
, int y
)
377 x
= max( min( x
, desktop
->cursor
.clip
.right
- 1 ), desktop
->cursor
.clip
.left
);
378 y
= max( min( y
, desktop
->cursor
.clip
.bottom
- 1 ), desktop
->cursor
.clip
.top
);
379 updated
= (desktop
->cursor
.x
!= x
|| desktop
->cursor
.y
!= y
);
380 desktop
->cursor
.x
= x
;
381 desktop
->cursor
.y
= y
;
382 desktop
->cursor
.last_change
= get_tick_count();
387 /* set the cursor position and queue the corresponding mouse message */
388 static void set_cursor_pos( struct desktop
*desktop
, int x
, int y
)
390 static const struct hw_msg_source source
= { IMDT_UNAVAILABLE
, IMO_SYSTEM
};
391 const struct rawinput_device
*device
;
394 if ((device
= current
->process
->rawinput_mouse
) && (device
->flags
& RIDEV_NOLEGACY
))
396 update_desktop_cursor_pos( desktop
, x
, y
);
400 if (!(msg
= alloc_hardware_message( 0, source
, get_tick_count(), 0 ))) return;
402 msg
->msg
= WM_MOUSEMOVE
;
405 queue_hardware_message( desktop
, msg
, 1 );
408 /* retrieve default position and time for synthesized messages */
409 static void get_message_defaults( struct msg_queue
*queue
, int *x
, int *y
, unsigned int *time
)
411 struct desktop
*desktop
= queue
->input
->desktop
;
413 *x
= desktop
->cursor
.x
;
414 *y
= desktop
->cursor
.y
;
415 *time
= get_tick_count();
418 /* set the cursor clip rectangle */
419 static void set_clip_rectangle( struct desktop
*desktop
, const rectangle_t
*rect
, int send_clip_msg
)
421 rectangle_t top_rect
;
424 get_top_window_rectangle( desktop
, &top_rect
);
427 rectangle_t new_rect
= *rect
;
428 if (new_rect
.left
< top_rect
.left
) new_rect
.left
= top_rect
.left
;
429 if (new_rect
.right
> top_rect
.right
) new_rect
.right
= top_rect
.right
;
430 if (new_rect
.top
< top_rect
.top
) new_rect
.top
= top_rect
.top
;
431 if (new_rect
.bottom
> top_rect
.bottom
) new_rect
.bottom
= top_rect
.bottom
;
432 if (new_rect
.left
> new_rect
.right
|| new_rect
.top
> new_rect
.bottom
) new_rect
= top_rect
;
433 desktop
->cursor
.clip
= new_rect
;
435 else desktop
->cursor
.clip
= top_rect
;
437 if (desktop
->cursor
.clip_msg
&& send_clip_msg
)
438 post_desktop_message( desktop
, desktop
->cursor
.clip_msg
, rect
!= NULL
, 0 );
440 /* warp the mouse to be inside the clip rect */
441 x
= max( min( desktop
->cursor
.x
, desktop
->cursor
.clip
.right
- 1 ), desktop
->cursor
.clip
.left
);
442 y
= max( min( desktop
->cursor
.y
, desktop
->cursor
.clip
.bottom
- 1 ), desktop
->cursor
.clip
.top
);
443 if (x
!= desktop
->cursor
.x
|| y
!= desktop
->cursor
.y
) set_cursor_pos( desktop
, x
, y
);
446 /* change the foreground input and reset the cursor clip rect */
447 static void set_foreground_input( struct desktop
*desktop
, struct thread_input
*input
)
449 if (desktop
->foreground_input
== input
) return;
450 set_clip_rectangle( desktop
, NULL
, 1 );
451 desktop
->foreground_input
= input
;
454 /* get the hook table for a given thread */
455 struct hook_table
*get_queue_hooks( struct thread
*thread
)
457 if (!thread
->queue
) return NULL
;
458 return thread
->queue
->hooks
;
461 /* set the hook table for a given thread, allocating the queue if needed */
462 void set_queue_hooks( struct thread
*thread
, struct hook_table
*hooks
)
464 struct msg_queue
*queue
= thread
->queue
;
465 if (!queue
&& !(queue
= create_msg_queue( thread
, NULL
))) return;
466 if (queue
->hooks
) release_object( queue
->hooks
);
467 queue
->hooks
= hooks
;
470 /* check the queue status */
471 static inline int is_signaled( struct msg_queue
*queue
)
473 return ((queue
->wake_bits
& queue
->wake_mask
) || (queue
->changed_bits
& queue
->changed_mask
));
476 /* set some queue bits */
477 static inline void set_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
479 queue
->wake_bits
|= bits
;
480 queue
->changed_bits
|= bits
;
481 if (is_signaled( queue
)) wake_up( &queue
->obj
, 0 );
484 /* clear some queue bits */
485 static inline void clear_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
487 queue
->wake_bits
&= ~bits
;
488 queue
->changed_bits
&= ~bits
;
491 /* check whether msg is a keyboard message */
492 static inline int is_keyboard_msg( struct message
*msg
)
494 return (msg
->msg
>= WM_KEYFIRST
&& msg
->msg
<= WM_KEYLAST
);
497 /* check if message is matched by the filter */
498 static inline int check_msg_filter( unsigned int msg
, unsigned int first
, unsigned int last
)
500 return (msg
>= first
&& msg
<= last
);
503 /* check whether a message filter contains at least one potential hardware message */
504 static inline int filter_contains_hw_range( unsigned int first
, unsigned int last
)
506 /* hardware message ranges are (in numerical order):
507 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
508 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
509 * WM_MOUSEFIRST .. WM_MOUSELAST
511 if (last
< WM_NCMOUSEFIRST
) return 0;
512 if (first
> WM_NCMOUSELAST
&& last
< WM_INPUT_DEVICE_CHANGE
) return 0;
513 if (first
> WM_KEYLAST
&& last
< WM_MOUSEFIRST
) return 0;
514 if (first
> WM_MOUSELAST
) return 0;
518 /* get the QS_* bit corresponding to a given hardware message */
519 static inline int get_hardware_msg_bit( struct message
*msg
)
521 if (msg
->msg
== WM_INPUT_DEVICE_CHANGE
|| msg
->msg
== WM_INPUT
) return QS_RAWINPUT
;
522 if (msg
->msg
== WM_MOUSEMOVE
|| msg
->msg
== WM_NCMOUSEMOVE
) return QS_MOUSEMOVE
;
523 if (is_keyboard_msg( msg
)) return QS_KEY
;
524 return QS_MOUSEBUTTON
;
527 /* get the current thread queue, creating it if needed */
528 static inline struct msg_queue
*get_current_queue(void)
530 struct msg_queue
*queue
= current
->queue
;
531 if (!queue
) queue
= create_msg_queue( current
, NULL
);
535 /* get a (pseudo-)unique id to tag hardware messages */
536 static inline unsigned int get_unique_id(void)
538 static unsigned int id
;
539 if (!++id
) id
= 1; /* avoid an id of 0 */
543 /* try to merge a message with the last in the list; return 1 if successful */
544 static int merge_message( struct thread_input
*input
, const struct message
*msg
)
546 struct message
*prev
;
549 if (msg
->msg
!= WM_MOUSEMOVE
) return 0;
550 for (ptr
= list_tail( &input
->msg_list
); ptr
; ptr
= list_prev( &input
->msg_list
, ptr
))
552 prev
= LIST_ENTRY( ptr
, struct message
, entry
);
553 if (prev
->msg
!= WM_INPUT
) break;
556 if (prev
->result
) return 0;
557 if (prev
->win
&& msg
->win
&& prev
->win
!= msg
->win
) return 0;
558 if (prev
->msg
!= msg
->msg
) return 0;
559 if (prev
->type
!= msg
->type
) return 0;
560 /* now we can merge it */
561 prev
->wparam
= msg
->wparam
;
562 prev
->lparam
= msg
->lparam
;
565 prev
->time
= msg
->time
;
566 if (msg
->type
== MSG_HARDWARE
&& prev
->data
&& msg
->data
)
568 struct hardware_msg_data
*prev_data
= prev
->data
;
569 struct hardware_msg_data
*msg_data
= msg
->data
;
570 prev_data
->info
= msg_data
->info
;
573 list_add_tail( &input
->msg_list
, ptr
);
577 /* free a result structure */
578 static void free_result( struct message_result
*result
)
580 if (result
->timeout
) remove_timeout_user( result
->timeout
);
581 free( result
->data
);
582 if (result
->callback_msg
) free_message( result
->callback_msg
);
583 if (result
->hardware_msg
) free_message( result
->hardware_msg
);
584 if (result
->desktop
) release_object( result
->desktop
);
588 /* remove the result from the sender list it is on */
589 static inline void remove_result_from_sender( struct message_result
*result
)
591 assert( result
->sender
);
593 list_remove( &result
->sender_entry
);
594 result
->sender
= NULL
;
595 if (!result
->receiver
) free_result( result
);
598 /* store the message result in the appropriate structure */
599 static void store_message_result( struct message_result
*res
, lparam_t result
, unsigned int error
)
601 res
->result
= result
;
606 remove_timeout_user( res
->timeout
);
610 if (res
->hardware_msg
)
612 if (!error
&& result
) /* rejected by the hook */
613 free_message( res
->hardware_msg
);
615 queue_hardware_message( res
->desktop
, res
->hardware_msg
, 0 );
617 res
->hardware_msg
= NULL
;
622 if (res
->callback_msg
)
624 /* queue the callback message in the sender queue */
625 struct callback_msg_data
*data
= res
->callback_msg
->data
;
626 data
->result
= result
;
627 list_add_tail( &res
->sender
->msg_list
[SEND_MESSAGE
], &res
->callback_msg
->entry
);
628 set_queue_bits( res
->sender
, QS_SENDMESSAGE
);
629 res
->callback_msg
= NULL
;
630 remove_result_from_sender( res
);
634 /* wake sender queue if waiting on this result */
635 if (list_head(&res
->sender
->send_result
) == &res
->sender_entry
)
636 set_queue_bits( res
->sender
, QS_SMRESULT
);
639 else if (!res
->receiver
) free_result( res
);
642 /* free a message when deleting a queue or window */
643 static void free_message( struct message
*msg
)
645 struct message_result
*result
= msg
->result
;
649 result
->receiver
= NULL
;
650 store_message_result( result
, 0, STATUS_ACCESS_DENIED
/*FIXME*/ );
656 /* remove (and free) a message from a message list */
657 static void remove_queue_message( struct msg_queue
*queue
, struct message
*msg
,
658 enum message_kind kind
)
660 list_remove( &msg
->entry
);
664 if (list_empty( &queue
->msg_list
[kind
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
667 if (list_empty( &queue
->msg_list
[kind
] ) && !queue
->quit_message
)
668 clear_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
669 if (msg
->msg
== WM_HOTKEY
&& --queue
->hotkey_count
== 0)
670 clear_queue_bits( queue
, QS_HOTKEY
);
676 /* message timed out without getting a reply */
677 static void result_timeout( void *private )
679 struct message_result
*result
= private;
681 assert( !result
->replied
);
683 result
->timeout
= NULL
;
685 if (result
->msg
) /* not received yet */
687 struct message
*msg
= result
->msg
;
691 remove_queue_message( result
->receiver
, msg
, SEND_MESSAGE
);
692 result
->receiver
= NULL
;
694 store_message_result( result
, 0, STATUS_TIMEOUT
);
697 /* allocate and fill a message result structure */
698 static struct message_result
*alloc_message_result( struct msg_queue
*send_queue
,
699 struct msg_queue
*recv_queue
,
700 struct message
*msg
, timeout_t timeout
)
702 struct message_result
*result
= mem_alloc( sizeof(*result
) );
706 result
->sender
= send_queue
;
707 result
->receiver
= recv_queue
;
710 result
->data_size
= 0;
711 result
->timeout
= NULL
;
712 result
->hardware_msg
= NULL
;
713 result
->desktop
= NULL
;
714 result
->callback_msg
= NULL
;
716 if (msg
->type
== MSG_CALLBACK
)
718 struct message
*callback_msg
= mem_alloc( sizeof(*callback_msg
) );
725 callback_msg
->type
= MSG_CALLBACK_RESULT
;
726 callback_msg
->win
= msg
->win
;
727 callback_msg
->msg
= msg
->msg
;
728 callback_msg
->wparam
= 0;
729 callback_msg
->lparam
= 0;
730 callback_msg
->time
= get_tick_count();
731 callback_msg
->result
= NULL
;
732 /* steal the data from the original message */
733 callback_msg
->data
= msg
->data
;
734 callback_msg
->data_size
= msg
->data_size
;
738 result
->callback_msg
= callback_msg
;
739 list_add_head( &send_queue
->callback_result
, &result
->sender_entry
);
743 list_add_head( &send_queue
->send_result
, &result
->sender_entry
);
744 clear_queue_bits( send_queue
, QS_SMRESULT
);
747 if (timeout
!= TIMEOUT_INFINITE
)
748 result
->timeout
= add_timeout_user( timeout
, result_timeout
, result
);
753 /* receive a message, removing it from the sent queue */
754 static void receive_message( struct msg_queue
*queue
, struct message
*msg
,
755 struct get_message_reply
*reply
)
757 struct message_result
*result
= msg
->result
;
759 reply
->total
= msg
->data_size
;
760 if (msg
->data_size
> get_reply_max_size())
762 set_error( STATUS_BUFFER_OVERFLOW
);
765 reply
->type
= msg
->type
;
766 reply
->win
= msg
->win
;
767 reply
->msg
= msg
->msg
;
768 reply
->wparam
= msg
->wparam
;
769 reply
->lparam
= msg
->lparam
;
772 reply
->time
= msg
->time
;
774 if (msg
->data
) set_reply_data_ptr( msg
->data
, msg
->data_size
);
776 list_remove( &msg
->entry
);
777 /* put the result on the receiver result stack */
781 result
->recv_next
= queue
->recv_result
;
782 queue
->recv_result
= result
;
785 if (list_empty( &queue
->msg_list
[SEND_MESSAGE
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
788 /* set the result of the current received message */
789 static void reply_message( struct msg_queue
*queue
, lparam_t result
,
790 unsigned int error
, int remove
, const void *data
, data_size_t len
)
792 struct message_result
*res
= queue
->recv_result
;
796 queue
->recv_result
= res
->recv_next
;
797 res
->receiver
= NULL
;
798 if (!res
->sender
&& !res
->hardware_msg
) /* no one waiting for it */
806 if (len
&& (res
->data
= memdup( data
, len
))) res
->data_size
= len
;
807 store_message_result( res
, result
, error
);
811 static int match_window( user_handle_t win
, user_handle_t msg_win
)
814 if (win
== -1 || win
== 1) return !msg_win
;
815 if (msg_win
== win
) return 1;
816 return is_child_window( win
, msg_win
);
819 /* retrieve a posted message */
820 static int get_posted_message( struct msg_queue
*queue
, user_handle_t win
,
821 unsigned int first
, unsigned int last
, unsigned int flags
,
822 struct get_message_reply
*reply
)
826 /* check against the filters */
827 LIST_FOR_EACH_ENTRY( msg
, &queue
->msg_list
[POST_MESSAGE
], struct message
, entry
)
829 if (!match_window( win
, msg
->win
)) continue;
830 if (!check_msg_filter( msg
->msg
, first
, last
)) continue;
831 goto found
; /* found one */
835 /* return it to the app */
837 reply
->total
= msg
->data_size
;
838 if (msg
->data_size
> get_reply_max_size())
840 set_error( STATUS_BUFFER_OVERFLOW
);
843 reply
->type
= msg
->type
;
844 reply
->win
= msg
->win
;
845 reply
->msg
= msg
->msg
;
846 reply
->wparam
= msg
->wparam
;
847 reply
->lparam
= msg
->lparam
;
850 reply
->time
= msg
->time
;
852 if (flags
& PM_REMOVE
)
856 set_reply_data_ptr( msg
->data
, msg
->data_size
);
860 remove_queue_message( queue
, msg
, POST_MESSAGE
);
862 else if (msg
->data
) set_reply_data( msg
->data
, msg
->data_size
);
867 static int get_quit_message( struct msg_queue
*queue
, unsigned int flags
,
868 struct get_message_reply
*reply
)
870 if (queue
->quit_message
)
873 reply
->type
= MSG_POSTED
;
875 reply
->msg
= WM_QUIT
;
876 reply
->wparam
= queue
->exit_code
;
879 get_message_defaults( queue
, &reply
->x
, &reply
->y
, &reply
->time
);
881 if (flags
& PM_REMOVE
)
883 queue
->quit_message
= 0;
884 if (list_empty( &queue
->msg_list
[POST_MESSAGE
] ))
885 clear_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
893 /* empty a message list and free all the messages */
894 static void empty_msg_list( struct list
*list
)
898 while ((ptr
= list_head( list
)) != NULL
)
900 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
901 list_remove( &msg
->entry
);
906 /* cleanup all pending results when deleting a queue */
907 static void cleanup_results( struct msg_queue
*queue
)
911 while ((entry
= list_head( &queue
->send_result
)) != NULL
)
913 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
916 while ((entry
= list_head( &queue
->callback_result
)) != NULL
)
918 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
921 while (queue
->recv_result
)
922 reply_message( queue
, 0, STATUS_ACCESS_DENIED
/*FIXME*/, 1, NULL
, 0 );
925 /* check if the thread owning the queue is hung (not checking for messages) */
926 static int is_queue_hung( struct msg_queue
*queue
)
928 struct wait_queue_entry
*entry
;
930 if (current_time
- queue
->last_get_msg
<= 5 * TICKS_PER_SEC
)
931 return 0; /* less than 5 seconds since last get message -> not hung */
933 LIST_FOR_EACH_ENTRY( entry
, &queue
->obj
.wait_queue
, struct wait_queue_entry
, entry
)
935 if (get_wait_queue_thread(entry
)->queue
== queue
)
936 return 0; /* thread is waiting on queue -> not hung */
941 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
943 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
944 struct process
*process
= get_wait_queue_thread(entry
)->process
;
946 /* a thread can only wait on its own queue */
947 if (get_wait_queue_thread(entry
)->queue
!= queue
)
949 set_error( STATUS_ACCESS_DENIED
);
952 if (process
->idle_event
&& !(queue
->wake_mask
& QS_SMRESULT
)) set_event( process
->idle_event
);
954 if (queue
->fd
&& list_empty( &obj
->wait_queue
)) /* first on the queue */
955 set_fd_events( queue
->fd
, POLLIN
);
956 add_queue( obj
, entry
);
960 static void msg_queue_remove_queue(struct object
*obj
, struct wait_queue_entry
*entry
)
962 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
964 remove_queue( obj
, entry
);
965 if (queue
->fd
&& list_empty( &obj
->wait_queue
)) /* last on the queue is gone */
966 set_fd_events( queue
->fd
, 0 );
969 static void msg_queue_dump( struct object
*obj
, int verbose
)
971 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
972 fprintf( stderr
, "Msg queue bits=%x mask=%x\n",
973 queue
->wake_bits
, queue
->wake_mask
);
976 static int msg_queue_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
978 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
983 if ((ret
= check_fd_events( queue
->fd
, POLLIN
)))
984 /* stop waiting on select() if we are signaled */
985 set_fd_events( queue
->fd
, 0 );
986 else if (!list_empty( &obj
->wait_queue
))
987 /* restart waiting on poll() if we are no longer signaled */
988 set_fd_events( queue
->fd
, POLLIN
);
990 return ret
|| is_signaled( queue
);
993 static void msg_queue_satisfied( struct object
*obj
, struct wait_queue_entry
*entry
)
995 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
996 queue
->wake_mask
= 0;
997 queue
->changed_mask
= 0;
1000 static void msg_queue_destroy( struct object
*obj
)
1002 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
1004 struct hotkey
*hotkey
, *hotkey2
;
1007 cleanup_results( queue
);
1008 for (i
= 0; i
< NB_MSG_KINDS
; i
++) empty_msg_list( &queue
->msg_list
[i
] );
1010 LIST_FOR_EACH_ENTRY_SAFE( hotkey
, hotkey2
, &queue
->input
->desktop
->hotkeys
, struct hotkey
, entry
)
1012 if (hotkey
->queue
== queue
)
1014 list_remove( &hotkey
->entry
);
1019 while ((ptr
= list_head( &queue
->pending_timers
)))
1021 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1022 list_remove( &timer
->entry
);
1025 while ((ptr
= list_head( &queue
->expired_timers
)))
1027 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1028 list_remove( &timer
->entry
);
1031 if (queue
->timeout
) remove_timeout_user( queue
->timeout
);
1032 queue
->input
->cursor_count
-= queue
->cursor_count
;
1033 release_object( queue
->input
);
1034 if (queue
->hooks
) release_object( queue
->hooks
);
1035 if (queue
->fd
) release_object( queue
->fd
);
1038 static void msg_queue_poll_event( struct fd
*fd
, int event
)
1040 struct msg_queue
*queue
= get_fd_user( fd
);
1041 assert( queue
->obj
.ops
== &msg_queue_ops
);
1043 if (event
& (POLLERR
| POLLHUP
)) set_fd_events( fd
, -1 );
1044 else set_fd_events( queue
->fd
, 0 );
1045 wake_up( &queue
->obj
, 0 );
1048 static void thread_input_dump( struct object
*obj
, int verbose
)
1050 struct thread_input
*input
= (struct thread_input
*)obj
;
1051 fprintf( stderr
, "Thread input focus=%08x capture=%08x active=%08x\n",
1052 input
->focus
, input
->capture
, input
->active
);
1055 static void thread_input_destroy( struct object
*obj
)
1057 struct thread_input
*input
= (struct thread_input
*)obj
;
1059 empty_msg_list( &input
->msg_list
);
1062 if (input
->desktop
->foreground_input
== input
) set_foreground_input( input
->desktop
, NULL
);
1063 release_object( input
->desktop
);
1067 /* fix the thread input data when a window is destroyed */
1068 static inline void thread_input_cleanup_window( struct msg_queue
*queue
, user_handle_t window
)
1070 struct thread_input
*input
= queue
->input
;
1072 if (window
== input
->focus
) input
->focus
= 0;
1073 if (window
== input
->capture
) input
->capture
= 0;
1074 if (window
== input
->active
) input
->active
= 0;
1075 if (window
== input
->menu_owner
) input
->menu_owner
= 0;
1076 if (window
== input
->move_size
) input
->move_size
= 0;
1077 if (window
== input
->caret
) set_caret_window( input
, 0 );
1080 /* check if the specified window can be set in the input data of a given queue */
1081 static int check_queue_input_window( struct msg_queue
*queue
, user_handle_t window
)
1083 struct thread
*thread
;
1086 if (!window
) return 1; /* we can always clear the data */
1088 if ((thread
= get_window_thread( window
)))
1090 ret
= (queue
->input
== thread
->queue
->input
);
1091 if (!ret
) set_error( STATUS_ACCESS_DENIED
);
1092 release_object( thread
);
1094 else set_error( STATUS_INVALID_HANDLE
);
1099 /* make sure the specified thread has a queue */
1100 int init_thread_queue( struct thread
*thread
)
1102 if (thread
->queue
) return 1;
1103 return (create_msg_queue( thread
, NULL
) != NULL
);
1106 /* attach two thread input data structures */
1107 int attach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
1109 struct desktop
*desktop
;
1110 struct thread_input
*input
;
1113 if (!thread_to
->queue
&& !(thread_to
->queue
= create_msg_queue( thread_to
, NULL
))) return 0;
1114 if (!(desktop
= get_thread_desktop( thread_from
, 0 ))) return 0;
1115 input
= (struct thread_input
*)grab_object( thread_to
->queue
->input
);
1116 if (input
->desktop
!= desktop
)
1118 set_error( STATUS_ACCESS_DENIED
);
1119 release_object( input
);
1120 release_object( desktop
);
1123 release_object( desktop
);
1125 if (thread_from
->queue
)
1127 if (!input
->focus
) input
->focus
= thread_from
->queue
->input
->focus
;
1128 if (!input
->active
) input
->active
= thread_from
->queue
->input
->active
;
1131 ret
= assign_thread_input( thread_from
, input
);
1132 if (ret
) memset( input
->keystate
, 0, sizeof(input
->keystate
) );
1133 release_object( input
);
1137 /* detach two thread input data structures */
1138 void detach_thread_input( struct thread
*thread_from
)
1140 struct thread
*thread
;
1141 struct thread_input
*input
, *old_input
= thread_from
->queue
->input
;
1143 if ((input
= create_thread_input( thread_from
)))
1145 if (old_input
->focus
&& (thread
= get_window_thread( old_input
->focus
)))
1147 if (thread
== thread_from
)
1149 input
->focus
= old_input
->focus
;
1150 old_input
->focus
= 0;
1152 release_object( thread
);
1154 if (old_input
->active
&& (thread
= get_window_thread( old_input
->active
)))
1156 if (thread
== thread_from
)
1158 input
->active
= old_input
->active
;
1159 old_input
->active
= 0;
1161 release_object( thread
);
1163 assign_thread_input( thread_from
, input
);
1164 release_object( input
);
1169 /* set the next timer to expire */
1170 static void set_next_timer( struct msg_queue
*queue
)
1176 remove_timeout_user( queue
->timeout
);
1177 queue
->timeout
= NULL
;
1179 if ((ptr
= list_head( &queue
->pending_timers
)))
1181 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1182 queue
->timeout
= add_timeout_user( abstime_to_timeout(timer
->when
), timer_callback
, queue
);
1184 /* set/clear QS_TIMER bit */
1185 if (list_empty( &queue
->expired_timers
))
1186 clear_queue_bits( queue
, QS_TIMER
);
1188 set_queue_bits( queue
, QS_TIMER
);
1191 /* find a timer from its window and id */
1192 static struct timer
*find_timer( struct msg_queue
*queue
, user_handle_t win
,
1193 unsigned int msg
, lparam_t id
)
1197 /* we need to search both lists */
1199 LIST_FOR_EACH( ptr
, &queue
->pending_timers
)
1201 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1202 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
1204 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
1206 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1207 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
1212 /* callback for the next timer expiration */
1213 static void timer_callback( void *private )
1215 struct msg_queue
*queue
= private;
1218 queue
->timeout
= NULL
;
1219 /* move on to the next timer */
1220 ptr
= list_head( &queue
->pending_timers
);
1222 list_add_tail( &queue
->expired_timers
, ptr
);
1223 set_next_timer( queue
);
1226 /* link a timer at its rightful place in the queue list */
1227 static void link_timer( struct msg_queue
*queue
, struct timer
*timer
)
1231 for (ptr
= queue
->pending_timers
.next
; ptr
!= &queue
->pending_timers
; ptr
= ptr
->next
)
1233 struct timer
*t
= LIST_ENTRY( ptr
, struct timer
, entry
);
1234 if (t
->when
<= timer
->when
) break;
1236 list_add_before( ptr
, &timer
->entry
);
1239 /* remove a timer from the queue timer list and free it */
1240 static void free_timer( struct msg_queue
*queue
, struct timer
*timer
)
1242 list_remove( &timer
->entry
);
1244 set_next_timer( queue
);
1247 /* restart an expired timer */
1248 static void restart_timer( struct msg_queue
*queue
, struct timer
*timer
)
1250 list_remove( &timer
->entry
);
1251 while (-timer
->when
<= monotonic_time
) timer
->when
-= (timeout_t
)timer
->rate
* 10000;
1252 link_timer( queue
, timer
);
1253 set_next_timer( queue
);
1256 /* find an expired timer matching the filtering parameters */
1257 static struct timer
*find_expired_timer( struct msg_queue
*queue
, user_handle_t win
,
1258 unsigned int get_first
, unsigned int get_last
,
1263 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
1265 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1266 if (win
&& timer
->win
!= win
) continue;
1267 if (check_msg_filter( timer
->msg
, get_first
, get_last
))
1269 if (remove
) restart_timer( queue
, timer
);
1277 static struct timer
*set_timer( struct msg_queue
*queue
, unsigned int rate
)
1279 struct timer
*timer
= mem_alloc( sizeof(*timer
) );
1282 timer
->rate
= max( rate
, 1 );
1283 timer
->when
= -monotonic_time
- (timeout_t
)timer
->rate
* 10000;
1284 link_timer( queue
, timer
);
1285 /* check if we replaced the next timer */
1286 if (list_head( &queue
->pending_timers
) == &timer
->entry
) set_next_timer( queue
);
1291 /* change the input key state for a given key */
1292 static void set_input_key_state( unsigned char *keystate
, unsigned char key
, int down
)
1296 if (!(keystate
[key
] & 0x80)) keystate
[key
] ^= 0x01;
1297 keystate
[key
] |= down
;
1299 else keystate
[key
] &= ~0x80;
1302 /* update the input key state for a keyboard message */
1303 static void update_input_key_state( struct desktop
*desktop
, unsigned char *keystate
,
1304 unsigned int msg
, lparam_t wparam
)
1311 case WM_LBUTTONDOWN
:
1312 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1315 set_input_key_state( keystate
, VK_LBUTTON
, down
);
1317 case WM_MBUTTONDOWN
:
1318 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1321 set_input_key_state( keystate
, VK_MBUTTON
, down
);
1323 case WM_RBUTTONDOWN
:
1324 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1327 set_input_key_state( keystate
, VK_RBUTTON
, down
);
1329 case WM_XBUTTONDOWN
:
1330 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1333 if (wparam
>> 16 == XBUTTON1
) set_input_key_state( keystate
, VK_XBUTTON1
, down
);
1334 else if (wparam
>> 16 == XBUTTON2
) set_input_key_state( keystate
, VK_XBUTTON2
, down
);
1338 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1342 key
= (unsigned char)wparam
;
1343 set_input_key_state( keystate
, key
, down
);
1348 down
= (keystate
[VK_LCONTROL
] | keystate
[VK_RCONTROL
]) & 0x80;
1349 set_input_key_state( keystate
, VK_CONTROL
, down
);
1353 down
= (keystate
[VK_LMENU
] | keystate
[VK_RMENU
]) & 0x80;
1354 set_input_key_state( keystate
, VK_MENU
, down
);
1358 down
= (keystate
[VK_LSHIFT
] | keystate
[VK_RSHIFT
]) & 0x80;
1359 set_input_key_state( keystate
, VK_SHIFT
, down
);
1366 /* update the desktop key state according to a mouse message flags */
1367 static void update_desktop_mouse_state( struct desktop
*desktop
, unsigned int flags
,
1368 int x
, int y
, lparam_t wparam
)
1370 if (flags
& MOUSEEVENTF_MOVE
)
1371 update_desktop_cursor_pos( desktop
, x
, y
);
1372 if (flags
& MOUSEEVENTF_LEFTDOWN
)
1373 update_input_key_state( desktop
, desktop
->keystate
, WM_LBUTTONDOWN
, wparam
);
1374 if (flags
& MOUSEEVENTF_LEFTUP
)
1375 update_input_key_state( desktop
, desktop
->keystate
, WM_LBUTTONUP
, wparam
);
1376 if (flags
& MOUSEEVENTF_RIGHTDOWN
)
1377 update_input_key_state( desktop
, desktop
->keystate
, WM_RBUTTONDOWN
, wparam
);
1378 if (flags
& MOUSEEVENTF_RIGHTUP
)
1379 update_input_key_state( desktop
, desktop
->keystate
, WM_RBUTTONUP
, wparam
);
1380 if (flags
& MOUSEEVENTF_MIDDLEDOWN
)
1381 update_input_key_state( desktop
, desktop
->keystate
, WM_MBUTTONDOWN
, wparam
);
1382 if (flags
& MOUSEEVENTF_MIDDLEUP
)
1383 update_input_key_state( desktop
, desktop
->keystate
, WM_MBUTTONUP
, wparam
);
1384 if (flags
& MOUSEEVENTF_XDOWN
)
1385 update_input_key_state( desktop
, desktop
->keystate
, WM_XBUTTONDOWN
, wparam
);
1386 if (flags
& MOUSEEVENTF_XUP
)
1387 update_input_key_state( desktop
, desktop
->keystate
, WM_XBUTTONUP
, wparam
);
1390 /* release the hardware message currently being processed by the given thread */
1391 static void release_hardware_message( struct msg_queue
*queue
, unsigned int hw_id
)
1393 struct thread_input
*input
= queue
->input
;
1394 struct message
*msg
, *other
;
1397 LIST_FOR_EACH_ENTRY( msg
, &input
->msg_list
, struct message
, entry
)
1399 if (msg
->unique_id
== hw_id
) break;
1401 if (&msg
->entry
== &input
->msg_list
) return; /* not found */
1403 /* clear the queue bit for that message */
1404 clr_bit
= get_hardware_msg_bit( msg
);
1405 LIST_FOR_EACH_ENTRY( other
, &input
->msg_list
, struct message
, entry
)
1407 if (other
!= msg
&& get_hardware_msg_bit( other
) == clr_bit
)
1413 if (clr_bit
) clear_queue_bits( queue
, clr_bit
);
1415 update_input_key_state( input
->desktop
, input
->keystate
, msg
->msg
, msg
->wparam
);
1416 list_remove( &msg
->entry
);
1417 free_message( msg
);
1420 static int queue_hotkey_message( struct desktop
*desktop
, struct message
*msg
)
1422 struct hotkey
*hotkey
;
1423 unsigned int modifiers
= 0;
1425 if (msg
->msg
!= WM_KEYDOWN
&& msg
->msg
!= WM_SYSKEYDOWN
) return 0;
1427 if (desktop
->keystate
[VK_MENU
] & 0x80) modifiers
|= MOD_ALT
;
1428 if (desktop
->keystate
[VK_CONTROL
] & 0x80) modifiers
|= MOD_CONTROL
;
1429 if (desktop
->keystate
[VK_SHIFT
] & 0x80) modifiers
|= MOD_SHIFT
;
1430 if ((desktop
->keystate
[VK_LWIN
] & 0x80) || (desktop
->keystate
[VK_RWIN
] & 0x80)) modifiers
|= MOD_WIN
;
1432 LIST_FOR_EACH_ENTRY( hotkey
, &desktop
->hotkeys
, struct hotkey
, entry
)
1434 if (hotkey
->vkey
!= msg
->wparam
) continue;
1435 if ((hotkey
->flags
& (MOD_ALT
|MOD_CONTROL
|MOD_SHIFT
|MOD_WIN
)) == modifiers
) goto found
;
1441 msg
->type
= MSG_POSTED
;
1442 msg
->win
= hotkey
->win
;
1443 msg
->msg
= WM_HOTKEY
;
1444 msg
->wparam
= hotkey
->id
;
1445 msg
->lparam
= ((hotkey
->vkey
& 0xffff) << 16) | modifiers
;
1451 list_add_tail( &hotkey
->queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1452 set_queue_bits( hotkey
->queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
|QS_HOTKEY
);
1453 hotkey
->queue
->hotkey_count
++;
1457 /* find the window that should receive a given hardware message */
1458 static user_handle_t
find_hardware_message_window( struct desktop
*desktop
, struct thread_input
*input
,
1459 struct message
*msg
, unsigned int *msg_code
,
1460 struct thread
**thread
)
1462 user_handle_t win
= 0;
1465 *msg_code
= msg
->msg
;
1466 if (msg
->msg
== WM_INPUT
|| msg
->msg
== WM_INPUT_DEVICE_CHANGE
)
1468 if (!(win
= msg
->win
) && input
) win
= input
->focus
;
1470 else if (is_keyboard_msg( msg
))
1472 if (input
&& !(win
= input
->focus
))
1474 win
= input
->active
;
1475 if (*msg_code
< WM_SYSKEYDOWN
) *msg_code
+= WM_SYSKEYDOWN
- WM_KEYDOWN
;
1478 else if (!input
|| !(win
= input
->capture
)) /* mouse message */
1480 if (is_window_visible( msg
->win
) && !is_window_transparent( msg
->win
)) win
= msg
->win
;
1481 else win
= shallow_window_from_point( desktop
, msg
->x
, msg
->y
);
1483 *thread
= window_thread_from_point( win
, msg
->x
, msg
->y
);
1487 *thread
= get_window_thread( win
);
1491 static struct rawinput_device_entry
*find_rawinput_device( struct process
*process
, unsigned short usage_page
, unsigned short usage
)
1493 struct rawinput_device_entry
*e
;
1495 LIST_FOR_EACH_ENTRY( e
, &process
->rawinput_devices
, struct rawinput_device_entry
, entry
)
1497 if (e
->device
.usage_page
!= usage_page
|| e
->device
.usage
!= usage
) continue;
1504 static void update_rawinput_device(const struct rawinput_device
*device
)
1506 struct rawinput_device_entry
*e
;
1508 if (!(e
= find_rawinput_device( current
->process
, device
->usage_page
, device
->usage
)))
1510 if (!(e
= mem_alloc( sizeof(*e
) ))) return;
1511 list_add_tail( ¤t
->process
->rawinput_devices
, &e
->entry
);
1514 if (device
->flags
& RIDEV_REMOVE
)
1516 list_remove( &e
->entry
);
1521 e
->device
= *device
;
1522 e
->device
.target
= get_user_full_handle( e
->device
.target
);
1525 static void prepend_cursor_history( int x
, int y
, unsigned int time
, lparam_t info
)
1527 cursor_pos_t
*pos
= &cursor_history
[--cursor_history_latest
% ARRAY_SIZE(cursor_history
)];
1535 /* queue a hardware message into a given thread input */
1536 static void queue_hardware_message( struct desktop
*desktop
, struct message
*msg
, int always_queue
)
1539 struct thread
*thread
;
1540 struct thread_input
*input
;
1541 struct hardware_msg_data
*msg_data
= msg
->data
;
1542 unsigned int msg_code
;
1544 update_input_key_state( desktop
, desktop
->keystate
, msg
->msg
, msg
->wparam
);
1545 last_input_time
= get_tick_count();
1546 if (msg
->msg
!= WM_MOUSEMOVE
) always_queue
= 1;
1548 if (is_keyboard_msg( msg
))
1550 if (queue_hotkey_message( desktop
, msg
)) return;
1551 if (desktop
->keystate
[VK_MENU
] & 0x80) msg
->lparam
|= KF_ALTDOWN
<< 16;
1552 if (msg
->wparam
== VK_SHIFT
|| msg
->wparam
== VK_LSHIFT
|| msg
->wparam
== VK_RSHIFT
)
1553 msg
->lparam
&= ~(KF_EXTENDED
<< 16);
1555 else if (msg
->msg
!= WM_INPUT
&& msg
->msg
!= WM_INPUT_DEVICE_CHANGE
)
1557 if (msg
->msg
== WM_MOUSEMOVE
)
1559 prepend_cursor_history( msg
->x
, msg
->y
, msg
->time
, msg_data
->info
);
1560 if (update_desktop_cursor_pos( desktop
, msg
->x
, msg
->y
)) always_queue
= 1;
1562 if (desktop
->keystate
[VK_LBUTTON
] & 0x80) msg
->wparam
|= MK_LBUTTON
;
1563 if (desktop
->keystate
[VK_MBUTTON
] & 0x80) msg
->wparam
|= MK_MBUTTON
;
1564 if (desktop
->keystate
[VK_RBUTTON
] & 0x80) msg
->wparam
|= MK_RBUTTON
;
1565 if (desktop
->keystate
[VK_SHIFT
] & 0x80) msg
->wparam
|= MK_SHIFT
;
1566 if (desktop
->keystate
[VK_CONTROL
] & 0x80) msg
->wparam
|= MK_CONTROL
;
1567 if (desktop
->keystate
[VK_XBUTTON1
] & 0x80) msg
->wparam
|= MK_XBUTTON1
;
1568 if (desktop
->keystate
[VK_XBUTTON2
] & 0x80) msg
->wparam
|= MK_XBUTTON2
;
1570 msg
->x
= desktop
->cursor
.x
;
1571 msg
->y
= desktop
->cursor
.y
;
1573 if (msg
->win
&& (thread
= get_window_thread( msg
->win
)))
1575 input
= thread
->queue
->input
;
1576 release_object( thread
);
1578 else input
= desktop
->foreground_input
;
1580 win
= find_hardware_message_window( desktop
, input
, msg
, &msg_code
, &thread
);
1581 if (!win
|| !thread
)
1583 if (input
) update_input_key_state( input
->desktop
, input
->keystate
, msg
->msg
, msg
->wparam
);
1584 free_message( msg
);
1587 input
= thread
->queue
->input
;
1589 if (win
!= desktop
->cursor
.win
) always_queue
= 1;
1590 desktop
->cursor
.win
= win
;
1592 if (!always_queue
|| merge_message( input
, msg
)) free_message( msg
);
1595 msg
->unique_id
= 0; /* will be set once we return it to the app */
1596 list_add_tail( &input
->msg_list
, &msg
->entry
);
1597 set_queue_bits( thread
->queue
, get_hardware_msg_bit(msg
) );
1599 release_object( thread
);
1602 /* send the low-level hook message for a given hardware message */
1603 static int send_hook_ll_message( struct desktop
*desktop
, struct message
*hardware_msg
,
1604 const hw_input_t
*input
, struct msg_queue
*sender
)
1606 struct thread
*hook_thread
;
1607 struct msg_queue
*queue
;
1608 struct message
*msg
;
1609 timeout_t timeout
= 2000 * -10000; /* FIXME: load from registry */
1610 int id
= (input
->type
== INPUT_MOUSE
) ? WH_MOUSE_LL
: WH_KEYBOARD_LL
;
1612 if (!(hook_thread
= get_first_global_hook( id
))) return 0;
1613 if (!(queue
= hook_thread
->queue
)) return 0;
1614 if (is_queue_hung( queue
)) return 0;
1616 if (!(msg
= mem_alloc( sizeof(*msg
) ))) return 0;
1618 msg
->type
= MSG_HOOK_LL
;
1621 msg
->wparam
= hardware_msg
->msg
;
1622 msg
->x
= hardware_msg
->x
;
1623 msg
->y
= hardware_msg
->y
;
1624 msg
->time
= hardware_msg
->time
;
1625 msg
->data_size
= hardware_msg
->data_size
;
1628 if (input
->type
== INPUT_KEYBOARD
)
1630 unsigned short vkey
= input
->kbd
.vkey
;
1631 if (input
->kbd
.flags
& KEYEVENTF_UNICODE
) vkey
= VK_PACKET
;
1632 msg
->lparam
= (input
->kbd
.scan
<< 16) | vkey
;
1634 else msg
->lparam
= input
->mouse
.data
<< 16;
1636 if (!(msg
->data
= memdup( hardware_msg
->data
, hardware_msg
->data_size
)) ||
1637 !(msg
->result
= alloc_message_result( sender
, queue
, msg
, timeout
)))
1639 free_message( msg
);
1642 msg
->result
->hardware_msg
= hardware_msg
;
1643 msg
->result
->desktop
= (struct desktop
*)grab_object( desktop
);
1644 list_add_tail( &queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1645 set_queue_bits( queue
, QS_SENDMESSAGE
);
1649 /* get the foreground thread for a desktop and a window receiving input */
1650 static struct thread
*get_foreground_thread( struct desktop
*desktop
, user_handle_t window
)
1652 /* if desktop has no foreground process, assume the receiving window is */
1653 if (desktop
->foreground_input
) return get_window_thread( desktop
->foreground_input
->focus
);
1654 if (window
) return get_window_thread( window
);
1658 struct rawinput_message
1660 struct thread
*foreground
;
1661 struct desktop
*desktop
;
1662 struct hw_msg_source source
;
1664 unsigned int message
;
1665 struct hardware_msg_data data
;
1666 const void *hid_report
;
1669 /* check if process is supposed to receive a WM_INPUT message and eventually queue it */
1670 static int queue_rawinput_message( struct process
* process
, void *arg
)
1672 const struct rawinput_message
* raw_msg
= arg
;
1673 const struct rawinput_device_entry
*entry
;
1674 const struct rawinput_device
*device
= NULL
;
1675 struct desktop
*target_desktop
= NULL
, *desktop
= NULL
;
1676 struct thread
*target_thread
= NULL
, *foreground
= NULL
;
1677 struct message
*msg
;
1678 data_size_t report_size
;
1679 int wparam
= RIM_INPUT
;
1681 if (raw_msg
->data
.rawinput
.type
== RIM_TYPEMOUSE
)
1682 device
= process
->rawinput_mouse
;
1683 else if (raw_msg
->data
.rawinput
.type
== RIM_TYPEKEYBOARD
)
1684 device
= process
->rawinput_kbd
;
1685 else if ((entry
= find_rawinput_device( process
, raw_msg
->data
.rawinput
.hid
.usage_page
, raw_msg
->data
.rawinput
.hid
.usage
)))
1686 device
= &entry
->device
;
1687 if (!device
) return 0;
1689 if (raw_msg
->message
== WM_INPUT_DEVICE_CHANGE
&& !(device
->flags
& RIDEV_DEVNOTIFY
)) return 0;
1691 if (raw_msg
->desktop
) desktop
= (struct desktop
*)grab_object( raw_msg
->desktop
);
1692 else if (!(desktop
= get_desktop_obj( process
, process
->desktop
, 0 ))) goto done
;
1694 if (raw_msg
->foreground
) foreground
= (struct thread
*)grab_object( raw_msg
->foreground
);
1695 else if (!(foreground
= get_foreground_thread( desktop
, 0 ))) goto done
;
1697 if (process
!= foreground
->process
)
1699 if (raw_msg
->message
== WM_INPUT
&& !(device
->flags
& RIDEV_INPUTSINK
)) goto done
;
1700 if (!(target_thread
= get_window_thread( device
->target
))) goto done
;
1701 if (!(target_desktop
= get_thread_desktop( target_thread
, 0 ))) goto done
;
1702 if (target_desktop
!= desktop
) goto done
;
1703 wparam
= RIM_INPUTSINK
;
1706 if (raw_msg
->data
.rawinput
.type
!= RIM_TYPEHID
|| !raw_msg
->hid_report
) report_size
= 0;
1707 else report_size
= raw_msg
->data
.size
- sizeof(raw_msg
->data
);
1709 if (!(msg
= alloc_hardware_message( raw_msg
->data
.info
, raw_msg
->source
, raw_msg
->time
, report_size
)))
1712 msg
->win
= device
->target
;
1713 msg
->msg
= raw_msg
->message
;
1714 msg
->wparam
= wparam
;
1716 memcpy( msg
->data
, &raw_msg
->data
, sizeof(raw_msg
->data
) );
1717 if (report_size
) memcpy( (struct hardware_msg_data
*)msg
->data
+ 1, raw_msg
->hid_report
, report_size
);
1719 if (raw_msg
->message
== WM_INPUT_DEVICE_CHANGE
&& raw_msg
->data
.rawinput
.type
== RIM_TYPEHID
)
1721 msg
->wparam
= raw_msg
->data
.rawinput
.hid
.param
;
1722 msg
->lparam
= raw_msg
->data
.rawinput
.hid
.device
;
1725 queue_hardware_message( desktop
, msg
, 1 );
1728 if (target_thread
) release_object( target_thread
);
1729 if (target_desktop
) release_object( target_desktop
);
1730 if (foreground
) release_object( foreground
);
1731 if (desktop
) release_object( desktop
);
1735 /* queue a hardware message for a mouse event */
1736 static int queue_mouse_message( struct desktop
*desktop
, user_handle_t win
, const hw_input_t
*input
,
1737 unsigned int origin
, struct msg_queue
*sender
)
1739 const struct rawinput_device
*device
;
1740 struct hardware_msg_data
*msg_data
;
1741 struct rawinput_message raw_msg
;
1742 struct message
*msg
;
1743 struct thread
*foreground
;
1744 unsigned int i
, time
, flags
;
1745 struct hw_msg_source source
= { IMDT_MOUSE
, origin
};
1748 static const unsigned int messages
[] =
1750 WM_MOUSEMOVE
, /* 0x0001 = MOUSEEVENTF_MOVE */
1751 WM_LBUTTONDOWN
, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1752 WM_LBUTTONUP
, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1753 WM_RBUTTONDOWN
, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1754 WM_RBUTTONUP
, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1755 WM_MBUTTONDOWN
, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1756 WM_MBUTTONUP
, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1757 WM_XBUTTONDOWN
, /* 0x0080 = MOUSEEVENTF_XDOWN */
1758 WM_XBUTTONUP
, /* 0x0100 = MOUSEEVENTF_XUP */
1759 0, /* 0x0200 = unused */
1760 0, /* 0x0400 = unused */
1761 WM_MOUSEWHEEL
, /* 0x0800 = MOUSEEVENTF_WHEEL */
1762 WM_MOUSEHWHEEL
/* 0x1000 = MOUSEEVENTF_HWHEEL */
1765 desktop
->cursor
.last_change
= get_tick_count();
1766 flags
= input
->mouse
.flags
;
1767 time
= input
->mouse
.time
;
1768 if (!time
) time
= desktop
->cursor
.last_change
;
1770 if (flags
& MOUSEEVENTF_MOVE
)
1772 if (flags
& MOUSEEVENTF_ABSOLUTE
)
1776 if (flags
& ~(MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
) &&
1777 x
== desktop
->cursor
.x
&& y
== desktop
->cursor
.y
)
1778 flags
&= ~MOUSEEVENTF_MOVE
;
1782 x
= desktop
->cursor
.x
+ input
->mouse
.x
;
1783 y
= desktop
->cursor
.y
+ input
->mouse
.y
;
1788 x
= desktop
->cursor
.x
;
1789 y
= desktop
->cursor
.y
;
1792 if ((foreground
= get_foreground_thread( desktop
, win
)))
1794 memset( &raw_msg
, 0, sizeof(raw_msg
) );
1795 raw_msg
.foreground
= foreground
;
1796 raw_msg
.desktop
= desktop
;
1797 raw_msg
.source
= source
;
1798 raw_msg
.time
= time
;
1799 raw_msg
.message
= WM_INPUT
;
1801 msg_data
= &raw_msg
.data
;
1802 msg_data
->info
= input
->mouse
.info
;
1803 msg_data
->size
= sizeof(*msg_data
);
1804 msg_data
->flags
= flags
;
1805 msg_data
->rawinput
.type
= RIM_TYPEMOUSE
;
1806 msg_data
->rawinput
.mouse
.x
= x
- desktop
->cursor
.x
;
1807 msg_data
->rawinput
.mouse
.y
= y
- desktop
->cursor
.y
;
1808 msg_data
->rawinput
.mouse
.data
= input
->mouse
.data
;
1810 enum_processes( queue_rawinput_message
, &raw_msg
);
1811 release_object( foreground
);
1814 if ((device
= current
->process
->rawinput_mouse
) && (device
->flags
& RIDEV_NOLEGACY
))
1816 update_desktop_mouse_state( desktop
, flags
, x
, y
, input
->mouse
.data
<< 16 );
1820 for (i
= 0; i
< ARRAY_SIZE( messages
); i
++)
1822 if (!messages
[i
]) continue;
1823 if (!(flags
& (1 << i
))) continue;
1826 if (!(msg
= alloc_hardware_message( input
->mouse
.info
, source
, time
, 0 ))) return 0;
1827 msg_data
= msg
->data
;
1829 msg
->win
= get_user_full_handle( win
);
1830 msg
->msg
= messages
[i
];
1831 msg
->wparam
= input
->mouse
.data
<< 16;
1835 if (origin
== IMO_INJECTED
) msg_data
->flags
= LLMHF_INJECTED
;
1837 /* specify a sender only when sending the last message */
1838 if (!(flags
& ((1 << ARRAY_SIZE( messages
)) - 1)))
1840 if (!(wait
= send_hook_ll_message( desktop
, msg
, input
, sender
)))
1841 queue_hardware_message( desktop
, msg
, 0 );
1843 else if (!send_hook_ll_message( desktop
, msg
, input
, NULL
))
1844 queue_hardware_message( desktop
, msg
, 0 );
1849 /* queue a hardware message for a keyboard event */
1850 static int queue_keyboard_message( struct desktop
*desktop
, user_handle_t win
, const hw_input_t
*input
,
1851 unsigned int origin
, struct msg_queue
*sender
)
1853 struct hw_msg_source source
= { IMDT_KEYBOARD
, origin
};
1854 const struct rawinput_device
*device
;
1855 struct hardware_msg_data
*msg_data
;
1856 struct rawinput_message raw_msg
;
1857 struct message
*msg
;
1858 struct thread
*foreground
;
1859 unsigned char vkey
= input
->kbd
.vkey
;
1860 unsigned int message_code
, time
;
1863 if (!(time
= input
->kbd
.time
)) time
= get_tick_count();
1865 if (!(input
->kbd
.flags
& KEYEVENTF_UNICODE
))
1872 vkey
= (input
->kbd
.flags
& KEYEVENTF_EXTENDEDKEY
) ? VK_RMENU
: VK_LMENU
;
1877 vkey
= (input
->kbd
.flags
& KEYEVENTF_EXTENDEDKEY
) ? VK_RCONTROL
: VK_LCONTROL
;
1882 vkey
= (input
->kbd
.flags
& KEYEVENTF_EXTENDEDKEY
) ? VK_RSHIFT
: VK_LSHIFT
;
1887 message_code
= (input
->kbd
.flags
& KEYEVENTF_KEYUP
) ? WM_KEYUP
: WM_KEYDOWN
;
1892 if (input
->kbd
.flags
& KEYEVENTF_KEYUP
)
1894 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1895 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1896 if ((desktop
->keystate
[VK_MENU
] & 0x82) != 0x82) break;
1897 message_code
= WM_SYSKEYUP
;
1898 desktop
->keystate
[VK_MENU
] &= ~0x02;
1902 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1903 if (desktop
->keystate
[VK_CONTROL
] & 0x80) break;
1904 message_code
= WM_SYSKEYDOWN
;
1905 desktop
->keystate
[VK_MENU
] |= 0x02;
1911 /* send WM_SYSKEYUP on release if Alt still pressed */
1912 if (!(input
->kbd
.flags
& KEYEVENTF_KEYUP
)) break;
1913 if (!(desktop
->keystate
[VK_MENU
] & 0x80)) break;
1914 message_code
= WM_SYSKEYUP
;
1915 desktop
->keystate
[VK_MENU
] &= ~0x02;
1919 /* send WM_SYSKEY for Alt-anykey and for F10 */
1920 if (desktop
->keystate
[VK_CONTROL
] & 0x80) break;
1921 if (!(desktop
->keystate
[VK_MENU
] & 0x80)) break;
1924 message_code
= (input
->kbd
.flags
& KEYEVENTF_KEYUP
) ? WM_SYSKEYUP
: WM_SYSKEYDOWN
;
1925 desktop
->keystate
[VK_MENU
] &= ~0x02;
1929 if ((foreground
= get_foreground_thread( desktop
, win
)))
1931 memset( &raw_msg
, 0, sizeof(raw_msg
) );
1932 raw_msg
.foreground
= foreground
;
1933 raw_msg
.desktop
= desktop
;
1934 raw_msg
.source
= source
;
1935 raw_msg
.time
= time
;
1936 raw_msg
.message
= WM_INPUT
;
1938 msg_data
= &raw_msg
.data
;
1939 msg_data
->info
= input
->kbd
.info
;
1940 msg_data
->size
= sizeof(*msg_data
);
1941 msg_data
->flags
= input
->kbd
.flags
;
1942 msg_data
->rawinput
.type
= RIM_TYPEKEYBOARD
;
1943 msg_data
->rawinput
.kbd
.message
= message_code
;
1944 msg_data
->rawinput
.kbd
.vkey
= vkey
;
1945 msg_data
->rawinput
.kbd
.scan
= input
->kbd
.scan
;
1947 enum_processes( queue_rawinput_message
, &raw_msg
);
1948 release_object( foreground
);
1951 if ((device
= current
->process
->rawinput_kbd
) && (device
->flags
& RIDEV_NOLEGACY
))
1953 update_input_key_state( desktop
, desktop
->keystate
, message_code
, vkey
);
1957 if (!(msg
= alloc_hardware_message( input
->kbd
.info
, source
, time
, 0 ))) return 0;
1958 msg_data
= msg
->data
;
1960 msg
->win
= get_user_full_handle( win
);
1961 msg
->msg
= message_code
;
1962 msg
->lparam
= (input
->kbd
.scan
<< 16) | 1u; /* repeat count */
1963 if (origin
== IMO_INJECTED
) msg_data
->flags
= LLKHF_INJECTED
;
1965 if (input
->kbd
.flags
& KEYEVENTF_UNICODE
&& !vkey
)
1967 msg
->wparam
= VK_PACKET
;
1971 unsigned int flags
= 0;
1972 if (input
->kbd
.flags
& KEYEVENTF_EXTENDEDKEY
) flags
|= KF_EXTENDED
;
1973 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1974 if (input
->kbd
.flags
& KEYEVENTF_KEYUP
) flags
|= KF_REPEAT
| KF_UP
;
1975 else if (desktop
->keystate
[vkey
] & 0x80) flags
|= KF_REPEAT
;
1978 msg
->lparam
|= flags
<< 16;
1979 msg_data
->flags
|= (flags
& (KF_EXTENDED
| KF_ALTDOWN
| KF_UP
)) >> 8;
1982 if (!(wait
= send_hook_ll_message( desktop
, msg
, input
, sender
)))
1983 queue_hardware_message( desktop
, msg
, 1 );
1988 /* queue a hardware message for a custom type of event */
1989 static void queue_custom_hardware_message( struct desktop
*desktop
, user_handle_t win
,
1990 unsigned int origin
, const hw_input_t
*input
)
1992 struct hw_msg_source source
= { IMDT_UNAVAILABLE
, origin
};
1993 struct hardware_msg_data
*msg_data
;
1994 struct rawinput_message raw_msg
;
1995 struct message
*msg
;
1996 data_size_t report_size
= 0;
1998 switch (input
->hw
.msg
)
2001 case WM_INPUT_DEVICE_CHANGE
:
2002 memset( &raw_msg
, 0, sizeof(raw_msg
) );
2003 raw_msg
.source
= source
;
2004 raw_msg
.time
= get_tick_count();
2005 raw_msg
.message
= input
->hw
.msg
;
2007 if (input
->hw
.rawinput
.type
== RIM_TYPEHID
)
2009 raw_msg
.hid_report
= get_req_data();
2010 report_size
= input
->hw
.rawinput
.hid
.length
* input
->hw
.rawinput
.hid
.count
;
2013 if (report_size
!= get_req_data_size())
2015 set_error( STATUS_INVALID_PARAMETER
);
2019 msg_data
= &raw_msg
.data
;
2020 msg_data
->size
= sizeof(*msg_data
) + report_size
;
2021 msg_data
->rawinput
= input
->hw
.rawinput
;
2023 enum_processes( queue_rawinput_message
, &raw_msg
);
2025 if (raw_msg
.foreground
) release_object( raw_msg
.foreground
);
2029 if (!(msg
= alloc_hardware_message( 0, source
, get_tick_count(), 0 ))) return;
2031 msg
->win
= get_user_full_handle( win
);
2032 msg
->msg
= input
->hw
.msg
;
2034 msg
->lparam
= input
->hw
.lparam
;
2035 msg
->x
= desktop
->cursor
.x
;
2036 msg
->y
= desktop
->cursor
.y
;
2038 queue_hardware_message( desktop
, msg
, 1 );
2041 /* check message filter for a hardware message */
2042 static int check_hw_message_filter( user_handle_t win
, unsigned int msg_code
,
2043 user_handle_t filter_win
, unsigned int first
, unsigned int last
)
2045 if (msg_code
>= WM_KEYFIRST
&& msg_code
<= WM_KEYLAST
)
2047 /* we can only test the window for a keyboard message since the
2048 * dest window for a mouse message depends on hittest */
2049 if (filter_win
&& win
!= filter_win
&& !is_child_window( filter_win
, win
))
2051 /* the message code is final for a keyboard message, we can simply check it */
2052 return check_msg_filter( msg_code
, first
, last
);
2054 else /* mouse message */
2056 /* we need to check all possible values that the message can have in the end */
2058 if (check_msg_filter( msg_code
, first
, last
)) return 1;
2059 if (msg_code
== WM_MOUSEWHEEL
) return 0; /* no other possible value for this one */
2061 /* all other messages can become non-client messages */
2062 if (check_msg_filter( msg_code
+ (WM_NCMOUSEFIRST
- WM_MOUSEFIRST
), first
, last
)) return 1;
2064 /* clicks can become double-clicks or non-client double-clicks */
2065 if (msg_code
== WM_LBUTTONDOWN
|| msg_code
== WM_MBUTTONDOWN
||
2066 msg_code
== WM_RBUTTONDOWN
|| msg_code
== WM_XBUTTONDOWN
)
2068 if (check_msg_filter( msg_code
+ (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
2069 if (check_msg_filter( msg_code
+ (WM_NCLBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
2076 /* find a hardware message for the given queue */
2077 static int get_hardware_message( struct thread
*thread
, unsigned int hw_id
, user_handle_t filter_win
,
2078 unsigned int first
, unsigned int last
, unsigned int flags
,
2079 struct get_message_reply
*reply
)
2081 struct thread_input
*input
= thread
->queue
->input
;
2082 struct thread
*win_thread
;
2085 int clear_bits
, got_one
= 0;
2086 unsigned int msg_code
;
2088 ptr
= list_head( &input
->msg_list
);
2093 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
2094 if (msg
->unique_id
== hw_id
) break;
2095 ptr
= list_next( &input
->msg_list
, ptr
);
2097 if (!ptr
) ptr
= list_head( &input
->msg_list
);
2098 else ptr
= list_next( &input
->msg_list
, ptr
); /* start from the next one */
2101 if (ptr
== list_head( &input
->msg_list
))
2102 clear_bits
= QS_INPUT
;
2104 clear_bits
= 0; /* don't clear bits if we don't go through the whole list */
2108 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
2109 struct hardware_msg_data
*data
= msg
->data
;
2111 ptr
= list_next( &input
->msg_list
, ptr
);
2112 win
= find_hardware_message_window( input
->desktop
, input
, msg
, &msg_code
, &win_thread
);
2113 if (!win
|| !win_thread
)
2115 /* no window at all, remove it */
2116 update_input_key_state( input
->desktop
, input
->keystate
, msg
->msg
, msg
->wparam
);
2117 list_remove( &msg
->entry
);
2118 free_message( msg
);
2121 if (win_thread
!= thread
)
2123 if (win_thread
->queue
->input
== input
)
2125 /* wake the other thread */
2126 set_queue_bits( win_thread
->queue
, get_hardware_msg_bit(msg
) );
2131 /* for another thread input, drop it */
2132 update_input_key_state( input
->desktop
, input
->keystate
, msg
->msg
, msg
->wparam
);
2133 list_remove( &msg
->entry
);
2134 free_message( msg
);
2136 release_object( win_thread
);
2139 release_object( win_thread
);
2141 /* if we already got a message for another thread, or if it doesn't
2142 * match the filter we skip it */
2143 if (got_one
|| !check_hw_message_filter( win
, msg_code
, filter_win
, first
, last
))
2145 clear_bits
&= ~get_hardware_msg_bit( msg
);
2148 /* now we can return it */
2149 if (!msg
->unique_id
) msg
->unique_id
= get_unique_id();
2150 reply
->type
= MSG_HARDWARE
;
2152 reply
->msg
= msg_code
;
2153 reply
->wparam
= msg
->wparam
;
2154 reply
->lparam
= msg
->lparam
;
2157 reply
->time
= msg
->time
;
2159 data
->hw_id
= msg
->unique_id
;
2160 set_reply_data( msg
->data
, msg
->data_size
);
2161 if ((msg
->msg
== WM_INPUT
|| msg
->msg
== WM_INPUT_DEVICE_CHANGE
) && (flags
& PM_REMOVE
))
2162 release_hardware_message( current
->queue
, data
->hw_id
);
2165 /* nothing found, clear the hardware queue bits */
2166 clear_queue_bits( thread
->queue
, clear_bits
);
2170 /* increment (or decrement if 'incr' is negative) the queue paint count */
2171 void inc_queue_paint_count( struct thread
*thread
, int incr
)
2173 struct msg_queue
*queue
= thread
->queue
;
2177 if ((queue
->paint_count
+= incr
) < 0) queue
->paint_count
= 0;
2179 if (queue
->paint_count
)
2180 set_queue_bits( queue
, QS_PAINT
);
2182 clear_queue_bits( queue
, QS_PAINT
);
2186 /* remove all messages and timers belonging to a certain window */
2187 void queue_cleanup_window( struct thread
*thread
, user_handle_t win
)
2189 struct msg_queue
*queue
= thread
->queue
;
2197 ptr
= list_head( &queue
->pending_timers
);
2200 struct list
*next
= list_next( &queue
->pending_timers
, ptr
);
2201 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
2202 if (timer
->win
== win
) free_timer( queue
, timer
);
2205 ptr
= list_head( &queue
->expired_timers
);
2208 struct list
*next
= list_next( &queue
->expired_timers
, ptr
);
2209 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
2210 if (timer
->win
== win
) free_timer( queue
, timer
);
2214 /* remove messages */
2215 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
2217 struct list
*ptr
, *next
;
2219 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->msg_list
[i
] )
2221 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
2222 if (msg
->win
== win
)
2224 if (msg
->msg
== WM_QUIT
&& !queue
->quit_message
)
2226 queue
->quit_message
= 1;
2227 queue
->exit_code
= msg
->wparam
;
2229 remove_queue_message( queue
, msg
, i
);
2234 thread_input_cleanup_window( queue
, win
);
2237 /* post a message to a window */
2238 void post_message( user_handle_t win
, unsigned int message
, lparam_t wparam
, lparam_t lparam
)
2240 struct message
*msg
;
2241 struct thread
*thread
= get_window_thread( win
);
2243 if (!thread
) return;
2245 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
2247 msg
->type
= MSG_POSTED
;
2248 msg
->win
= get_user_full_handle( win
);
2250 msg
->wparam
= wparam
;
2251 msg
->lparam
= lparam
;
2256 get_message_defaults( thread
->queue
, &msg
->x
, &msg
->y
, &msg
->time
);
2258 list_add_tail( &thread
->queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
2259 set_queue_bits( thread
->queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
2260 if (message
== WM_HOTKEY
)
2262 set_queue_bits( thread
->queue
, QS_HOTKEY
);
2263 thread
->queue
->hotkey_count
++;
2266 release_object( thread
);
2269 /* send a notify message to a window */
2270 void send_notify_message( user_handle_t win
, unsigned int message
, lparam_t wparam
, lparam_t lparam
)
2272 struct message
*msg
;
2273 struct thread
*thread
= get_window_thread( win
);
2275 if (!thread
) return;
2277 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
2279 msg
->type
= MSG_NOTIFY
;
2280 msg
->win
= get_user_full_handle( win
);
2282 msg
->wparam
= wparam
;
2283 msg
->lparam
= lparam
;
2288 get_message_defaults( thread
->queue
, &msg
->x
, &msg
->y
, &msg
->time
);
2290 list_add_tail( &thread
->queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
2291 set_queue_bits( thread
->queue
, QS_SENDMESSAGE
);
2293 release_object( thread
);
2296 /* post a win event */
2297 void post_win_event( struct thread
*thread
, unsigned int event
,
2298 user_handle_t win
, unsigned int object_id
,
2299 unsigned int child_id
, client_ptr_t hook_proc
,
2300 const WCHAR
*module
, data_size_t module_size
,
2303 struct message
*msg
;
2305 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
2307 struct winevent_msg_data
*data
;
2309 msg
->type
= MSG_WINEVENT
;
2310 msg
->win
= get_user_full_handle( win
);
2312 msg
->wparam
= object_id
;
2313 msg
->lparam
= child_id
;
2314 msg
->time
= get_tick_count();
2317 if ((data
= malloc( sizeof(*data
) + module_size
)))
2320 data
->tid
= get_thread_id( current
);
2321 data
->hook_proc
= hook_proc
;
2322 memcpy( data
+ 1, module
, module_size
);
2325 msg
->data_size
= sizeof(*data
) + module_size
;
2327 if (debug_level
> 1)
2328 fprintf( stderr
, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2329 get_thread_id(thread
), event
, win
, object_id
, child_id
);
2330 list_add_tail( &thread
->queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
2331 set_queue_bits( thread
->queue
, QS_SENDMESSAGE
);
2338 /* free all hotkeys on a desktop, optionally filtering by window */
2339 void free_hotkeys( struct desktop
*desktop
, user_handle_t window
)
2341 struct hotkey
*hotkey
, *hotkey2
;
2343 LIST_FOR_EACH_ENTRY_SAFE( hotkey
, hotkey2
, &desktop
->hotkeys
, struct hotkey
, entry
)
2345 if (!window
|| hotkey
->win
== window
)
2347 list_remove( &hotkey
->entry
);
2354 /* check if the thread owning the window is hung */
2355 DECL_HANDLER(is_window_hung
)
2357 struct thread
*thread
;
2359 thread
= get_window_thread( req
->win
);
2362 reply
->is_hung
= is_queue_hung( thread
->queue
);
2363 release_object( thread
);
2365 else reply
->is_hung
= 0;
2369 /* get the message queue of the current thread */
2370 DECL_HANDLER(get_msg_queue
)
2372 struct msg_queue
*queue
= get_current_queue();
2375 if (queue
) reply
->handle
= alloc_handle( current
->process
, queue
, SYNCHRONIZE
, 0 );
2379 /* set the file descriptor associated to the current thread queue */
2380 DECL_HANDLER(set_queue_fd
)
2382 struct msg_queue
*queue
= get_current_queue();
2386 if (queue
->fd
) /* fd can only be set once */
2388 set_error( STATUS_ACCESS_DENIED
);
2391 if (!(file
= get_file_obj( current
->process
, req
->handle
, SYNCHRONIZE
))) return;
2393 if ((unix_fd
= get_file_unix_fd( file
)) != -1)
2395 if ((unix_fd
= dup( unix_fd
)) != -1)
2396 queue
->fd
= create_anonymous_fd( &msg_queue_fd_ops
, unix_fd
, &queue
->obj
, 0 );
2400 release_object( file
);
2404 /* set the current message queue wakeup mask */
2405 DECL_HANDLER(set_queue_mask
)
2407 struct msg_queue
*queue
= get_current_queue();
2411 queue
->wake_mask
= req
->wake_mask
;
2412 queue
->changed_mask
= req
->changed_mask
;
2413 reply
->wake_bits
= queue
->wake_bits
;
2414 reply
->changed_bits
= queue
->changed_bits
;
2415 if (is_signaled( queue
))
2417 /* if skip wait is set, do what would have been done in the subsequent wait */
2418 if (req
->skip_wait
) queue
->wake_mask
= queue
->changed_mask
= 0;
2419 else wake_up( &queue
->obj
, 0 );
2425 /* get the current message queue status */
2426 DECL_HANDLER(get_queue_status
)
2428 struct msg_queue
*queue
= current
->queue
;
2431 reply
->wake_bits
= queue
->wake_bits
;
2432 reply
->changed_bits
= queue
->changed_bits
;
2433 queue
->changed_bits
&= ~req
->clear_bits
;
2435 else reply
->wake_bits
= reply
->changed_bits
= 0;
2439 /* send a message to a thread queue */
2440 DECL_HANDLER(send_message
)
2442 struct message
*msg
;
2443 struct msg_queue
*send_queue
= get_current_queue();
2444 struct msg_queue
*recv_queue
= NULL
;
2445 struct thread
*thread
= NULL
;
2447 if (!(thread
= get_thread_from_id( req
->id
))) return;
2449 if (!(recv_queue
= thread
->queue
))
2451 set_error( STATUS_INVALID_PARAMETER
);
2452 release_object( thread
);
2455 if ((req
->flags
& SEND_MSG_ABORT_IF_HUNG
) && is_queue_hung(recv_queue
))
2457 set_error( STATUS_TIMEOUT
);
2458 release_object( thread
);
2462 if ((msg
= mem_alloc( sizeof(*msg
) )))
2464 msg
->type
= req
->type
;
2465 msg
->win
= get_user_full_handle( req
->win
);
2466 msg
->msg
= req
->msg
;
2467 msg
->wparam
= req
->wparam
;
2468 msg
->lparam
= req
->lparam
;
2471 msg
->data_size
= get_req_data_size();
2473 get_message_defaults( recv_queue
, &msg
->x
, &msg
->y
, &msg
->time
);
2475 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
2478 release_object( thread
);
2484 case MSG_OTHER_PROCESS
:
2488 if (!(msg
->result
= alloc_message_result( send_queue
, recv_queue
, msg
, req
->timeout
)))
2490 free_message( msg
);
2495 list_add_tail( &recv_queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
2496 set_queue_bits( recv_queue
, QS_SENDMESSAGE
);
2499 list_add_tail( &recv_queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
2500 set_queue_bits( recv_queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
2501 if (msg
->msg
== WM_HOTKEY
)
2503 set_queue_bits( recv_queue
, QS_HOTKEY
);
2504 recv_queue
->hotkey_count
++;
2507 case MSG_HARDWARE
: /* should use send_hardware_message instead */
2508 case MSG_CALLBACK_RESULT
: /* cannot send this one */
2509 case MSG_HOOK_LL
: /* generated internally */
2511 set_error( STATUS_INVALID_PARAMETER
);
2516 release_object( thread
);
2519 /* send a hardware message to a thread queue */
2520 DECL_HANDLER(send_hardware_message
)
2522 struct thread
*thread
= NULL
;
2523 struct desktop
*desktop
;
2524 unsigned int origin
= (req
->flags
& SEND_HWMSG_INJECTED
? IMO_INJECTED
: IMO_HARDWARE
);
2525 struct msg_queue
*sender
= get_current_queue();
2526 data_size_t size
= min( 256, get_reply_max_size() );
2528 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2532 if (!(thread
= get_window_thread( req
->win
))) return;
2533 if (desktop
!= thread
->queue
->input
->desktop
)
2535 /* don't allow queuing events to a different desktop */
2536 release_object( desktop
);
2541 reply
->prev_x
= desktop
->cursor
.x
;
2542 reply
->prev_y
= desktop
->cursor
.y
;
2544 switch (req
->input
.type
)
2547 reply
->wait
= queue_mouse_message( desktop
, req
->win
, &req
->input
, origin
, sender
);
2549 case INPUT_KEYBOARD
:
2550 reply
->wait
= queue_keyboard_message( desktop
, req
->win
, &req
->input
, origin
, sender
);
2552 case INPUT_HARDWARE
:
2553 queue_custom_hardware_message( desktop
, req
->win
, origin
, &req
->input
);
2556 set_error( STATUS_INVALID_PARAMETER
);
2558 if (thread
) release_object( thread
);
2560 reply
->new_x
= desktop
->cursor
.x
;
2561 reply
->new_y
= desktop
->cursor
.y
;
2562 set_reply_data( desktop
->keystate
, size
);
2563 release_object( desktop
);
2566 /* post a quit message to the current queue */
2567 DECL_HANDLER(post_quit_message
)
2569 struct msg_queue
*queue
= get_current_queue();
2574 queue
->quit_message
= 1;
2575 queue
->exit_code
= req
->exit_code
;
2576 set_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
2579 /* get a message from the current queue */
2580 DECL_HANDLER(get_message
)
2582 struct timer
*timer
;
2584 struct msg_queue
*queue
= get_current_queue();
2585 user_handle_t get_win
= get_user_full_handle( req
->get_win
);
2586 unsigned int filter
= req
->flags
>> 16;
2588 reply
->active_hooks
= get_active_hooks();
2590 if (get_win
&& get_win
!= 1 && get_win
!= -1 && !get_user_object( get_win
, USER_WINDOW
))
2592 set_win32_error( ERROR_INVALID_WINDOW_HANDLE
);
2597 queue
->last_get_msg
= current_time
;
2598 if (!filter
) filter
= QS_ALLINPUT
;
2600 /* first check for sent messages */
2601 if ((ptr
= list_head( &queue
->msg_list
[SEND_MESSAGE
] )))
2603 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
2604 receive_message( queue
, msg
, reply
);
2608 /* clear changed bits so we can wait on them if we don't find a message */
2609 if (filter
& QS_POSTMESSAGE
)
2611 queue
->changed_bits
&= ~(QS_POSTMESSAGE
| QS_HOTKEY
| QS_TIMER
);
2612 if (req
->get_first
== 0 && req
->get_last
== ~0U) queue
->changed_bits
&= ~QS_ALLPOSTMESSAGE
;
2614 if (filter
& QS_INPUT
) queue
->changed_bits
&= ~QS_INPUT
;
2615 if (filter
& QS_PAINT
) queue
->changed_bits
&= ~QS_PAINT
;
2617 /* then check for posted messages */
2618 if ((filter
& QS_POSTMESSAGE
) &&
2619 get_posted_message( queue
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
2622 if ((filter
& QS_HOTKEY
) && queue
->hotkey_count
&&
2623 req
->get_first
<= WM_HOTKEY
&& req
->get_last
>= WM_HOTKEY
&&
2624 get_posted_message( queue
, get_win
, WM_HOTKEY
, WM_HOTKEY
, req
->flags
, reply
))
2627 /* only check for quit messages if not posted messages pending */
2628 if ((filter
& QS_POSTMESSAGE
) && get_quit_message( queue
, req
->flags
, reply
))
2631 /* then check for any raw hardware message */
2632 if ((filter
& QS_INPUT
) &&
2633 filter_contains_hw_range( req
->get_first
, req
->get_last
) &&
2634 get_hardware_message( current
, req
->hw_id
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
2637 /* now check for WM_PAINT */
2638 if ((filter
& QS_PAINT
) &&
2639 queue
->paint_count
&&
2640 check_msg_filter( WM_PAINT
, req
->get_first
, req
->get_last
) &&
2641 (reply
->win
= find_window_to_repaint( get_win
, current
)))
2643 reply
->type
= MSG_POSTED
;
2644 reply
->msg
= WM_PAINT
;
2647 get_message_defaults( queue
, &reply
->x
, &reply
->y
, &reply
->time
);
2651 /* now check for timer */
2652 if ((filter
& QS_TIMER
) &&
2653 (timer
= find_expired_timer( queue
, get_win
, req
->get_first
,
2654 req
->get_last
, (req
->flags
& PM_REMOVE
) )))
2656 reply
->type
= MSG_POSTED
;
2657 reply
->win
= timer
->win
;
2658 reply
->msg
= timer
->msg
;
2659 reply
->wparam
= timer
->id
;
2660 reply
->lparam
= timer
->lparam
;
2661 get_message_defaults( queue
, &reply
->x
, &reply
->y
, &reply
->time
);
2662 if (!(req
->flags
& PM_NOYIELD
) && current
->process
->idle_event
)
2663 set_event( current
->process
->idle_event
);
2667 if (get_win
== -1 && current
->process
->idle_event
) set_event( current
->process
->idle_event
);
2668 queue
->wake_mask
= req
->wake_mask
;
2669 queue
->changed_mask
= req
->changed_mask
;
2670 set_error( STATUS_PENDING
); /* FIXME */
2674 /* reply to a sent message */
2675 DECL_HANDLER(reply_message
)
2677 if (!current
->queue
) set_error( STATUS_ACCESS_DENIED
);
2678 else if (current
->queue
->recv_result
)
2679 reply_message( current
->queue
, req
->result
, 0, req
->remove
,
2680 get_req_data(), get_req_data_size() );
2684 /* accept the current hardware message */
2685 DECL_HANDLER(accept_hardware_message
)
2688 release_hardware_message( current
->queue
, req
->hw_id
);
2690 set_error( STATUS_ACCESS_DENIED
);
2694 /* retrieve the reply for the last message sent */
2695 DECL_HANDLER(get_message_reply
)
2697 struct message_result
*result
;
2699 struct msg_queue
*queue
= current
->queue
;
2703 set_error( STATUS_PENDING
);
2706 if (!(entry
= list_head( &queue
->send_result
))) return; /* no reply ready */
2708 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
2709 if (result
->replied
|| req
->cancel
)
2711 if (result
->replied
)
2713 reply
->result
= result
->result
;
2714 set_error( result
->error
);
2717 data_size_t data_len
= min( result
->data_size
, get_reply_max_size() );
2718 set_reply_data_ptr( result
->data
, data_len
);
2719 result
->data
= NULL
;
2720 result
->data_size
= 0;
2723 remove_result_from_sender( result
);
2725 entry
= list_head( &queue
->send_result
);
2726 if (!entry
) clear_queue_bits( queue
, QS_SMRESULT
);
2729 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
2730 if (result
->replied
) set_queue_bits( queue
, QS_SMRESULT
);
2731 else clear_queue_bits( queue
, QS_SMRESULT
);
2735 else set_error( STATUS_ACCESS_DENIED
);
2739 /* set a window timer */
2740 DECL_HANDLER(set_win_timer
)
2742 struct timer
*timer
;
2743 struct msg_queue
*queue
;
2744 struct thread
*thread
= NULL
;
2745 user_handle_t win
= 0;
2746 lparam_t id
= req
->id
;
2750 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
2752 set_error( STATUS_INVALID_HANDLE
);
2755 if (thread
->process
!= current
->process
)
2757 release_object( thread
);
2758 set_error( STATUS_ACCESS_DENIED
);
2761 queue
= thread
->queue
;
2762 /* remove it if it existed already */
2763 if ((timer
= find_timer( queue
, win
, req
->msg
, id
))) free_timer( queue
, timer
);
2767 queue
= get_current_queue();
2768 /* look for a timer with this id */
2769 if (id
&& (timer
= find_timer( queue
, 0, req
->msg
, id
)))
2771 /* free and reuse id */
2772 free_timer( queue
, timer
);
2776 lparam_t end_id
= queue
->next_timer_id
;
2778 /* find a free id for it */
2781 id
= queue
->next_timer_id
;
2782 if (--queue
->next_timer_id
<= 0x100) queue
->next_timer_id
= 0x7fff;
2784 if (!find_timer( queue
, 0, req
->msg
, id
)) break;
2785 if (queue
->next_timer_id
== end_id
)
2787 set_win32_error( ERROR_NO_MORE_USER_HANDLES
);
2794 if ((timer
= set_timer( queue
, req
->rate
)))
2797 timer
->msg
= req
->msg
;
2799 timer
->lparam
= req
->lparam
;
2802 if (thread
) release_object( thread
);
2805 /* kill a window timer */
2806 DECL_HANDLER(kill_win_timer
)
2808 struct timer
*timer
;
2809 struct thread
*thread
;
2810 user_handle_t win
= 0;
2814 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
2816 set_error( STATUS_INVALID_HANDLE
);
2819 if (thread
->process
!= current
->process
)
2821 release_object( thread
);
2822 set_error( STATUS_ACCESS_DENIED
);
2826 else thread
= (struct thread
*)grab_object( current
);
2828 if (thread
->queue
&& (timer
= find_timer( thread
->queue
, win
, req
->msg
, req
->id
)))
2829 free_timer( thread
->queue
, timer
);
2831 set_error( STATUS_INVALID_PARAMETER
);
2833 release_object( thread
);
2836 DECL_HANDLER(register_hotkey
)
2838 struct desktop
*desktop
;
2839 user_handle_t win_handle
= req
->window
;
2840 struct hotkey
*hotkey
;
2841 struct hotkey
*new_hotkey
= NULL
;
2842 struct thread
*thread
;
2843 const int modifier_flags
= MOD_ALT
|MOD_CONTROL
|MOD_SHIFT
|MOD_WIN
;
2845 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2849 if (!(win_handle
= get_valid_window_handle( win_handle
)))
2851 release_object( desktop
);
2855 thread
= get_window_thread( win_handle
);
2856 if (thread
) release_object( thread
);
2858 if (thread
!= current
)
2860 release_object( desktop
);
2861 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD
);
2866 LIST_FOR_EACH_ENTRY( hotkey
, &desktop
->hotkeys
, struct hotkey
, entry
)
2868 if (req
->vkey
== hotkey
->vkey
&&
2869 (req
->flags
& modifier_flags
) == (hotkey
->flags
& modifier_flags
))
2871 release_object( desktop
);
2872 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED
);
2875 if (current
->queue
== hotkey
->queue
&& win_handle
== hotkey
->win
&& req
->id
== hotkey
->id
)
2876 new_hotkey
= hotkey
;
2881 reply
->replaced
= 1;
2882 reply
->flags
= new_hotkey
->flags
;
2883 reply
->vkey
= new_hotkey
->vkey
;
2887 new_hotkey
= mem_alloc( sizeof(*new_hotkey
) );
2890 list_add_tail( &desktop
->hotkeys
, &new_hotkey
->entry
);
2891 new_hotkey
->queue
= current
->queue
;
2892 new_hotkey
->win
= win_handle
;
2893 new_hotkey
->id
= req
->id
;
2899 new_hotkey
->flags
= req
->flags
;
2900 new_hotkey
->vkey
= req
->vkey
;
2903 release_object( desktop
);
2906 DECL_HANDLER(unregister_hotkey
)
2908 struct desktop
*desktop
;
2909 user_handle_t win_handle
= req
->window
;
2910 struct hotkey
*hotkey
;
2911 struct thread
*thread
;
2913 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2917 if (!(win_handle
= get_valid_window_handle( win_handle
)))
2919 release_object( desktop
);
2923 thread
= get_window_thread( win_handle
);
2924 if (thread
) release_object( thread
);
2926 if (thread
!= current
)
2928 release_object( desktop
);
2929 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD
);
2934 LIST_FOR_EACH_ENTRY( hotkey
, &desktop
->hotkeys
, struct hotkey
, entry
)
2936 if (current
->queue
== hotkey
->queue
&& win_handle
== hotkey
->win
&& req
->id
== hotkey
->id
)
2940 release_object( desktop
);
2941 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED
);
2945 reply
->flags
= hotkey
->flags
;
2946 reply
->vkey
= hotkey
->vkey
;
2947 list_remove( &hotkey
->entry
);
2949 release_object( desktop
);
2952 /* attach (or detach) thread inputs */
2953 DECL_HANDLER(attach_thread_input
)
2955 struct thread
*thread_from
= get_thread_from_id( req
->tid_from
);
2956 struct thread
*thread_to
= get_thread_from_id( req
->tid_to
);
2958 if (!thread_from
|| !thread_to
)
2960 if (thread_from
) release_object( thread_from
);
2961 if (thread_to
) release_object( thread_to
);
2964 if (thread_from
!= thread_to
)
2968 if ((thread_to
->queue
|| thread_to
== current
) &&
2969 (thread_from
->queue
|| thread_from
== current
))
2970 attach_thread_input( thread_from
, thread_to
);
2972 set_error( STATUS_INVALID_PARAMETER
);
2976 if (thread_from
->queue
&& thread_to
->queue
&&
2977 thread_from
->queue
->input
== thread_to
->queue
->input
)
2978 detach_thread_input( thread_from
);
2980 set_error( STATUS_ACCESS_DENIED
);
2983 else set_error( STATUS_ACCESS_DENIED
);
2984 release_object( thread_from
);
2985 release_object( thread_to
);
2989 /* get thread input data */
2990 DECL_HANDLER(get_thread_input
)
2992 struct thread
*thread
= NULL
;
2993 struct desktop
*desktop
;
2994 struct thread_input
*input
;
2998 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2999 if (!(desktop
= get_thread_desktop( thread
, 0 )))
3001 release_object( thread
);
3004 input
= thread
->queue
? thread
->queue
->input
: NULL
;
3008 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
3009 input
= desktop
->foreground_input
; /* get the foreground thread info */
3014 reply
->focus
= input
->focus
;
3015 reply
->capture
= input
->capture
;
3016 reply
->active
= input
->active
;
3017 reply
->menu_owner
= input
->menu_owner
;
3018 reply
->move_size
= input
->move_size
;
3019 reply
->caret
= input
->caret
;
3020 reply
->cursor
= input
->cursor
;
3021 reply
->show_count
= input
->cursor_count
;
3022 reply
->rect
= input
->caret_rect
;
3025 /* foreground window is active window of foreground thread */
3026 reply
->foreground
= desktop
->foreground_input
? desktop
->foreground_input
->active
: 0;
3027 if (thread
) release_object( thread
);
3028 release_object( desktop
);
3032 /* retrieve queue keyboard state for current thread or global async state */
3033 DECL_HANDLER(get_key_state
)
3035 struct desktop
*desktop
;
3036 data_size_t size
= min( 256, get_reply_max_size() );
3038 if (req
->async
) /* get global async key state */
3040 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
3043 reply
->state
= desktop
->keystate
[req
->key
& 0xff];
3044 desktop
->keystate
[req
->key
& 0xff] &= ~0x40;
3046 set_reply_data( desktop
->keystate
, size
);
3047 release_object( desktop
);
3049 else if (!current
->queue
)
3051 unsigned char *keystate
;
3052 /* fallback to desktop keystate */
3053 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
3054 if (req
->key
>= 0) reply
->state
= desktop
->keystate
[req
->key
& 0xff] & ~0x40;
3055 if ((keystate
= set_reply_data_size( size
)))
3058 for (i
= 0; i
< size
; i
++) keystate
[i
] = desktop
->keystate
[i
] & ~0x40;
3060 release_object( desktop
);
3064 unsigned char *keystate
= current
->queue
->input
->keystate
;
3065 if (req
->key
>= 0) reply
->state
= keystate
[req
->key
& 0xff];
3066 set_reply_data( keystate
, size
);
3071 /* set queue keyboard state for current thread */
3072 DECL_HANDLER(set_key_state
)
3074 struct desktop
*desktop
;
3075 data_size_t size
= min( 256, get_req_data_size() );
3077 if (current
->queue
) memcpy( current
->queue
->input
->keystate
, get_req_data(), size
);
3078 if (req
->async
&& (desktop
= get_thread_desktop( current
, 0 )))
3080 memcpy( desktop
->keystate
, get_req_data(), size
);
3081 release_object( desktop
);
3086 /* set the system foreground window */
3087 DECL_HANDLER(set_foreground_window
)
3089 struct thread
*thread
= NULL
;
3090 struct desktop
*desktop
;
3091 struct msg_queue
*queue
= get_current_queue();
3093 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
3094 reply
->previous
= desktop
->foreground_input
? desktop
->foreground_input
->active
: 0;
3095 reply
->send_msg_old
= (reply
->previous
&& desktop
->foreground_input
!= queue
->input
);
3096 reply
->send_msg_new
= FALSE
;
3098 if (is_valid_foreground_window( req
->handle
) &&
3099 (thread
= get_window_thread( req
->handle
)) &&
3100 thread
->queue
->input
->desktop
== desktop
)
3102 set_foreground_input( desktop
, thread
->queue
->input
);
3103 reply
->send_msg_new
= (desktop
->foreground_input
!= queue
->input
);
3105 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE
);
3107 if (thread
) release_object( thread
);
3108 release_object( desktop
);
3112 /* set the current thread focus window */
3113 DECL_HANDLER(set_focus_window
)
3115 struct msg_queue
*queue
= get_current_queue();
3117 reply
->previous
= 0;
3118 if (queue
&& check_queue_input_window( queue
, req
->handle
))
3120 reply
->previous
= queue
->input
->focus
;
3121 queue
->input
->focus
= get_user_full_handle( req
->handle
);
3126 /* set the current thread active window */
3127 DECL_HANDLER(set_active_window
)
3129 struct msg_queue
*queue
= get_current_queue();
3131 reply
->previous
= 0;
3132 if (queue
&& check_queue_input_window( queue
, req
->handle
))
3134 if (!req
->handle
|| make_window_active( req
->handle
))
3136 reply
->previous
= queue
->input
->active
;
3137 queue
->input
->active
= get_user_full_handle( req
->handle
);
3139 else set_error( STATUS_INVALID_HANDLE
);
3144 /* set the current thread capture window */
3145 DECL_HANDLER(set_capture_window
)
3147 struct msg_queue
*queue
= get_current_queue();
3149 reply
->previous
= reply
->full_handle
= 0;
3150 if (queue
&& check_queue_input_window( queue
, req
->handle
))
3152 struct thread_input
*input
= queue
->input
;
3154 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
3155 if (input
->menu_owner
&& !(req
->flags
& CAPTURE_MENU
))
3157 set_error(STATUS_ACCESS_DENIED
);
3160 reply
->previous
= input
->capture
;
3161 input
->capture
= get_user_full_handle( req
->handle
);
3162 input
->menu_owner
= (req
->flags
& CAPTURE_MENU
) ? input
->capture
: 0;
3163 input
->move_size
= (req
->flags
& CAPTURE_MOVESIZE
) ? input
->capture
: 0;
3164 reply
->full_handle
= input
->capture
;
3169 /* Set the current thread caret window */
3170 DECL_HANDLER(set_caret_window
)
3172 struct msg_queue
*queue
= get_current_queue();
3174 reply
->previous
= 0;
3175 if (queue
&& check_queue_input_window( queue
, req
->handle
))
3177 struct thread_input
*input
= queue
->input
;
3179 reply
->previous
= input
->caret
;
3180 reply
->old_rect
= input
->caret_rect
;
3181 reply
->old_hide
= input
->caret_hide
;
3182 reply
->old_state
= input
->caret_state
;
3184 set_caret_window( input
, get_user_full_handle(req
->handle
) );
3185 input
->caret_rect
.right
= input
->caret_rect
.left
+ req
->width
;
3186 input
->caret_rect
.bottom
= input
->caret_rect
.top
+ req
->height
;
3191 /* Set the current thread caret information */
3192 DECL_HANDLER(set_caret_info
)
3194 struct msg_queue
*queue
= get_current_queue();
3195 struct thread_input
*input
;
3198 input
= queue
->input
;
3199 reply
->full_handle
= input
->caret
;
3200 reply
->old_rect
= input
->caret_rect
;
3201 reply
->old_hide
= input
->caret_hide
;
3202 reply
->old_state
= input
->caret_state
;
3204 if (req
->handle
&& get_user_full_handle(req
->handle
) != input
->caret
)
3206 set_error( STATUS_ACCESS_DENIED
);
3209 if (req
->flags
& SET_CARET_POS
)
3211 input
->caret_rect
.right
+= req
->x
- input
->caret_rect
.left
;
3212 input
->caret_rect
.bottom
+= req
->y
- input
->caret_rect
.top
;
3213 input
->caret_rect
.left
= req
->x
;
3214 input
->caret_rect
.top
= req
->y
;
3216 if (req
->flags
& SET_CARET_HIDE
)
3218 input
->caret_hide
+= req
->hide
;
3219 if (input
->caret_hide
< 0) input
->caret_hide
= 0;
3221 if (req
->flags
& SET_CARET_STATE
)
3225 case CARET_STATE_OFF
: input
->caret_state
= 0; break;
3226 case CARET_STATE_ON
: input
->caret_state
= 1; break;
3227 case CARET_STATE_TOGGLE
: input
->caret_state
= !input
->caret_state
; break;
3228 case CARET_STATE_ON_IF_MOVED
:
3229 if (req
->x
!= reply
->old_rect
.left
|| req
->y
!= reply
->old_rect
.top
) input
->caret_state
= 1;
3236 /* get the time of the last input event */
3237 DECL_HANDLER(get_last_input_time
)
3239 reply
->time
= last_input_time
;
3242 /* set/get the current cursor */
3243 DECL_HANDLER(set_cursor
)
3245 struct msg_queue
*queue
= get_current_queue();
3246 struct thread_input
*input
;
3249 input
= queue
->input
;
3251 reply
->prev_handle
= input
->cursor
;
3252 reply
->prev_count
= input
->cursor_count
;
3253 reply
->prev_x
= input
->desktop
->cursor
.x
;
3254 reply
->prev_y
= input
->desktop
->cursor
.y
;
3256 if (req
->flags
& SET_CURSOR_HANDLE
)
3258 if (req
->handle
&& !get_user_object( req
->handle
, USER_CLIENT
))
3260 set_win32_error( ERROR_INVALID_CURSOR_HANDLE
);
3263 input
->cursor
= req
->handle
;
3265 if (req
->flags
& SET_CURSOR_COUNT
)
3267 queue
->cursor_count
+= req
->show_count
;
3268 input
->cursor_count
+= req
->show_count
;
3270 if (req
->flags
& SET_CURSOR_POS
)
3272 set_cursor_pos( input
->desktop
, req
->x
, req
->y
);
3274 if (req
->flags
& (SET_CURSOR_CLIP
| SET_CURSOR_NOCLIP
))
3276 struct desktop
*desktop
= input
->desktop
;
3278 /* only the desktop owner can set the message */
3279 if (req
->clip_msg
&& get_top_window_owner(desktop
) == current
->process
)
3280 desktop
->cursor
.clip_msg
= req
->clip_msg
;
3282 set_clip_rectangle( desktop
, (req
->flags
& SET_CURSOR_NOCLIP
) ? NULL
: &req
->clip
, 0 );
3285 reply
->new_x
= input
->desktop
->cursor
.x
;
3286 reply
->new_y
= input
->desktop
->cursor
.y
;
3287 reply
->new_clip
= input
->desktop
->cursor
.clip
;
3288 reply
->last_change
= input
->desktop
->cursor
.last_change
;
3291 /* Get the history of the 64 last cursor positions */
3292 DECL_HANDLER(get_cursor_history
)
3295 unsigned int i
, count
= min( 64, get_reply_max_size() / sizeof(*pos
) );
3297 if ((pos
= set_reply_data_size( count
* sizeof(*pos
) )))
3298 for (i
= 0; i
< count
; i
++)
3299 pos
[i
] = cursor_history
[(i
+ cursor_history_latest
) % ARRAY_SIZE(cursor_history
)];
3302 DECL_HANDLER(get_rawinput_buffer
)
3304 struct thread_input
*input
= current
->queue
->input
;
3305 data_size_t size
= 0, next_size
= 0;
3307 char *buf
, *cur
, *tmp
;
3308 int count
= 0, buf_size
= 16 * sizeof(struct hardware_msg_data
);
3310 if (!req
->buffer_size
) buf
= NULL
;
3311 else if (!(buf
= mem_alloc( buf_size
))) return;
3314 ptr
= list_head( &input
->msg_list
);
3317 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
3318 struct hardware_msg_data
*data
= msg
->data
;
3319 data_size_t extra_size
= data
->size
- sizeof(*data
);
3321 ptr
= list_next( &input
->msg_list
, ptr
);
3322 if (msg
->msg
!= WM_INPUT
) continue;
3324 next_size
= req
->rawinput_size
+ extra_size
;
3325 if (size
+ next_size
> req
->buffer_size
) break;
3326 if (cur
+ data
->size
> buf
+ get_reply_max_size()) break;
3327 if (cur
+ data
->size
> buf
+ buf_size
)
3329 buf_size
+= buf_size
/ 2 + extra_size
;
3330 if (!(tmp
= realloc( buf
, buf_size
)))
3332 set_error( STATUS_NO_MEMORY
);
3335 cur
= tmp
+ (cur
- buf
);
3339 memcpy( cur
, data
, data
->size
);
3340 list_remove( &msg
->entry
);
3341 free_message( msg
);
3344 cur
+= sizeof(*data
);
3348 reply
->next_size
= next_size
;
3349 reply
->count
= count
;
3350 set_reply_data_ptr( buf
, cur
- buf
);
3353 DECL_HANDLER(update_rawinput_devices
)
3355 const struct rawinput_device
*devices
= get_req_data();
3356 unsigned int device_count
= get_req_data_size() / sizeof (*devices
);
3357 const struct rawinput_device_entry
*e
;
3360 for (i
= 0; i
< device_count
; ++i
)
3362 update_rawinput_device(&devices
[i
]);
3365 e
= find_rawinput_device( current
->process
, 1, 2 );
3366 current
->process
->rawinput_mouse
= e
? &e
->device
: NULL
;
3367 e
= find_rawinput_device( current
->process
, 1, 6 );
3368 current
->process
->rawinput_kbd
= e
? &e
->device
: NULL
;
3371 DECL_HANDLER(get_rawinput_devices
)
3373 struct rawinput_device_entry
*e
;
3374 struct rawinput_device
*devices
;
3375 unsigned int i
= 0, device_count
= list_count( ¤t
->process
->rawinput_devices
);
3376 unsigned int size
= device_count
* sizeof(*devices
);
3378 reply
->device_count
= device_count
;
3380 /* no buffer provided, nothing else to do */
3381 if (!get_reply_max_size()) return;
3383 if (size
> get_reply_max_size())
3384 set_error( STATUS_BUFFER_TOO_SMALL
);
3385 else if ((devices
= set_reply_data_size( size
)))
3387 LIST_FOR_EACH_ENTRY( e
, ¤t
->process
->rawinput_devices
, struct rawinput_device_entry
, entry
)
3388 devices
[i
++] = e
->device
;