mshtml: Fix DOCUMENT_NODE handling in nsnode_to_nsstring_rec.
[wine/multimedia.git] / server / queue.c
blob03b0e92b08e0d157a02deb66173c5f970ccbe774
1 /*
2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "winternl.h"
37 #include "handle.h"
38 #include "file.h"
39 #include "thread.h"
40 #include "process.h"
41 #include "request.h"
42 #include "user.h"
44 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
45 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
47 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
48 #define NB_MSG_KINDS (POST_MESSAGE+1)
51 struct message_result
53 struct list sender_entry; /* entry in sender list */
54 struct message *msg; /* message the result is for */
55 struct message_result *recv_next; /* next in receiver list */
56 struct msg_queue *sender; /* sender queue */
57 struct msg_queue *receiver; /* receiver queue */
58 int replied; /* has it been replied to? */
59 unsigned int error; /* error code to pass back to sender */
60 lparam_t result; /* reply result */
61 struct message *hardware_msg; /* hardware message if low-level hook result */
62 struct desktop *desktop; /* desktop for hardware message */
63 struct message *callback_msg; /* message to queue for callback */
64 void *data; /* message reply data */
65 unsigned int data_size; /* size of message reply data */
66 struct timeout_user *timeout; /* result timeout */
69 struct message
71 struct list entry; /* entry in message list */
72 enum message_type type; /* message type */
73 user_handle_t win; /* window handle */
74 unsigned int msg; /* message code */
75 lparam_t wparam; /* parameters */
76 lparam_t lparam; /* parameters */
77 unsigned int time; /* message time */
78 void *data; /* message data for sent messages */
79 unsigned int data_size; /* size of message data */
80 unsigned int unique_id; /* unique id for nested hw message waits */
81 struct message_result *result; /* result in sender queue */
84 struct timer
86 struct list entry; /* entry in timer list */
87 timeout_t when; /* next expiration */
88 unsigned int rate; /* timer rate in ms */
89 user_handle_t win; /* window handle */
90 unsigned int msg; /* message to post */
91 lparam_t id; /* timer id */
92 lparam_t lparam; /* lparam for message */
95 struct thread_input
97 struct object obj; /* object header */
98 struct desktop *desktop; /* desktop that this thread input belongs to */
99 user_handle_t focus; /* focus window */
100 user_handle_t capture; /* capture window */
101 user_handle_t active; /* active window */
102 user_handle_t menu_owner; /* current menu owner window */
103 user_handle_t move_size; /* current moving/resizing window */
104 user_handle_t caret; /* caret window */
105 rectangle_t caret_rect; /* caret rectangle */
106 int caret_hide; /* caret hide count */
107 int caret_state; /* caret on/off state */
108 user_handle_t cursor; /* current cursor */
109 int cursor_count; /* cursor show count */
110 struct list msg_list; /* list of hardware messages */
111 unsigned char keystate[256]; /* state of each key */
114 struct msg_queue
116 struct object obj; /* object header */
117 struct fd *fd; /* optional file descriptor to poll */
118 unsigned int wake_bits; /* wakeup bits */
119 unsigned int wake_mask; /* wakeup mask */
120 unsigned int changed_bits; /* changed wakeup bits */
121 unsigned int changed_mask; /* changed wakeup mask */
122 int paint_count; /* pending paint messages count */
123 int hotkey_count; /* pending hotkey messages count */
124 int quit_message; /* is there a pending quit message? */
125 int exit_code; /* exit code of pending quit message */
126 int cursor_count; /* per-queue cursor show count */
127 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
128 struct list send_result; /* stack of sent messages waiting for result */
129 struct list callback_result; /* list of callback messages waiting for result */
130 struct message_result *recv_result; /* stack of received messages waiting for result */
131 struct list pending_timers; /* list of pending timers */
132 struct list expired_timers; /* list of expired timers */
133 lparam_t next_timer_id; /* id for the next timer with a 0 window */
134 struct timeout_user *timeout; /* timeout for next timer to expire */
135 struct thread_input *input; /* thread input descriptor */
136 struct hook_table *hooks; /* hook table */
137 timeout_t last_get_msg; /* time of last get message call */
140 struct hotkey
142 struct list entry; /* entry in desktop hotkey list */
143 struct msg_queue *queue; /* queue owning this hotkey */
144 user_handle_t win; /* window handle */
145 int id; /* hotkey id */
146 unsigned int vkey; /* virtual key code */
147 unsigned int flags; /* key modifiers */
150 static void msg_queue_dump( struct object *obj, int verbose );
151 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
152 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
153 static int msg_queue_signaled( struct object *obj, struct thread *thread );
154 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
155 static void msg_queue_destroy( struct object *obj );
156 static void msg_queue_poll_event( struct fd *fd, int event );
157 static void thread_input_dump( struct object *obj, int verbose );
158 static void thread_input_destroy( struct object *obj );
159 static void timer_callback( void *private );
161 static const struct object_ops msg_queue_ops =
163 sizeof(struct msg_queue), /* size */
164 msg_queue_dump, /* dump */
165 no_get_type, /* get_type */
166 msg_queue_add_queue, /* add_queue */
167 msg_queue_remove_queue, /* remove_queue */
168 msg_queue_signaled, /* signaled */
169 msg_queue_satisfied, /* satisfied */
170 no_signal, /* signal */
171 no_get_fd, /* get_fd */
172 no_map_access, /* map_access */
173 default_get_sd, /* get_sd */
174 default_set_sd, /* set_sd */
175 no_lookup_name, /* lookup_name */
176 no_open_file, /* open_file */
177 no_close_handle, /* close_handle */
178 msg_queue_destroy /* destroy */
181 static const struct fd_ops msg_queue_fd_ops =
183 NULL, /* get_poll_events */
184 msg_queue_poll_event, /* poll_event */
185 NULL, /* flush */
186 NULL, /* get_fd_type */
187 NULL, /* ioctl */
188 NULL, /* queue_async */
189 NULL, /* reselect_async */
190 NULL /* cancel async */
194 static const struct object_ops thread_input_ops =
196 sizeof(struct thread_input), /* size */
197 thread_input_dump, /* dump */
198 no_get_type, /* get_type */
199 no_add_queue, /* add_queue */
200 NULL, /* remove_queue */
201 NULL, /* signaled */
202 NULL, /* satisfied */
203 no_signal, /* signal */
204 no_get_fd, /* get_fd */
205 no_map_access, /* map_access */
206 default_get_sd, /* get_sd */
207 default_set_sd, /* set_sd */
208 no_lookup_name, /* lookup_name */
209 no_open_file, /* open_file */
210 no_close_handle, /* close_handle */
211 thread_input_destroy /* destroy */
214 /* pointer to input structure of foreground thread */
215 static unsigned int last_input_time;
217 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
218 static void free_message( struct message *msg );
220 /* set the caret window in a given thread input */
221 static void set_caret_window( struct thread_input *input, user_handle_t win )
223 if (!win || win != input->caret)
225 input->caret_rect.left = 0;
226 input->caret_rect.top = 0;
227 input->caret_rect.right = 0;
228 input->caret_rect.bottom = 0;
230 input->caret = win;
231 input->caret_hide = 1;
232 input->caret_state = 0;
235 /* create a thread input object */
236 static struct thread_input *create_thread_input( struct thread *thread )
238 struct thread_input *input;
240 if ((input = alloc_object( &thread_input_ops )))
242 input->focus = 0;
243 input->capture = 0;
244 input->active = 0;
245 input->menu_owner = 0;
246 input->move_size = 0;
247 input->cursor = 0;
248 input->cursor_count = 0;
249 list_init( &input->msg_list );
250 set_caret_window( input, 0 );
251 memset( input->keystate, 0, sizeof(input->keystate) );
253 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
255 release_object( input );
256 return NULL;
259 return input;
262 /* create a message queue object */
263 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
265 struct thread_input *new_input = NULL;
266 struct msg_queue *queue;
267 int i;
269 if (!input)
271 if (!(new_input = create_thread_input( thread ))) return NULL;
272 input = new_input;
275 if ((queue = alloc_object( &msg_queue_ops )))
277 queue->fd = NULL;
278 queue->wake_bits = 0;
279 queue->wake_mask = 0;
280 queue->changed_bits = 0;
281 queue->changed_mask = 0;
282 queue->paint_count = 0;
283 queue->hotkey_count = 0;
284 queue->quit_message = 0;
285 queue->cursor_count = 0;
286 queue->recv_result = NULL;
287 queue->next_timer_id = 0x7fff;
288 queue->timeout = NULL;
289 queue->input = (struct thread_input *)grab_object( input );
290 queue->hooks = NULL;
291 queue->last_get_msg = current_time;
292 list_init( &queue->send_result );
293 list_init( &queue->callback_result );
294 list_init( &queue->pending_timers );
295 list_init( &queue->expired_timers );
296 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
298 thread->queue = queue;
300 if (new_input) release_object( new_input );
301 return queue;
304 /* free the message queue of a thread at thread exit */
305 void free_msg_queue( struct thread *thread )
307 remove_thread_hooks( thread );
308 if (!thread->queue) return;
309 release_object( thread->queue );
310 thread->queue = NULL;
313 /* change the thread input data of a given thread */
314 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
316 struct msg_queue *queue = thread->queue;
318 if (!queue)
320 thread->queue = create_msg_queue( thread, new_input );
321 return thread->queue != NULL;
323 if (queue->input)
325 queue->input->cursor_count -= queue->cursor_count;
326 release_object( queue->input );
328 queue->input = (struct thread_input *)grab_object( new_input );
329 new_input->cursor_count += queue->cursor_count;
330 return 1;
333 /* set the cursor position and queue the corresponding mouse message */
334 static void set_cursor_pos( struct desktop *desktop, int x, int y )
336 struct hardware_msg_data *msg_data;
337 struct message *msg;
339 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
340 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
342 free( msg );
343 return;
345 memset( msg_data, 0, sizeof(*msg_data) );
347 msg->type = MSG_HARDWARE;
348 msg->win = 0;
349 msg->msg = WM_MOUSEMOVE;
350 msg->wparam = 0;
351 msg->lparam = 0;
352 msg->time = get_tick_count();
353 msg->result = NULL;
354 msg->data = msg_data;
355 msg->data_size = sizeof(*msg_data);
356 msg_data->x = x;
357 msg_data->y = y;
358 queue_hardware_message( desktop, msg, 1 );
361 /* set the cursor clip rectangle */
362 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect )
364 rectangle_t top_rect;
365 int x, y;
367 get_top_window_rectangle( desktop, &top_rect );
368 if (rect)
370 rectangle_t new_rect = *rect;
371 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
372 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
373 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
374 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
375 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
376 desktop->cursor.clip = new_rect;
378 else desktop->cursor.clip = top_rect;
380 if (desktop->cursor.clip_msg)
381 post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
383 /* warp the mouse to be inside the clip rect */
384 x = min( max( desktop->cursor.x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
385 y = min( max( desktop->cursor.y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
386 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
389 /* change the foreground input and reset the cursor clip rect */
390 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
392 if (desktop->foreground_input == input) return;
393 set_clip_rectangle( desktop, NULL );
394 desktop->foreground_input = input;
397 /* get the hook table for a given thread */
398 struct hook_table *get_queue_hooks( struct thread *thread )
400 if (!thread->queue) return NULL;
401 return thread->queue->hooks;
404 /* set the hook table for a given thread, allocating the queue if needed */
405 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
407 struct msg_queue *queue = thread->queue;
408 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
409 if (queue->hooks) release_object( queue->hooks );
410 queue->hooks = hooks;
413 /* check the queue status */
414 static inline int is_signaled( struct msg_queue *queue )
416 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
419 /* set some queue bits */
420 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
422 queue->wake_bits |= bits;
423 queue->changed_bits |= bits;
424 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
427 /* clear some queue bits */
428 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
430 queue->wake_bits &= ~bits;
431 queue->changed_bits &= ~bits;
434 /* check whether msg is a keyboard message */
435 static inline int is_keyboard_msg( struct message *msg )
437 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
440 /* check if message is matched by the filter */
441 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
443 return (msg >= first && msg <= last);
446 /* check whether a message filter contains at least one potential hardware message */
447 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
449 /* hardware message ranges are (in numerical order):
450 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
451 * WM_KEYFIRST .. WM_KEYLAST
452 * WM_MOUSEFIRST .. WM_MOUSELAST
454 if (last < WM_NCMOUSEFIRST) return 0;
455 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
456 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
457 if (first > WM_MOUSELAST) return 0;
458 return 1;
461 /* get the QS_* bit corresponding to a given hardware message */
462 static inline int get_hardware_msg_bit( struct message *msg )
464 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
465 if (is_keyboard_msg( msg )) return QS_KEY;
466 return QS_MOUSEBUTTON;
469 /* get the current thread queue, creating it if needed */
470 static inline struct msg_queue *get_current_queue(void)
472 struct msg_queue *queue = current->queue;
473 if (!queue) queue = create_msg_queue( current, NULL );
474 return queue;
477 /* get a (pseudo-)unique id to tag hardware messages */
478 static inline unsigned int get_unique_id(void)
480 static unsigned int id;
481 if (!++id) id = 1; /* avoid an id of 0 */
482 return id;
485 /* try to merge a message with the last in the list; return 1 if successful */
486 static int merge_message( struct thread_input *input, const struct message *msg )
488 struct message *prev;
489 struct list *ptr;
491 if (msg->msg != WM_MOUSEMOVE) return 0;
492 if (!(ptr = list_tail( &input->msg_list ))) return 0;
493 prev = LIST_ENTRY( ptr, struct message, entry );
494 if (prev->result) return 0;
495 if (prev->win && msg->win && prev->win != msg->win) return 0;
496 if (prev->msg != msg->msg) return 0;
497 if (prev->type != msg->type) return 0;
498 /* now we can merge it */
499 prev->wparam = msg->wparam;
500 prev->lparam = msg->lparam;
501 prev->time = msg->time;
502 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
504 struct hardware_msg_data *prev_data = prev->data;
505 struct hardware_msg_data *msg_data = msg->data;
506 prev_data->x = msg_data->x;
507 prev_data->y = msg_data->y;
508 prev_data->info = msg_data->info;
510 return 1;
513 /* free a result structure */
514 static void free_result( struct message_result *result )
516 if (result->timeout) remove_timeout_user( result->timeout );
517 free( result->data );
518 if (result->callback_msg) free_message( result->callback_msg );
519 if (result->hardware_msg) free_message( result->hardware_msg );
520 if (result->desktop) release_object( result->desktop );
521 free( result );
524 /* remove the result from the sender list it is on */
525 static inline void remove_result_from_sender( struct message_result *result )
527 assert( result->sender );
529 list_remove( &result->sender_entry );
530 result->sender = NULL;
531 if (!result->receiver) free_result( result );
534 /* store the message result in the appropriate structure */
535 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
537 res->result = result;
538 res->error = error;
539 res->replied = 1;
540 if (res->timeout)
542 remove_timeout_user( res->timeout );
543 res->timeout = NULL;
546 if (res->hardware_msg)
548 if (!error && result) /* rejected by the hook */
549 free_message( res->hardware_msg );
550 else
551 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
553 res->hardware_msg = NULL;
556 if (res->sender)
558 if (res->callback_msg)
560 /* queue the callback message in the sender queue */
561 struct callback_msg_data *data = res->callback_msg->data;
562 data->result = result;
563 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
564 set_queue_bits( res->sender, QS_SENDMESSAGE );
565 res->callback_msg = NULL;
566 remove_result_from_sender( res );
568 else
570 /* wake sender queue if waiting on this result */
571 if (list_head(&res->sender->send_result) == &res->sender_entry)
572 set_queue_bits( res->sender, QS_SMRESULT );
575 else if (!res->receiver) free_result( res );
578 /* free a message when deleting a queue or window */
579 static void free_message( struct message *msg )
581 struct message_result *result = msg->result;
582 if (result)
584 result->msg = NULL;
585 result->receiver = NULL;
586 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
588 free( msg->data );
589 free( msg );
592 /* remove (and free) a message from a message list */
593 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
594 enum message_kind kind )
596 list_remove( &msg->entry );
597 switch(kind)
599 case SEND_MESSAGE:
600 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
601 break;
602 case POST_MESSAGE:
603 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
604 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
605 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
606 clear_queue_bits( queue, QS_HOTKEY );
607 break;
609 free_message( msg );
612 /* message timed out without getting a reply */
613 static void result_timeout( void *private )
615 struct message_result *result = private;
617 assert( !result->replied );
619 result->timeout = NULL;
621 if (result->msg) /* not received yet */
623 struct message *msg = result->msg;
625 result->msg = NULL;
626 msg->result = NULL;
627 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
628 result->receiver = NULL;
630 store_message_result( result, 0, STATUS_TIMEOUT );
633 /* allocate and fill a message result structure */
634 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
635 struct msg_queue *recv_queue,
636 struct message *msg, timeout_t timeout )
638 struct message_result *result = mem_alloc( sizeof(*result) );
639 if (result)
641 result->msg = msg;
642 result->sender = send_queue;
643 result->receiver = recv_queue;
644 result->replied = 0;
645 result->data = NULL;
646 result->data_size = 0;
647 result->timeout = NULL;
648 result->hardware_msg = NULL;
649 result->desktop = NULL;
650 result->callback_msg = NULL;
652 if (msg->type == MSG_CALLBACK)
654 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
656 if (!callback_msg)
658 free( result );
659 return NULL;
661 callback_msg->type = MSG_CALLBACK_RESULT;
662 callback_msg->win = msg->win;
663 callback_msg->msg = msg->msg;
664 callback_msg->wparam = 0;
665 callback_msg->lparam = 0;
666 callback_msg->time = get_tick_count();
667 callback_msg->result = NULL;
668 /* steal the data from the original message */
669 callback_msg->data = msg->data;
670 callback_msg->data_size = msg->data_size;
671 msg->data = NULL;
672 msg->data_size = 0;
674 result->callback_msg = callback_msg;
675 list_add_head( &send_queue->callback_result, &result->sender_entry );
677 else if (send_queue) list_add_head( &send_queue->send_result, &result->sender_entry );
679 if (timeout != TIMEOUT_INFINITE)
680 result->timeout = add_timeout_user( timeout, result_timeout, result );
682 return result;
685 /* receive a message, removing it from the sent queue */
686 static void receive_message( struct msg_queue *queue, struct message *msg,
687 struct get_message_reply *reply )
689 struct message_result *result = msg->result;
691 reply->total = msg->data_size;
692 if (msg->data_size > get_reply_max_size())
694 set_error( STATUS_BUFFER_OVERFLOW );
695 return;
697 reply->type = msg->type;
698 reply->win = msg->win;
699 reply->msg = msg->msg;
700 reply->wparam = msg->wparam;
701 reply->lparam = msg->lparam;
702 reply->time = msg->time;
704 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
706 list_remove( &msg->entry );
707 /* put the result on the receiver result stack */
708 if (result)
710 result->msg = NULL;
711 result->recv_next = queue->recv_result;
712 queue->recv_result = result;
714 free( msg );
715 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
718 /* set the result of the current received message */
719 static void reply_message( struct msg_queue *queue, lparam_t result,
720 unsigned int error, int remove, const void *data, data_size_t len )
722 struct message_result *res = queue->recv_result;
724 if (remove)
726 queue->recv_result = res->recv_next;
727 res->receiver = NULL;
728 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
730 free_result( res );
731 return;
734 if (!res->replied)
736 if (len && (res->data = memdup( data, len ))) res->data_size = len;
737 store_message_result( res, result, error );
741 static int match_window( user_handle_t win, user_handle_t msg_win )
743 if (!win) return 1;
744 if (win == -1 || win == 1) return !msg_win;
745 if (msg_win == win) return 1;
746 return is_child_window( win, msg_win );
749 /* retrieve a posted message */
750 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
751 unsigned int first, unsigned int last, unsigned int flags,
752 struct get_message_reply *reply )
754 struct message *msg;
756 /* check against the filters */
757 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
759 if (!match_window( win, msg->win )) continue;
760 if (!check_msg_filter( msg->msg, first, last )) continue;
761 goto found; /* found one */
763 return 0;
765 /* return it to the app */
766 found:
767 reply->total = msg->data_size;
768 if (msg->data_size > get_reply_max_size())
770 set_error( STATUS_BUFFER_OVERFLOW );
771 return 1;
773 reply->type = msg->type;
774 reply->win = msg->win;
775 reply->msg = msg->msg;
776 reply->wparam = msg->wparam;
777 reply->lparam = msg->lparam;
778 reply->time = msg->time;
780 if (flags & PM_REMOVE)
782 if (msg->data)
784 set_reply_data_ptr( msg->data, msg->data_size );
785 msg->data = NULL;
786 msg->data_size = 0;
788 remove_queue_message( queue, msg, POST_MESSAGE );
790 else if (msg->data) set_reply_data( msg->data, msg->data_size );
792 return 1;
795 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
796 struct get_message_reply *reply )
798 if (queue->quit_message)
800 reply->total = 0;
801 reply->type = MSG_POSTED;
802 reply->win = 0;
803 reply->msg = WM_QUIT;
804 reply->wparam = queue->exit_code;
805 reply->lparam = 0;
806 reply->time = get_tick_count();
808 if (flags & PM_REMOVE)
810 queue->quit_message = 0;
811 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
812 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
814 return 1;
816 else
817 return 0;
820 /* empty a message list and free all the messages */
821 static void empty_msg_list( struct list *list )
823 struct list *ptr;
825 while ((ptr = list_head( list )) != NULL)
827 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
828 list_remove( &msg->entry );
829 free_message( msg );
833 /* cleanup all pending results when deleting a queue */
834 static void cleanup_results( struct msg_queue *queue )
836 struct list *entry;
838 while ((entry = list_head( &queue->send_result )) != NULL)
840 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
843 while ((entry = list_head( &queue->callback_result )) != NULL)
845 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
848 while (queue->recv_result)
849 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
852 /* check if the thread owning the queue is hung (not checking for messages) */
853 static int is_queue_hung( struct msg_queue *queue )
855 struct wait_queue_entry *entry;
857 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
858 return 0; /* less than 5 seconds since last get message -> not hung */
860 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
862 if (entry->thread->queue == queue)
863 return 0; /* thread is waiting on queue -> not hung */
865 return 1;
868 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
870 struct msg_queue *queue = (struct msg_queue *)obj;
871 struct process *process = entry->thread->process;
873 /* a thread can only wait on its own queue */
874 if (entry->thread->queue != queue)
876 set_error( STATUS_ACCESS_DENIED );
877 return 0;
879 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
881 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
882 set_fd_events( queue->fd, POLLIN );
883 add_queue( obj, entry );
884 return 1;
887 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
889 struct msg_queue *queue = (struct msg_queue *)obj;
891 remove_queue( obj, entry );
892 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
893 set_fd_events( queue->fd, 0 );
896 static void msg_queue_dump( struct object *obj, int verbose )
898 struct msg_queue *queue = (struct msg_queue *)obj;
899 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
900 queue->wake_bits, queue->wake_mask );
903 static int msg_queue_signaled( struct object *obj, struct thread *thread )
905 struct msg_queue *queue = (struct msg_queue *)obj;
906 int ret = 0;
908 if (queue->fd)
910 if ((ret = check_fd_events( queue->fd, POLLIN )))
911 /* stop waiting on select() if we are signaled */
912 set_fd_events( queue->fd, 0 );
913 else if (!list_empty( &obj->wait_queue ))
914 /* restart waiting on poll() if we are no longer signaled */
915 set_fd_events( queue->fd, POLLIN );
917 return ret || is_signaled( queue );
920 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
922 struct msg_queue *queue = (struct msg_queue *)obj;
923 queue->wake_mask = 0;
924 queue->changed_mask = 0;
925 return 0; /* Not abandoned */
928 static void msg_queue_destroy( struct object *obj )
930 struct msg_queue *queue = (struct msg_queue *)obj;
931 struct list *ptr;
932 struct hotkey *hotkey, *hotkey2;
933 int i;
935 cleanup_results( queue );
936 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
938 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
940 if (hotkey->queue == queue)
942 list_remove( &hotkey->entry );
943 free( hotkey );
947 while ((ptr = list_head( &queue->pending_timers )))
949 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
950 list_remove( &timer->entry );
951 free( timer );
953 while ((ptr = list_head( &queue->expired_timers )))
955 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
956 list_remove( &timer->entry );
957 free( timer );
959 if (queue->timeout) remove_timeout_user( queue->timeout );
960 if (queue->input)
962 queue->input->cursor_count -= queue->cursor_count;
963 release_object( queue->input );
965 if (queue->hooks) release_object( queue->hooks );
966 if (queue->fd) release_object( queue->fd );
969 static void msg_queue_poll_event( struct fd *fd, int event )
971 struct msg_queue *queue = get_fd_user( fd );
972 assert( queue->obj.ops == &msg_queue_ops );
974 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
975 else set_fd_events( queue->fd, 0 );
976 wake_up( &queue->obj, 0 );
979 static void thread_input_dump( struct object *obj, int verbose )
981 struct thread_input *input = (struct thread_input *)obj;
982 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
983 input->focus, input->capture, input->active );
986 static void thread_input_destroy( struct object *obj )
988 struct thread_input *input = (struct thread_input *)obj;
990 empty_msg_list( &input->msg_list );
991 if (input->desktop)
993 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
994 release_object( input->desktop );
998 /* fix the thread input data when a window is destroyed */
999 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1001 struct thread_input *input = queue->input;
1003 if (window == input->focus) input->focus = 0;
1004 if (window == input->capture) input->capture = 0;
1005 if (window == input->active) input->active = 0;
1006 if (window == input->menu_owner) input->menu_owner = 0;
1007 if (window == input->move_size) input->move_size = 0;
1008 if (window == input->caret) set_caret_window( input, 0 );
1011 /* check if the specified window can be set in the input data of a given queue */
1012 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1014 struct thread *thread;
1015 int ret = 0;
1017 if (!window) return 1; /* we can always clear the data */
1019 if ((thread = get_window_thread( window )))
1021 ret = (queue->input == thread->queue->input);
1022 if (!ret) set_error( STATUS_ACCESS_DENIED );
1023 release_object( thread );
1025 else set_error( STATUS_INVALID_HANDLE );
1027 return ret;
1030 /* make sure the specified thread has a queue */
1031 int init_thread_queue( struct thread *thread )
1033 if (thread->queue) return 1;
1034 return (create_msg_queue( thread, NULL ) != NULL);
1037 /* attach two thread input data structures */
1038 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1040 struct desktop *desktop;
1041 struct thread_input *input;
1042 int ret;
1044 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1045 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1046 input = (struct thread_input *)grab_object( thread_to->queue->input );
1047 if (input->desktop != desktop)
1049 set_error( STATUS_ACCESS_DENIED );
1050 release_object( input );
1051 release_object( desktop );
1052 return 0;
1054 release_object( desktop );
1056 ret = assign_thread_input( thread_from, input );
1057 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1058 release_object( input );
1059 return ret;
1062 /* detach two thread input data structures */
1063 void detach_thread_input( struct thread *thread_from )
1065 struct thread_input *input;
1067 if ((input = create_thread_input( thread_from )))
1069 assign_thread_input( thread_from, input );
1070 release_object( input );
1075 /* set the next timer to expire */
1076 static void set_next_timer( struct msg_queue *queue )
1078 struct list *ptr;
1080 if (queue->timeout)
1082 remove_timeout_user( queue->timeout );
1083 queue->timeout = NULL;
1085 if ((ptr = list_head( &queue->pending_timers )))
1087 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1088 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1090 /* set/clear QS_TIMER bit */
1091 if (list_empty( &queue->expired_timers ))
1092 clear_queue_bits( queue, QS_TIMER );
1093 else
1094 set_queue_bits( queue, QS_TIMER );
1097 /* find a timer from its window and id */
1098 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1099 unsigned int msg, lparam_t id )
1101 struct list *ptr;
1103 /* we need to search both lists */
1105 LIST_FOR_EACH( ptr, &queue->pending_timers )
1107 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1108 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1110 LIST_FOR_EACH( ptr, &queue->expired_timers )
1112 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1113 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1115 return NULL;
1118 /* callback for the next timer expiration */
1119 static void timer_callback( void *private )
1121 struct msg_queue *queue = private;
1122 struct list *ptr;
1124 queue->timeout = NULL;
1125 /* move on to the next timer */
1126 ptr = list_head( &queue->pending_timers );
1127 list_remove( ptr );
1128 list_add_tail( &queue->expired_timers, ptr );
1129 set_next_timer( queue );
1132 /* link a timer at its rightful place in the queue list */
1133 static void link_timer( struct msg_queue *queue, struct timer *timer )
1135 struct list *ptr;
1137 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1139 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1140 if (t->when >= timer->when) break;
1142 list_add_before( ptr, &timer->entry );
1145 /* remove a timer from the queue timer list and free it */
1146 static void free_timer( struct msg_queue *queue, struct timer *timer )
1148 list_remove( &timer->entry );
1149 free( timer );
1150 set_next_timer( queue );
1153 /* restart an expired timer */
1154 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1156 list_remove( &timer->entry );
1157 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1158 link_timer( queue, timer );
1159 set_next_timer( queue );
1162 /* find an expired timer matching the filtering parameters */
1163 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1164 unsigned int get_first, unsigned int get_last,
1165 int remove )
1167 struct list *ptr;
1169 LIST_FOR_EACH( ptr, &queue->expired_timers )
1171 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1172 if (win && timer->win != win) continue;
1173 if (check_msg_filter( timer->msg, get_first, get_last ))
1175 if (remove) restart_timer( queue, timer );
1176 return timer;
1179 return NULL;
1182 /* add a timer */
1183 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1185 struct timer *timer = mem_alloc( sizeof(*timer) );
1186 if (timer)
1188 timer->rate = max( rate, 1 );
1189 timer->when = current_time + (timeout_t)timer->rate * 10000;
1190 link_timer( queue, timer );
1191 /* check if we replaced the next timer */
1192 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1194 return timer;
1197 /* change the input key state for a given key */
1198 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1200 if (down)
1202 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1203 keystate[key] |= down;
1205 else keystate[key] &= ~0x80;
1208 /* update the input key state for a keyboard message */
1209 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1210 const struct message *msg )
1212 unsigned char key;
1213 int down = 0;
1215 switch (msg->msg)
1217 case WM_LBUTTONDOWN:
1218 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1219 /* fall through */
1220 case WM_LBUTTONUP:
1221 set_input_key_state( keystate, VK_LBUTTON, down );
1222 break;
1223 case WM_MBUTTONDOWN:
1224 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1225 /* fall through */
1226 case WM_MBUTTONUP:
1227 set_input_key_state( keystate, VK_MBUTTON, down );
1228 break;
1229 case WM_RBUTTONDOWN:
1230 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1231 /* fall through */
1232 case WM_RBUTTONUP:
1233 set_input_key_state( keystate, VK_RBUTTON, down );
1234 break;
1235 case WM_XBUTTONDOWN:
1236 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1237 /* fall through */
1238 case WM_XBUTTONUP:
1239 if (msg->wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1240 else if (msg->wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1241 break;
1242 case WM_KEYDOWN:
1243 case WM_SYSKEYDOWN:
1244 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1245 /* fall through */
1246 case WM_KEYUP:
1247 case WM_SYSKEYUP:
1248 key = (unsigned char)msg->wparam;
1249 set_input_key_state( keystate, key, down );
1250 switch(key)
1252 case VK_LCONTROL:
1253 case VK_RCONTROL:
1254 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1255 set_input_key_state( keystate, VK_CONTROL, down );
1256 break;
1257 case VK_LMENU:
1258 case VK_RMENU:
1259 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1260 set_input_key_state( keystate, VK_MENU, down );
1261 break;
1262 case VK_LSHIFT:
1263 case VK_RSHIFT:
1264 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1265 set_input_key_state( keystate, VK_SHIFT, down );
1266 break;
1268 break;
1272 /* release the hardware message currently being processed by the given thread */
1273 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1274 int remove, user_handle_t new_win )
1276 struct thread_input *input = queue->input;
1277 struct message *msg;
1279 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1281 if (msg->unique_id == hw_id) break;
1283 if (&msg->entry == &input->msg_list) return; /* not found */
1285 /* clear the queue bit for that message */
1286 if (remove || new_win)
1288 struct message *other;
1289 int clr_bit;
1291 clr_bit = get_hardware_msg_bit( msg );
1292 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1294 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1296 clr_bit = 0;
1297 break;
1300 if (clr_bit) clear_queue_bits( queue, clr_bit );
1303 if (new_win) /* set the new window */
1305 struct thread *owner = get_window_thread( new_win );
1306 if (owner)
1308 msg->win = new_win;
1309 if (owner->queue->input != input)
1311 list_remove( &msg->entry );
1312 if (merge_message( owner->queue->input, msg ))
1314 free_message( msg );
1315 release_object( owner );
1316 return;
1318 list_add_tail( &owner->queue->input->msg_list, &msg->entry );
1320 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1321 remove = 0;
1322 release_object( owner );
1325 if (remove)
1327 update_input_key_state( input->desktop, input->keystate, msg );
1328 list_remove( &msg->entry );
1329 free_message( msg );
1333 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1335 struct hotkey *hotkey;
1336 unsigned int modifiers = 0;
1338 if (msg->msg != WM_KEYDOWN) return 0;
1340 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1341 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1342 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1343 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1345 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1347 if (hotkey->vkey != msg->wparam) continue;
1348 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1351 return 0;
1353 found:
1354 msg->type = MSG_POSTED;
1355 msg->win = hotkey->win;
1356 msg->msg = WM_HOTKEY;
1357 msg->wparam = hotkey->id;
1358 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1360 free( msg->data );
1361 msg->data = NULL;
1362 msg->data_size = 0;
1364 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1365 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1366 hotkey->queue->hotkey_count++;
1367 return 1;
1370 /* find the window that should receive a given hardware message */
1371 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1372 struct message *msg, unsigned int *msg_code )
1374 struct hardware_msg_data *data = msg->data;
1375 user_handle_t win = 0;
1377 *msg_code = msg->msg;
1378 if (is_keyboard_msg( msg ))
1380 if (input && !(win = input->focus))
1382 win = input->active;
1383 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1386 else /* mouse message */
1388 if (!input || !(win = input->capture))
1390 if (!(win = msg->win) || !is_window_visible( win ) || is_window_transparent( win ))
1391 win = window_from_point( desktop, data->x, data->y );
1394 return win;
1397 /* queue a hardware message into a given thread input */
1398 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1400 user_handle_t win;
1401 struct thread *thread;
1402 struct thread_input *input;
1403 unsigned int msg_code;
1404 struct hardware_msg_data *data = msg->data;
1406 update_input_key_state( desktop, desktop->keystate, msg );
1407 last_input_time = get_tick_count();
1408 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1410 if (is_keyboard_msg( msg ))
1412 if (queue_hotkey_message( desktop, msg )) return;
1413 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1414 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1415 msg->lparam &= ~(KF_EXTENDED << 16);
1417 else
1419 if (msg->msg == WM_MOUSEMOVE)
1421 int x = min( max( data->x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
1422 int y = min( max( data->y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
1423 if (desktop->cursor.x != x || desktop->cursor.y != y) always_queue = 1;
1424 desktop->cursor.x = x;
1425 desktop->cursor.y = y;
1426 desktop->cursor.last_change = get_tick_count();
1428 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1429 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1430 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1431 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1432 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1433 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1434 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1436 data->x = desktop->cursor.x;
1437 data->y = desktop->cursor.y;
1439 if (msg->win && (thread = get_window_thread( msg->win )))
1441 input = thread->queue->input;
1442 release_object( thread );
1444 else input = desktop->foreground_input;
1446 win = find_hardware_message_window( desktop, input, msg, &msg_code );
1447 if (!win || !(thread = get_window_thread(win)))
1449 if (input) update_input_key_state( input->desktop, input->keystate, msg );
1450 free_message( msg );
1451 return;
1453 input = thread->queue->input;
1455 if (win != desktop->cursor.win) always_queue = 1;
1456 desktop->cursor.win = win;
1458 if (!always_queue || merge_message( input, msg )) free_message( msg );
1459 else
1461 msg->unique_id = 0; /* will be set once we return it to the app */
1462 list_add_tail( &input->msg_list, &msg->entry );
1463 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1465 release_object( thread );
1468 /* send the low-level hook message for a given hardware message */
1469 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1470 const hw_input_t *input, struct msg_queue *sender )
1472 struct thread *hook_thread;
1473 struct msg_queue *queue;
1474 struct message *msg;
1475 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1476 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1478 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1479 if (!(queue = hook_thread->queue)) return 0;
1480 if (is_queue_hung( queue )) return 0;
1482 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1484 msg->type = MSG_HOOK_LL;
1485 msg->win = 0;
1486 msg->msg = id;
1487 msg->wparam = hardware_msg->msg;
1488 msg->time = hardware_msg->time;
1489 msg->data_size = hardware_msg->data_size;
1490 msg->result = NULL;
1492 if (input->type == INPUT_KEYBOARD)
1494 unsigned short vkey = input->kbd.vkey;
1495 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1496 msg->lparam = (input->kbd.scan << 16) | vkey;
1498 else msg->lparam = input->mouse.data << 16;
1500 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1501 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1503 free_message( msg );
1504 return 0;
1506 msg->result->hardware_msg = hardware_msg;
1507 msg->result->desktop = (struct desktop *)grab_object( desktop );
1508 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1509 set_queue_bits( queue, QS_SENDMESSAGE );
1510 return 1;
1513 /* queue a hardware message for a mouse event */
1514 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1515 unsigned int hook_flags, struct msg_queue *sender )
1517 struct hardware_msg_data *msg_data;
1518 struct message *msg;
1519 unsigned int i, time, flags;
1520 int wait = 0, x, y;
1522 static const unsigned int messages[] =
1524 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1525 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1526 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1527 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1528 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1529 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1530 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1531 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1532 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1533 0, /* 0x0200 = unused */
1534 0, /* 0x0400 = unused */
1535 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1536 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1539 desktop->cursor.last_change = get_tick_count();
1540 flags = input->mouse.flags;
1541 time = input->mouse.time;
1542 if (!time) time = desktop->cursor.last_change;
1544 if (flags & MOUSEEVENTF_MOVE)
1546 if (flags & MOUSEEVENTF_ABSOLUTE)
1548 x = input->mouse.x;
1549 y = input->mouse.y;
1550 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1551 x == desktop->cursor.x && y == desktop->cursor.y)
1552 flags &= ~MOUSEEVENTF_MOVE;
1554 else
1556 x = desktop->cursor.x + input->mouse.x;
1557 y = desktop->cursor.y + input->mouse.y;
1560 else
1562 x = desktop->cursor.x;
1563 y = desktop->cursor.y;
1566 for (i = 0; i < sizeof(messages)/sizeof(messages[0]); i++)
1568 if (!messages[i]) continue;
1569 if (!(flags & (1 << i))) continue;
1570 flags &= ~(1 << i);
1572 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1573 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1575 free( msg );
1576 return 0;
1578 memset( msg_data, 0, sizeof(*msg_data) );
1580 msg->type = MSG_HARDWARE;
1581 msg->win = get_user_full_handle( win );
1582 msg->msg = messages[i];
1583 msg->wparam = input->mouse.data << 16;
1584 msg->lparam = 0;
1585 msg->time = time;
1586 msg->result = NULL;
1587 msg->data = msg_data;
1588 msg->data_size = sizeof(*msg_data);
1589 msg_data->x = x;
1590 msg_data->y = y;
1591 msg_data->info = input->mouse.info;
1592 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLMHF_INJECTED;
1594 /* specify a sender only when sending the last message */
1595 if (!(flags & ((1 << sizeof(messages)/sizeof(messages[0])) - 1)))
1597 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1598 queue_hardware_message( desktop, msg, 0 );
1600 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1601 queue_hardware_message( desktop, msg, 0 );
1603 return wait;
1606 /* queue a hardware message for a keyboard event */
1607 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1608 unsigned int hook_flags, struct msg_queue *sender )
1610 struct hardware_msg_data *msg_data;
1611 struct message *msg;
1612 unsigned char vkey = input->kbd.vkey;
1613 int wait;
1615 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1616 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1618 free( msg );
1619 return 0;
1621 memset( msg_data, 0, sizeof(*msg_data) );
1623 msg->type = MSG_HARDWARE;
1624 msg->win = get_user_full_handle( win );
1625 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1626 msg->time = input->kbd.time;
1627 msg->result = NULL;
1628 msg->data = msg_data;
1629 msg->data_size = sizeof(*msg_data);
1630 msg_data->info = input->kbd.info;
1631 if (!msg->time) msg->time = get_tick_count();
1632 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLKHF_INJECTED;
1634 if (input->kbd.flags & KEYEVENTF_UNICODE)
1636 msg->wparam = VK_PACKET;
1638 else
1640 unsigned int flags = 0;
1641 switch (vkey)
1643 case VK_MENU:
1644 case VK_LMENU:
1645 case VK_RMENU:
1646 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1647 break;
1648 case VK_CONTROL:
1649 case VK_LCONTROL:
1650 case VK_RCONTROL:
1651 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1652 break;
1653 case VK_SHIFT:
1654 case VK_LSHIFT:
1655 case VK_RSHIFT:
1656 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1657 break;
1659 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1660 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1661 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1662 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1664 msg->wparam = vkey;
1665 msg->lparam |= flags << 16;
1666 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1669 msg->msg = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1671 switch (vkey)
1673 case VK_LMENU:
1674 case VK_RMENU:
1675 if (input->kbd.flags & KEYEVENTF_KEYUP)
1677 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1678 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1679 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1680 msg->msg = WM_SYSKEYUP;
1681 desktop->keystate[VK_MENU] &= ~0x02;
1683 else
1685 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1686 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1687 msg->msg = WM_SYSKEYDOWN;
1688 desktop->keystate[VK_MENU] |= 0x02;
1690 break;
1692 case VK_LCONTROL:
1693 case VK_RCONTROL:
1694 /* send WM_SYSKEYUP on release if Alt still pressed */
1695 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1696 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1697 msg->msg = WM_SYSKEYUP;
1698 desktop->keystate[VK_MENU] &= ~0x02;
1699 break;
1701 default:
1702 /* send WM_SYSKEY for Alt-anykey and for F10 */
1703 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1704 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1705 /* fall through */
1706 case VK_F10:
1707 msg->msg = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1708 desktop->keystate[VK_MENU] &= ~0x02;
1709 break;
1711 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1712 queue_hardware_message( desktop, msg, 1 );
1714 return wait;
1717 /* queue a hardware message for a custom type of event */
1718 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1719 const hw_input_t *input )
1721 struct hardware_msg_data *msg_data;
1722 struct message *msg;
1724 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
1725 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1727 free( msg );
1728 return;
1730 memset( msg_data, 0, sizeof(*msg_data) );
1732 msg->type = MSG_HARDWARE;
1733 msg->win = get_user_full_handle( win );
1734 msg->msg = input->hw.msg;
1735 msg->wparam = 0;
1736 msg->lparam = input->hw.lparam;
1737 msg->time = get_tick_count();
1738 msg->result = NULL;
1739 msg->data = msg_data;
1740 msg->data_size = sizeof(*msg_data);
1742 queue_hardware_message( desktop, msg, 1 );
1745 /* check message filter for a hardware message */
1746 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1747 user_handle_t filter_win, unsigned int first, unsigned int last )
1749 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1751 /* we can only test the window for a keyboard message since the
1752 * dest window for a mouse message depends on hittest */
1753 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1754 return 0;
1755 /* the message code is final for a keyboard message, we can simply check it */
1756 return check_msg_filter( msg_code, first, last );
1758 else /* mouse message */
1760 /* we need to check all possible values that the message can have in the end */
1762 if (check_msg_filter( msg_code, first, last )) return 1;
1763 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1765 /* all other messages can become non-client messages */
1766 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1768 /* clicks can become double-clicks or non-client double-clicks */
1769 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1770 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1772 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1773 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1775 return 0;
1780 /* find a hardware message for the given queue */
1781 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1782 unsigned int first, unsigned int last, struct get_message_reply *reply )
1784 struct thread_input *input = thread->queue->input;
1785 struct thread *win_thread;
1786 struct list *ptr;
1787 user_handle_t win;
1788 int clear_bits, got_one = 0;
1789 unsigned int msg_code;
1791 ptr = list_head( &input->msg_list );
1792 if (hw_id)
1794 while (ptr)
1796 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1797 if (msg->unique_id == hw_id) break;
1798 ptr = list_next( &input->msg_list, ptr );
1800 if (!ptr) ptr = list_head( &input->msg_list );
1801 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1804 if (ptr == list_head( &input->msg_list ))
1805 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1806 else
1807 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1809 while (ptr)
1811 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1812 struct hardware_msg_data *data = msg->data;
1814 ptr = list_next( &input->msg_list, ptr );
1815 win = find_hardware_message_window( input->desktop, input, msg, &msg_code );
1816 if (!win || !(win_thread = get_window_thread( win )))
1818 /* no window at all, remove it */
1819 update_input_key_state( input->desktop, input->keystate, msg );
1820 list_remove( &msg->entry );
1821 free_message( msg );
1822 continue;
1824 if (win_thread != thread)
1826 if (win_thread->queue->input == input)
1828 /* wake the other thread */
1829 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1830 got_one = 1;
1832 else
1834 /* for another thread input, drop it */
1835 update_input_key_state( input->desktop, input->keystate, msg );
1836 list_remove( &msg->entry );
1837 free_message( msg );
1839 release_object( win_thread );
1840 continue;
1842 release_object( win_thread );
1844 /* if we already got a message for another thread, or if it doesn't
1845 * match the filter we skip it */
1846 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1848 clear_bits &= ~get_hardware_msg_bit( msg );
1849 continue;
1851 /* now we can return it */
1852 if (!msg->unique_id) msg->unique_id = get_unique_id();
1853 reply->type = MSG_HARDWARE;
1854 reply->win = win;
1855 reply->msg = msg_code;
1856 reply->wparam = msg->wparam;
1857 reply->lparam = msg->lparam;
1858 reply->time = msg->time;
1860 data->hw_id = msg->unique_id;
1861 set_reply_data( msg->data, msg->data_size );
1862 return 1;
1864 /* nothing found, clear the hardware queue bits */
1865 clear_queue_bits( thread->queue, clear_bits );
1866 return 0;
1869 /* increment (or decrement if 'incr' is negative) the queue paint count */
1870 void inc_queue_paint_count( struct thread *thread, int incr )
1872 struct msg_queue *queue = thread->queue;
1874 assert( queue );
1876 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1878 if (queue->paint_count)
1879 set_queue_bits( queue, QS_PAINT );
1880 else
1881 clear_queue_bits( queue, QS_PAINT );
1885 /* remove all messages and timers belonging to a certain window */
1886 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1888 struct msg_queue *queue = thread->queue;
1889 struct list *ptr;
1890 int i;
1892 if (!queue) return;
1894 /* remove timers */
1896 ptr = list_head( &queue->pending_timers );
1897 while (ptr)
1899 struct list *next = list_next( &queue->pending_timers, ptr );
1900 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1901 if (timer->win == win) free_timer( queue, timer );
1902 ptr = next;
1904 ptr = list_head( &queue->expired_timers );
1905 while (ptr)
1907 struct list *next = list_next( &queue->expired_timers, ptr );
1908 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1909 if (timer->win == win) free_timer( queue, timer );
1910 ptr = next;
1913 /* remove messages */
1914 for (i = 0; i < NB_MSG_KINDS; i++)
1916 struct list *ptr, *next;
1918 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1920 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1921 if (msg->win == win) remove_queue_message( queue, msg, i );
1925 thread_input_cleanup_window( queue, win );
1928 /* post a message to a window; used by socket handling */
1929 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
1931 struct message *msg;
1932 struct thread *thread = get_window_thread( win );
1934 if (!thread) return;
1936 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1938 msg->type = MSG_POSTED;
1939 msg->win = get_user_full_handle( win );
1940 msg->msg = message;
1941 msg->wparam = wparam;
1942 msg->lparam = lparam;
1943 msg->time = get_tick_count();
1944 msg->result = NULL;
1945 msg->data = NULL;
1946 msg->data_size = 0;
1948 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1949 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1950 if (message == WM_HOTKEY)
1952 set_queue_bits( thread->queue, QS_HOTKEY );
1953 thread->queue->hotkey_count++;
1956 release_object( thread );
1959 /* post a win event */
1960 void post_win_event( struct thread *thread, unsigned int event,
1961 user_handle_t win, unsigned int object_id,
1962 unsigned int child_id, client_ptr_t hook_proc,
1963 const WCHAR *module, data_size_t module_size,
1964 user_handle_t hook)
1966 struct message *msg;
1968 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1970 struct winevent_msg_data *data;
1972 msg->type = MSG_WINEVENT;
1973 msg->win = get_user_full_handle( win );
1974 msg->msg = event;
1975 msg->wparam = object_id;
1976 msg->lparam = child_id;
1977 msg->time = get_tick_count();
1978 msg->result = NULL;
1980 if ((data = malloc( sizeof(*data) + module_size )))
1982 data->hook = hook;
1983 data->tid = get_thread_id( current );
1984 data->hook_proc = hook_proc;
1985 memcpy( data + 1, module, module_size );
1987 msg->data = data;
1988 msg->data_size = sizeof(*data) + module_size;
1990 if (debug_level > 1)
1991 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
1992 get_thread_id(thread), event, win, object_id, child_id );
1993 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1994 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1996 else
1997 free( msg );
2001 /* free all hotkeys on a desktop, optionally filtering by window */
2002 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2004 struct hotkey *hotkey, *hotkey2;
2006 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2008 if (!window || hotkey->win == window)
2010 list_remove( &hotkey->entry );
2011 free( hotkey );
2017 /* check if the thread owning the window is hung */
2018 DECL_HANDLER(is_window_hung)
2020 struct thread *thread;
2022 thread = get_window_thread( req->win );
2023 if (thread)
2025 reply->is_hung = is_queue_hung( thread->queue );
2026 release_object( thread );
2028 else reply->is_hung = 0;
2032 /* get the message queue of the current thread */
2033 DECL_HANDLER(get_msg_queue)
2035 struct msg_queue *queue = get_current_queue();
2037 reply->handle = 0;
2038 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2042 /* set the file descriptor associated to the current thread queue */
2043 DECL_HANDLER(set_queue_fd)
2045 struct msg_queue *queue = get_current_queue();
2046 struct file *file;
2047 int unix_fd;
2049 if (queue->fd) /* fd can only be set once */
2051 set_error( STATUS_ACCESS_DENIED );
2052 return;
2054 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2056 if ((unix_fd = get_file_unix_fd( file )) != -1)
2058 if ((unix_fd = dup( unix_fd )) != -1)
2059 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2060 else
2061 file_set_error();
2063 release_object( file );
2067 /* set the current message queue wakeup mask */
2068 DECL_HANDLER(set_queue_mask)
2070 struct msg_queue *queue = get_current_queue();
2072 if (queue)
2074 queue->wake_mask = req->wake_mask;
2075 queue->changed_mask = req->changed_mask;
2076 reply->wake_bits = queue->wake_bits;
2077 reply->changed_bits = queue->changed_bits;
2078 if (is_signaled( queue ))
2080 /* if skip wait is set, do what would have been done in the subsequent wait */
2081 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
2082 else wake_up( &queue->obj, 0 );
2088 /* get the current message queue status */
2089 DECL_HANDLER(get_queue_status)
2091 struct msg_queue *queue = current->queue;
2092 if (queue)
2094 reply->wake_bits = queue->wake_bits;
2095 reply->changed_bits = queue->changed_bits;
2096 if (req->clear) queue->changed_bits = 0;
2098 else reply->wake_bits = reply->changed_bits = 0;
2102 /* send a message to a thread queue */
2103 DECL_HANDLER(send_message)
2105 struct message *msg;
2106 struct msg_queue *send_queue = get_current_queue();
2107 struct msg_queue *recv_queue = NULL;
2108 struct thread *thread = NULL;
2110 if (!(thread = get_thread_from_id( req->id ))) return;
2112 if (!(recv_queue = thread->queue))
2114 set_error( STATUS_INVALID_PARAMETER );
2115 release_object( thread );
2116 return;
2118 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2120 set_error( STATUS_TIMEOUT );
2121 release_object( thread );
2122 return;
2125 if ((msg = mem_alloc( sizeof(*msg) )))
2127 msg->type = req->type;
2128 msg->win = get_user_full_handle( req->win );
2129 msg->msg = req->msg;
2130 msg->wparam = req->wparam;
2131 msg->lparam = req->lparam;
2132 msg->time = get_tick_count();
2133 msg->result = NULL;
2134 msg->data = NULL;
2135 msg->data_size = get_req_data_size();
2137 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2139 free( msg );
2140 release_object( thread );
2141 return;
2144 switch(msg->type)
2146 case MSG_OTHER_PROCESS:
2147 case MSG_ASCII:
2148 case MSG_UNICODE:
2149 case MSG_CALLBACK:
2150 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2152 free_message( msg );
2153 break;
2155 /* fall through */
2156 case MSG_NOTIFY:
2157 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2158 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2159 break;
2160 case MSG_POSTED:
2161 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2162 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2163 if (msg->msg == WM_HOTKEY)
2165 set_queue_bits( recv_queue, QS_HOTKEY );
2166 recv_queue->hotkey_count++;
2168 break;
2169 case MSG_HARDWARE: /* should use send_hardware_message instead */
2170 case MSG_CALLBACK_RESULT: /* cannot send this one */
2171 case MSG_HOOK_LL: /* generated internally */
2172 default:
2173 set_error( STATUS_INVALID_PARAMETER );
2174 free( msg );
2175 break;
2178 release_object( thread );
2181 /* send a hardware message to a thread queue */
2182 DECL_HANDLER(send_hardware_message)
2184 struct thread *thread = NULL;
2185 struct desktop *desktop;
2186 struct msg_queue *sender = get_current_queue();
2188 if (req->win)
2190 if (!(thread = get_window_thread( req->win ))) return;
2191 desktop = (struct desktop *)grab_object( thread->queue->input->desktop );
2193 else if (!(desktop = get_thread_desktop( current, 0 ))) return;
2195 switch (req->input.type)
2197 case INPUT_MOUSE:
2198 reply->wait = queue_mouse_message( desktop, req->win, &req->input, req->flags, sender );
2199 break;
2200 case INPUT_KEYBOARD:
2201 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, req->flags, sender );
2202 break;
2203 case INPUT_HARDWARE:
2204 queue_custom_hardware_message( desktop, req->win, &req->input );
2205 break;
2206 default:
2207 set_error( STATUS_INVALID_PARAMETER );
2209 if (thread) release_object( thread );
2210 release_object( desktop );
2213 /* post a quit message to the current queue */
2214 DECL_HANDLER(post_quit_message)
2216 struct msg_queue *queue = get_current_queue();
2218 if (!queue)
2219 return;
2221 queue->quit_message = 1;
2222 queue->exit_code = req->exit_code;
2223 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2226 /* get a message from the current queue */
2227 DECL_HANDLER(get_message)
2229 struct timer *timer;
2230 struct list *ptr;
2231 struct msg_queue *queue = get_current_queue();
2232 user_handle_t get_win = get_user_full_handle( req->get_win );
2233 unsigned int filter = req->flags >> 16;
2235 reply->active_hooks = get_active_hooks();
2237 if (!queue) return;
2238 queue->last_get_msg = current_time;
2239 if (!filter) filter = QS_ALLINPUT;
2241 /* first check for sent messages */
2242 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2244 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2245 receive_message( queue, msg, reply );
2246 return;
2249 /* clear changed bits so we can wait on them if we don't find a message */
2250 if (filter & QS_POSTMESSAGE)
2252 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2253 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2255 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2256 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2258 /* then check for posted messages */
2259 if ((filter & QS_POSTMESSAGE) &&
2260 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2261 return;
2263 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2264 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2265 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2266 return;
2268 /* only check for quit messages if not posted messages pending.
2269 * note: the quit message isn't filtered */
2270 if (get_quit_message( queue, req->flags, reply ))
2271 return;
2273 /* then check for any raw hardware message */
2274 if ((filter & QS_INPUT) &&
2275 filter_contains_hw_range( req->get_first, req->get_last ) &&
2276 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
2277 return;
2279 /* now check for WM_PAINT */
2280 if ((filter & QS_PAINT) &&
2281 queue->paint_count &&
2282 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2283 (reply->win = find_window_to_repaint( get_win, current )))
2285 reply->type = MSG_POSTED;
2286 reply->msg = WM_PAINT;
2287 reply->wparam = 0;
2288 reply->lparam = 0;
2289 reply->time = get_tick_count();
2290 return;
2293 /* now check for timer */
2294 if ((filter & QS_TIMER) &&
2295 (timer = find_expired_timer( queue, get_win, req->get_first,
2296 req->get_last, (req->flags & PM_REMOVE) )))
2298 reply->type = MSG_POSTED;
2299 reply->win = timer->win;
2300 reply->msg = timer->msg;
2301 reply->wparam = timer->id;
2302 reply->lparam = timer->lparam;
2303 reply->time = get_tick_count();
2304 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2305 set_event( current->process->idle_event );
2306 return;
2309 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2310 queue->wake_mask = req->wake_mask;
2311 queue->changed_mask = req->changed_mask;
2312 set_error( STATUS_PENDING ); /* FIXME */
2316 /* reply to a sent message */
2317 DECL_HANDLER(reply_message)
2319 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2320 else if (current->queue->recv_result)
2321 reply_message( current->queue, req->result, 0, req->remove,
2322 get_req_data(), get_req_data_size() );
2326 /* accept the current hardware message */
2327 DECL_HANDLER(accept_hardware_message)
2329 if (current->queue)
2330 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
2331 else
2332 set_error( STATUS_ACCESS_DENIED );
2336 /* retrieve the reply for the last message sent */
2337 DECL_HANDLER(get_message_reply)
2339 struct message_result *result;
2340 struct list *entry;
2341 struct msg_queue *queue = current->queue;
2343 if (queue)
2345 set_error( STATUS_PENDING );
2346 reply->result = 0;
2348 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2350 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2351 if (result->replied || req->cancel)
2353 if (result->replied)
2355 reply->result = result->result;
2356 set_error( result->error );
2357 if (result->data)
2359 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2360 set_reply_data_ptr( result->data, data_len );
2361 result->data = NULL;
2362 result->data_size = 0;
2365 remove_result_from_sender( result );
2367 entry = list_head( &queue->send_result );
2368 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2369 else
2371 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2372 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
2376 else set_error( STATUS_ACCESS_DENIED );
2380 /* set a window timer */
2381 DECL_HANDLER(set_win_timer)
2383 struct timer *timer;
2384 struct msg_queue *queue;
2385 struct thread *thread = NULL;
2386 user_handle_t win = 0;
2387 lparam_t id = req->id;
2389 if (req->win)
2391 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2393 set_error( STATUS_INVALID_HANDLE );
2394 return;
2396 if (thread->process != current->process)
2398 release_object( thread );
2399 set_error( STATUS_ACCESS_DENIED );
2400 return;
2402 queue = thread->queue;
2403 /* remove it if it existed already */
2404 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2406 else
2408 queue = get_current_queue();
2409 /* look for a timer with this id */
2410 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2412 /* free and reuse id */
2413 free_timer( queue, timer );
2415 else
2417 /* find a free id for it */
2420 id = queue->next_timer_id;
2421 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2423 while (find_timer( queue, 0, req->msg, id ));
2427 if ((timer = set_timer( queue, req->rate )))
2429 timer->win = win;
2430 timer->msg = req->msg;
2431 timer->id = id;
2432 timer->lparam = req->lparam;
2433 reply->id = id;
2435 if (thread) release_object( thread );
2438 /* kill a window timer */
2439 DECL_HANDLER(kill_win_timer)
2441 struct timer *timer;
2442 struct thread *thread;
2443 user_handle_t win = 0;
2445 if (req->win)
2447 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2449 set_error( STATUS_INVALID_HANDLE );
2450 return;
2452 if (thread->process != current->process)
2454 release_object( thread );
2455 set_error( STATUS_ACCESS_DENIED );
2456 return;
2459 else thread = (struct thread *)grab_object( current );
2461 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2462 free_timer( thread->queue, timer );
2463 else
2464 set_error( STATUS_INVALID_PARAMETER );
2466 release_object( thread );
2469 DECL_HANDLER(register_hotkey)
2471 struct desktop *desktop;
2472 user_handle_t win_handle = req->window;
2473 struct hotkey *hotkey;
2474 struct hotkey *new_hotkey = NULL;
2475 struct thread *thread;
2476 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2478 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2480 if (win_handle)
2482 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2484 release_object( desktop );
2485 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2486 return;
2489 thread = get_window_thread( win_handle );
2490 if (thread) release_object( thread );
2492 if (thread != current)
2494 release_object( desktop );
2495 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2496 return;
2500 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2502 if (req->vkey == hotkey->vkey &&
2503 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2505 release_object( desktop );
2506 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2507 return;
2509 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2510 new_hotkey = hotkey;
2513 if (new_hotkey)
2515 reply->replaced = 1;
2516 reply->flags = new_hotkey->flags;
2517 reply->vkey = new_hotkey->vkey;
2519 else
2521 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2522 if (new_hotkey)
2524 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2525 new_hotkey->queue = current->queue;
2526 new_hotkey->win = win_handle;
2527 new_hotkey->id = req->id;
2531 if (new_hotkey)
2533 new_hotkey->flags = req->flags;
2534 new_hotkey->vkey = req->vkey;
2537 release_object( desktop );
2540 DECL_HANDLER(unregister_hotkey)
2542 struct desktop *desktop;
2543 user_handle_t win_handle = req->window;
2544 struct hotkey *hotkey;
2545 struct thread *thread;
2547 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2549 if (win_handle)
2551 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2553 release_object( desktop );
2554 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2555 return;
2558 thread = get_window_thread( win_handle );
2559 if (thread) release_object( thread );
2561 if (thread != current)
2563 release_object( desktop );
2564 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2565 return;
2569 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2571 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2572 goto found;
2575 release_object( desktop );
2576 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2577 return;
2579 found:
2580 reply->flags = hotkey->flags;
2581 reply->vkey = hotkey->vkey;
2582 list_remove( &hotkey->entry );
2583 free( hotkey );
2584 release_object( desktop );
2587 /* attach (or detach) thread inputs */
2588 DECL_HANDLER(attach_thread_input)
2590 struct thread *thread_from = get_thread_from_id( req->tid_from );
2591 struct thread *thread_to = get_thread_from_id( req->tid_to );
2593 if (!thread_from || !thread_to)
2595 if (thread_from) release_object( thread_from );
2596 if (thread_to) release_object( thread_to );
2597 return;
2599 if (thread_from != thread_to)
2601 if (req->attach) attach_thread_input( thread_from, thread_to );
2602 else
2604 if (thread_from->queue && thread_to->queue &&
2605 thread_from->queue->input == thread_to->queue->input)
2606 detach_thread_input( thread_from );
2607 else
2608 set_error( STATUS_ACCESS_DENIED );
2611 else set_error( STATUS_ACCESS_DENIED );
2612 release_object( thread_from );
2613 release_object( thread_to );
2617 /* get thread input data */
2618 DECL_HANDLER(get_thread_input)
2620 struct thread *thread = NULL;
2621 struct desktop *desktop;
2622 struct thread_input *input;
2624 if (req->tid)
2626 if (!(thread = get_thread_from_id( req->tid ))) return;
2627 if (!(desktop = get_thread_desktop( thread, 0 )))
2629 release_object( thread );
2630 return;
2632 input = thread->queue ? thread->queue->input : NULL;
2634 else
2636 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2637 input = desktop->foreground_input; /* get the foreground thread info */
2640 if (input)
2642 reply->focus = input->focus;
2643 reply->capture = input->capture;
2644 reply->active = input->active;
2645 reply->menu_owner = input->menu_owner;
2646 reply->move_size = input->move_size;
2647 reply->caret = input->caret;
2648 reply->cursor = input->cursor;
2649 reply->show_count = input->cursor_count;
2650 reply->rect = input->caret_rect;
2653 /* foreground window is active window of foreground thread */
2654 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2655 if (thread) release_object( thread );
2656 release_object( desktop );
2660 /* retrieve queue keyboard state for a given thread */
2661 DECL_HANDLER(get_key_state)
2663 struct thread *thread;
2664 struct desktop *desktop;
2665 data_size_t size = min( 256, get_reply_max_size() );
2667 if (!req->tid) /* get global async key state */
2669 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2670 if (req->key >= 0)
2672 reply->state = desktop->keystate[req->key & 0xff];
2673 desktop->keystate[req->key & 0xff] &= ~0x40;
2675 set_reply_data( desktop->keystate, size );
2676 release_object( desktop );
2678 else
2680 if (!(thread = get_thread_from_id( req->tid ))) return;
2681 if (thread->queue)
2683 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2684 set_reply_data( thread->queue->input->keystate, size );
2686 release_object( thread );
2691 /* set queue keyboard state for a given thread */
2692 DECL_HANDLER(set_key_state)
2694 struct thread *thread;
2695 struct desktop *desktop;
2696 data_size_t size = min( 256, get_req_data_size() );
2698 if (!req->tid) /* set global async key state */
2700 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2701 memcpy( desktop->keystate, get_req_data(), size );
2702 release_object( desktop );
2704 else
2706 if (!(thread = get_thread_from_id( req->tid ))) return;
2707 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
2708 release_object( thread );
2713 /* set the system foreground window */
2714 DECL_HANDLER(set_foreground_window)
2716 struct thread *thread = NULL;
2717 struct desktop *desktop;
2718 struct msg_queue *queue = get_current_queue();
2720 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2721 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
2722 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
2723 reply->send_msg_new = FALSE;
2725 if (is_top_level_window( req->handle ) &&
2726 ((thread = get_window_thread( req->handle ))) &&
2727 (thread->queue->input->desktop == desktop))
2729 set_foreground_input( desktop, thread->queue->input );
2730 reply->send_msg_new = (desktop->foreground_input != queue->input);
2732 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2734 if (thread) release_object( thread );
2735 release_object( desktop );
2739 /* set the current thread focus window */
2740 DECL_HANDLER(set_focus_window)
2742 struct msg_queue *queue = get_current_queue();
2744 reply->previous = 0;
2745 if (queue && check_queue_input_window( queue, req->handle ))
2747 reply->previous = queue->input->focus;
2748 queue->input->focus = get_user_full_handle( req->handle );
2753 /* set the current thread active window */
2754 DECL_HANDLER(set_active_window)
2756 struct msg_queue *queue = get_current_queue();
2758 reply->previous = 0;
2759 if (queue && check_queue_input_window( queue, req->handle ))
2761 if (!req->handle || make_window_active( req->handle ))
2763 reply->previous = queue->input->active;
2764 queue->input->active = get_user_full_handle( req->handle );
2766 else set_error( STATUS_INVALID_HANDLE );
2771 /* set the current thread capture window */
2772 DECL_HANDLER(set_capture_window)
2774 struct msg_queue *queue = get_current_queue();
2776 reply->previous = reply->full_handle = 0;
2777 if (queue && check_queue_input_window( queue, req->handle ))
2779 struct thread_input *input = queue->input;
2781 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2782 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2784 set_error(STATUS_ACCESS_DENIED);
2785 return;
2787 reply->previous = input->capture;
2788 input->capture = get_user_full_handle( req->handle );
2789 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2790 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2791 reply->full_handle = input->capture;
2796 /* Set the current thread caret window */
2797 DECL_HANDLER(set_caret_window)
2799 struct msg_queue *queue = get_current_queue();
2801 reply->previous = 0;
2802 if (queue && check_queue_input_window( queue, req->handle ))
2804 struct thread_input *input = queue->input;
2806 reply->previous = input->caret;
2807 reply->old_rect = input->caret_rect;
2808 reply->old_hide = input->caret_hide;
2809 reply->old_state = input->caret_state;
2811 set_caret_window( input, get_user_full_handle(req->handle) );
2812 input->caret_rect.right = input->caret_rect.left + req->width;
2813 input->caret_rect.bottom = input->caret_rect.top + req->height;
2818 /* Set the current thread caret information */
2819 DECL_HANDLER(set_caret_info)
2821 struct msg_queue *queue = get_current_queue();
2822 struct thread_input *input;
2824 if (!queue) return;
2825 input = queue->input;
2826 reply->full_handle = input->caret;
2827 reply->old_rect = input->caret_rect;
2828 reply->old_hide = input->caret_hide;
2829 reply->old_state = input->caret_state;
2831 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2833 set_error( STATUS_ACCESS_DENIED );
2834 return;
2836 if (req->flags & SET_CARET_POS)
2838 input->caret_rect.right += req->x - input->caret_rect.left;
2839 input->caret_rect.bottom += req->y - input->caret_rect.top;
2840 input->caret_rect.left = req->x;
2841 input->caret_rect.top = req->y;
2843 if (req->flags & SET_CARET_HIDE)
2845 input->caret_hide += req->hide;
2846 if (input->caret_hide < 0) input->caret_hide = 0;
2848 if (req->flags & SET_CARET_STATE)
2850 if (req->state == -1) input->caret_state = !input->caret_state;
2851 else input->caret_state = !!req->state;
2856 /* get the time of the last input event */
2857 DECL_HANDLER(get_last_input_time)
2859 reply->time = last_input_time;
2862 /* set/get the current cursor */
2863 DECL_HANDLER(set_cursor)
2865 struct msg_queue *queue = get_current_queue();
2866 struct thread_input *input;
2868 if (!queue) return;
2869 input = queue->input;
2871 reply->prev_handle = input->cursor;
2872 reply->prev_count = input->cursor_count;
2873 reply->prev_x = input->desktop->cursor.x;
2874 reply->prev_y = input->desktop->cursor.y;
2876 if (req->flags & SET_CURSOR_HANDLE)
2878 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
2880 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
2881 return;
2883 input->cursor = req->handle;
2885 if (req->flags & SET_CURSOR_COUNT)
2887 queue->cursor_count += req->show_count;
2888 input->cursor_count += req->show_count;
2890 if (req->flags & SET_CURSOR_POS)
2892 set_cursor_pos( input->desktop, req->x, req->y );
2894 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
2896 struct desktop *desktop = input->desktop;
2898 /* only the desktop owner can set the message */
2899 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
2900 desktop->cursor.clip_msg = req->clip_msg;
2902 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip );
2905 reply->new_x = input->desktop->cursor.x;
2906 reply->new_y = input->desktop->cursor.y;
2907 reply->new_clip = input->desktop->cursor.clip;
2908 reply->last_change = input->desktop->cursor.last_change;