winspool: Don't assume that CUPS is available.
[wine.git] / server / queue.c
blob4fd32ed0e999198c1d2e4ea5ffd32d832d1ac7c0
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 queue->input->cursor_count -= queue->cursor_count;
961 release_object( queue->input );
962 if (queue->hooks) release_object( queue->hooks );
963 if (queue->fd) release_object( queue->fd );
966 static void msg_queue_poll_event( struct fd *fd, int event )
968 struct msg_queue *queue = get_fd_user( fd );
969 assert( queue->obj.ops == &msg_queue_ops );
971 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
972 else set_fd_events( queue->fd, 0 );
973 wake_up( &queue->obj, 0 );
976 static void thread_input_dump( struct object *obj, int verbose )
978 struct thread_input *input = (struct thread_input *)obj;
979 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
980 input->focus, input->capture, input->active );
983 static void thread_input_destroy( struct object *obj )
985 struct thread_input *input = (struct thread_input *)obj;
987 empty_msg_list( &input->msg_list );
988 if (input->desktop)
990 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
991 release_object( input->desktop );
995 /* fix the thread input data when a window is destroyed */
996 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
998 struct thread_input *input = queue->input;
1000 if (window == input->focus) input->focus = 0;
1001 if (window == input->capture) input->capture = 0;
1002 if (window == input->active) input->active = 0;
1003 if (window == input->menu_owner) input->menu_owner = 0;
1004 if (window == input->move_size) input->move_size = 0;
1005 if (window == input->caret) set_caret_window( input, 0 );
1008 /* check if the specified window can be set in the input data of a given queue */
1009 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1011 struct thread *thread;
1012 int ret = 0;
1014 if (!window) return 1; /* we can always clear the data */
1016 if ((thread = get_window_thread( window )))
1018 ret = (queue->input == thread->queue->input);
1019 if (!ret) set_error( STATUS_ACCESS_DENIED );
1020 release_object( thread );
1022 else set_error( STATUS_INVALID_HANDLE );
1024 return ret;
1027 /* make sure the specified thread has a queue */
1028 int init_thread_queue( struct thread *thread )
1030 if (thread->queue) return 1;
1031 return (create_msg_queue( thread, NULL ) != NULL);
1034 /* attach two thread input data structures */
1035 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1037 struct desktop *desktop;
1038 struct thread_input *input;
1039 int ret;
1041 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1042 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1043 input = (struct thread_input *)grab_object( thread_to->queue->input );
1044 if (input->desktop != desktop)
1046 set_error( STATUS_ACCESS_DENIED );
1047 release_object( input );
1048 release_object( desktop );
1049 return 0;
1051 release_object( desktop );
1053 ret = assign_thread_input( thread_from, input );
1054 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1055 release_object( input );
1056 return ret;
1059 /* detach two thread input data structures */
1060 void detach_thread_input( struct thread *thread_from )
1062 struct thread_input *input;
1064 if ((input = create_thread_input( thread_from )))
1066 assign_thread_input( thread_from, input );
1067 release_object( input );
1072 /* set the next timer to expire */
1073 static void set_next_timer( struct msg_queue *queue )
1075 struct list *ptr;
1077 if (queue->timeout)
1079 remove_timeout_user( queue->timeout );
1080 queue->timeout = NULL;
1082 if ((ptr = list_head( &queue->pending_timers )))
1084 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1085 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1087 /* set/clear QS_TIMER bit */
1088 if (list_empty( &queue->expired_timers ))
1089 clear_queue_bits( queue, QS_TIMER );
1090 else
1091 set_queue_bits( queue, QS_TIMER );
1094 /* find a timer from its window and id */
1095 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1096 unsigned int msg, lparam_t id )
1098 struct list *ptr;
1100 /* we need to search both lists */
1102 LIST_FOR_EACH( ptr, &queue->pending_timers )
1104 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1105 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1107 LIST_FOR_EACH( ptr, &queue->expired_timers )
1109 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1110 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1112 return NULL;
1115 /* callback for the next timer expiration */
1116 static void timer_callback( void *private )
1118 struct msg_queue *queue = private;
1119 struct list *ptr;
1121 queue->timeout = NULL;
1122 /* move on to the next timer */
1123 ptr = list_head( &queue->pending_timers );
1124 list_remove( ptr );
1125 list_add_tail( &queue->expired_timers, ptr );
1126 set_next_timer( queue );
1129 /* link a timer at its rightful place in the queue list */
1130 static void link_timer( struct msg_queue *queue, struct timer *timer )
1132 struct list *ptr;
1134 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1136 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1137 if (t->when >= timer->when) break;
1139 list_add_before( ptr, &timer->entry );
1142 /* remove a timer from the queue timer list and free it */
1143 static void free_timer( struct msg_queue *queue, struct timer *timer )
1145 list_remove( &timer->entry );
1146 free( timer );
1147 set_next_timer( queue );
1150 /* restart an expired timer */
1151 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1153 list_remove( &timer->entry );
1154 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1155 link_timer( queue, timer );
1156 set_next_timer( queue );
1159 /* find an expired timer matching the filtering parameters */
1160 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1161 unsigned int get_first, unsigned int get_last,
1162 int remove )
1164 struct list *ptr;
1166 LIST_FOR_EACH( ptr, &queue->expired_timers )
1168 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1169 if (win && timer->win != win) continue;
1170 if (check_msg_filter( timer->msg, get_first, get_last ))
1172 if (remove) restart_timer( queue, timer );
1173 return timer;
1176 return NULL;
1179 /* add a timer */
1180 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1182 struct timer *timer = mem_alloc( sizeof(*timer) );
1183 if (timer)
1185 timer->rate = max( rate, 1 );
1186 timer->when = current_time + (timeout_t)timer->rate * 10000;
1187 link_timer( queue, timer );
1188 /* check if we replaced the next timer */
1189 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1191 return timer;
1194 /* change the input key state for a given key */
1195 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1197 if (down)
1199 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1200 keystate[key] |= down;
1202 else keystate[key] &= ~0x80;
1205 /* update the input key state for a keyboard message */
1206 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1207 const struct message *msg )
1209 unsigned char key;
1210 int down = 0;
1212 switch (msg->msg)
1214 case WM_LBUTTONDOWN:
1215 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1216 /* fall through */
1217 case WM_LBUTTONUP:
1218 set_input_key_state( keystate, VK_LBUTTON, down );
1219 break;
1220 case WM_MBUTTONDOWN:
1221 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1222 /* fall through */
1223 case WM_MBUTTONUP:
1224 set_input_key_state( keystate, VK_MBUTTON, down );
1225 break;
1226 case WM_RBUTTONDOWN:
1227 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1228 /* fall through */
1229 case WM_RBUTTONUP:
1230 set_input_key_state( keystate, VK_RBUTTON, down );
1231 break;
1232 case WM_XBUTTONDOWN:
1233 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1234 /* fall through */
1235 case WM_XBUTTONUP:
1236 if (msg->wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1237 else if (msg->wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1238 break;
1239 case WM_KEYDOWN:
1240 case WM_SYSKEYDOWN:
1241 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1242 /* fall through */
1243 case WM_KEYUP:
1244 case WM_SYSKEYUP:
1245 key = (unsigned char)msg->wparam;
1246 set_input_key_state( keystate, key, down );
1247 switch(key)
1249 case VK_LCONTROL:
1250 case VK_RCONTROL:
1251 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1252 set_input_key_state( keystate, VK_CONTROL, down );
1253 break;
1254 case VK_LMENU:
1255 case VK_RMENU:
1256 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1257 set_input_key_state( keystate, VK_MENU, down );
1258 break;
1259 case VK_LSHIFT:
1260 case VK_RSHIFT:
1261 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1262 set_input_key_state( keystate, VK_SHIFT, down );
1263 break;
1265 break;
1269 /* release the hardware message currently being processed by the given thread */
1270 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1271 int remove, user_handle_t new_win )
1273 struct thread_input *input = queue->input;
1274 struct message *msg;
1276 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1278 if (msg->unique_id == hw_id) break;
1280 if (&msg->entry == &input->msg_list) return; /* not found */
1282 /* clear the queue bit for that message */
1283 if (remove || new_win)
1285 struct message *other;
1286 int clr_bit;
1288 clr_bit = get_hardware_msg_bit( msg );
1289 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1291 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1293 clr_bit = 0;
1294 break;
1297 if (clr_bit) clear_queue_bits( queue, clr_bit );
1300 if (new_win) /* set the new window */
1302 struct thread *owner = get_window_thread( new_win );
1303 if (owner)
1305 msg->win = new_win;
1306 if (owner->queue->input != input)
1308 list_remove( &msg->entry );
1309 if (merge_message( owner->queue->input, msg ))
1311 free_message( msg );
1312 release_object( owner );
1313 return;
1315 list_add_tail( &owner->queue->input->msg_list, &msg->entry );
1317 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1318 remove = 0;
1319 release_object( owner );
1322 if (remove)
1324 update_input_key_state( input->desktop, input->keystate, msg );
1325 list_remove( &msg->entry );
1326 free_message( msg );
1330 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1332 struct hotkey *hotkey;
1333 unsigned int modifiers = 0;
1335 if (msg->msg != WM_KEYDOWN) return 0;
1337 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1338 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1339 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1340 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1342 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1344 if (hotkey->vkey != msg->wparam) continue;
1345 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1348 return 0;
1350 found:
1351 msg->type = MSG_POSTED;
1352 msg->win = hotkey->win;
1353 msg->msg = WM_HOTKEY;
1354 msg->wparam = hotkey->id;
1355 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1357 free( msg->data );
1358 msg->data = NULL;
1359 msg->data_size = 0;
1361 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1362 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1363 hotkey->queue->hotkey_count++;
1364 return 1;
1367 /* find the window that should receive a given hardware message */
1368 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1369 struct message *msg, unsigned int *msg_code )
1371 struct hardware_msg_data *data = msg->data;
1372 user_handle_t win = 0;
1374 *msg_code = msg->msg;
1375 if (is_keyboard_msg( msg ))
1377 if (input && !(win = input->focus))
1379 win = input->active;
1380 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1383 else /* mouse message */
1385 if (!input || !(win = input->capture))
1387 if (!(win = msg->win) || !is_window_visible( win ) || is_window_transparent( win ))
1388 win = window_from_point( desktop, data->x, data->y );
1391 return win;
1394 /* queue a hardware message into a given thread input */
1395 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1397 user_handle_t win;
1398 struct thread *thread;
1399 struct thread_input *input;
1400 unsigned int msg_code;
1401 struct hardware_msg_data *data = msg->data;
1403 update_input_key_state( desktop, desktop->keystate, msg );
1404 last_input_time = get_tick_count();
1405 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1407 if (is_keyboard_msg( msg ))
1409 if (queue_hotkey_message( desktop, msg )) return;
1410 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1411 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1412 msg->lparam &= ~(KF_EXTENDED << 16);
1414 else
1416 if (msg->msg == WM_MOUSEMOVE)
1418 int x = min( max( data->x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
1419 int y = min( max( data->y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
1420 if (desktop->cursor.x != x || desktop->cursor.y != y) always_queue = 1;
1421 desktop->cursor.x = x;
1422 desktop->cursor.y = y;
1423 desktop->cursor.last_change = get_tick_count();
1425 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1426 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1427 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1428 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1429 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1430 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1431 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1433 data->x = desktop->cursor.x;
1434 data->y = desktop->cursor.y;
1436 if (msg->win && (thread = get_window_thread( msg->win )))
1438 input = thread->queue->input;
1439 release_object( thread );
1441 else input = desktop->foreground_input;
1443 win = find_hardware_message_window( desktop, input, msg, &msg_code );
1444 if (!win || !(thread = get_window_thread(win)))
1446 if (input) update_input_key_state( input->desktop, input->keystate, msg );
1447 free_message( msg );
1448 return;
1450 input = thread->queue->input;
1452 if (win != desktop->cursor.win) always_queue = 1;
1453 desktop->cursor.win = win;
1455 if (!always_queue || merge_message( input, msg )) free_message( msg );
1456 else
1458 msg->unique_id = 0; /* will be set once we return it to the app */
1459 list_add_tail( &input->msg_list, &msg->entry );
1460 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1462 release_object( thread );
1465 /* send the low-level hook message for a given hardware message */
1466 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1467 const hw_input_t *input, struct msg_queue *sender )
1469 struct thread *hook_thread;
1470 struct msg_queue *queue;
1471 struct message *msg;
1472 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1473 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1475 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1476 if (!(queue = hook_thread->queue)) return 0;
1477 if (is_queue_hung( queue )) return 0;
1479 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1481 msg->type = MSG_HOOK_LL;
1482 msg->win = 0;
1483 msg->msg = id;
1484 msg->wparam = hardware_msg->msg;
1485 msg->time = hardware_msg->time;
1486 msg->data_size = hardware_msg->data_size;
1487 msg->result = NULL;
1489 if (input->type == INPUT_KEYBOARD)
1491 unsigned short vkey = input->kbd.vkey;
1492 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1493 msg->lparam = (input->kbd.scan << 16) | vkey;
1495 else msg->lparam = input->mouse.data << 16;
1497 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1498 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1500 free_message( msg );
1501 return 0;
1503 msg->result->hardware_msg = hardware_msg;
1504 msg->result->desktop = (struct desktop *)grab_object( desktop );
1505 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1506 set_queue_bits( queue, QS_SENDMESSAGE );
1507 return 1;
1510 /* queue a hardware message for a mouse event */
1511 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1512 unsigned int hook_flags, struct msg_queue *sender )
1514 struct hardware_msg_data *msg_data;
1515 struct message *msg;
1516 unsigned int i, time, flags;
1517 int wait = 0, x, y;
1519 static const unsigned int messages[] =
1521 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1522 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1523 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1524 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1525 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1526 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1527 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1528 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1529 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1530 0, /* 0x0200 = unused */
1531 0, /* 0x0400 = unused */
1532 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1533 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1536 desktop->cursor.last_change = get_tick_count();
1537 flags = input->mouse.flags;
1538 time = input->mouse.time;
1539 if (!time) time = desktop->cursor.last_change;
1541 if (flags & MOUSEEVENTF_MOVE)
1543 if (flags & MOUSEEVENTF_ABSOLUTE)
1545 x = input->mouse.x;
1546 y = input->mouse.y;
1547 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1548 x == desktop->cursor.x && y == desktop->cursor.y)
1549 flags &= ~MOUSEEVENTF_MOVE;
1551 else
1553 x = desktop->cursor.x + input->mouse.x;
1554 y = desktop->cursor.y + input->mouse.y;
1557 else
1559 x = desktop->cursor.x;
1560 y = desktop->cursor.y;
1563 for (i = 0; i < sizeof(messages)/sizeof(messages[0]); i++)
1565 if (!messages[i]) continue;
1566 if (!(flags & (1 << i))) continue;
1567 flags &= ~(1 << i);
1569 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1570 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1572 free( msg );
1573 return 0;
1575 memset( msg_data, 0, sizeof(*msg_data) );
1577 msg->type = MSG_HARDWARE;
1578 msg->win = get_user_full_handle( win );
1579 msg->msg = messages[i];
1580 msg->wparam = input->mouse.data << 16;
1581 msg->lparam = 0;
1582 msg->time = time;
1583 msg->result = NULL;
1584 msg->data = msg_data;
1585 msg->data_size = sizeof(*msg_data);
1586 msg_data->x = x;
1587 msg_data->y = y;
1588 msg_data->info = input->mouse.info;
1589 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLMHF_INJECTED;
1591 /* specify a sender only when sending the last message */
1592 if (!(flags & ((1 << sizeof(messages)/sizeof(messages[0])) - 1)))
1594 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1595 queue_hardware_message( desktop, msg, 0 );
1597 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1598 queue_hardware_message( desktop, msg, 0 );
1600 return wait;
1603 /* queue a hardware message for a keyboard event */
1604 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1605 unsigned int hook_flags, struct msg_queue *sender )
1607 struct hardware_msg_data *msg_data;
1608 struct message *msg;
1609 unsigned char vkey = input->kbd.vkey;
1610 int wait;
1612 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1613 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1615 free( msg );
1616 return 0;
1618 memset( msg_data, 0, sizeof(*msg_data) );
1620 msg->type = MSG_HARDWARE;
1621 msg->win = get_user_full_handle( win );
1622 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1623 msg->time = input->kbd.time;
1624 msg->result = NULL;
1625 msg->data = msg_data;
1626 msg->data_size = sizeof(*msg_data);
1627 msg_data->info = input->kbd.info;
1628 if (!msg->time) msg->time = get_tick_count();
1629 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLKHF_INJECTED;
1631 if (input->kbd.flags & KEYEVENTF_UNICODE)
1633 msg->wparam = VK_PACKET;
1635 else
1637 unsigned int flags = 0;
1638 switch (vkey)
1640 case VK_MENU:
1641 case VK_LMENU:
1642 case VK_RMENU:
1643 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1644 break;
1645 case VK_CONTROL:
1646 case VK_LCONTROL:
1647 case VK_RCONTROL:
1648 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1649 break;
1650 case VK_SHIFT:
1651 case VK_LSHIFT:
1652 case VK_RSHIFT:
1653 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1654 break;
1656 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1657 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1658 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1659 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1661 msg->wparam = vkey;
1662 msg->lparam |= flags << 16;
1663 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1666 msg->msg = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1668 switch (vkey)
1670 case VK_LMENU:
1671 case VK_RMENU:
1672 if (input->kbd.flags & KEYEVENTF_KEYUP)
1674 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1675 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1676 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1677 msg->msg = WM_SYSKEYUP;
1678 desktop->keystate[VK_MENU] &= ~0x02;
1680 else
1682 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1683 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1684 msg->msg = WM_SYSKEYDOWN;
1685 desktop->keystate[VK_MENU] |= 0x02;
1687 break;
1689 case VK_LCONTROL:
1690 case VK_RCONTROL:
1691 /* send WM_SYSKEYUP on release if Alt still pressed */
1692 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1693 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1694 msg->msg = WM_SYSKEYUP;
1695 desktop->keystate[VK_MENU] &= ~0x02;
1696 break;
1698 default:
1699 /* send WM_SYSKEY for Alt-anykey and for F10 */
1700 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1701 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1702 /* fall through */
1703 case VK_F10:
1704 msg->msg = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1705 desktop->keystate[VK_MENU] &= ~0x02;
1706 break;
1708 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1709 queue_hardware_message( desktop, msg, 1 );
1711 return wait;
1714 /* queue a hardware message for a custom type of event */
1715 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1716 const hw_input_t *input )
1718 struct hardware_msg_data *msg_data;
1719 struct message *msg;
1721 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
1722 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1724 free( msg );
1725 return;
1727 memset( msg_data, 0, sizeof(*msg_data) );
1729 msg->type = MSG_HARDWARE;
1730 msg->win = get_user_full_handle( win );
1731 msg->msg = input->hw.msg;
1732 msg->wparam = 0;
1733 msg->lparam = input->hw.lparam;
1734 msg->time = get_tick_count();
1735 msg->result = NULL;
1736 msg->data = msg_data;
1737 msg->data_size = sizeof(*msg_data);
1739 queue_hardware_message( desktop, msg, 1 );
1742 /* check message filter for a hardware message */
1743 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1744 user_handle_t filter_win, unsigned int first, unsigned int last )
1746 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1748 /* we can only test the window for a keyboard message since the
1749 * dest window for a mouse message depends on hittest */
1750 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1751 return 0;
1752 /* the message code is final for a keyboard message, we can simply check it */
1753 return check_msg_filter( msg_code, first, last );
1755 else /* mouse message */
1757 /* we need to check all possible values that the message can have in the end */
1759 if (check_msg_filter( msg_code, first, last )) return 1;
1760 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1762 /* all other messages can become non-client messages */
1763 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1765 /* clicks can become double-clicks or non-client double-clicks */
1766 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1767 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1769 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1770 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1772 return 0;
1777 /* find a hardware message for the given queue */
1778 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1779 unsigned int first, unsigned int last, struct get_message_reply *reply )
1781 struct thread_input *input = thread->queue->input;
1782 struct thread *win_thread;
1783 struct list *ptr;
1784 user_handle_t win;
1785 int clear_bits, got_one = 0;
1786 unsigned int msg_code;
1788 ptr = list_head( &input->msg_list );
1789 if (hw_id)
1791 while (ptr)
1793 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1794 if (msg->unique_id == hw_id) break;
1795 ptr = list_next( &input->msg_list, ptr );
1797 if (!ptr) ptr = list_head( &input->msg_list );
1798 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1801 if (ptr == list_head( &input->msg_list ))
1802 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1803 else
1804 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1806 while (ptr)
1808 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1809 struct hardware_msg_data *data = msg->data;
1811 ptr = list_next( &input->msg_list, ptr );
1812 win = find_hardware_message_window( input->desktop, input, msg, &msg_code );
1813 if (!win || !(win_thread = get_window_thread( win )))
1815 /* no window at all, remove it */
1816 update_input_key_state( input->desktop, input->keystate, msg );
1817 list_remove( &msg->entry );
1818 free_message( msg );
1819 continue;
1821 if (win_thread != thread)
1823 if (win_thread->queue->input == input)
1825 /* wake the other thread */
1826 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1827 got_one = 1;
1829 else
1831 /* for another thread input, drop it */
1832 update_input_key_state( input->desktop, input->keystate, msg );
1833 list_remove( &msg->entry );
1834 free_message( msg );
1836 release_object( win_thread );
1837 continue;
1839 release_object( win_thread );
1841 /* if we already got a message for another thread, or if it doesn't
1842 * match the filter we skip it */
1843 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1845 clear_bits &= ~get_hardware_msg_bit( msg );
1846 continue;
1848 /* now we can return it */
1849 if (!msg->unique_id) msg->unique_id = get_unique_id();
1850 reply->type = MSG_HARDWARE;
1851 reply->win = win;
1852 reply->msg = msg_code;
1853 reply->wparam = msg->wparam;
1854 reply->lparam = msg->lparam;
1855 reply->time = msg->time;
1857 data->hw_id = msg->unique_id;
1858 set_reply_data( msg->data, msg->data_size );
1859 return 1;
1861 /* nothing found, clear the hardware queue bits */
1862 clear_queue_bits( thread->queue, clear_bits );
1863 return 0;
1866 /* increment (or decrement if 'incr' is negative) the queue paint count */
1867 void inc_queue_paint_count( struct thread *thread, int incr )
1869 struct msg_queue *queue = thread->queue;
1871 assert( queue );
1873 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1875 if (queue->paint_count)
1876 set_queue_bits( queue, QS_PAINT );
1877 else
1878 clear_queue_bits( queue, QS_PAINT );
1882 /* remove all messages and timers belonging to a certain window */
1883 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1885 struct msg_queue *queue = thread->queue;
1886 struct list *ptr;
1887 int i;
1889 if (!queue) return;
1891 /* remove timers */
1893 ptr = list_head( &queue->pending_timers );
1894 while (ptr)
1896 struct list *next = list_next( &queue->pending_timers, ptr );
1897 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1898 if (timer->win == win) free_timer( queue, timer );
1899 ptr = next;
1901 ptr = list_head( &queue->expired_timers );
1902 while (ptr)
1904 struct list *next = list_next( &queue->expired_timers, ptr );
1905 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1906 if (timer->win == win) free_timer( queue, timer );
1907 ptr = next;
1910 /* remove messages */
1911 for (i = 0; i < NB_MSG_KINDS; i++)
1913 struct list *ptr, *next;
1915 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1917 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1918 if (msg->win == win)
1920 if (msg->msg == WM_QUIT && !queue->quit_message)
1922 queue->quit_message = 1;
1923 queue->exit_code = msg->wparam;
1925 remove_queue_message( queue, msg, i );
1930 thread_input_cleanup_window( queue, win );
1933 /* post a message to a window; used by socket handling */
1934 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
1936 struct message *msg;
1937 struct thread *thread = get_window_thread( win );
1939 if (!thread) return;
1941 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1943 msg->type = MSG_POSTED;
1944 msg->win = get_user_full_handle( win );
1945 msg->msg = message;
1946 msg->wparam = wparam;
1947 msg->lparam = lparam;
1948 msg->time = get_tick_count();
1949 msg->result = NULL;
1950 msg->data = NULL;
1951 msg->data_size = 0;
1953 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1954 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1955 if (message == WM_HOTKEY)
1957 set_queue_bits( thread->queue, QS_HOTKEY );
1958 thread->queue->hotkey_count++;
1961 release_object( thread );
1964 /* post a win event */
1965 void post_win_event( struct thread *thread, unsigned int event,
1966 user_handle_t win, unsigned int object_id,
1967 unsigned int child_id, client_ptr_t hook_proc,
1968 const WCHAR *module, data_size_t module_size,
1969 user_handle_t hook)
1971 struct message *msg;
1973 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1975 struct winevent_msg_data *data;
1977 msg->type = MSG_WINEVENT;
1978 msg->win = get_user_full_handle( win );
1979 msg->msg = event;
1980 msg->wparam = object_id;
1981 msg->lparam = child_id;
1982 msg->time = get_tick_count();
1983 msg->result = NULL;
1985 if ((data = malloc( sizeof(*data) + module_size )))
1987 data->hook = hook;
1988 data->tid = get_thread_id( current );
1989 data->hook_proc = hook_proc;
1990 memcpy( data + 1, module, module_size );
1992 msg->data = data;
1993 msg->data_size = sizeof(*data) + module_size;
1995 if (debug_level > 1)
1996 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
1997 get_thread_id(thread), event, win, object_id, child_id );
1998 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1999 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2001 else
2002 free( msg );
2006 /* free all hotkeys on a desktop, optionally filtering by window */
2007 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2009 struct hotkey *hotkey, *hotkey2;
2011 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2013 if (!window || hotkey->win == window)
2015 list_remove( &hotkey->entry );
2016 free( hotkey );
2022 /* check if the thread owning the window is hung */
2023 DECL_HANDLER(is_window_hung)
2025 struct thread *thread;
2027 thread = get_window_thread( req->win );
2028 if (thread)
2030 reply->is_hung = is_queue_hung( thread->queue );
2031 release_object( thread );
2033 else reply->is_hung = 0;
2037 /* get the message queue of the current thread */
2038 DECL_HANDLER(get_msg_queue)
2040 struct msg_queue *queue = get_current_queue();
2042 reply->handle = 0;
2043 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2047 /* set the file descriptor associated to the current thread queue */
2048 DECL_HANDLER(set_queue_fd)
2050 struct msg_queue *queue = get_current_queue();
2051 struct file *file;
2052 int unix_fd;
2054 if (queue->fd) /* fd can only be set once */
2056 set_error( STATUS_ACCESS_DENIED );
2057 return;
2059 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2061 if ((unix_fd = get_file_unix_fd( file )) != -1)
2063 if ((unix_fd = dup( unix_fd )) != -1)
2064 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2065 else
2066 file_set_error();
2068 release_object( file );
2072 /* set the current message queue wakeup mask */
2073 DECL_HANDLER(set_queue_mask)
2075 struct msg_queue *queue = get_current_queue();
2077 if (queue)
2079 queue->wake_mask = req->wake_mask;
2080 queue->changed_mask = req->changed_mask;
2081 reply->wake_bits = queue->wake_bits;
2082 reply->changed_bits = queue->changed_bits;
2083 if (is_signaled( queue ))
2085 /* if skip wait is set, do what would have been done in the subsequent wait */
2086 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
2087 else wake_up( &queue->obj, 0 );
2093 /* get the current message queue status */
2094 DECL_HANDLER(get_queue_status)
2096 struct msg_queue *queue = current->queue;
2097 if (queue)
2099 reply->wake_bits = queue->wake_bits;
2100 reply->changed_bits = queue->changed_bits;
2101 if (req->clear) queue->changed_bits = 0;
2103 else reply->wake_bits = reply->changed_bits = 0;
2107 /* send a message to a thread queue */
2108 DECL_HANDLER(send_message)
2110 struct message *msg;
2111 struct msg_queue *send_queue = get_current_queue();
2112 struct msg_queue *recv_queue = NULL;
2113 struct thread *thread = NULL;
2115 if (!(thread = get_thread_from_id( req->id ))) return;
2117 if (!(recv_queue = thread->queue))
2119 set_error( STATUS_INVALID_PARAMETER );
2120 release_object( thread );
2121 return;
2123 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2125 set_error( STATUS_TIMEOUT );
2126 release_object( thread );
2127 return;
2130 if ((msg = mem_alloc( sizeof(*msg) )))
2132 msg->type = req->type;
2133 msg->win = get_user_full_handle( req->win );
2134 msg->msg = req->msg;
2135 msg->wparam = req->wparam;
2136 msg->lparam = req->lparam;
2137 msg->time = get_tick_count();
2138 msg->result = NULL;
2139 msg->data = NULL;
2140 msg->data_size = get_req_data_size();
2142 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2144 free( msg );
2145 release_object( thread );
2146 return;
2149 switch(msg->type)
2151 case MSG_OTHER_PROCESS:
2152 case MSG_ASCII:
2153 case MSG_UNICODE:
2154 case MSG_CALLBACK:
2155 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2157 free_message( msg );
2158 break;
2160 /* fall through */
2161 case MSG_NOTIFY:
2162 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2163 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2164 break;
2165 case MSG_POSTED:
2166 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2167 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2168 if (msg->msg == WM_HOTKEY)
2170 set_queue_bits( recv_queue, QS_HOTKEY );
2171 recv_queue->hotkey_count++;
2173 break;
2174 case MSG_HARDWARE: /* should use send_hardware_message instead */
2175 case MSG_CALLBACK_RESULT: /* cannot send this one */
2176 case MSG_HOOK_LL: /* generated internally */
2177 default:
2178 set_error( STATUS_INVALID_PARAMETER );
2179 free( msg );
2180 break;
2183 release_object( thread );
2186 /* send a hardware message to a thread queue */
2187 DECL_HANDLER(send_hardware_message)
2189 struct thread *thread = NULL;
2190 struct desktop *desktop;
2191 struct msg_queue *sender = get_current_queue();
2192 data_size_t size = min( 256, get_reply_max_size() );
2194 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2196 if (req->win)
2198 if (!(thread = get_window_thread( req->win ))) return;
2199 if (desktop != thread->queue->input->desktop)
2201 /* don't allow queuing events to a different desktop */
2202 release_object( desktop );
2203 return;
2207 reply->prev_x = desktop->cursor.x;
2208 reply->prev_y = desktop->cursor.y;
2210 switch (req->input.type)
2212 case INPUT_MOUSE:
2213 reply->wait = queue_mouse_message( desktop, req->win, &req->input, req->flags, sender );
2214 break;
2215 case INPUT_KEYBOARD:
2216 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, req->flags, sender );
2217 break;
2218 case INPUT_HARDWARE:
2219 queue_custom_hardware_message( desktop, req->win, &req->input );
2220 break;
2221 default:
2222 set_error( STATUS_INVALID_PARAMETER );
2224 if (thread) release_object( thread );
2226 reply->new_x = desktop->cursor.x;
2227 reply->new_y = desktop->cursor.y;
2228 set_reply_data( desktop->keystate, size );
2229 release_object( desktop );
2232 /* post a quit message to the current queue */
2233 DECL_HANDLER(post_quit_message)
2235 struct msg_queue *queue = get_current_queue();
2237 if (!queue)
2238 return;
2240 queue->quit_message = 1;
2241 queue->exit_code = req->exit_code;
2242 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2245 /* get a message from the current queue */
2246 DECL_HANDLER(get_message)
2248 struct timer *timer;
2249 struct list *ptr;
2250 struct msg_queue *queue = get_current_queue();
2251 user_handle_t get_win = get_user_full_handle( req->get_win );
2252 unsigned int filter = req->flags >> 16;
2254 reply->active_hooks = get_active_hooks();
2256 if (!queue) return;
2257 queue->last_get_msg = current_time;
2258 if (!filter) filter = QS_ALLINPUT;
2260 /* first check for sent messages */
2261 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2263 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2264 receive_message( queue, msg, reply );
2265 return;
2268 /* clear changed bits so we can wait on them if we don't find a message */
2269 if (filter & QS_POSTMESSAGE)
2271 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2272 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2274 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2275 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2277 /* then check for posted messages */
2278 if ((filter & QS_POSTMESSAGE) &&
2279 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2280 return;
2282 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2283 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2284 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2285 return;
2287 /* only check for quit messages if not posted messages pending.
2288 * note: the quit message isn't filtered */
2289 if (get_quit_message( queue, req->flags, reply ))
2290 return;
2292 /* then check for any raw hardware message */
2293 if ((filter & QS_INPUT) &&
2294 filter_contains_hw_range( req->get_first, req->get_last ) &&
2295 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
2296 return;
2298 /* now check for WM_PAINT */
2299 if ((filter & QS_PAINT) &&
2300 queue->paint_count &&
2301 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2302 (reply->win = find_window_to_repaint( get_win, current )))
2304 reply->type = MSG_POSTED;
2305 reply->msg = WM_PAINT;
2306 reply->wparam = 0;
2307 reply->lparam = 0;
2308 reply->time = get_tick_count();
2309 return;
2312 /* now check for timer */
2313 if ((filter & QS_TIMER) &&
2314 (timer = find_expired_timer( queue, get_win, req->get_first,
2315 req->get_last, (req->flags & PM_REMOVE) )))
2317 reply->type = MSG_POSTED;
2318 reply->win = timer->win;
2319 reply->msg = timer->msg;
2320 reply->wparam = timer->id;
2321 reply->lparam = timer->lparam;
2322 reply->time = get_tick_count();
2323 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2324 set_event( current->process->idle_event );
2325 return;
2328 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2329 queue->wake_mask = req->wake_mask;
2330 queue->changed_mask = req->changed_mask;
2331 set_error( STATUS_PENDING ); /* FIXME */
2335 /* reply to a sent message */
2336 DECL_HANDLER(reply_message)
2338 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2339 else if (current->queue->recv_result)
2340 reply_message( current->queue, req->result, 0, req->remove,
2341 get_req_data(), get_req_data_size() );
2345 /* accept the current hardware message */
2346 DECL_HANDLER(accept_hardware_message)
2348 if (current->queue)
2349 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
2350 else
2351 set_error( STATUS_ACCESS_DENIED );
2355 /* retrieve the reply for the last message sent */
2356 DECL_HANDLER(get_message_reply)
2358 struct message_result *result;
2359 struct list *entry;
2360 struct msg_queue *queue = current->queue;
2362 if (queue)
2364 set_error( STATUS_PENDING );
2365 reply->result = 0;
2367 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2369 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2370 if (result->replied || req->cancel)
2372 if (result->replied)
2374 reply->result = result->result;
2375 set_error( result->error );
2376 if (result->data)
2378 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2379 set_reply_data_ptr( result->data, data_len );
2380 result->data = NULL;
2381 result->data_size = 0;
2384 remove_result_from_sender( result );
2386 entry = list_head( &queue->send_result );
2387 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2388 else
2390 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2391 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
2395 else set_error( STATUS_ACCESS_DENIED );
2399 /* set a window timer */
2400 DECL_HANDLER(set_win_timer)
2402 struct timer *timer;
2403 struct msg_queue *queue;
2404 struct thread *thread = NULL;
2405 user_handle_t win = 0;
2406 lparam_t id = req->id;
2408 if (req->win)
2410 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2412 set_error( STATUS_INVALID_HANDLE );
2413 return;
2415 if (thread->process != current->process)
2417 release_object( thread );
2418 set_error( STATUS_ACCESS_DENIED );
2419 return;
2421 queue = thread->queue;
2422 /* remove it if it existed already */
2423 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2425 else
2427 queue = get_current_queue();
2428 /* look for a timer with this id */
2429 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2431 /* free and reuse id */
2432 free_timer( queue, timer );
2434 else
2436 /* find a free id for it */
2439 id = queue->next_timer_id;
2440 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2442 while (find_timer( queue, 0, req->msg, id ));
2446 if ((timer = set_timer( queue, req->rate )))
2448 timer->win = win;
2449 timer->msg = req->msg;
2450 timer->id = id;
2451 timer->lparam = req->lparam;
2452 reply->id = id;
2454 if (thread) release_object( thread );
2457 /* kill a window timer */
2458 DECL_HANDLER(kill_win_timer)
2460 struct timer *timer;
2461 struct thread *thread;
2462 user_handle_t win = 0;
2464 if (req->win)
2466 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2468 set_error( STATUS_INVALID_HANDLE );
2469 return;
2471 if (thread->process != current->process)
2473 release_object( thread );
2474 set_error( STATUS_ACCESS_DENIED );
2475 return;
2478 else thread = (struct thread *)grab_object( current );
2480 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2481 free_timer( thread->queue, timer );
2482 else
2483 set_error( STATUS_INVALID_PARAMETER );
2485 release_object( thread );
2488 DECL_HANDLER(register_hotkey)
2490 struct desktop *desktop;
2491 user_handle_t win_handle = req->window;
2492 struct hotkey *hotkey;
2493 struct hotkey *new_hotkey = NULL;
2494 struct thread *thread;
2495 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2497 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2499 if (win_handle)
2501 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2503 release_object( desktop );
2504 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2505 return;
2508 thread = get_window_thread( win_handle );
2509 if (thread) release_object( thread );
2511 if (thread != current)
2513 release_object( desktop );
2514 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2515 return;
2519 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2521 if (req->vkey == hotkey->vkey &&
2522 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2524 release_object( desktop );
2525 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2526 return;
2528 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2529 new_hotkey = hotkey;
2532 if (new_hotkey)
2534 reply->replaced = 1;
2535 reply->flags = new_hotkey->flags;
2536 reply->vkey = new_hotkey->vkey;
2538 else
2540 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2541 if (new_hotkey)
2543 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2544 new_hotkey->queue = current->queue;
2545 new_hotkey->win = win_handle;
2546 new_hotkey->id = req->id;
2550 if (new_hotkey)
2552 new_hotkey->flags = req->flags;
2553 new_hotkey->vkey = req->vkey;
2556 release_object( desktop );
2559 DECL_HANDLER(unregister_hotkey)
2561 struct desktop *desktop;
2562 user_handle_t win_handle = req->window;
2563 struct hotkey *hotkey;
2564 struct thread *thread;
2566 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2568 if (win_handle)
2570 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2572 release_object( desktop );
2573 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2574 return;
2577 thread = get_window_thread( win_handle );
2578 if (thread) release_object( thread );
2580 if (thread != current)
2582 release_object( desktop );
2583 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2584 return;
2588 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2590 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2591 goto found;
2594 release_object( desktop );
2595 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2596 return;
2598 found:
2599 reply->flags = hotkey->flags;
2600 reply->vkey = hotkey->vkey;
2601 list_remove( &hotkey->entry );
2602 free( hotkey );
2603 release_object( desktop );
2606 /* attach (or detach) thread inputs */
2607 DECL_HANDLER(attach_thread_input)
2609 struct thread *thread_from = get_thread_from_id( req->tid_from );
2610 struct thread *thread_to = get_thread_from_id( req->tid_to );
2612 if (!thread_from || !thread_to)
2614 if (thread_from) release_object( thread_from );
2615 if (thread_to) release_object( thread_to );
2616 return;
2618 if (thread_from != thread_to)
2620 if (req->attach) attach_thread_input( thread_from, thread_to );
2621 else
2623 if (thread_from->queue && thread_to->queue &&
2624 thread_from->queue->input == thread_to->queue->input)
2625 detach_thread_input( thread_from );
2626 else
2627 set_error( STATUS_ACCESS_DENIED );
2630 else set_error( STATUS_ACCESS_DENIED );
2631 release_object( thread_from );
2632 release_object( thread_to );
2636 /* get thread input data */
2637 DECL_HANDLER(get_thread_input)
2639 struct thread *thread = NULL;
2640 struct desktop *desktop;
2641 struct thread_input *input;
2643 if (req->tid)
2645 if (!(thread = get_thread_from_id( req->tid ))) return;
2646 if (!(desktop = get_thread_desktop( thread, 0 )))
2648 release_object( thread );
2649 return;
2651 input = thread->queue ? thread->queue->input : NULL;
2653 else
2655 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2656 input = desktop->foreground_input; /* get the foreground thread info */
2659 if (input)
2661 reply->focus = input->focus;
2662 reply->capture = input->capture;
2663 reply->active = input->active;
2664 reply->menu_owner = input->menu_owner;
2665 reply->move_size = input->move_size;
2666 reply->caret = input->caret;
2667 reply->cursor = input->cursor;
2668 reply->show_count = input->cursor_count;
2669 reply->rect = input->caret_rect;
2672 /* foreground window is active window of foreground thread */
2673 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2674 if (thread) release_object( thread );
2675 release_object( desktop );
2679 /* retrieve queue keyboard state for a given thread */
2680 DECL_HANDLER(get_key_state)
2682 struct thread *thread;
2683 struct desktop *desktop;
2684 data_size_t size = min( 256, get_reply_max_size() );
2686 if (!req->tid) /* get global async key state */
2688 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2689 if (req->key >= 0)
2691 reply->state = desktop->keystate[req->key & 0xff];
2692 desktop->keystate[req->key & 0xff] &= ~0x40;
2694 set_reply_data( desktop->keystate, size );
2695 release_object( desktop );
2697 else
2699 if (!(thread = get_thread_from_id( req->tid ))) return;
2700 if (thread->queue)
2702 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2703 set_reply_data( thread->queue->input->keystate, size );
2705 release_object( thread );
2710 /* set queue keyboard state for a given thread */
2711 DECL_HANDLER(set_key_state)
2713 struct thread *thread;
2714 struct desktop *desktop;
2715 data_size_t size = min( 256, get_req_data_size() );
2717 if (!req->tid) /* set global async key state */
2719 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2720 memcpy( desktop->keystate, get_req_data(), size );
2721 release_object( desktop );
2723 else
2725 if (!(thread = get_thread_from_id( req->tid ))) return;
2726 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
2727 if (req->async && (desktop = get_thread_desktop( thread, 0 )))
2729 memcpy( desktop->keystate, get_req_data(), size );
2730 release_object( desktop );
2732 release_object( thread );
2737 /* set the system foreground window */
2738 DECL_HANDLER(set_foreground_window)
2740 struct thread *thread = NULL;
2741 struct desktop *desktop;
2742 struct msg_queue *queue = get_current_queue();
2744 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2745 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
2746 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
2747 reply->send_msg_new = FALSE;
2749 if (is_top_level_window( req->handle ) &&
2750 ((thread = get_window_thread( req->handle ))) &&
2751 (thread->queue->input->desktop == desktop))
2753 set_foreground_input( desktop, thread->queue->input );
2754 reply->send_msg_new = (desktop->foreground_input != queue->input);
2756 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2758 if (thread) release_object( thread );
2759 release_object( desktop );
2763 /* set the current thread focus window */
2764 DECL_HANDLER(set_focus_window)
2766 struct msg_queue *queue = get_current_queue();
2768 reply->previous = 0;
2769 if (queue && check_queue_input_window( queue, req->handle ))
2771 reply->previous = queue->input->focus;
2772 queue->input->focus = get_user_full_handle( req->handle );
2777 /* set the current thread active window */
2778 DECL_HANDLER(set_active_window)
2780 struct msg_queue *queue = get_current_queue();
2782 reply->previous = 0;
2783 if (queue && check_queue_input_window( queue, req->handle ))
2785 if (!req->handle || make_window_active( req->handle ))
2787 reply->previous = queue->input->active;
2788 queue->input->active = get_user_full_handle( req->handle );
2790 else set_error( STATUS_INVALID_HANDLE );
2795 /* set the current thread capture window */
2796 DECL_HANDLER(set_capture_window)
2798 struct msg_queue *queue = get_current_queue();
2800 reply->previous = reply->full_handle = 0;
2801 if (queue && check_queue_input_window( queue, req->handle ))
2803 struct thread_input *input = queue->input;
2805 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2806 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2808 set_error(STATUS_ACCESS_DENIED);
2809 return;
2811 reply->previous = input->capture;
2812 input->capture = get_user_full_handle( req->handle );
2813 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2814 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2815 reply->full_handle = input->capture;
2820 /* Set the current thread caret window */
2821 DECL_HANDLER(set_caret_window)
2823 struct msg_queue *queue = get_current_queue();
2825 reply->previous = 0;
2826 if (queue && check_queue_input_window( queue, req->handle ))
2828 struct thread_input *input = queue->input;
2830 reply->previous = input->caret;
2831 reply->old_rect = input->caret_rect;
2832 reply->old_hide = input->caret_hide;
2833 reply->old_state = input->caret_state;
2835 set_caret_window( input, get_user_full_handle(req->handle) );
2836 input->caret_rect.right = input->caret_rect.left + req->width;
2837 input->caret_rect.bottom = input->caret_rect.top + req->height;
2842 /* Set the current thread caret information */
2843 DECL_HANDLER(set_caret_info)
2845 struct msg_queue *queue = get_current_queue();
2846 struct thread_input *input;
2848 if (!queue) return;
2849 input = queue->input;
2850 reply->full_handle = input->caret;
2851 reply->old_rect = input->caret_rect;
2852 reply->old_hide = input->caret_hide;
2853 reply->old_state = input->caret_state;
2855 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2857 set_error( STATUS_ACCESS_DENIED );
2858 return;
2860 if (req->flags & SET_CARET_POS)
2862 input->caret_rect.right += req->x - input->caret_rect.left;
2863 input->caret_rect.bottom += req->y - input->caret_rect.top;
2864 input->caret_rect.left = req->x;
2865 input->caret_rect.top = req->y;
2867 if (req->flags & SET_CARET_HIDE)
2869 input->caret_hide += req->hide;
2870 if (input->caret_hide < 0) input->caret_hide = 0;
2872 if (req->flags & SET_CARET_STATE)
2874 if (req->state == -1) input->caret_state = !input->caret_state;
2875 else input->caret_state = !!req->state;
2880 /* get the time of the last input event */
2881 DECL_HANDLER(get_last_input_time)
2883 reply->time = last_input_time;
2886 /* set/get the current cursor */
2887 DECL_HANDLER(set_cursor)
2889 struct msg_queue *queue = get_current_queue();
2890 struct thread_input *input;
2892 if (!queue) return;
2893 input = queue->input;
2895 reply->prev_handle = input->cursor;
2896 reply->prev_count = input->cursor_count;
2897 reply->prev_x = input->desktop->cursor.x;
2898 reply->prev_y = input->desktop->cursor.y;
2900 if (req->flags & SET_CURSOR_HANDLE)
2902 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
2904 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
2905 return;
2907 input->cursor = req->handle;
2909 if (req->flags & SET_CURSOR_COUNT)
2911 queue->cursor_count += req->show_count;
2912 input->cursor_count += req->show_count;
2914 if (req->flags & SET_CURSOR_POS)
2916 set_cursor_pos( input->desktop, req->x, req->y );
2918 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
2920 struct desktop *desktop = input->desktop;
2922 /* only the desktop owner can set the message */
2923 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
2924 desktop->cursor.clip_msg = req->clip_msg;
2926 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip );
2929 reply->new_x = input->desktop->cursor.x;
2930 reply->new_y = input->desktop->cursor.y;
2931 reply->new_clip = input->desktop->cursor.clip;
2932 reply->last_change = input->desktop->cursor.last_change;