2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
30 #define WIN32_NO_STATUS
44 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
45 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
47 enum message_kind
{ SEND_MESSAGE
, POST_MESSAGE
};
48 #define NB_MSG_KINDS (POST_MESSAGE+1)
53 struct list sender_entry
; /* entry in sender list */
54 struct message
*msg
; /* message the result is for */
55 struct message_result
*recv_next
; /* next in receiver list */
56 struct msg_queue
*sender
; /* sender queue */
57 struct msg_queue
*receiver
; /* receiver queue */
58 int replied
; /* has it been replied to? */
59 unsigned int error
; /* error code to pass back to sender */
60 lparam_t result
; /* reply result */
61 struct message
*hardware_msg
; /* hardware message if low-level hook result */
62 struct desktop
*desktop
; /* desktop for hardware message */
63 struct message
*callback_msg
; /* message to queue for callback */
64 void *data
; /* message reply data */
65 unsigned int data_size
; /* size of message reply data */
66 struct timeout_user
*timeout
; /* result timeout */
71 struct list entry
; /* entry in message list */
72 enum message_type type
; /* message type */
73 user_handle_t win
; /* window handle */
74 unsigned int msg
; /* message code */
75 lparam_t wparam
; /* parameters */
76 lparam_t lparam
; /* parameters */
77 unsigned int time
; /* message time */
78 void *data
; /* message data for sent messages */
79 unsigned int data_size
; /* size of message data */
80 unsigned int unique_id
; /* unique id for nested hw message waits */
81 struct message_result
*result
; /* result in sender queue */
86 struct list entry
; /* entry in timer list */
87 timeout_t when
; /* next expiration */
88 unsigned int rate
; /* timer rate in ms */
89 user_handle_t win
; /* window handle */
90 unsigned int msg
; /* message to post */
91 lparam_t id
; /* timer id */
92 lparam_t lparam
; /* lparam for message */
97 struct object obj
; /* object header */
98 struct desktop
*desktop
; /* desktop that this thread input belongs to */
99 user_handle_t focus
; /* focus window */
100 user_handle_t capture
; /* capture window */
101 user_handle_t active
; /* active window */
102 user_handle_t menu_owner
; /* current menu owner window */
103 user_handle_t move_size
; /* current moving/resizing window */
104 user_handle_t caret
; /* caret window */
105 rectangle_t caret_rect
; /* caret rectangle */
106 int caret_hide
; /* caret hide count */
107 int caret_state
; /* caret on/off state */
108 user_handle_t cursor
; /* current cursor */
109 int cursor_count
; /* cursor show count */
110 struct list msg_list
; /* list of hardware messages */
111 unsigned char keystate
[256]; /* state of each key */
116 struct object obj
; /* object header */
117 struct fd
*fd
; /* optional file descriptor to poll */
118 unsigned int wake_bits
; /* wakeup bits */
119 unsigned int wake_mask
; /* wakeup mask */
120 unsigned int changed_bits
; /* changed wakeup bits */
121 unsigned int changed_mask
; /* changed wakeup mask */
122 int paint_count
; /* pending paint messages count */
123 int quit_message
; /* is there a pending quit message? */
124 int exit_code
; /* exit code of pending quit message */
125 int cursor_count
; /* per-queue cursor show count */
126 struct list msg_list
[NB_MSG_KINDS
]; /* lists of messages */
127 struct list send_result
; /* stack of sent messages waiting for result */
128 struct list callback_result
; /* list of callback messages waiting for result */
129 struct message_result
*recv_result
; /* stack of received messages waiting for result */
130 struct list pending_timers
; /* list of pending timers */
131 struct list expired_timers
; /* list of expired timers */
132 lparam_t next_timer_id
; /* id for the next timer with a 0 window */
133 struct timeout_user
*timeout
; /* timeout for next timer to expire */
134 struct thread_input
*input
; /* thread input descriptor */
135 struct hook_table
*hooks
; /* hook table */
136 timeout_t last_get_msg
; /* time of last get message call */
141 struct list entry
; /* entry in desktop hotkey list */
142 struct msg_queue
*queue
; /* queue owning this hotkey */
143 user_handle_t win
; /* window handle */
144 int id
; /* hotkey id */
145 unsigned int vkey
; /* virtual key code */
146 unsigned int flags
; /* key modifiers */
149 static void msg_queue_dump( struct object
*obj
, int verbose
);
150 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
151 static void msg_queue_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
152 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
);
153 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
);
154 static void msg_queue_destroy( struct object
*obj
);
155 static void msg_queue_poll_event( struct fd
*fd
, int event
);
156 static void thread_input_dump( struct object
*obj
, int verbose
);
157 static void thread_input_destroy( struct object
*obj
);
158 static void timer_callback( void *private );
160 static const struct object_ops msg_queue_ops
=
162 sizeof(struct msg_queue
), /* size */
163 msg_queue_dump
, /* dump */
164 no_get_type
, /* get_type */
165 msg_queue_add_queue
, /* add_queue */
166 msg_queue_remove_queue
, /* remove_queue */
167 msg_queue_signaled
, /* signaled */
168 msg_queue_satisfied
, /* satisfied */
169 no_signal
, /* signal */
170 no_get_fd
, /* get_fd */
171 no_map_access
, /* map_access */
172 default_get_sd
, /* get_sd */
173 default_set_sd
, /* set_sd */
174 no_lookup_name
, /* lookup_name */
175 no_open_file
, /* open_file */
176 no_close_handle
, /* close_handle */
177 msg_queue_destroy
/* destroy */
180 static const struct fd_ops msg_queue_fd_ops
=
182 NULL
, /* get_poll_events */
183 msg_queue_poll_event
, /* poll_event */
185 NULL
, /* get_fd_type */
187 NULL
, /* queue_async */
188 NULL
, /* reselect_async */
189 NULL
/* cancel async */
193 static const struct object_ops thread_input_ops
=
195 sizeof(struct thread_input
), /* size */
196 thread_input_dump
, /* dump */
197 no_get_type
, /* get_type */
198 no_add_queue
, /* add_queue */
199 NULL
, /* remove_queue */
201 NULL
, /* satisfied */
202 no_signal
, /* signal */
203 no_get_fd
, /* get_fd */
204 no_map_access
, /* map_access */
205 default_get_sd
, /* get_sd */
206 default_set_sd
, /* set_sd */
207 no_lookup_name
, /* lookup_name */
208 no_open_file
, /* open_file */
209 no_close_handle
, /* close_handle */
210 thread_input_destroy
/* destroy */
213 /* pointer to input structure of foreground thread */
214 static unsigned int last_input_time
;
216 static void queue_hardware_message( struct desktop
*desktop
, struct message
*msg
, int always_queue
);
217 static void free_message( struct message
*msg
);
219 /* set the caret window in a given thread input */
220 static void set_caret_window( struct thread_input
*input
, user_handle_t win
)
222 if (!win
|| win
!= input
->caret
)
224 input
->caret_rect
.left
= 0;
225 input
->caret_rect
.top
= 0;
226 input
->caret_rect
.right
= 0;
227 input
->caret_rect
.bottom
= 0;
230 input
->caret_hide
= 1;
231 input
->caret_state
= 0;
234 /* create a thread input object */
235 static struct thread_input
*create_thread_input( struct thread
*thread
)
237 struct thread_input
*input
;
239 if ((input
= alloc_object( &thread_input_ops
)))
244 input
->menu_owner
= 0;
245 input
->move_size
= 0;
247 input
->cursor_count
= 0;
248 list_init( &input
->msg_list
);
249 set_caret_window( input
, 0 );
250 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
252 if (!(input
->desktop
= get_thread_desktop( thread
, 0 /* FIXME: access rights */ )))
254 release_object( input
);
261 /* create a message queue object */
262 static struct msg_queue
*create_msg_queue( struct thread
*thread
, struct thread_input
*input
)
264 struct thread_input
*new_input
= NULL
;
265 struct msg_queue
*queue
;
270 if (!(new_input
= create_thread_input( thread
))) return NULL
;
274 if ((queue
= alloc_object( &msg_queue_ops
)))
277 queue
->wake_bits
= 0;
278 queue
->wake_mask
= 0;
279 queue
->changed_bits
= 0;
280 queue
->changed_mask
= 0;
281 queue
->paint_count
= 0;
282 queue
->quit_message
= 0;
283 queue
->cursor_count
= 0;
284 queue
->recv_result
= NULL
;
285 queue
->next_timer_id
= 0x7fff;
286 queue
->timeout
= NULL
;
287 queue
->input
= (struct thread_input
*)grab_object( input
);
289 queue
->last_get_msg
= current_time
;
290 list_init( &queue
->send_result
);
291 list_init( &queue
->callback_result
);
292 list_init( &queue
->pending_timers
);
293 list_init( &queue
->expired_timers
);
294 for (i
= 0; i
< NB_MSG_KINDS
; i
++) list_init( &queue
->msg_list
[i
] );
296 thread
->queue
= queue
;
298 if (new_input
) release_object( new_input
);
302 /* free the message queue of a thread at thread exit */
303 void free_msg_queue( struct thread
*thread
)
305 remove_thread_hooks( thread
);
306 if (!thread
->queue
) return;
307 release_object( thread
->queue
);
308 thread
->queue
= NULL
;
311 /* change the thread input data of a given thread */
312 static int assign_thread_input( struct thread
*thread
, struct thread_input
*new_input
)
314 struct msg_queue
*queue
= thread
->queue
;
318 thread
->queue
= create_msg_queue( thread
, new_input
);
319 return thread
->queue
!= NULL
;
323 queue
->input
->cursor_count
-= queue
->cursor_count
;
324 release_object( queue
->input
);
326 queue
->input
= (struct thread_input
*)grab_object( new_input
);
327 new_input
->cursor_count
+= queue
->cursor_count
;
331 /* set the cursor position and queue the corresponding mouse message */
332 static void set_cursor_pos( struct desktop
*desktop
, int x
, int y
)
334 struct hardware_msg_data
*msg_data
;
337 if (!(msg
= mem_alloc( sizeof(*msg
) ))) return;
338 if (!(msg_data
= mem_alloc( sizeof(*msg_data
) )))
343 memset( msg_data
, 0, sizeof(*msg_data
) );
345 msg
->type
= MSG_HARDWARE
;
347 msg
->msg
= WM_MOUSEMOVE
;
350 msg
->time
= get_tick_count();
352 msg
->data
= msg_data
;
353 msg
->data_size
= sizeof(*msg_data
);
356 queue_hardware_message( desktop
, msg
, 1 );
359 /* set the cursor clip rectangle */
360 static void set_clip_rectangle( struct desktop
*desktop
, const rectangle_t
*rect
)
362 rectangle_t top_rect
;
365 get_top_window_rectangle( desktop
, &top_rect
);
368 rectangle_t new_rect
= *rect
;
369 if (new_rect
.left
< top_rect
.left
) new_rect
.left
= top_rect
.left
;
370 if (new_rect
.right
> top_rect
.right
) new_rect
.right
= top_rect
.right
;
371 if (new_rect
.top
< top_rect
.top
) new_rect
.top
= top_rect
.top
;
372 if (new_rect
.bottom
> top_rect
.bottom
) new_rect
.bottom
= top_rect
.bottom
;
373 if (new_rect
.left
> new_rect
.right
|| new_rect
.top
> new_rect
.bottom
) new_rect
= top_rect
;
374 desktop
->cursor
.clip
= new_rect
;
376 else desktop
->cursor
.clip
= top_rect
;
378 if (desktop
->cursor
.clip_msg
)
379 post_desktop_message( desktop
, desktop
->cursor
.clip_msg
, rect
!= NULL
, 0 );
381 /* warp the mouse to be inside the clip rect */
382 x
= min( max( desktop
->cursor
.x
, desktop
->cursor
.clip
.left
), desktop
->cursor
.clip
.right
-1 );
383 y
= min( max( desktop
->cursor
.y
, desktop
->cursor
.clip
.top
), desktop
->cursor
.clip
.bottom
-1 );
384 if (x
!= desktop
->cursor
.x
|| y
!= desktop
->cursor
.y
) set_cursor_pos( desktop
, x
, y
);
387 /* change the foreground input and reset the cursor clip rect */
388 static void set_foreground_input( struct desktop
*desktop
, struct thread_input
*input
)
390 if (desktop
->foreground_input
== input
) return;
391 set_clip_rectangle( desktop
, NULL
);
392 desktop
->foreground_input
= input
;
395 /* get the hook table for a given thread */
396 struct hook_table
*get_queue_hooks( struct thread
*thread
)
398 if (!thread
->queue
) return NULL
;
399 return thread
->queue
->hooks
;
402 /* set the hook table for a given thread, allocating the queue if needed */
403 void set_queue_hooks( struct thread
*thread
, struct hook_table
*hooks
)
405 struct msg_queue
*queue
= thread
->queue
;
406 if (!queue
&& !(queue
= create_msg_queue( thread
, NULL
))) return;
407 if (queue
->hooks
) release_object( queue
->hooks
);
408 queue
->hooks
= hooks
;
411 /* check the queue status */
412 static inline int is_signaled( struct msg_queue
*queue
)
414 return ((queue
->wake_bits
& queue
->wake_mask
) || (queue
->changed_bits
& queue
->changed_mask
));
417 /* set some queue bits */
418 static inline void set_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
420 queue
->wake_bits
|= bits
;
421 queue
->changed_bits
|= bits
;
422 if (is_signaled( queue
)) wake_up( &queue
->obj
, 0 );
425 /* clear some queue bits */
426 static inline void clear_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
428 queue
->wake_bits
&= ~bits
;
429 queue
->changed_bits
&= ~bits
;
432 /* check whether msg is a keyboard message */
433 static inline int is_keyboard_msg( struct message
*msg
)
435 return (msg
->msg
>= WM_KEYFIRST
&& msg
->msg
<= WM_KEYLAST
);
438 /* check if message is matched by the filter */
439 static inline int check_msg_filter( unsigned int msg
, unsigned int first
, unsigned int last
)
441 return (msg
>= first
&& msg
<= last
);
444 /* check whether a message filter contains at least one potential hardware message */
445 static inline int filter_contains_hw_range( unsigned int first
, unsigned int last
)
447 /* hardware message ranges are (in numerical order):
448 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
449 * WM_KEYFIRST .. WM_KEYLAST
450 * WM_MOUSEFIRST .. WM_MOUSELAST
452 if (last
< WM_NCMOUSEFIRST
) return 0;
453 if (first
> WM_NCMOUSELAST
&& last
< WM_KEYFIRST
) return 0;
454 if (first
> WM_KEYLAST
&& last
< WM_MOUSEFIRST
) return 0;
455 if (first
> WM_MOUSELAST
) return 0;
459 /* get the QS_* bit corresponding to a given hardware message */
460 static inline int get_hardware_msg_bit( struct message
*msg
)
462 if (msg
->msg
== WM_MOUSEMOVE
|| msg
->msg
== WM_NCMOUSEMOVE
) return QS_MOUSEMOVE
;
463 if (is_keyboard_msg( msg
)) return QS_KEY
;
464 return QS_MOUSEBUTTON
;
467 /* get the current thread queue, creating it if needed */
468 static inline struct msg_queue
*get_current_queue(void)
470 struct msg_queue
*queue
= current
->queue
;
471 if (!queue
) queue
= create_msg_queue( current
, NULL
);
475 /* get a (pseudo-)unique id to tag hardware messages */
476 static inline unsigned int get_unique_id(void)
478 static unsigned int id
;
479 if (!++id
) id
= 1; /* avoid an id of 0 */
483 /* try to merge a message with the last in the list; return 1 if successful */
484 static int merge_message( struct thread_input
*input
, const struct message
*msg
)
486 struct message
*prev
;
489 if (msg
->msg
!= WM_MOUSEMOVE
) return 0;
490 if (!(ptr
= list_tail( &input
->msg_list
))) return 0;
491 prev
= LIST_ENTRY( ptr
, struct message
, entry
);
492 if (prev
->result
) return 0;
493 if (prev
->win
&& msg
->win
&& prev
->win
!= msg
->win
) return 0;
494 if (prev
->msg
!= msg
->msg
) return 0;
495 if (prev
->type
!= msg
->type
) return 0;
496 /* now we can merge it */
497 prev
->wparam
= msg
->wparam
;
498 prev
->lparam
= msg
->lparam
;
499 prev
->time
= msg
->time
;
500 if (msg
->type
== MSG_HARDWARE
&& prev
->data
&& msg
->data
)
502 struct hardware_msg_data
*prev_data
= prev
->data
;
503 struct hardware_msg_data
*msg_data
= msg
->data
;
504 prev_data
->x
= msg_data
->x
;
505 prev_data
->y
= msg_data
->y
;
506 prev_data
->info
= msg_data
->info
;
511 /* free a result structure */
512 static void free_result( struct message_result
*result
)
514 if (result
->timeout
) remove_timeout_user( result
->timeout
);
515 free( result
->data
);
516 if (result
->callback_msg
) free_message( result
->callback_msg
);
517 if (result
->hardware_msg
) free_message( result
->hardware_msg
);
518 if (result
->desktop
) release_object( result
->desktop
);
522 /* remove the result from the sender list it is on */
523 static inline void remove_result_from_sender( struct message_result
*result
)
525 assert( result
->sender
);
527 list_remove( &result
->sender_entry
);
528 result
->sender
= NULL
;
529 if (!result
->receiver
) free_result( result
);
532 /* store the message result in the appropriate structure */
533 static void store_message_result( struct message_result
*res
, lparam_t result
, unsigned int error
)
535 res
->result
= result
;
540 remove_timeout_user( res
->timeout
);
544 if (res
->hardware_msg
)
546 if (!error
&& result
) /* rejected by the hook */
547 free_message( res
->hardware_msg
);
549 queue_hardware_message( res
->desktop
, res
->hardware_msg
, 0 );
551 res
->hardware_msg
= NULL
;
556 if (res
->callback_msg
)
558 /* queue the callback message in the sender queue */
559 struct callback_msg_data
*data
= res
->callback_msg
->data
;
560 data
->result
= result
;
561 list_add_tail( &res
->sender
->msg_list
[SEND_MESSAGE
], &res
->callback_msg
->entry
);
562 set_queue_bits( res
->sender
, QS_SENDMESSAGE
);
563 res
->callback_msg
= NULL
;
564 remove_result_from_sender( res
);
568 /* wake sender queue if waiting on this result */
569 if (list_head(&res
->sender
->send_result
) == &res
->sender_entry
)
570 set_queue_bits( res
->sender
, QS_SMRESULT
);
573 else if (!res
->receiver
) free_result( res
);
576 /* free a message when deleting a queue or window */
577 static void free_message( struct message
*msg
)
579 struct message_result
*result
= msg
->result
;
583 result
->receiver
= NULL
;
584 store_message_result( result
, 0, STATUS_ACCESS_DENIED
/*FIXME*/ );
590 /* remove (and free) a message from a message list */
591 static void remove_queue_message( struct msg_queue
*queue
, struct message
*msg
,
592 enum message_kind kind
)
594 list_remove( &msg
->entry
);
598 if (list_empty( &queue
->msg_list
[kind
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
601 if (list_empty( &queue
->msg_list
[kind
] ) && !queue
->quit_message
)
602 clear_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
608 /* message timed out without getting a reply */
609 static void result_timeout( void *private )
611 struct message_result
*result
= private;
613 assert( !result
->replied
);
615 result
->timeout
= NULL
;
617 if (result
->msg
) /* not received yet */
619 struct message
*msg
= result
->msg
;
623 remove_queue_message( result
->receiver
, msg
, SEND_MESSAGE
);
624 result
->receiver
= NULL
;
626 store_message_result( result
, 0, STATUS_TIMEOUT
);
629 /* allocate and fill a message result structure */
630 static struct message_result
*alloc_message_result( struct msg_queue
*send_queue
,
631 struct msg_queue
*recv_queue
,
632 struct message
*msg
, timeout_t timeout
)
634 struct message_result
*result
= mem_alloc( sizeof(*result
) );
638 result
->sender
= send_queue
;
639 result
->receiver
= recv_queue
;
642 result
->data_size
= 0;
643 result
->timeout
= NULL
;
644 result
->hardware_msg
= NULL
;
645 result
->desktop
= NULL
;
646 result
->callback_msg
= NULL
;
648 if (msg
->type
== MSG_CALLBACK
)
650 struct message
*callback_msg
= mem_alloc( sizeof(*callback_msg
) );
657 callback_msg
->type
= MSG_CALLBACK_RESULT
;
658 callback_msg
->win
= msg
->win
;
659 callback_msg
->msg
= msg
->msg
;
660 callback_msg
->wparam
= 0;
661 callback_msg
->lparam
= 0;
662 callback_msg
->time
= get_tick_count();
663 callback_msg
->result
= NULL
;
664 /* steal the data from the original message */
665 callback_msg
->data
= msg
->data
;
666 callback_msg
->data_size
= msg
->data_size
;
670 result
->callback_msg
= callback_msg
;
671 list_add_head( &send_queue
->callback_result
, &result
->sender_entry
);
673 else if (send_queue
) list_add_head( &send_queue
->send_result
, &result
->sender_entry
);
675 if (timeout
!= TIMEOUT_INFINITE
)
676 result
->timeout
= add_timeout_user( timeout
, result_timeout
, result
);
681 /* receive a message, removing it from the sent queue */
682 static void receive_message( struct msg_queue
*queue
, struct message
*msg
,
683 struct get_message_reply
*reply
)
685 struct message_result
*result
= msg
->result
;
687 reply
->total
= msg
->data_size
;
688 if (msg
->data_size
> get_reply_max_size())
690 set_error( STATUS_BUFFER_OVERFLOW
);
693 reply
->type
= msg
->type
;
694 reply
->win
= msg
->win
;
695 reply
->msg
= msg
->msg
;
696 reply
->wparam
= msg
->wparam
;
697 reply
->lparam
= msg
->lparam
;
698 reply
->time
= msg
->time
;
700 if (msg
->data
) set_reply_data_ptr( msg
->data
, msg
->data_size
);
702 list_remove( &msg
->entry
);
703 /* put the result on the receiver result stack */
707 result
->recv_next
= queue
->recv_result
;
708 queue
->recv_result
= result
;
711 if (list_empty( &queue
->msg_list
[SEND_MESSAGE
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
714 /* set the result of the current received message */
715 static void reply_message( struct msg_queue
*queue
, lparam_t result
,
716 unsigned int error
, int remove
, const void *data
, data_size_t len
)
718 struct message_result
*res
= queue
->recv_result
;
722 queue
->recv_result
= res
->recv_next
;
723 res
->receiver
= NULL
;
724 if (!res
->sender
&& !res
->hardware_msg
) /* no one waiting for it */
732 if (len
&& (res
->data
= memdup( data
, len
))) res
->data_size
= len
;
733 store_message_result( res
, result
, error
);
737 static int match_window( user_handle_t win
, user_handle_t msg_win
)
740 if (win
== -1 || win
== 1) return !msg_win
;
741 if (msg_win
== win
) return 1;
742 return is_child_window( win
, msg_win
);
745 /* retrieve a posted message */
746 static int get_posted_message( struct msg_queue
*queue
, user_handle_t win
,
747 unsigned int first
, unsigned int last
, unsigned int flags
,
748 struct get_message_reply
*reply
)
752 /* check against the filters */
753 LIST_FOR_EACH_ENTRY( msg
, &queue
->msg_list
[POST_MESSAGE
], struct message
, entry
)
755 if (!match_window( win
, msg
->win
)) continue;
756 if (!check_msg_filter( msg
->msg
, first
, last
)) continue;
757 goto found
; /* found one */
761 /* return it to the app */
763 reply
->total
= msg
->data_size
;
764 if (msg
->data_size
> get_reply_max_size())
766 set_error( STATUS_BUFFER_OVERFLOW
);
769 reply
->type
= msg
->type
;
770 reply
->win
= msg
->win
;
771 reply
->msg
= msg
->msg
;
772 reply
->wparam
= msg
->wparam
;
773 reply
->lparam
= msg
->lparam
;
774 reply
->time
= msg
->time
;
776 if (flags
& PM_REMOVE
)
780 set_reply_data_ptr( msg
->data
, msg
->data_size
);
784 remove_queue_message( queue
, msg
, POST_MESSAGE
);
786 else if (msg
->data
) set_reply_data( msg
->data
, msg
->data_size
);
791 static int get_quit_message( struct msg_queue
*queue
, unsigned int flags
,
792 struct get_message_reply
*reply
)
794 if (queue
->quit_message
)
797 reply
->type
= MSG_POSTED
;
799 reply
->msg
= WM_QUIT
;
800 reply
->wparam
= queue
->exit_code
;
802 reply
->time
= get_tick_count();
804 if (flags
& PM_REMOVE
)
806 queue
->quit_message
= 0;
807 if (list_empty( &queue
->msg_list
[POST_MESSAGE
] ))
808 clear_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
816 /* empty a message list and free all the messages */
817 static void empty_msg_list( struct list
*list
)
821 while ((ptr
= list_head( list
)) != NULL
)
823 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
824 list_remove( &msg
->entry
);
829 /* cleanup all pending results when deleting a queue */
830 static void cleanup_results( struct msg_queue
*queue
)
834 while ((entry
= list_head( &queue
->send_result
)) != NULL
)
836 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
839 while ((entry
= list_head( &queue
->callback_result
)) != NULL
)
841 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
844 while (queue
->recv_result
)
845 reply_message( queue
, 0, STATUS_ACCESS_DENIED
/*FIXME*/, 1, NULL
, 0 );
848 /* check if the thread owning the queue is hung (not checking for messages) */
849 static int is_queue_hung( struct msg_queue
*queue
)
851 struct wait_queue_entry
*entry
;
853 if (current_time
- queue
->last_get_msg
<= 5 * TICKS_PER_SEC
)
854 return 0; /* less than 5 seconds since last get message -> not hung */
856 LIST_FOR_EACH_ENTRY( entry
, &queue
->obj
.wait_queue
, struct wait_queue_entry
, entry
)
858 if (entry
->thread
->queue
== queue
)
859 return 0; /* thread is waiting on queue -> not hung */
864 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
866 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
867 struct process
*process
= entry
->thread
->process
;
869 /* a thread can only wait on its own queue */
870 if (entry
->thread
->queue
!= queue
)
872 set_error( STATUS_ACCESS_DENIED
);
875 if (process
->idle_event
&& !(queue
->wake_mask
& QS_SMRESULT
)) set_event( process
->idle_event
);
877 if (queue
->fd
&& list_empty( &obj
->wait_queue
)) /* first on the queue */
878 set_fd_events( queue
->fd
, POLLIN
);
879 add_queue( obj
, entry
);
883 static void msg_queue_remove_queue(struct object
*obj
, struct wait_queue_entry
*entry
)
885 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
887 remove_queue( obj
, entry
);
888 if (queue
->fd
&& list_empty( &obj
->wait_queue
)) /* last on the queue is gone */
889 set_fd_events( queue
->fd
, 0 );
892 static void msg_queue_dump( struct object
*obj
, int verbose
)
894 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
895 fprintf( stderr
, "Msg queue bits=%x mask=%x\n",
896 queue
->wake_bits
, queue
->wake_mask
);
899 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
)
901 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
906 if ((ret
= check_fd_events( queue
->fd
, POLLIN
)))
907 /* stop waiting on select() if we are signaled */
908 set_fd_events( queue
->fd
, 0 );
909 else if (!list_empty( &obj
->wait_queue
))
910 /* restart waiting on poll() if we are no longer signaled */
911 set_fd_events( queue
->fd
, POLLIN
);
913 return ret
|| is_signaled( queue
);
916 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
)
918 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
919 queue
->wake_mask
= 0;
920 queue
->changed_mask
= 0;
921 return 0; /* Not abandoned */
924 static void msg_queue_destroy( struct object
*obj
)
926 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
928 struct hotkey
*hotkey
, *hotkey2
;
931 cleanup_results( queue
);
932 for (i
= 0; i
< NB_MSG_KINDS
; i
++) empty_msg_list( &queue
->msg_list
[i
] );
934 LIST_FOR_EACH_ENTRY_SAFE( hotkey
, hotkey2
, &queue
->input
->desktop
->hotkeys
, struct hotkey
, entry
)
936 if (hotkey
->queue
== queue
)
938 list_remove( &hotkey
->entry
);
943 while ((ptr
= list_head( &queue
->pending_timers
)))
945 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
946 list_remove( &timer
->entry
);
949 while ((ptr
= list_head( &queue
->expired_timers
)))
951 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
952 list_remove( &timer
->entry
);
955 if (queue
->timeout
) remove_timeout_user( queue
->timeout
);
958 queue
->input
->cursor_count
-= queue
->cursor_count
;
959 release_object( queue
->input
);
961 if (queue
->hooks
) release_object( queue
->hooks
);
962 if (queue
->fd
) release_object( queue
->fd
);
965 static void msg_queue_poll_event( struct fd
*fd
, int event
)
967 struct msg_queue
*queue
= get_fd_user( fd
);
968 assert( queue
->obj
.ops
== &msg_queue_ops
);
970 if (event
& (POLLERR
| POLLHUP
)) set_fd_events( fd
, -1 );
971 else set_fd_events( queue
->fd
, 0 );
972 wake_up( &queue
->obj
, 0 );
975 static void thread_input_dump( struct object
*obj
, int verbose
)
977 struct thread_input
*input
= (struct thread_input
*)obj
;
978 fprintf( stderr
, "Thread input focus=%08x capture=%08x active=%08x\n",
979 input
->focus
, input
->capture
, input
->active
);
982 static void thread_input_destroy( struct object
*obj
)
984 struct thread_input
*input
= (struct thread_input
*)obj
;
986 empty_msg_list( &input
->msg_list
);
989 if (input
->desktop
->foreground_input
== input
) set_foreground_input( input
->desktop
, NULL
);
990 release_object( input
->desktop
);
994 /* fix the thread input data when a window is destroyed */
995 static inline void thread_input_cleanup_window( struct msg_queue
*queue
, user_handle_t window
)
997 struct thread_input
*input
= queue
->input
;
999 if (window
== input
->focus
) input
->focus
= 0;
1000 if (window
== input
->capture
) input
->capture
= 0;
1001 if (window
== input
->active
) input
->active
= 0;
1002 if (window
== input
->menu_owner
) input
->menu_owner
= 0;
1003 if (window
== input
->move_size
) input
->move_size
= 0;
1004 if (window
== input
->caret
) set_caret_window( input
, 0 );
1007 /* check if the specified window can be set in the input data of a given queue */
1008 static int check_queue_input_window( struct msg_queue
*queue
, user_handle_t window
)
1010 struct thread
*thread
;
1013 if (!window
) return 1; /* we can always clear the data */
1015 if ((thread
= get_window_thread( window
)))
1017 ret
= (queue
->input
== thread
->queue
->input
);
1018 if (!ret
) set_error( STATUS_ACCESS_DENIED
);
1019 release_object( thread
);
1021 else set_error( STATUS_INVALID_HANDLE
);
1026 /* make sure the specified thread has a queue */
1027 int init_thread_queue( struct thread
*thread
)
1029 if (thread
->queue
) return 1;
1030 return (create_msg_queue( thread
, NULL
) != NULL
);
1033 /* attach two thread input data structures */
1034 int attach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
1036 struct desktop
*desktop
;
1037 struct thread_input
*input
;
1040 if (!thread_to
->queue
&& !(thread_to
->queue
= create_msg_queue( thread_to
, NULL
))) return 0;
1041 if (!(desktop
= get_thread_desktop( thread_from
, 0 ))) return 0;
1042 input
= (struct thread_input
*)grab_object( thread_to
->queue
->input
);
1043 if (input
->desktop
!= desktop
)
1045 set_error( STATUS_ACCESS_DENIED
);
1046 release_object( input
);
1047 release_object( desktop
);
1050 release_object( desktop
);
1052 ret
= assign_thread_input( thread_from
, input
);
1053 if (ret
) memset( input
->keystate
, 0, sizeof(input
->keystate
) );
1054 release_object( input
);
1058 /* detach two thread input data structures */
1059 void detach_thread_input( struct thread
*thread_from
)
1061 struct thread_input
*input
;
1063 if ((input
= create_thread_input( thread_from
)))
1065 assign_thread_input( thread_from
, input
);
1066 release_object( input
);
1071 /* set the next timer to expire */
1072 static void set_next_timer( struct msg_queue
*queue
)
1078 remove_timeout_user( queue
->timeout
);
1079 queue
->timeout
= NULL
;
1081 if ((ptr
= list_head( &queue
->pending_timers
)))
1083 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1084 queue
->timeout
= add_timeout_user( timer
->when
, timer_callback
, queue
);
1086 /* set/clear QS_TIMER bit */
1087 if (list_empty( &queue
->expired_timers
))
1088 clear_queue_bits( queue
, QS_TIMER
);
1090 set_queue_bits( queue
, QS_TIMER
);
1093 /* find a timer from its window and id */
1094 static struct timer
*find_timer( struct msg_queue
*queue
, user_handle_t win
,
1095 unsigned int msg
, lparam_t id
)
1099 /* we need to search both lists */
1101 LIST_FOR_EACH( ptr
, &queue
->pending_timers
)
1103 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1104 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
1106 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
1108 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1109 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
1114 /* callback for the next timer expiration */
1115 static void timer_callback( void *private )
1117 struct msg_queue
*queue
= private;
1120 queue
->timeout
= NULL
;
1121 /* move on to the next timer */
1122 ptr
= list_head( &queue
->pending_timers
);
1124 list_add_tail( &queue
->expired_timers
, ptr
);
1125 set_next_timer( queue
);
1128 /* link a timer at its rightful place in the queue list */
1129 static void link_timer( struct msg_queue
*queue
, struct timer
*timer
)
1133 for (ptr
= queue
->pending_timers
.next
; ptr
!= &queue
->pending_timers
; ptr
= ptr
->next
)
1135 struct timer
*t
= LIST_ENTRY( ptr
, struct timer
, entry
);
1136 if (t
->when
>= timer
->when
) break;
1138 list_add_before( ptr
, &timer
->entry
);
1141 /* remove a timer from the queue timer list and free it */
1142 static void free_timer( struct msg_queue
*queue
, struct timer
*timer
)
1144 list_remove( &timer
->entry
);
1146 set_next_timer( queue
);
1149 /* restart an expired timer */
1150 static void restart_timer( struct msg_queue
*queue
, struct timer
*timer
)
1152 list_remove( &timer
->entry
);
1153 while (timer
->when
<= current_time
) timer
->when
+= (timeout_t
)timer
->rate
* 10000;
1154 link_timer( queue
, timer
);
1155 set_next_timer( queue
);
1158 /* find an expired timer matching the filtering parameters */
1159 static struct timer
*find_expired_timer( struct msg_queue
*queue
, user_handle_t win
,
1160 unsigned int get_first
, unsigned int get_last
,
1165 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
1167 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1168 if (win
&& timer
->win
!= win
) continue;
1169 if (check_msg_filter( timer
->msg
, get_first
, get_last
))
1171 if (remove
) restart_timer( queue
, timer
);
1179 static struct timer
*set_timer( struct msg_queue
*queue
, unsigned int rate
)
1181 struct timer
*timer
= mem_alloc( sizeof(*timer
) );
1184 timer
->rate
= max( rate
, 1 );
1185 timer
->when
= current_time
+ (timeout_t
)timer
->rate
* 10000;
1186 link_timer( queue
, timer
);
1187 /* check if we replaced the next timer */
1188 if (list_head( &queue
->pending_timers
) == &timer
->entry
) set_next_timer( queue
);
1193 /* change the input key state for a given key */
1194 static void set_input_key_state( unsigned char *keystate
, unsigned char key
, int down
)
1198 if (!(keystate
[key
] & 0x80)) keystate
[key
] ^= 0x01;
1199 keystate
[key
] |= down
;
1201 else keystate
[key
] &= ~0x80;
1204 /* update the input key state for a keyboard message */
1205 static void update_input_key_state( struct desktop
*desktop
, unsigned char *keystate
,
1206 const struct message
*msg
)
1213 case WM_LBUTTONDOWN
:
1214 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1217 set_input_key_state( keystate
, VK_LBUTTON
, down
);
1219 case WM_MBUTTONDOWN
:
1220 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1223 set_input_key_state( keystate
, VK_MBUTTON
, down
);
1225 case WM_RBUTTONDOWN
:
1226 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1229 set_input_key_state( keystate
, VK_RBUTTON
, down
);
1231 case WM_XBUTTONDOWN
:
1232 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1235 if (msg
->wparam
>> 16 == XBUTTON1
) set_input_key_state( keystate
, VK_XBUTTON1
, down
);
1236 else if (msg
->wparam
>> 16 == XBUTTON2
) set_input_key_state( keystate
, VK_XBUTTON2
, down
);
1240 down
= (keystate
== desktop
->keystate
) ? 0xc0 : 0x80;
1244 key
= (unsigned char)msg
->wparam
;
1245 set_input_key_state( keystate
, key
, down
);
1250 down
= (keystate
[VK_LCONTROL
] | keystate
[VK_RCONTROL
]) & 0x80;
1251 set_input_key_state( keystate
, VK_CONTROL
, down
);
1255 down
= (keystate
[VK_LMENU
] | keystate
[VK_RMENU
]) & 0x80;
1256 set_input_key_state( keystate
, VK_MENU
, down
);
1260 down
= (keystate
[VK_LSHIFT
] | keystate
[VK_RSHIFT
]) & 0x80;
1261 set_input_key_state( keystate
, VK_SHIFT
, down
);
1268 /* release the hardware message currently being processed by the given thread */
1269 static void release_hardware_message( struct msg_queue
*queue
, unsigned int hw_id
,
1270 int remove
, user_handle_t new_win
)
1272 struct thread_input
*input
= queue
->input
;
1273 struct message
*msg
;
1275 LIST_FOR_EACH_ENTRY( msg
, &input
->msg_list
, struct message
, entry
)
1277 if (msg
->unique_id
== hw_id
) break;
1279 if (&msg
->entry
== &input
->msg_list
) return; /* not found */
1281 /* clear the queue bit for that message */
1282 if (remove
|| new_win
)
1284 struct message
*other
;
1287 clr_bit
= get_hardware_msg_bit( msg
);
1288 LIST_FOR_EACH_ENTRY( other
, &input
->msg_list
, struct message
, entry
)
1290 if (other
!= msg
&& get_hardware_msg_bit( other
) == clr_bit
)
1296 if (clr_bit
) clear_queue_bits( queue
, clr_bit
);
1299 if (new_win
) /* set the new window */
1301 struct thread
*owner
= get_window_thread( new_win
);
1305 if (owner
->queue
->input
!= input
)
1307 list_remove( &msg
->entry
);
1308 if (merge_message( owner
->queue
->input
, msg
))
1310 free_message( msg
);
1311 release_object( owner
);
1314 list_add_tail( &owner
->queue
->input
->msg_list
, &msg
->entry
);
1316 set_queue_bits( owner
->queue
, get_hardware_msg_bit( msg
));
1318 release_object( owner
);
1323 update_input_key_state( input
->desktop
, input
->keystate
, msg
);
1324 list_remove( &msg
->entry
);
1325 free_message( msg
);
1329 static int queue_hotkey_message( struct desktop
*desktop
, struct message
*msg
)
1331 struct hotkey
*hotkey
;
1332 unsigned int modifiers
= 0;
1334 if (msg
->msg
!= WM_KEYDOWN
) return 0;
1336 if (desktop
->keystate
[VK_MENU
] & 0x80) modifiers
|= MOD_ALT
;
1337 if (desktop
->keystate
[VK_CONTROL
] & 0x80) modifiers
|= MOD_CONTROL
;
1338 if (desktop
->keystate
[VK_SHIFT
] & 0x80) modifiers
|= MOD_SHIFT
;
1339 if ((desktop
->keystate
[VK_LWIN
] & 0x80) || (desktop
->keystate
[VK_RWIN
] & 0x80)) modifiers
|= MOD_WIN
;
1341 LIST_FOR_EACH_ENTRY( hotkey
, &desktop
->hotkeys
, struct hotkey
, entry
)
1343 if (hotkey
->vkey
!= msg
->wparam
) continue;
1344 if ((hotkey
->flags
& (MOD_ALT
|MOD_CONTROL
|MOD_SHIFT
|MOD_WIN
)) == modifiers
) goto found
;
1350 msg
->type
= MSG_POSTED
;
1351 msg
->win
= hotkey
->win
;
1352 msg
->msg
= WM_HOTKEY
;
1353 msg
->wparam
= hotkey
->id
;
1354 msg
->lparam
= ((hotkey
->vkey
& 0xffff) << 16) | modifiers
;
1360 list_add_tail( &hotkey
->queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1361 set_queue_bits( hotkey
->queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
1365 /* find the window that should receive a given hardware message */
1366 static user_handle_t
find_hardware_message_window( struct desktop
*desktop
, struct thread_input
*input
,
1367 struct message
*msg
, unsigned int *msg_code
)
1369 struct hardware_msg_data
*data
= msg
->data
;
1370 user_handle_t win
= 0;
1372 *msg_code
= msg
->msg
;
1373 if (is_keyboard_msg( msg
))
1375 if (input
&& !(win
= input
->focus
))
1377 win
= input
->active
;
1378 if (*msg_code
< WM_SYSKEYDOWN
) *msg_code
+= WM_SYSKEYDOWN
- WM_KEYDOWN
;
1381 else /* mouse message */
1383 if (!input
|| !(win
= input
->capture
))
1385 if (!(win
= msg
->win
) || !is_window_visible( win
) || is_window_transparent( win
))
1386 win
= window_from_point( desktop
, data
->x
, data
->y
);
1392 /* queue a hardware message into a given thread input */
1393 static void queue_hardware_message( struct desktop
*desktop
, struct message
*msg
, int always_queue
)
1396 struct thread
*thread
;
1397 struct thread_input
*input
;
1398 unsigned int msg_code
;
1399 struct hardware_msg_data
*data
= msg
->data
;
1401 update_input_key_state( desktop
, desktop
->keystate
, msg
);
1402 last_input_time
= get_tick_count();
1403 if (msg
->msg
!= WM_MOUSEMOVE
) always_queue
= 1;
1405 if (is_keyboard_msg( msg
))
1407 if (queue_hotkey_message( desktop
, msg
)) return;
1408 if (desktop
->keystate
[VK_MENU
] & 0x80) msg
->lparam
|= KF_ALTDOWN
<< 16;
1409 if (msg
->wparam
== VK_SHIFT
|| msg
->wparam
== VK_LSHIFT
|| msg
->wparam
== VK_RSHIFT
)
1410 msg
->lparam
&= ~(KF_EXTENDED
<< 16);
1414 if (msg
->msg
== WM_MOUSEMOVE
)
1416 int x
= min( max( data
->x
, desktop
->cursor
.clip
.left
), desktop
->cursor
.clip
.right
-1 );
1417 int y
= min( max( data
->y
, desktop
->cursor
.clip
.top
), desktop
->cursor
.clip
.bottom
-1 );
1418 if (desktop
->cursor
.x
!= x
|| desktop
->cursor
.y
!= y
) always_queue
= 1;
1419 desktop
->cursor
.x
= x
;
1420 desktop
->cursor
.y
= y
;
1421 desktop
->cursor
.last_change
= get_tick_count();
1423 if (desktop
->keystate
[VK_LBUTTON
] & 0x80) msg
->wparam
|= MK_LBUTTON
;
1424 if (desktop
->keystate
[VK_MBUTTON
] & 0x80) msg
->wparam
|= MK_MBUTTON
;
1425 if (desktop
->keystate
[VK_RBUTTON
] & 0x80) msg
->wparam
|= MK_RBUTTON
;
1426 if (desktop
->keystate
[VK_SHIFT
] & 0x80) msg
->wparam
|= MK_SHIFT
;
1427 if (desktop
->keystate
[VK_CONTROL
] & 0x80) msg
->wparam
|= MK_CONTROL
;
1428 if (desktop
->keystate
[VK_XBUTTON1
] & 0x80) msg
->wparam
|= MK_XBUTTON1
;
1429 if (desktop
->keystate
[VK_XBUTTON2
] & 0x80) msg
->wparam
|= MK_XBUTTON2
;
1431 data
->x
= desktop
->cursor
.x
;
1432 data
->y
= desktop
->cursor
.y
;
1434 if (msg
->win
&& (thread
= get_window_thread( msg
->win
)))
1436 input
= thread
->queue
->input
;
1437 release_object( thread
);
1439 else input
= desktop
->foreground_input
;
1441 win
= find_hardware_message_window( desktop
, input
, msg
, &msg_code
);
1442 if (!win
|| !(thread
= get_window_thread(win
)))
1444 if (input
) update_input_key_state( input
->desktop
, input
->keystate
, msg
);
1445 free_message( msg
);
1448 input
= thread
->queue
->input
;
1450 if (win
!= desktop
->cursor
.win
) always_queue
= 1;
1451 desktop
->cursor
.win
= win
;
1453 if (!always_queue
|| merge_message( input
, msg
)) free_message( msg
);
1456 msg
->unique_id
= 0; /* will be set once we return it to the app */
1457 list_add_tail( &input
->msg_list
, &msg
->entry
);
1458 set_queue_bits( thread
->queue
, get_hardware_msg_bit(msg
) );
1460 release_object( thread
);
1463 /* send the low-level hook message for a given hardware message */
1464 static int send_hook_ll_message( struct desktop
*desktop
, struct message
*hardware_msg
,
1465 const hw_input_t
*input
, struct msg_queue
*sender
)
1467 struct thread
*hook_thread
;
1468 struct msg_queue
*queue
;
1469 struct message
*msg
;
1470 timeout_t timeout
= 2000 * -10000; /* FIXME: load from registry */
1471 int id
= (input
->type
== INPUT_MOUSE
) ? WH_MOUSE_LL
: WH_KEYBOARD_LL
;
1473 if (!(hook_thread
= get_first_global_hook( id
))) return 0;
1474 if (!(queue
= hook_thread
->queue
)) return 0;
1475 if (is_queue_hung( queue
)) return 0;
1477 if (!(msg
= mem_alloc( sizeof(*msg
) ))) return 0;
1479 msg
->type
= MSG_HOOK_LL
;
1482 msg
->wparam
= hardware_msg
->msg
;
1483 msg
->time
= hardware_msg
->time
;
1484 msg
->data_size
= hardware_msg
->data_size
;
1487 if (input
->type
== INPUT_KEYBOARD
)
1489 unsigned short vkey
= input
->kbd
.vkey
;
1490 if (input
->kbd
.flags
& KEYEVENTF_UNICODE
) vkey
= VK_PACKET
;
1491 msg
->lparam
= (input
->kbd
.scan
<< 16) | vkey
;
1493 else msg
->lparam
= input
->mouse
.data
<< 16;
1495 if (!(msg
->data
= memdup( hardware_msg
->data
, hardware_msg
->data_size
)) ||
1496 !(msg
->result
= alloc_message_result( sender
, queue
, msg
, timeout
)))
1498 free_message( msg
);
1501 msg
->result
->hardware_msg
= hardware_msg
;
1502 msg
->result
->desktop
= (struct desktop
*)grab_object( desktop
);
1503 list_add_tail( &queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1504 set_queue_bits( queue
, QS_SENDMESSAGE
);
1508 /* queue a hardware message for a mouse event */
1509 static int queue_mouse_message( struct desktop
*desktop
, user_handle_t win
, const hw_input_t
*input
,
1510 unsigned int hook_flags
, struct msg_queue
*sender
)
1512 struct hardware_msg_data
*msg_data
;
1513 struct message
*msg
;
1514 unsigned int i
, time
, flags
;
1517 static const unsigned int messages
[] =
1519 WM_MOUSEMOVE
, /* 0x0001 = MOUSEEVENTF_MOVE */
1520 WM_LBUTTONDOWN
, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1521 WM_LBUTTONUP
, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1522 WM_RBUTTONDOWN
, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1523 WM_RBUTTONUP
, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1524 WM_MBUTTONDOWN
, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1525 WM_MBUTTONUP
, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1526 WM_XBUTTONDOWN
, /* 0x0080 = MOUSEEVENTF_XDOWN */
1527 WM_XBUTTONUP
, /* 0x0100 = MOUSEEVENTF_XUP */
1528 0, /* 0x0200 = unused */
1529 0, /* 0x0400 = unused */
1530 WM_MOUSEWHEEL
, /* 0x0800 = MOUSEEVENTF_WHEEL */
1531 WM_MOUSEHWHEEL
/* 0x1000 = MOUSEEVENTF_HWHEEL */
1534 desktop
->cursor
.last_change
= get_tick_count();
1535 flags
= input
->mouse
.flags
;
1536 time
= input
->mouse
.time
;
1537 if (!time
) time
= desktop
->cursor
.last_change
;
1539 if (flags
& MOUSEEVENTF_MOVE
)
1541 if (flags
& MOUSEEVENTF_ABSOLUTE
)
1545 if (flags
& ~(MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
) &&
1546 x
== desktop
->cursor
.x
&& y
== desktop
->cursor
.y
)
1547 flags
&= ~MOUSEEVENTF_MOVE
;
1551 x
= desktop
->cursor
.x
+ input
->mouse
.x
;
1552 y
= desktop
->cursor
.y
+ input
->mouse
.y
;
1557 x
= desktop
->cursor
.x
;
1558 y
= desktop
->cursor
.y
;
1561 for (i
= 0; i
< sizeof(messages
)/sizeof(messages
[0]); i
++)
1563 if (!messages
[i
]) continue;
1564 if (!(flags
& (1 << i
))) continue;
1567 if (!(msg
= mem_alloc( sizeof(*msg
) ))) return 0;
1568 if (!(msg_data
= mem_alloc( sizeof(*msg_data
) )))
1573 memset( msg_data
, 0, sizeof(*msg_data
) );
1575 msg
->type
= MSG_HARDWARE
;
1576 msg
->win
= get_user_full_handle( win
);
1577 msg
->msg
= messages
[i
];
1578 msg
->wparam
= input
->mouse
.data
<< 16;
1582 msg
->data
= msg_data
;
1583 msg
->data_size
= sizeof(*msg_data
);
1586 msg_data
->info
= input
->mouse
.info
;
1587 if (hook_flags
& SEND_HWMSG_INJECTED
) msg_data
->flags
= LLMHF_INJECTED
;
1589 /* specify a sender only when sending the last message */
1590 if (!(flags
& ((1 << sizeof(messages
)/sizeof(messages
[0])) - 1)))
1592 if (!(wait
= send_hook_ll_message( desktop
, msg
, input
, sender
)))
1593 queue_hardware_message( desktop
, msg
, 0 );
1595 else if (!send_hook_ll_message( desktop
, msg
, input
, NULL
))
1596 queue_hardware_message( desktop
, msg
, 0 );
1601 /* queue a hardware message for a keyboard event */
1602 static int queue_keyboard_message( struct desktop
*desktop
, user_handle_t win
, const hw_input_t
*input
,
1603 unsigned int hook_flags
, struct msg_queue
*sender
)
1605 struct hardware_msg_data
*msg_data
;
1606 struct message
*msg
;
1607 unsigned char vkey
= input
->kbd
.vkey
;
1610 if (!(msg
= mem_alloc( sizeof(*msg
) ))) return 0;
1611 if (!(msg_data
= mem_alloc( sizeof(*msg_data
) )))
1616 memset( msg_data
, 0, sizeof(*msg_data
) );
1618 msg
->type
= MSG_HARDWARE
;
1619 msg
->win
= get_user_full_handle( win
);
1620 msg
->lparam
= (input
->kbd
.scan
<< 16) | 1u; /* repeat count */
1621 msg
->time
= input
->kbd
.time
;
1623 msg
->data
= msg_data
;
1624 msg
->data_size
= sizeof(*msg_data
);
1625 msg_data
->info
= input
->kbd
.info
;
1626 if (!msg
->time
) msg
->time
= get_tick_count();
1627 if (hook_flags
& SEND_HWMSG_INJECTED
) msg_data
->flags
= LLKHF_INJECTED
;
1629 if (input
->kbd
.flags
& KEYEVENTF_UNICODE
)
1631 msg
->wparam
= VK_PACKET
;
1635 unsigned int flags
= 0;
1641 vkey
= (input
->kbd
.flags
& KEYEVENTF_EXTENDEDKEY
) ? VK_RMENU
: VK_LMENU
;
1646 vkey
= (input
->kbd
.flags
& KEYEVENTF_EXTENDEDKEY
) ? VK_RCONTROL
: VK_LCONTROL
;
1651 vkey
= (input
->kbd
.flags
& KEYEVENTF_EXTENDEDKEY
) ? VK_RSHIFT
: VK_LSHIFT
;
1654 if (input
->kbd
.flags
& KEYEVENTF_EXTENDEDKEY
) flags
|= KF_EXTENDED
;
1655 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1656 if (input
->kbd
.flags
& KEYEVENTF_KEYUP
) flags
|= KF_REPEAT
| KF_UP
;
1657 else if (desktop
->keystate
[vkey
] & 0x80) flags
|= KF_REPEAT
;
1660 msg
->lparam
|= flags
<< 16;
1661 msg_data
->flags
|= (flags
& (KF_EXTENDED
| KF_ALTDOWN
| KF_UP
)) >> 8;
1664 msg
->msg
= (input
->kbd
.flags
& KEYEVENTF_KEYUP
) ? WM_KEYUP
: WM_KEYDOWN
;
1670 if (input
->kbd
.flags
& KEYEVENTF_KEYUP
)
1672 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1673 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1674 if ((desktop
->keystate
[VK_MENU
] & 0x82) != 0x82) break;
1675 msg
->msg
= WM_SYSKEYUP
;
1676 desktop
->keystate
[VK_MENU
] &= ~0x02;
1680 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1681 if (desktop
->keystate
[VK_CONTROL
] & 0x80) break;
1682 msg
->msg
= WM_SYSKEYDOWN
;
1683 desktop
->keystate
[VK_MENU
] |= 0x02;
1689 /* send WM_SYSKEYUP on release if Alt still pressed */
1690 if (!(input
->kbd
.flags
& KEYEVENTF_KEYUP
)) break;
1691 if (!(desktop
->keystate
[VK_MENU
] & 0x80)) break;
1692 msg
->msg
= WM_SYSKEYUP
;
1693 desktop
->keystate
[VK_MENU
] &= ~0x02;
1697 /* send WM_SYSKEY for Alt-anykey and for F10 */
1698 if (desktop
->keystate
[VK_CONTROL
] & 0x80) break;
1699 if (!(desktop
->keystate
[VK_MENU
] & 0x80)) break;
1702 msg
->msg
= (input
->kbd
.flags
& KEYEVENTF_KEYUP
) ? WM_SYSKEYUP
: WM_SYSKEYDOWN
;
1703 desktop
->keystate
[VK_MENU
] &= ~0x02;
1706 if (!(wait
= send_hook_ll_message( desktop
, msg
, input
, sender
)))
1707 queue_hardware_message( desktop
, msg
, 1 );
1712 /* queue a hardware message for a custom type of event */
1713 static void queue_custom_hardware_message( struct desktop
*desktop
, user_handle_t win
,
1714 const hw_input_t
*input
)
1716 struct hardware_msg_data
*msg_data
;
1717 struct message
*msg
;
1719 if (!(msg
= mem_alloc( sizeof(*msg
) ))) return;
1720 if (!(msg_data
= mem_alloc( sizeof(*msg_data
) )))
1725 memset( msg_data
, 0, sizeof(*msg_data
) );
1727 msg
->type
= MSG_HARDWARE
;
1728 msg
->win
= get_user_full_handle( win
);
1729 msg
->msg
= input
->hw
.msg
;
1731 msg
->lparam
= input
->hw
.lparam
;
1732 msg
->time
= get_tick_count();
1734 msg
->data
= msg_data
;
1735 msg
->data_size
= sizeof(*msg_data
);
1737 queue_hardware_message( desktop
, msg
, 1 );
1740 /* check message filter for a hardware message */
1741 static int check_hw_message_filter( user_handle_t win
, unsigned int msg_code
,
1742 user_handle_t filter_win
, unsigned int first
, unsigned int last
)
1744 if (msg_code
>= WM_KEYFIRST
&& msg_code
<= WM_KEYLAST
)
1746 /* we can only test the window for a keyboard message since the
1747 * dest window for a mouse message depends on hittest */
1748 if (filter_win
&& win
!= filter_win
&& !is_child_window( filter_win
, win
))
1750 /* the message code is final for a keyboard message, we can simply check it */
1751 return check_msg_filter( msg_code
, first
, last
);
1753 else /* mouse message */
1755 /* we need to check all possible values that the message can have in the end */
1757 if (check_msg_filter( msg_code
, first
, last
)) return 1;
1758 if (msg_code
== WM_MOUSEWHEEL
) return 0; /* no other possible value for this one */
1760 /* all other messages can become non-client messages */
1761 if (check_msg_filter( msg_code
+ (WM_NCMOUSEFIRST
- WM_MOUSEFIRST
), first
, last
)) return 1;
1763 /* clicks can become double-clicks or non-client double-clicks */
1764 if (msg_code
== WM_LBUTTONDOWN
|| msg_code
== WM_MBUTTONDOWN
||
1765 msg_code
== WM_RBUTTONDOWN
|| msg_code
== WM_XBUTTONDOWN
)
1767 if (check_msg_filter( msg_code
+ (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
1768 if (check_msg_filter( msg_code
+ (WM_NCLBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
1775 /* find a hardware message for the given queue */
1776 static int get_hardware_message( struct thread
*thread
, unsigned int hw_id
, user_handle_t filter_win
,
1777 unsigned int first
, unsigned int last
, struct get_message_reply
*reply
)
1779 struct thread_input
*input
= thread
->queue
->input
;
1780 struct thread
*win_thread
;
1783 int clear_bits
, got_one
= 0;
1784 unsigned int msg_code
;
1786 ptr
= list_head( &input
->msg_list
);
1791 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1792 if (msg
->unique_id
== hw_id
) break;
1793 ptr
= list_next( &input
->msg_list
, ptr
);
1795 if (!ptr
) ptr
= list_head( &input
->msg_list
);
1796 else ptr
= list_next( &input
->msg_list
, ptr
); /* start from the next one */
1799 if (ptr
== list_head( &input
->msg_list
))
1800 clear_bits
= QS_KEY
| QS_MOUSEMOVE
| QS_MOUSEBUTTON
;
1802 clear_bits
= 0; /* don't clear bits if we don't go through the whole list */
1806 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1807 struct hardware_msg_data
*data
= msg
->data
;
1809 ptr
= list_next( &input
->msg_list
, ptr
);
1810 win
= find_hardware_message_window( input
->desktop
, input
, msg
, &msg_code
);
1811 if (!win
|| !(win_thread
= get_window_thread( win
)))
1813 /* no window at all, remove it */
1814 update_input_key_state( input
->desktop
, input
->keystate
, msg
);
1815 list_remove( &msg
->entry
);
1816 free_message( msg
);
1819 if (win_thread
!= thread
)
1821 if (win_thread
->queue
->input
== input
)
1823 /* wake the other thread */
1824 set_queue_bits( win_thread
->queue
, get_hardware_msg_bit(msg
) );
1829 /* for another thread input, drop it */
1830 update_input_key_state( input
->desktop
, input
->keystate
, msg
);
1831 list_remove( &msg
->entry
);
1832 free_message( msg
);
1834 release_object( win_thread
);
1837 release_object( win_thread
);
1839 /* if we already got a message for another thread, or if it doesn't
1840 * match the filter we skip it */
1841 if (got_one
|| !check_hw_message_filter( win
, msg_code
, filter_win
, first
, last
))
1843 clear_bits
&= ~get_hardware_msg_bit( msg
);
1846 /* now we can return it */
1847 if (!msg
->unique_id
) msg
->unique_id
= get_unique_id();
1848 reply
->type
= MSG_HARDWARE
;
1850 reply
->msg
= msg_code
;
1851 reply
->wparam
= msg
->wparam
;
1852 reply
->lparam
= msg
->lparam
;
1853 reply
->time
= msg
->time
;
1855 data
->hw_id
= msg
->unique_id
;
1856 set_reply_data( msg
->data
, msg
->data_size
);
1859 /* nothing found, clear the hardware queue bits */
1860 clear_queue_bits( thread
->queue
, clear_bits
);
1864 /* increment (or decrement if 'incr' is negative) the queue paint count */
1865 void inc_queue_paint_count( struct thread
*thread
, int incr
)
1867 struct msg_queue
*queue
= thread
->queue
;
1871 if ((queue
->paint_count
+= incr
) < 0) queue
->paint_count
= 0;
1873 if (queue
->paint_count
)
1874 set_queue_bits( queue
, QS_PAINT
);
1876 clear_queue_bits( queue
, QS_PAINT
);
1880 /* remove all messages and timers belonging to a certain window */
1881 void queue_cleanup_window( struct thread
*thread
, user_handle_t win
)
1883 struct msg_queue
*queue
= thread
->queue
;
1891 ptr
= list_head( &queue
->pending_timers
);
1894 struct list
*next
= list_next( &queue
->pending_timers
, ptr
);
1895 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1896 if (timer
->win
== win
) free_timer( queue
, timer
);
1899 ptr
= list_head( &queue
->expired_timers
);
1902 struct list
*next
= list_next( &queue
->expired_timers
, ptr
);
1903 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1904 if (timer
->win
== win
) free_timer( queue
, timer
);
1908 /* remove messages */
1909 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
1911 struct list
*ptr
, *next
;
1913 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->msg_list
[i
] )
1915 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1916 if (msg
->win
== win
) remove_queue_message( queue
, msg
, i
);
1920 thread_input_cleanup_window( queue
, win
);
1923 /* post a message to a window; used by socket handling */
1924 void post_message( user_handle_t win
, unsigned int message
, lparam_t wparam
, lparam_t lparam
)
1926 struct message
*msg
;
1927 struct thread
*thread
= get_window_thread( win
);
1929 if (!thread
) return;
1931 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1933 msg
->type
= MSG_POSTED
;
1934 msg
->win
= get_user_full_handle( win
);
1936 msg
->wparam
= wparam
;
1937 msg
->lparam
= lparam
;
1938 msg
->time
= get_tick_count();
1943 list_add_tail( &thread
->queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1944 set_queue_bits( thread
->queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
1946 release_object( thread
);
1949 /* post a win event */
1950 void post_win_event( struct thread
*thread
, unsigned int event
,
1951 user_handle_t win
, unsigned int object_id
,
1952 unsigned int child_id
, client_ptr_t hook_proc
,
1953 const WCHAR
*module
, data_size_t module_size
,
1956 struct message
*msg
;
1958 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1960 struct winevent_msg_data
*data
;
1962 msg
->type
= MSG_WINEVENT
;
1963 msg
->win
= get_user_full_handle( win
);
1965 msg
->wparam
= object_id
;
1966 msg
->lparam
= child_id
;
1967 msg
->time
= get_tick_count();
1970 if ((data
= malloc( sizeof(*data
) + module_size
)))
1973 data
->tid
= get_thread_id( current
);
1974 data
->hook_proc
= hook_proc
;
1975 memcpy( data
+ 1, module
, module_size
);
1978 msg
->data_size
= sizeof(*data
) + module_size
;
1980 if (debug_level
> 1)
1981 fprintf( stderr
, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
1982 get_thread_id(thread
), event
, win
, object_id
, child_id
);
1983 list_add_tail( &thread
->queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1984 set_queue_bits( thread
->queue
, QS_SENDMESSAGE
);
1991 /* free all hotkeys on a desktop, optionally filtering by window */
1992 void free_hotkeys( struct desktop
*desktop
, user_handle_t window
)
1994 struct hotkey
*hotkey
, *hotkey2
;
1996 LIST_FOR_EACH_ENTRY_SAFE( hotkey
, hotkey2
, &desktop
->hotkeys
, struct hotkey
, entry
)
1998 if (!window
|| hotkey
->win
== window
)
2000 list_remove( &hotkey
->entry
);
2007 /* check if the thread owning the window is hung */
2008 DECL_HANDLER(is_window_hung
)
2010 struct thread
*thread
;
2012 thread
= get_window_thread( req
->win
);
2015 reply
->is_hung
= is_queue_hung( thread
->queue
);
2016 release_object( thread
);
2018 else reply
->is_hung
= 0;
2022 /* get the message queue of the current thread */
2023 DECL_HANDLER(get_msg_queue
)
2025 struct msg_queue
*queue
= get_current_queue();
2028 if (queue
) reply
->handle
= alloc_handle( current
->process
, queue
, SYNCHRONIZE
, 0 );
2032 /* set the file descriptor associated to the current thread queue */
2033 DECL_HANDLER(set_queue_fd
)
2035 struct msg_queue
*queue
= get_current_queue();
2039 if (queue
->fd
) /* fd can only be set once */
2041 set_error( STATUS_ACCESS_DENIED
);
2044 if (!(file
= get_file_obj( current
->process
, req
->handle
, SYNCHRONIZE
))) return;
2046 if ((unix_fd
= get_file_unix_fd( file
)) != -1)
2048 if ((unix_fd
= dup( unix_fd
)) != -1)
2049 queue
->fd
= create_anonymous_fd( &msg_queue_fd_ops
, unix_fd
, &queue
->obj
, 0 );
2053 release_object( file
);
2057 /* set the current message queue wakeup mask */
2058 DECL_HANDLER(set_queue_mask
)
2060 struct msg_queue
*queue
= get_current_queue();
2064 queue
->wake_mask
= req
->wake_mask
;
2065 queue
->changed_mask
= req
->changed_mask
;
2066 reply
->wake_bits
= queue
->wake_bits
;
2067 reply
->changed_bits
= queue
->changed_bits
;
2068 if (is_signaled( queue
))
2070 /* if skip wait is set, do what would have been done in the subsequent wait */
2071 if (req
->skip_wait
) msg_queue_satisfied( &queue
->obj
, current
);
2072 else wake_up( &queue
->obj
, 0 );
2078 /* get the current message queue status */
2079 DECL_HANDLER(get_queue_status
)
2081 struct msg_queue
*queue
= current
->queue
;
2084 reply
->wake_bits
= queue
->wake_bits
;
2085 reply
->changed_bits
= queue
->changed_bits
;
2086 if (req
->clear
) queue
->changed_bits
= 0;
2088 else reply
->wake_bits
= reply
->changed_bits
= 0;
2092 /* send a message to a thread queue */
2093 DECL_HANDLER(send_message
)
2095 struct message
*msg
;
2096 struct msg_queue
*send_queue
= get_current_queue();
2097 struct msg_queue
*recv_queue
= NULL
;
2098 struct thread
*thread
= NULL
;
2100 if (!(thread
= get_thread_from_id( req
->id
))) return;
2102 if (!(recv_queue
= thread
->queue
))
2104 set_error( STATUS_INVALID_PARAMETER
);
2105 release_object( thread
);
2108 if ((req
->flags
& SEND_MSG_ABORT_IF_HUNG
) && is_queue_hung(recv_queue
))
2110 set_error( STATUS_TIMEOUT
);
2111 release_object( thread
);
2115 if ((msg
= mem_alloc( sizeof(*msg
) )))
2117 msg
->type
= req
->type
;
2118 msg
->win
= get_user_full_handle( req
->win
);
2119 msg
->msg
= req
->msg
;
2120 msg
->wparam
= req
->wparam
;
2121 msg
->lparam
= req
->lparam
;
2122 msg
->time
= get_tick_count();
2125 msg
->data_size
= get_req_data_size();
2127 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
2130 release_object( thread
);
2136 case MSG_OTHER_PROCESS
:
2140 if (!(msg
->result
= alloc_message_result( send_queue
, recv_queue
, msg
, req
->timeout
)))
2142 free_message( msg
);
2147 list_add_tail( &recv_queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
2148 set_queue_bits( recv_queue
, QS_SENDMESSAGE
);
2151 list_add_tail( &recv_queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
2152 set_queue_bits( recv_queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
2154 case MSG_HARDWARE
: /* should use send_hardware_message instead */
2155 case MSG_CALLBACK_RESULT
: /* cannot send this one */
2156 case MSG_HOOK_LL
: /* generated internally */
2158 set_error( STATUS_INVALID_PARAMETER
);
2163 release_object( thread
);
2166 /* send a hardware message to a thread queue */
2167 DECL_HANDLER(send_hardware_message
)
2169 struct thread
*thread
= NULL
;
2170 struct desktop
*desktop
;
2171 struct msg_queue
*sender
= get_current_queue();
2175 if (!(thread
= get_window_thread( req
->win
))) return;
2176 desktop
= (struct desktop
*)grab_object( thread
->queue
->input
->desktop
);
2178 else if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2180 switch (req
->input
.type
)
2183 reply
->wait
= queue_mouse_message( desktop
, req
->win
, &req
->input
, req
->flags
, sender
);
2185 case INPUT_KEYBOARD
:
2186 reply
->wait
= queue_keyboard_message( desktop
, req
->win
, &req
->input
, req
->flags
, sender
);
2188 case INPUT_HARDWARE
:
2189 queue_custom_hardware_message( desktop
, req
->win
, &req
->input
);
2192 set_error( STATUS_INVALID_PARAMETER
);
2194 if (thread
) release_object( thread
);
2195 release_object( desktop
);
2198 /* post a quit message to the current queue */
2199 DECL_HANDLER(post_quit_message
)
2201 struct msg_queue
*queue
= get_current_queue();
2206 queue
->quit_message
= 1;
2207 queue
->exit_code
= req
->exit_code
;
2208 set_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
2211 /* get a message from the current queue */
2212 DECL_HANDLER(get_message
)
2214 struct timer
*timer
;
2216 struct msg_queue
*queue
= get_current_queue();
2217 user_handle_t get_win
= get_user_full_handle( req
->get_win
);
2218 unsigned int filter
= req
->flags
>> 16;
2220 reply
->active_hooks
= get_active_hooks();
2223 queue
->last_get_msg
= current_time
;
2224 if (!filter
) filter
= QS_ALLINPUT
;
2226 /* first check for sent messages */
2227 if ((ptr
= list_head( &queue
->msg_list
[SEND_MESSAGE
] )))
2229 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
2230 receive_message( queue
, msg
, reply
);
2234 /* clear changed bits so we can wait on them if we don't find a message */
2235 if (filter
& QS_POSTMESSAGE
)
2237 queue
->changed_bits
&= ~(QS_POSTMESSAGE
| QS_HOTKEY
| QS_TIMER
);
2238 if (req
->get_first
== 0 && req
->get_last
== ~0U) queue
->changed_bits
&= ~QS_ALLPOSTMESSAGE
;
2240 if (filter
& QS_INPUT
) queue
->changed_bits
&= ~QS_INPUT
;
2241 if (filter
& QS_PAINT
) queue
->changed_bits
&= ~QS_PAINT
;
2243 /* then check for posted messages */
2244 if ((filter
& QS_POSTMESSAGE
) &&
2245 get_posted_message( queue
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
2248 /* only check for quit messages if not posted messages pending.
2249 * note: the quit message isn't filtered */
2250 if (get_quit_message( queue
, req
->flags
, reply
))
2253 /* then check for any raw hardware message */
2254 if ((filter
& QS_INPUT
) &&
2255 filter_contains_hw_range( req
->get_first
, req
->get_last
) &&
2256 get_hardware_message( current
, req
->hw_id
, get_win
, req
->get_first
, req
->get_last
, reply
))
2259 /* now check for WM_PAINT */
2260 if ((filter
& QS_PAINT
) &&
2261 queue
->paint_count
&&
2262 check_msg_filter( WM_PAINT
, req
->get_first
, req
->get_last
) &&
2263 (reply
->win
= find_window_to_repaint( get_win
, current
)))
2265 reply
->type
= MSG_POSTED
;
2266 reply
->msg
= WM_PAINT
;
2269 reply
->time
= get_tick_count();
2273 /* now check for timer */
2274 if ((filter
& QS_TIMER
) &&
2275 (timer
= find_expired_timer( queue
, get_win
, req
->get_first
,
2276 req
->get_last
, (req
->flags
& PM_REMOVE
) )))
2278 reply
->type
= MSG_POSTED
;
2279 reply
->win
= timer
->win
;
2280 reply
->msg
= timer
->msg
;
2281 reply
->wparam
= timer
->id
;
2282 reply
->lparam
= timer
->lparam
;
2283 reply
->time
= get_tick_count();
2284 if (!(req
->flags
& PM_NOYIELD
) && current
->process
->idle_event
)
2285 set_event( current
->process
->idle_event
);
2289 if (get_win
== -1 && current
->process
->idle_event
) set_event( current
->process
->idle_event
);
2290 queue
->wake_mask
= req
->wake_mask
;
2291 queue
->changed_mask
= req
->changed_mask
;
2292 set_error( STATUS_PENDING
); /* FIXME */
2296 /* reply to a sent message */
2297 DECL_HANDLER(reply_message
)
2299 if (!current
->queue
) set_error( STATUS_ACCESS_DENIED
);
2300 else if (current
->queue
->recv_result
)
2301 reply_message( current
->queue
, req
->result
, 0, req
->remove
,
2302 get_req_data(), get_req_data_size() );
2306 /* accept the current hardware message */
2307 DECL_HANDLER(accept_hardware_message
)
2310 release_hardware_message( current
->queue
, req
->hw_id
, req
->remove
, req
->new_win
);
2312 set_error( STATUS_ACCESS_DENIED
);
2316 /* retrieve the reply for the last message sent */
2317 DECL_HANDLER(get_message_reply
)
2319 struct message_result
*result
;
2321 struct msg_queue
*queue
= current
->queue
;
2325 set_error( STATUS_PENDING
);
2328 if (!(entry
= list_head( &queue
->send_result
))) return; /* no reply ready */
2330 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
2331 if (result
->replied
|| req
->cancel
)
2333 if (result
->replied
)
2335 reply
->result
= result
->result
;
2336 set_error( result
->error
);
2339 data_size_t data_len
= min( result
->data_size
, get_reply_max_size() );
2340 set_reply_data_ptr( result
->data
, data_len
);
2341 result
->data
= NULL
;
2342 result
->data_size
= 0;
2345 remove_result_from_sender( result
);
2347 entry
= list_head( &queue
->send_result
);
2348 if (!entry
) clear_queue_bits( queue
, QS_SMRESULT
);
2351 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
2352 if (!result
->replied
) clear_queue_bits( queue
, QS_SMRESULT
);
2356 else set_error( STATUS_ACCESS_DENIED
);
2360 /* set a window timer */
2361 DECL_HANDLER(set_win_timer
)
2363 struct timer
*timer
;
2364 struct msg_queue
*queue
;
2365 struct thread
*thread
= NULL
;
2366 user_handle_t win
= 0;
2367 lparam_t id
= req
->id
;
2371 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
2373 set_error( STATUS_INVALID_HANDLE
);
2376 if (thread
->process
!= current
->process
)
2378 release_object( thread
);
2379 set_error( STATUS_ACCESS_DENIED
);
2382 queue
= thread
->queue
;
2383 /* remove it if it existed already */
2384 if ((timer
= find_timer( queue
, win
, req
->msg
, id
))) free_timer( queue
, timer
);
2388 queue
= get_current_queue();
2389 /* look for a timer with this id */
2390 if (id
&& (timer
= find_timer( queue
, 0, req
->msg
, id
)))
2392 /* free and reuse id */
2393 free_timer( queue
, timer
);
2397 /* find a free id for it */
2400 id
= queue
->next_timer_id
;
2401 if (--queue
->next_timer_id
<= 0x100) queue
->next_timer_id
= 0x7fff;
2403 while (find_timer( queue
, 0, req
->msg
, id
));
2407 if ((timer
= set_timer( queue
, req
->rate
)))
2410 timer
->msg
= req
->msg
;
2412 timer
->lparam
= req
->lparam
;
2415 if (thread
) release_object( thread
);
2418 /* kill a window timer */
2419 DECL_HANDLER(kill_win_timer
)
2421 struct timer
*timer
;
2422 struct thread
*thread
;
2423 user_handle_t win
= 0;
2427 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
2429 set_error( STATUS_INVALID_HANDLE
);
2432 if (thread
->process
!= current
->process
)
2434 release_object( thread
);
2435 set_error( STATUS_ACCESS_DENIED
);
2439 else thread
= (struct thread
*)grab_object( current
);
2441 if (thread
->queue
&& (timer
= find_timer( thread
->queue
, win
, req
->msg
, req
->id
)))
2442 free_timer( thread
->queue
, timer
);
2444 set_error( STATUS_INVALID_PARAMETER
);
2446 release_object( thread
);
2449 DECL_HANDLER(register_hotkey
)
2451 struct desktop
*desktop
;
2452 user_handle_t win_handle
= req
->window
;
2453 struct hotkey
*hotkey
;
2454 struct hotkey
*new_hotkey
= NULL
;
2455 struct thread
*thread
;
2456 const int modifier_flags
= MOD_ALT
|MOD_CONTROL
|MOD_SHIFT
|MOD_WIN
;
2458 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2462 if (!get_user_object_handle( &win_handle
, USER_WINDOW
))
2464 release_object( desktop
);
2465 set_win32_error( ERROR_INVALID_WINDOW_HANDLE
);
2469 thread
= get_window_thread( win_handle
);
2470 if (thread
) release_object( thread
);
2472 if (thread
!= current
)
2474 release_object( desktop
);
2475 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD
);
2480 LIST_FOR_EACH_ENTRY( hotkey
, &desktop
->hotkeys
, struct hotkey
, entry
)
2482 if (req
->vkey
== hotkey
->vkey
&&
2483 (req
->flags
& modifier_flags
) == (hotkey
->flags
& modifier_flags
))
2485 release_object( desktop
);
2486 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED
);
2489 if (current
->queue
== hotkey
->queue
&& win_handle
== hotkey
->win
&& req
->id
== hotkey
->id
)
2490 new_hotkey
= hotkey
;
2495 reply
->replaced
= 1;
2496 reply
->flags
= new_hotkey
->flags
;
2497 reply
->vkey
= new_hotkey
->vkey
;
2501 new_hotkey
= mem_alloc( sizeof(*new_hotkey
) );
2504 list_add_tail( &desktop
->hotkeys
, &new_hotkey
->entry
);
2505 new_hotkey
->queue
= current
->queue
;
2506 new_hotkey
->win
= win_handle
;
2507 new_hotkey
->id
= req
->id
;
2513 new_hotkey
->flags
= req
->flags
;
2514 new_hotkey
->vkey
= req
->vkey
;
2517 release_object( desktop
);
2520 DECL_HANDLER(unregister_hotkey
)
2522 struct desktop
*desktop
;
2523 user_handle_t win_handle
= req
->window
;
2524 struct hotkey
*hotkey
;
2525 struct thread
*thread
;
2527 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2531 if (!get_user_object_handle( &win_handle
, USER_WINDOW
))
2533 release_object( desktop
);
2534 set_win32_error( ERROR_INVALID_WINDOW_HANDLE
);
2538 thread
= get_window_thread( win_handle
);
2539 if (thread
) release_object( thread
);
2541 if (thread
!= current
)
2543 release_object( desktop
);
2544 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD
);
2549 LIST_FOR_EACH_ENTRY( hotkey
, &desktop
->hotkeys
, struct hotkey
, entry
)
2551 if (current
->queue
== hotkey
->queue
&& win_handle
== hotkey
->win
&& req
->id
== hotkey
->id
)
2555 release_object( desktop
);
2556 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED
);
2560 reply
->flags
= hotkey
->flags
;
2561 reply
->vkey
= hotkey
->vkey
;
2562 list_remove( &hotkey
->entry
);
2564 release_object( desktop
);
2567 /* attach (or detach) thread inputs */
2568 DECL_HANDLER(attach_thread_input
)
2570 struct thread
*thread_from
= get_thread_from_id( req
->tid_from
);
2571 struct thread
*thread_to
= get_thread_from_id( req
->tid_to
);
2573 if (!thread_from
|| !thread_to
)
2575 if (thread_from
) release_object( thread_from
);
2576 if (thread_to
) release_object( thread_to
);
2579 if (thread_from
!= thread_to
)
2581 if (req
->attach
) attach_thread_input( thread_from
, thread_to
);
2584 if (thread_from
->queue
&& thread_to
->queue
&&
2585 thread_from
->queue
->input
== thread_to
->queue
->input
)
2586 detach_thread_input( thread_from
);
2588 set_error( STATUS_ACCESS_DENIED
);
2591 else set_error( STATUS_ACCESS_DENIED
);
2592 release_object( thread_from
);
2593 release_object( thread_to
);
2597 /* get thread input data */
2598 DECL_HANDLER(get_thread_input
)
2600 struct thread
*thread
= NULL
;
2601 struct desktop
*desktop
;
2602 struct thread_input
*input
;
2606 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2607 if (!(desktop
= get_thread_desktop( thread
, 0 )))
2609 release_object( thread
);
2612 input
= thread
->queue
? thread
->queue
->input
: NULL
;
2616 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2617 input
= desktop
->foreground_input
; /* get the foreground thread info */
2622 reply
->focus
= input
->focus
;
2623 reply
->capture
= input
->capture
;
2624 reply
->active
= input
->active
;
2625 reply
->menu_owner
= input
->menu_owner
;
2626 reply
->move_size
= input
->move_size
;
2627 reply
->caret
= input
->caret
;
2628 reply
->cursor
= input
->cursor
;
2629 reply
->show_count
= input
->cursor_count
;
2630 reply
->rect
= input
->caret_rect
;
2633 /* foreground window is active window of foreground thread */
2634 reply
->foreground
= desktop
->foreground_input
? desktop
->foreground_input
->active
: 0;
2635 if (thread
) release_object( thread
);
2636 release_object( desktop
);
2640 /* retrieve queue keyboard state for a given thread */
2641 DECL_HANDLER(get_key_state
)
2643 struct thread
*thread
;
2644 struct desktop
*desktop
;
2645 data_size_t size
= min( 256, get_reply_max_size() );
2647 if (!req
->tid
) /* get global async key state */
2649 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2652 reply
->state
= desktop
->keystate
[req
->key
& 0xff];
2653 desktop
->keystate
[req
->key
& 0xff] &= ~0x40;
2655 set_reply_data( desktop
->keystate
, size
);
2656 release_object( desktop
);
2660 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2663 if (req
->key
>= 0) reply
->state
= thread
->queue
->input
->keystate
[req
->key
& 0xff];
2664 set_reply_data( thread
->queue
->input
->keystate
, size
);
2666 release_object( thread
);
2671 /* set queue keyboard state for a given thread */
2672 DECL_HANDLER(set_key_state
)
2674 struct thread
*thread
;
2675 struct desktop
*desktop
;
2676 data_size_t size
= min( 256, get_req_data_size() );
2678 if (!req
->tid
) /* set global async key state */
2680 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2681 memcpy( desktop
->keystate
, get_req_data(), size
);
2682 release_object( desktop
);
2686 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2687 if (thread
->queue
) memcpy( thread
->queue
->input
->keystate
, get_req_data(), size
);
2688 release_object( thread
);
2693 /* set the system foreground window */
2694 DECL_HANDLER(set_foreground_window
)
2696 struct thread
*thread
= NULL
;
2697 struct desktop
*desktop
;
2698 struct msg_queue
*queue
= get_current_queue();
2700 if (!(desktop
= get_thread_desktop( current
, 0 ))) return;
2701 reply
->previous
= desktop
->foreground_input
? desktop
->foreground_input
->active
: 0;
2702 reply
->send_msg_old
= (reply
->previous
&& desktop
->foreground_input
!= queue
->input
);
2703 reply
->send_msg_new
= FALSE
;
2705 if (is_top_level_window( req
->handle
) &&
2706 ((thread
= get_window_thread( req
->handle
))) &&
2707 (thread
->queue
->input
->desktop
== desktop
))
2709 set_foreground_input( desktop
, thread
->queue
->input
);
2710 reply
->send_msg_new
= (desktop
->foreground_input
!= queue
->input
);
2712 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE
);
2714 if (thread
) release_object( thread
);
2715 release_object( desktop
);
2719 /* set the current thread focus window */
2720 DECL_HANDLER(set_focus_window
)
2722 struct msg_queue
*queue
= get_current_queue();
2724 reply
->previous
= 0;
2725 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2727 reply
->previous
= queue
->input
->focus
;
2728 queue
->input
->focus
= get_user_full_handle( req
->handle
);
2733 /* set the current thread active window */
2734 DECL_HANDLER(set_active_window
)
2736 struct msg_queue
*queue
= get_current_queue();
2738 reply
->previous
= 0;
2739 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2741 if (!req
->handle
|| make_window_active( req
->handle
))
2743 reply
->previous
= queue
->input
->active
;
2744 queue
->input
->active
= get_user_full_handle( req
->handle
);
2746 else set_error( STATUS_INVALID_HANDLE
);
2751 /* set the current thread capture window */
2752 DECL_HANDLER(set_capture_window
)
2754 struct msg_queue
*queue
= get_current_queue();
2756 reply
->previous
= reply
->full_handle
= 0;
2757 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2759 struct thread_input
*input
= queue
->input
;
2761 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2762 if (input
->menu_owner
&& !(req
->flags
& CAPTURE_MENU
))
2764 set_error(STATUS_ACCESS_DENIED
);
2767 reply
->previous
= input
->capture
;
2768 input
->capture
= get_user_full_handle( req
->handle
);
2769 input
->menu_owner
= (req
->flags
& CAPTURE_MENU
) ? input
->capture
: 0;
2770 input
->move_size
= (req
->flags
& CAPTURE_MOVESIZE
) ? input
->capture
: 0;
2771 reply
->full_handle
= input
->capture
;
2776 /* Set the current thread caret window */
2777 DECL_HANDLER(set_caret_window
)
2779 struct msg_queue
*queue
= get_current_queue();
2781 reply
->previous
= 0;
2782 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2784 struct thread_input
*input
= queue
->input
;
2786 reply
->previous
= input
->caret
;
2787 reply
->old_rect
= input
->caret_rect
;
2788 reply
->old_hide
= input
->caret_hide
;
2789 reply
->old_state
= input
->caret_state
;
2791 set_caret_window( input
, get_user_full_handle(req
->handle
) );
2792 input
->caret_rect
.right
= input
->caret_rect
.left
+ req
->width
;
2793 input
->caret_rect
.bottom
= input
->caret_rect
.top
+ req
->height
;
2798 /* Set the current thread caret information */
2799 DECL_HANDLER(set_caret_info
)
2801 struct msg_queue
*queue
= get_current_queue();
2802 struct thread_input
*input
;
2805 input
= queue
->input
;
2806 reply
->full_handle
= input
->caret
;
2807 reply
->old_rect
= input
->caret_rect
;
2808 reply
->old_hide
= input
->caret_hide
;
2809 reply
->old_state
= input
->caret_state
;
2811 if (req
->handle
&& get_user_full_handle(req
->handle
) != input
->caret
)
2813 set_error( STATUS_ACCESS_DENIED
);
2816 if (req
->flags
& SET_CARET_POS
)
2818 input
->caret_rect
.right
+= req
->x
- input
->caret_rect
.left
;
2819 input
->caret_rect
.bottom
+= req
->y
- input
->caret_rect
.top
;
2820 input
->caret_rect
.left
= req
->x
;
2821 input
->caret_rect
.top
= req
->y
;
2823 if (req
->flags
& SET_CARET_HIDE
)
2825 input
->caret_hide
+= req
->hide
;
2826 if (input
->caret_hide
< 0) input
->caret_hide
= 0;
2828 if (req
->flags
& SET_CARET_STATE
)
2830 if (req
->state
== -1) input
->caret_state
= !input
->caret_state
;
2831 else input
->caret_state
= !!req
->state
;
2836 /* get the time of the last input event */
2837 DECL_HANDLER(get_last_input_time
)
2839 reply
->time
= last_input_time
;
2842 /* set/get the current cursor */
2843 DECL_HANDLER(set_cursor
)
2845 struct msg_queue
*queue
= get_current_queue();
2846 struct thread_input
*input
;
2849 input
= queue
->input
;
2851 reply
->prev_handle
= input
->cursor
;
2852 reply
->prev_count
= input
->cursor_count
;
2853 reply
->prev_x
= input
->desktop
->cursor
.x
;
2854 reply
->prev_y
= input
->desktop
->cursor
.y
;
2856 if (req
->flags
& SET_CURSOR_HANDLE
)
2858 if (req
->handle
&& !get_user_object( req
->handle
, USER_CLIENT
))
2860 set_win32_error( ERROR_INVALID_CURSOR_HANDLE
);
2863 input
->cursor
= req
->handle
;
2865 if (req
->flags
& SET_CURSOR_COUNT
)
2867 queue
->cursor_count
+= req
->show_count
;
2868 input
->cursor_count
+= req
->show_count
;
2870 if (req
->flags
& SET_CURSOR_POS
)
2872 set_cursor_pos( input
->desktop
, req
->x
, req
->y
);
2874 if (req
->flags
& (SET_CURSOR_CLIP
| SET_CURSOR_NOCLIP
))
2876 struct desktop
*desktop
= input
->desktop
;
2878 /* only the desktop owner can set the message */
2879 if (req
->clip_msg
&& get_top_window_owner(desktop
) == current
->process
)
2880 desktop
->cursor
.clip_msg
= req
->clip_msg
;
2882 set_clip_rectangle( desktop
, (req
->flags
& SET_CURSOR_NOCLIP
) ? NULL
: &req
->clip
);
2885 reply
->new_x
= input
->desktop
->cursor
.x
;
2886 reply
->new_y
= input
->desktop
->cursor
.y
;
2887 reply
->new_clip
= input
->desktop
->cursor
.clip
;
2888 reply
->last_change
= input
->desktop
->cursor
.last_change
;