makedep: Add support for specifying an object directory different from the current...
[wine.git] / server / queue.c
blobd919f019b591c3048d5e706879abed52c574f85c
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 wake_get_msg; /* wakeup mask of last get_message */
121 unsigned int changed_bits; /* changed wakeup bits */
122 unsigned int changed_mask; /* changed wakeup mask */
123 unsigned int changed_get_msg; /* changed wakeup mask of last get_message */
124 int paint_count; /* pending paint messages count */
125 int hotkey_count; /* pending hotkey messages count */
126 int quit_message; /* is there a pending quit message? */
127 int exit_code; /* exit code of pending quit message */
128 int cursor_count; /* per-queue cursor show count */
129 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
130 struct list send_result; /* stack of sent messages waiting for result */
131 struct list callback_result; /* list of callback messages waiting for result */
132 struct message_result *recv_result; /* stack of received messages waiting for result */
133 struct list pending_timers; /* list of pending timers */
134 struct list expired_timers; /* list of expired timers */
135 lparam_t next_timer_id; /* id for the next timer with a 0 window */
136 struct timeout_user *timeout; /* timeout for next timer to expire */
137 struct thread_input *input; /* thread input descriptor */
138 struct hook_table *hooks; /* hook table */
139 timeout_t last_get_msg; /* time of last get message call */
142 struct hotkey
144 struct list entry; /* entry in desktop hotkey list */
145 struct msg_queue *queue; /* queue owning this hotkey */
146 user_handle_t win; /* window handle */
147 int id; /* hotkey id */
148 unsigned int vkey; /* virtual key code */
149 unsigned int flags; /* key modifiers */
152 static void msg_queue_dump( struct object *obj, int verbose );
153 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
154 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
155 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
156 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
157 static void msg_queue_destroy( struct object *obj );
158 static void msg_queue_poll_event( struct fd *fd, int event );
159 static void thread_input_dump( struct object *obj, int verbose );
160 static void thread_input_destroy( struct object *obj );
161 static void timer_callback( void *private );
163 static const struct object_ops msg_queue_ops =
165 sizeof(struct msg_queue), /* size */
166 msg_queue_dump, /* dump */
167 no_get_type, /* get_type */
168 msg_queue_add_queue, /* add_queue */
169 msg_queue_remove_queue, /* remove_queue */
170 msg_queue_signaled, /* signaled */
171 msg_queue_satisfied, /* satisfied */
172 no_signal, /* signal */
173 no_get_fd, /* get_fd */
174 no_map_access, /* map_access */
175 default_get_sd, /* get_sd */
176 default_set_sd, /* set_sd */
177 no_lookup_name, /* lookup_name */
178 no_open_file, /* open_file */
179 no_close_handle, /* close_handle */
180 msg_queue_destroy /* destroy */
183 static const struct fd_ops msg_queue_fd_ops =
185 NULL, /* get_poll_events */
186 msg_queue_poll_event, /* poll_event */
187 NULL, /* flush */
188 NULL, /* get_fd_type */
189 NULL, /* ioctl */
190 NULL, /* queue_async */
191 NULL, /* reselect_async */
192 NULL /* cancel async */
196 static const struct object_ops thread_input_ops =
198 sizeof(struct thread_input), /* size */
199 thread_input_dump, /* dump */
200 no_get_type, /* get_type */
201 no_add_queue, /* add_queue */
202 NULL, /* remove_queue */
203 NULL, /* signaled */
204 NULL, /* satisfied */
205 no_signal, /* signal */
206 no_get_fd, /* get_fd */
207 no_map_access, /* map_access */
208 default_get_sd, /* get_sd */
209 default_set_sd, /* set_sd */
210 no_lookup_name, /* lookup_name */
211 no_open_file, /* open_file */
212 no_close_handle, /* close_handle */
213 thread_input_destroy /* destroy */
216 /* pointer to input structure of foreground thread */
217 static unsigned int last_input_time;
219 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
220 static void free_message( struct message *msg );
222 /* set the caret window in a given thread input */
223 static void set_caret_window( struct thread_input *input, user_handle_t win )
225 if (!win || win != input->caret)
227 input->caret_rect.left = 0;
228 input->caret_rect.top = 0;
229 input->caret_rect.right = 0;
230 input->caret_rect.bottom = 0;
232 input->caret = win;
233 input->caret_hide = 1;
234 input->caret_state = 0;
237 /* create a thread input object */
238 static struct thread_input *create_thread_input( struct thread *thread )
240 struct thread_input *input;
242 if ((input = alloc_object( &thread_input_ops )))
244 input->focus = 0;
245 input->capture = 0;
246 input->active = 0;
247 input->menu_owner = 0;
248 input->move_size = 0;
249 input->cursor = 0;
250 input->cursor_count = 0;
251 list_init( &input->msg_list );
252 set_caret_window( input, 0 );
253 memset( input->keystate, 0, sizeof(input->keystate) );
255 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
257 release_object( input );
258 return NULL;
261 return input;
264 /* create a message queue object */
265 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
267 struct thread_input *new_input = NULL;
268 struct msg_queue *queue;
269 int i;
271 if (!input)
273 if (!(new_input = create_thread_input( thread ))) return NULL;
274 input = new_input;
277 if ((queue = alloc_object( &msg_queue_ops )))
279 queue->fd = NULL;
280 queue->wake_bits = 0;
281 queue->wake_mask = 0;
282 queue->wake_get_msg = 0;
283 queue->changed_bits = 0;
284 queue->changed_mask = 0;
285 queue->changed_get_msg = 0;
286 queue->paint_count = 0;
287 queue->hotkey_count = 0;
288 queue->quit_message = 0;
289 queue->cursor_count = 0;
290 queue->recv_result = NULL;
291 queue->next_timer_id = 0x7fff;
292 queue->timeout = NULL;
293 queue->input = (struct thread_input *)grab_object( input );
294 queue->hooks = NULL;
295 queue->last_get_msg = current_time;
296 list_init( &queue->send_result );
297 list_init( &queue->callback_result );
298 list_init( &queue->pending_timers );
299 list_init( &queue->expired_timers );
300 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
302 thread->queue = queue;
304 if (new_input) release_object( new_input );
305 return queue;
308 /* free the message queue of a thread at thread exit */
309 void free_msg_queue( struct thread *thread )
311 remove_thread_hooks( thread );
312 if (!thread->queue) return;
313 release_object( thread->queue );
314 thread->queue = NULL;
317 /* change the thread input data of a given thread */
318 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
320 struct msg_queue *queue = thread->queue;
322 if (!queue)
324 thread->queue = create_msg_queue( thread, new_input );
325 return thread->queue != NULL;
327 if (queue->input)
329 queue->input->cursor_count -= queue->cursor_count;
330 release_object( queue->input );
332 queue->input = (struct thread_input *)grab_object( new_input );
333 new_input->cursor_count += queue->cursor_count;
334 return 1;
337 /* set the cursor position and queue the corresponding mouse message */
338 static void set_cursor_pos( struct desktop *desktop, int x, int y )
340 struct hardware_msg_data *msg_data;
341 struct message *msg;
343 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
344 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
346 free( msg );
347 return;
349 memset( msg_data, 0, sizeof(*msg_data) );
351 msg->type = MSG_HARDWARE;
352 msg->win = 0;
353 msg->msg = WM_MOUSEMOVE;
354 msg->wparam = 0;
355 msg->lparam = 0;
356 msg->time = get_tick_count();
357 msg->result = NULL;
358 msg->data = msg_data;
359 msg->data_size = sizeof(*msg_data);
360 msg_data->x = x;
361 msg_data->y = y;
362 queue_hardware_message( desktop, msg, 1 );
365 /* set the cursor clip rectangle */
366 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect )
368 rectangle_t top_rect;
369 int x, y;
371 get_top_window_rectangle( desktop, &top_rect );
372 if (rect)
374 rectangle_t new_rect = *rect;
375 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
376 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
377 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
378 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
379 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
380 desktop->cursor.clip = new_rect;
382 else desktop->cursor.clip = top_rect;
384 if (desktop->cursor.clip_msg)
385 post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
387 /* warp the mouse to be inside the clip rect */
388 x = min( max( desktop->cursor.x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
389 y = min( max( desktop->cursor.y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
390 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
393 /* change the foreground input and reset the cursor clip rect */
394 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
396 if (desktop->foreground_input == input) return;
397 set_clip_rectangle( desktop, NULL );
398 desktop->foreground_input = input;
401 /* get the hook table for a given thread */
402 struct hook_table *get_queue_hooks( struct thread *thread )
404 if (!thread->queue) return NULL;
405 return thread->queue->hooks;
408 /* set the hook table for a given thread, allocating the queue if needed */
409 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
411 struct msg_queue *queue = thread->queue;
412 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
413 if (queue->hooks) release_object( queue->hooks );
414 queue->hooks = hooks;
417 /* check the queue status */
418 static inline int is_signaled( struct msg_queue *queue )
420 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
423 /* set some queue bits */
424 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
426 queue->wake_bits |= bits;
427 queue->changed_bits |= bits;
428 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
431 /* clear some queue bits */
432 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
434 queue->wake_bits &= ~bits;
435 queue->changed_bits &= ~bits;
438 /* check whether msg is a keyboard message */
439 static inline int is_keyboard_msg( struct message *msg )
441 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
444 /* check if message is matched by the filter */
445 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
447 return (msg >= first && msg <= last);
450 /* check whether a message filter contains at least one potential hardware message */
451 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
453 /* hardware message ranges are (in numerical order):
454 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
455 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
456 * WM_MOUSEFIRST .. WM_MOUSELAST
458 if (last < WM_NCMOUSEFIRST) return 0;
459 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
460 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
461 if (first > WM_MOUSELAST) return 0;
462 return 1;
465 /* get the QS_* bit corresponding to a given hardware message */
466 static inline int get_hardware_msg_bit( struct message *msg )
468 if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
469 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
470 if (is_keyboard_msg( msg )) return QS_KEY;
471 return QS_MOUSEBUTTON;
474 /* get the current thread queue, creating it if needed */
475 static inline struct msg_queue *get_current_queue(void)
477 struct msg_queue *queue = current->queue;
478 if (!queue) queue = create_msg_queue( current, NULL );
479 return queue;
482 /* get a (pseudo-)unique id to tag hardware messages */
483 static inline unsigned int get_unique_id(void)
485 static unsigned int id;
486 if (!++id) id = 1; /* avoid an id of 0 */
487 return id;
490 /* try to merge a message with the last in the list; return 1 if successful */
491 static int merge_message( struct thread_input *input, const struct message *msg )
493 struct message *prev;
494 struct list *ptr;
496 if (msg->msg != WM_MOUSEMOVE) return 0;
497 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
499 prev = LIST_ENTRY( ptr, struct message, entry );
500 if (prev->msg != WM_INPUT) break;
502 if (!ptr) return 0;
503 if (prev->result) return 0;
504 if (prev->win && msg->win && prev->win != msg->win) return 0;
505 if (prev->msg != msg->msg) return 0;
506 if (prev->type != msg->type) return 0;
507 /* now we can merge it */
508 prev->wparam = msg->wparam;
509 prev->lparam = msg->lparam;
510 prev->time = msg->time;
511 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
513 struct hardware_msg_data *prev_data = prev->data;
514 struct hardware_msg_data *msg_data = msg->data;
515 prev_data->x = msg_data->x;
516 prev_data->y = msg_data->y;
517 prev_data->info = msg_data->info;
519 list_remove( ptr );
520 list_add_tail( &input->msg_list, ptr );
521 return 1;
524 /* free a result structure */
525 static void free_result( struct message_result *result )
527 if (result->timeout) remove_timeout_user( result->timeout );
528 free( result->data );
529 if (result->callback_msg) free_message( result->callback_msg );
530 if (result->hardware_msg) free_message( result->hardware_msg );
531 if (result->desktop) release_object( result->desktop );
532 free( result );
535 /* remove the result from the sender list it is on */
536 static inline void remove_result_from_sender( struct message_result *result )
538 assert( result->sender );
540 list_remove( &result->sender_entry );
541 result->sender = NULL;
542 if (!result->receiver) free_result( result );
545 /* store the message result in the appropriate structure */
546 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
548 res->result = result;
549 res->error = error;
550 res->replied = 1;
551 if (res->timeout)
553 remove_timeout_user( res->timeout );
554 res->timeout = NULL;
557 if (res->hardware_msg)
559 if (!error && result) /* rejected by the hook */
560 free_message( res->hardware_msg );
561 else
562 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
564 res->hardware_msg = NULL;
567 if (res->sender)
569 if (res->callback_msg)
571 /* queue the callback message in the sender queue */
572 struct callback_msg_data *data = res->callback_msg->data;
573 data->result = result;
574 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
575 set_queue_bits( res->sender, QS_SENDMESSAGE );
576 res->callback_msg = NULL;
577 remove_result_from_sender( res );
579 else
581 /* wake sender queue if waiting on this result */
582 if (list_head(&res->sender->send_result) == &res->sender_entry)
583 set_queue_bits( res->sender, QS_SMRESULT );
586 else if (!res->receiver) free_result( res );
589 /* free a message when deleting a queue or window */
590 static void free_message( struct message *msg )
592 struct message_result *result = msg->result;
593 if (result)
595 result->msg = NULL;
596 result->receiver = NULL;
597 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
599 free( msg->data );
600 free( msg );
603 /* remove (and free) a message from a message list */
604 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
605 enum message_kind kind )
607 list_remove( &msg->entry );
608 switch(kind)
610 case SEND_MESSAGE:
611 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
612 break;
613 case POST_MESSAGE:
614 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
615 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
616 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
617 clear_queue_bits( queue, QS_HOTKEY );
618 break;
620 free_message( msg );
623 /* message timed out without getting a reply */
624 static void result_timeout( void *private )
626 struct message_result *result = private;
628 assert( !result->replied );
630 result->timeout = NULL;
632 if (result->msg) /* not received yet */
634 struct message *msg = result->msg;
636 result->msg = NULL;
637 msg->result = NULL;
638 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
639 result->receiver = NULL;
641 store_message_result( result, 0, STATUS_TIMEOUT );
644 /* allocate and fill a message result structure */
645 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
646 struct msg_queue *recv_queue,
647 struct message *msg, timeout_t timeout )
649 struct message_result *result = mem_alloc( sizeof(*result) );
650 if (result)
652 result->msg = msg;
653 result->sender = send_queue;
654 result->receiver = recv_queue;
655 result->replied = 0;
656 result->data = NULL;
657 result->data_size = 0;
658 result->timeout = NULL;
659 result->hardware_msg = NULL;
660 result->desktop = NULL;
661 result->callback_msg = NULL;
663 if (msg->type == MSG_CALLBACK)
665 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
667 if (!callback_msg)
669 free( result );
670 return NULL;
672 callback_msg->type = MSG_CALLBACK_RESULT;
673 callback_msg->win = msg->win;
674 callback_msg->msg = msg->msg;
675 callback_msg->wparam = 0;
676 callback_msg->lparam = 0;
677 callback_msg->time = get_tick_count();
678 callback_msg->result = NULL;
679 /* steal the data from the original message */
680 callback_msg->data = msg->data;
681 callback_msg->data_size = msg->data_size;
682 msg->data = NULL;
683 msg->data_size = 0;
685 result->callback_msg = callback_msg;
686 list_add_head( &send_queue->callback_result, &result->sender_entry );
688 else if (send_queue) list_add_head( &send_queue->send_result, &result->sender_entry );
690 if (timeout != TIMEOUT_INFINITE)
691 result->timeout = add_timeout_user( timeout, result_timeout, result );
693 return result;
696 /* receive a message, removing it from the sent queue */
697 static void receive_message( struct msg_queue *queue, struct message *msg,
698 struct get_message_reply *reply )
700 struct message_result *result = msg->result;
702 reply->total = msg->data_size;
703 if (msg->data_size > get_reply_max_size())
705 set_error( STATUS_BUFFER_OVERFLOW );
706 return;
708 reply->type = msg->type;
709 reply->win = msg->win;
710 reply->msg = msg->msg;
711 reply->wparam = msg->wparam;
712 reply->lparam = msg->lparam;
713 reply->time = msg->time;
715 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
717 list_remove( &msg->entry );
718 /* put the result on the receiver result stack */
719 if (result)
721 result->msg = NULL;
722 result->recv_next = queue->recv_result;
723 queue->recv_result = result;
725 free( msg );
726 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
729 /* set the result of the current received message */
730 static void reply_message( struct msg_queue *queue, lparam_t result,
731 unsigned int error, int remove, const void *data, data_size_t len )
733 struct message_result *res = queue->recv_result;
735 if (remove)
737 queue->recv_result = res->recv_next;
738 res->receiver = NULL;
739 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
741 free_result( res );
742 return;
745 if (!res->replied)
747 if (len && (res->data = memdup( data, len ))) res->data_size = len;
748 store_message_result( res, result, error );
752 static int match_window( user_handle_t win, user_handle_t msg_win )
754 if (!win) return 1;
755 if (win == -1 || win == 1) return !msg_win;
756 if (msg_win == win) return 1;
757 return is_child_window( win, msg_win );
760 /* retrieve a posted message */
761 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
762 unsigned int first, unsigned int last, unsigned int flags,
763 struct get_message_reply *reply )
765 struct message *msg;
767 /* check against the filters */
768 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
770 if (!match_window( win, msg->win )) continue;
771 if (!check_msg_filter( msg->msg, first, last )) continue;
772 goto found; /* found one */
774 return 0;
776 /* return it to the app */
777 found:
778 reply->total = msg->data_size;
779 if (msg->data_size > get_reply_max_size())
781 set_error( STATUS_BUFFER_OVERFLOW );
782 return 1;
784 reply->type = msg->type;
785 reply->win = msg->win;
786 reply->msg = msg->msg;
787 reply->wparam = msg->wparam;
788 reply->lparam = msg->lparam;
789 reply->time = msg->time;
791 if (flags & PM_REMOVE)
793 if (msg->data)
795 set_reply_data_ptr( msg->data, msg->data_size );
796 msg->data = NULL;
797 msg->data_size = 0;
799 remove_queue_message( queue, msg, POST_MESSAGE );
801 else if (msg->data) set_reply_data( msg->data, msg->data_size );
803 return 1;
806 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
807 struct get_message_reply *reply )
809 if (queue->quit_message)
811 reply->total = 0;
812 reply->type = MSG_POSTED;
813 reply->win = 0;
814 reply->msg = WM_QUIT;
815 reply->wparam = queue->exit_code;
816 reply->lparam = 0;
817 reply->time = get_tick_count();
819 if (flags & PM_REMOVE)
821 queue->quit_message = 0;
822 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
823 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
825 return 1;
827 else
828 return 0;
831 /* empty a message list and free all the messages */
832 static void empty_msg_list( struct list *list )
834 struct list *ptr;
836 while ((ptr = list_head( list )) != NULL)
838 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
839 list_remove( &msg->entry );
840 free_message( msg );
844 /* cleanup all pending results when deleting a queue */
845 static void cleanup_results( struct msg_queue *queue )
847 struct list *entry;
849 while ((entry = list_head( &queue->send_result )) != NULL)
851 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
854 while ((entry = list_head( &queue->callback_result )) != NULL)
856 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
859 while (queue->recv_result)
860 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
863 /* check if the thread owning the queue is hung (not checking for messages) */
864 static int is_queue_hung( struct msg_queue *queue )
866 struct wait_queue_entry *entry;
868 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
869 return 0; /* less than 5 seconds since last get message -> not hung */
871 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
873 if (get_wait_queue_thread(entry)->queue == queue)
874 return 0; /* thread is waiting on queue -> not hung */
876 return 1;
879 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
881 struct msg_queue *queue = (struct msg_queue *)obj;
882 struct process *process = get_wait_queue_thread(entry)->process;
884 /* a thread can only wait on its own queue */
885 if (get_wait_queue_thread(entry)->queue != queue)
887 set_error( STATUS_ACCESS_DENIED );
888 return 0;
890 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
892 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
893 set_fd_events( queue->fd, POLLIN );
894 add_queue( obj, entry );
895 return 1;
898 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
900 struct msg_queue *queue = (struct msg_queue *)obj;
902 remove_queue( obj, entry );
903 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
904 set_fd_events( queue->fd, 0 );
907 static void msg_queue_dump( struct object *obj, int verbose )
909 struct msg_queue *queue = (struct msg_queue *)obj;
910 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
911 queue->wake_bits, queue->wake_mask );
914 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
916 struct msg_queue *queue = (struct msg_queue *)obj;
917 int ret = 0;
919 if (queue->fd)
921 if ((ret = check_fd_events( queue->fd, POLLIN )))
922 /* stop waiting on select() if we are signaled */
923 set_fd_events( queue->fd, 0 );
924 else if (!list_empty( &obj->wait_queue ))
925 /* restart waiting on poll() if we are no longer signaled */
926 set_fd_events( queue->fd, POLLIN );
928 return ret || is_signaled( queue );
931 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
933 struct msg_queue *queue = (struct msg_queue *)obj;
934 queue->wake_mask = queue->wake_get_msg;
935 queue->changed_mask = queue->changed_get_msg;
938 static void msg_queue_destroy( struct object *obj )
940 struct msg_queue *queue = (struct msg_queue *)obj;
941 struct list *ptr;
942 struct hotkey *hotkey, *hotkey2;
943 int i;
945 cleanup_results( queue );
946 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
948 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
950 if (hotkey->queue == queue)
952 list_remove( &hotkey->entry );
953 free( hotkey );
957 while ((ptr = list_head( &queue->pending_timers )))
959 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
960 list_remove( &timer->entry );
961 free( timer );
963 while ((ptr = list_head( &queue->expired_timers )))
965 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
966 list_remove( &timer->entry );
967 free( timer );
969 if (queue->timeout) remove_timeout_user( queue->timeout );
970 queue->input->cursor_count -= queue->cursor_count;
971 release_object( queue->input );
972 if (queue->hooks) release_object( queue->hooks );
973 if (queue->fd) release_object( queue->fd );
976 static void msg_queue_poll_event( struct fd *fd, int event )
978 struct msg_queue *queue = get_fd_user( fd );
979 assert( queue->obj.ops == &msg_queue_ops );
981 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
982 else set_fd_events( queue->fd, 0 );
983 wake_up( &queue->obj, 0 );
986 static void thread_input_dump( struct object *obj, int verbose )
988 struct thread_input *input = (struct thread_input *)obj;
989 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
990 input->focus, input->capture, input->active );
993 static void thread_input_destroy( struct object *obj )
995 struct thread_input *input = (struct thread_input *)obj;
997 empty_msg_list( &input->msg_list );
998 if (input->desktop)
1000 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
1001 release_object( input->desktop );
1005 /* fix the thread input data when a window is destroyed */
1006 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1008 struct thread_input *input = queue->input;
1010 if (window == input->focus) input->focus = 0;
1011 if (window == input->capture) input->capture = 0;
1012 if (window == input->active) input->active = 0;
1013 if (window == input->menu_owner) input->menu_owner = 0;
1014 if (window == input->move_size) input->move_size = 0;
1015 if (window == input->caret) set_caret_window( input, 0 );
1018 /* check if the specified window can be set in the input data of a given queue */
1019 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1021 struct thread *thread;
1022 int ret = 0;
1024 if (!window) return 1; /* we can always clear the data */
1026 if ((thread = get_window_thread( window )))
1028 ret = (queue->input == thread->queue->input);
1029 if (!ret) set_error( STATUS_ACCESS_DENIED );
1030 release_object( thread );
1032 else set_error( STATUS_INVALID_HANDLE );
1034 return ret;
1037 /* make sure the specified thread has a queue */
1038 int init_thread_queue( struct thread *thread )
1040 if (thread->queue) return 1;
1041 return (create_msg_queue( thread, NULL ) != NULL);
1044 /* attach two thread input data structures */
1045 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1047 struct desktop *desktop;
1048 struct thread_input *input;
1049 int ret;
1051 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1052 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1053 input = (struct thread_input *)grab_object( thread_to->queue->input );
1054 if (input->desktop != desktop)
1056 set_error( STATUS_ACCESS_DENIED );
1057 release_object( input );
1058 release_object( desktop );
1059 return 0;
1061 release_object( desktop );
1063 ret = assign_thread_input( thread_from, input );
1064 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1065 release_object( input );
1066 return ret;
1069 /* detach two thread input data structures */
1070 void detach_thread_input( struct thread *thread_from )
1072 struct thread_input *input;
1074 if ((input = create_thread_input( thread_from )))
1076 assign_thread_input( thread_from, input );
1077 release_object( input );
1082 /* set the next timer to expire */
1083 static void set_next_timer( struct msg_queue *queue )
1085 struct list *ptr;
1087 if (queue->timeout)
1089 remove_timeout_user( queue->timeout );
1090 queue->timeout = NULL;
1092 if ((ptr = list_head( &queue->pending_timers )))
1094 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1095 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1097 /* set/clear QS_TIMER bit */
1098 if (list_empty( &queue->expired_timers ))
1099 clear_queue_bits( queue, QS_TIMER );
1100 else
1101 set_queue_bits( queue, QS_TIMER );
1104 /* find a timer from its window and id */
1105 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1106 unsigned int msg, lparam_t id )
1108 struct list *ptr;
1110 /* we need to search both lists */
1112 LIST_FOR_EACH( ptr, &queue->pending_timers )
1114 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1115 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1117 LIST_FOR_EACH( ptr, &queue->expired_timers )
1119 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1120 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1122 return NULL;
1125 /* callback for the next timer expiration */
1126 static void timer_callback( void *private )
1128 struct msg_queue *queue = private;
1129 struct list *ptr;
1131 queue->timeout = NULL;
1132 /* move on to the next timer */
1133 ptr = list_head( &queue->pending_timers );
1134 list_remove( ptr );
1135 list_add_tail( &queue->expired_timers, ptr );
1136 set_next_timer( queue );
1139 /* link a timer at its rightful place in the queue list */
1140 static void link_timer( struct msg_queue *queue, struct timer *timer )
1142 struct list *ptr;
1144 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1146 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1147 if (t->when >= timer->when) break;
1149 list_add_before( ptr, &timer->entry );
1152 /* remove a timer from the queue timer list and free it */
1153 static void free_timer( struct msg_queue *queue, struct timer *timer )
1155 list_remove( &timer->entry );
1156 free( timer );
1157 set_next_timer( queue );
1160 /* restart an expired timer */
1161 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1163 list_remove( &timer->entry );
1164 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1165 link_timer( queue, timer );
1166 set_next_timer( queue );
1169 /* find an expired timer matching the filtering parameters */
1170 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1171 unsigned int get_first, unsigned int get_last,
1172 int remove )
1174 struct list *ptr;
1176 LIST_FOR_EACH( ptr, &queue->expired_timers )
1178 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1179 if (win && timer->win != win) continue;
1180 if (check_msg_filter( timer->msg, get_first, get_last ))
1182 if (remove) restart_timer( queue, timer );
1183 return timer;
1186 return NULL;
1189 /* add a timer */
1190 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1192 struct timer *timer = mem_alloc( sizeof(*timer) );
1193 if (timer)
1195 timer->rate = max( rate, 1 );
1196 timer->when = current_time + (timeout_t)timer->rate * 10000;
1197 link_timer( queue, timer );
1198 /* check if we replaced the next timer */
1199 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1201 return timer;
1204 /* change the input key state for a given key */
1205 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1207 if (down)
1209 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1210 keystate[key] |= down;
1212 else keystate[key] &= ~0x80;
1215 /* update the input key state for a keyboard message */
1216 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1217 const struct message *msg )
1219 unsigned char key;
1220 int down = 0;
1222 switch (msg->msg)
1224 case WM_LBUTTONDOWN:
1225 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1226 /* fall through */
1227 case WM_LBUTTONUP:
1228 set_input_key_state( keystate, VK_LBUTTON, down );
1229 break;
1230 case WM_MBUTTONDOWN:
1231 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1232 /* fall through */
1233 case WM_MBUTTONUP:
1234 set_input_key_state( keystate, VK_MBUTTON, down );
1235 break;
1236 case WM_RBUTTONDOWN:
1237 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1238 /* fall through */
1239 case WM_RBUTTONUP:
1240 set_input_key_state( keystate, VK_RBUTTON, down );
1241 break;
1242 case WM_XBUTTONDOWN:
1243 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1244 /* fall through */
1245 case WM_XBUTTONUP:
1246 if (msg->wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1247 else if (msg->wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1248 break;
1249 case WM_KEYDOWN:
1250 case WM_SYSKEYDOWN:
1251 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1252 /* fall through */
1253 case WM_KEYUP:
1254 case WM_SYSKEYUP:
1255 key = (unsigned char)msg->wparam;
1256 set_input_key_state( keystate, key, down );
1257 switch(key)
1259 case VK_LCONTROL:
1260 case VK_RCONTROL:
1261 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1262 set_input_key_state( keystate, VK_CONTROL, down );
1263 break;
1264 case VK_LMENU:
1265 case VK_RMENU:
1266 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1267 set_input_key_state( keystate, VK_MENU, down );
1268 break;
1269 case VK_LSHIFT:
1270 case VK_RSHIFT:
1271 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1272 set_input_key_state( keystate, VK_SHIFT, down );
1273 break;
1275 break;
1279 /* release the hardware message currently being processed by the given thread */
1280 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1281 int remove, user_handle_t new_win )
1283 struct thread_input *input = queue->input;
1284 struct message *msg;
1286 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1288 if (msg->unique_id == hw_id) break;
1290 if (&msg->entry == &input->msg_list) return; /* not found */
1292 /* clear the queue bit for that message */
1293 if (remove || new_win)
1295 struct message *other;
1296 int clr_bit;
1298 clr_bit = get_hardware_msg_bit( msg );
1299 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1301 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1303 clr_bit = 0;
1304 break;
1307 if (clr_bit) clear_queue_bits( queue, clr_bit );
1310 if (new_win) /* set the new window */
1312 struct thread *owner = get_window_thread( new_win );
1313 if (owner)
1315 msg->win = new_win;
1316 if (owner->queue->input != input)
1318 list_remove( &msg->entry );
1319 if (merge_message( owner->queue->input, msg ))
1321 free_message( msg );
1322 release_object( owner );
1323 return;
1325 list_add_tail( &owner->queue->input->msg_list, &msg->entry );
1327 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1328 remove = 0;
1329 release_object( owner );
1332 if (remove)
1334 update_input_key_state( input->desktop, input->keystate, msg );
1335 list_remove( &msg->entry );
1336 free_message( msg );
1340 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1342 struct hotkey *hotkey;
1343 unsigned int modifiers = 0;
1345 if (msg->msg != WM_KEYDOWN) return 0;
1347 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1348 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1349 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1350 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1352 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1354 if (hotkey->vkey != msg->wparam) continue;
1355 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1358 return 0;
1360 found:
1361 msg->type = MSG_POSTED;
1362 msg->win = hotkey->win;
1363 msg->msg = WM_HOTKEY;
1364 msg->wparam = hotkey->id;
1365 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1367 free( msg->data );
1368 msg->data = NULL;
1369 msg->data_size = 0;
1371 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1372 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1373 hotkey->queue->hotkey_count++;
1374 return 1;
1377 /* find the window that should receive a given hardware message */
1378 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1379 struct message *msg, unsigned int *msg_code )
1381 struct hardware_msg_data *data = msg->data;
1382 user_handle_t win = 0;
1384 *msg_code = msg->msg;
1385 if (msg->msg == WM_INPUT)
1387 if (!(win = msg->win) && input) win = input->focus;
1389 else if (is_keyboard_msg( msg ))
1391 if (input && !(win = input->focus))
1393 win = input->active;
1394 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1397 else /* mouse message */
1399 if (!input || !(win = input->capture))
1401 if (!(win = msg->win) || !is_window_visible( win ) || is_window_transparent( win ))
1402 win = window_from_point( desktop, data->x, data->y );
1405 return win;
1408 static struct rawinput_device_entry *find_rawinput_device( unsigned short usage_page, unsigned short usage )
1410 struct rawinput_device_entry *e;
1412 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
1414 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1415 return e;
1418 return NULL;
1421 static void update_rawinput_device(const struct rawinput_device *device)
1423 struct rawinput_device_entry *e;
1425 if (!(e = find_rawinput_device( device->usage_page, device->usage )))
1427 if (!(e = mem_alloc( sizeof(*e) ))) return;
1428 list_add_tail( &current->process->rawinput_devices, &e->entry );
1431 if (device->flags & RIDEV_REMOVE)
1433 list_remove( &e->entry );
1434 free( e );
1435 return;
1438 e->device = *device;
1439 e->device.target = get_user_full_handle( e->device.target );
1442 /* queue a hardware message into a given thread input */
1443 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1445 user_handle_t win;
1446 struct thread *thread;
1447 struct thread_input *input;
1448 unsigned int msg_code;
1449 struct hardware_msg_data *data = msg->data;
1451 update_input_key_state( desktop, desktop->keystate, msg );
1452 last_input_time = get_tick_count();
1453 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1455 if (is_keyboard_msg( msg ))
1457 if (queue_hotkey_message( desktop, msg )) return;
1458 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1459 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1460 msg->lparam &= ~(KF_EXTENDED << 16);
1462 else if (msg->msg != WM_INPUT)
1464 if (msg->msg == WM_MOUSEMOVE)
1466 int x = min( max( data->x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
1467 int y = min( max( data->y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
1468 if (desktop->cursor.x != x || desktop->cursor.y != y) always_queue = 1;
1469 desktop->cursor.x = x;
1470 desktop->cursor.y = y;
1471 desktop->cursor.last_change = get_tick_count();
1473 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1474 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1475 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1476 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1477 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1478 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1479 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1481 data->x = desktop->cursor.x;
1482 data->y = desktop->cursor.y;
1484 if (msg->win && (thread = get_window_thread( msg->win )))
1486 input = thread->queue->input;
1487 release_object( thread );
1489 else input = desktop->foreground_input;
1491 win = find_hardware_message_window( desktop, input, msg, &msg_code );
1492 if (!win || !(thread = get_window_thread(win)))
1494 if (input) update_input_key_state( input->desktop, input->keystate, msg );
1495 free_message( msg );
1496 return;
1498 input = thread->queue->input;
1500 if (win != desktop->cursor.win) always_queue = 1;
1501 desktop->cursor.win = win;
1503 if (!always_queue || merge_message( input, msg )) free_message( msg );
1504 else
1506 msg->unique_id = 0; /* will be set once we return it to the app */
1507 list_add_tail( &input->msg_list, &msg->entry );
1508 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1510 release_object( thread );
1513 /* send the low-level hook message for a given hardware message */
1514 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1515 const hw_input_t *input, struct msg_queue *sender )
1517 struct thread *hook_thread;
1518 struct msg_queue *queue;
1519 struct message *msg;
1520 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1521 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1523 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1524 if (!(queue = hook_thread->queue)) return 0;
1525 if (is_queue_hung( queue )) return 0;
1527 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1529 msg->type = MSG_HOOK_LL;
1530 msg->win = 0;
1531 msg->msg = id;
1532 msg->wparam = hardware_msg->msg;
1533 msg->time = hardware_msg->time;
1534 msg->data_size = hardware_msg->data_size;
1535 msg->result = NULL;
1537 if (input->type == INPUT_KEYBOARD)
1539 unsigned short vkey = input->kbd.vkey;
1540 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1541 msg->lparam = (input->kbd.scan << 16) | vkey;
1543 else msg->lparam = input->mouse.data << 16;
1545 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1546 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1548 free_message( msg );
1549 return 0;
1551 msg->result->hardware_msg = hardware_msg;
1552 msg->result->desktop = (struct desktop *)grab_object( desktop );
1553 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1554 set_queue_bits( queue, QS_SENDMESSAGE );
1555 return 1;
1558 /* queue a hardware message for a mouse event */
1559 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1560 unsigned int hook_flags, struct msg_queue *sender )
1562 const struct rawinput_device *device;
1563 struct hardware_msg_data *msg_data;
1564 struct message *msg;
1565 unsigned int i, time, flags;
1566 int wait = 0, x, y;
1568 static const unsigned int messages[] =
1570 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1571 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1572 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1573 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1574 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1575 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1576 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1577 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1578 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1579 0, /* 0x0200 = unused */
1580 0, /* 0x0400 = unused */
1581 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1582 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1585 desktop->cursor.last_change = get_tick_count();
1586 flags = input->mouse.flags;
1587 time = input->mouse.time;
1588 if (!time) time = desktop->cursor.last_change;
1590 if (flags & MOUSEEVENTF_MOVE)
1592 if (flags & MOUSEEVENTF_ABSOLUTE)
1594 x = input->mouse.x;
1595 y = input->mouse.y;
1596 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1597 x == desktop->cursor.x && y == desktop->cursor.y)
1598 flags &= ~MOUSEEVENTF_MOVE;
1600 else
1602 x = desktop->cursor.x + input->mouse.x;
1603 y = desktop->cursor.y + input->mouse.y;
1606 else
1608 x = desktop->cursor.x;
1609 y = desktop->cursor.y;
1612 if ((device = current->process->rawinput_mouse))
1614 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1615 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1617 free( msg );
1618 return 0;
1621 msg->type = MSG_HARDWARE;
1622 msg->win = device->target;
1623 msg->msg = WM_INPUT;
1624 msg->wparam = RIM_INPUT;
1625 msg->lparam = 0;
1626 msg->time = time;
1627 msg->data = msg_data;
1628 msg->data_size = sizeof(*msg_data);
1629 msg->result = NULL;
1631 msg_data->info = input->mouse.info;
1632 msg_data->flags = flags;
1633 msg_data->rawinput.type = RIM_TYPEMOUSE;
1634 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1635 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1636 msg_data->rawinput.mouse.data = input->mouse.data;
1638 queue_hardware_message( desktop, msg, 0 );
1641 for (i = 0; i < sizeof(messages)/sizeof(messages[0]); i++)
1643 if (!messages[i]) continue;
1644 if (!(flags & (1 << i))) continue;
1645 flags &= ~(1 << i);
1647 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1648 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1650 free( msg );
1651 return 0;
1653 memset( msg_data, 0, sizeof(*msg_data) );
1655 msg->type = MSG_HARDWARE;
1656 msg->win = get_user_full_handle( win );
1657 msg->msg = messages[i];
1658 msg->wparam = input->mouse.data << 16;
1659 msg->lparam = 0;
1660 msg->time = time;
1661 msg->result = NULL;
1662 msg->data = msg_data;
1663 msg->data_size = sizeof(*msg_data);
1664 msg_data->x = x;
1665 msg_data->y = y;
1666 msg_data->info = input->mouse.info;
1667 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLMHF_INJECTED;
1669 /* specify a sender only when sending the last message */
1670 if (!(flags & ((1 << sizeof(messages)/sizeof(messages[0])) - 1)))
1672 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1673 queue_hardware_message( desktop, msg, 0 );
1675 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1676 queue_hardware_message( desktop, msg, 0 );
1678 return wait;
1681 /* queue a hardware message for a keyboard event */
1682 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1683 unsigned int hook_flags, struct msg_queue *sender )
1685 const struct rawinput_device *device;
1686 struct hardware_msg_data *msg_data;
1687 struct message *msg;
1688 unsigned char vkey = input->kbd.vkey;
1689 unsigned int message_code, time;
1690 int wait;
1692 if (!(time = input->kbd.time)) time = get_tick_count();
1694 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1696 switch (vkey)
1698 case VK_MENU:
1699 case VK_LMENU:
1700 case VK_RMENU:
1701 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1702 break;
1703 case VK_CONTROL:
1704 case VK_LCONTROL:
1705 case VK_RCONTROL:
1706 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1707 break;
1708 case VK_SHIFT:
1709 case VK_LSHIFT:
1710 case VK_RSHIFT:
1711 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1712 break;
1716 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1717 switch (vkey)
1719 case VK_LMENU:
1720 case VK_RMENU:
1721 if (input->kbd.flags & KEYEVENTF_KEYUP)
1723 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1724 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1725 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1726 message_code = WM_SYSKEYUP;
1727 desktop->keystate[VK_MENU] &= ~0x02;
1729 else
1731 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1732 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1733 message_code = WM_SYSKEYDOWN;
1734 desktop->keystate[VK_MENU] |= 0x02;
1736 break;
1738 case VK_LCONTROL:
1739 case VK_RCONTROL:
1740 /* send WM_SYSKEYUP on release if Alt still pressed */
1741 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1742 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1743 message_code = WM_SYSKEYUP;
1744 desktop->keystate[VK_MENU] &= ~0x02;
1745 break;
1747 default:
1748 /* send WM_SYSKEY for Alt-anykey and for F10 */
1749 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1750 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1751 /* fall through */
1752 case VK_F10:
1753 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1754 desktop->keystate[VK_MENU] &= ~0x02;
1755 break;
1758 if ((device = current->process->rawinput_kbd))
1760 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1761 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1763 free( msg );
1764 return 0;
1767 msg->type = MSG_HARDWARE;
1768 msg->win = device->target;
1769 msg->msg = WM_INPUT;
1770 msg->wparam = RIM_INPUT;
1771 msg->lparam = 0;
1772 msg->time = time;
1773 msg->data = msg_data;
1774 msg->data_size = sizeof(*msg_data);
1775 msg->result = NULL;
1777 msg_data->info = input->kbd.info;
1778 msg_data->flags = input->kbd.flags;
1779 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1780 msg_data->rawinput.kbd.message = message_code;
1781 msg_data->rawinput.kbd.vkey = vkey;
1782 msg_data->rawinput.kbd.scan = input->kbd.scan;
1784 queue_hardware_message( desktop, msg, 0 );
1787 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1788 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1790 free( msg );
1791 return 0;
1793 memset( msg_data, 0, sizeof(*msg_data) );
1795 msg->type = MSG_HARDWARE;
1796 msg->win = get_user_full_handle( win );
1797 msg->msg = message_code;
1798 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1799 msg->time = time;
1800 msg->result = NULL;
1801 msg->data = msg_data;
1802 msg->data_size = sizeof(*msg_data);
1803 msg_data->info = input->kbd.info;
1804 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLKHF_INJECTED;
1806 if (input->kbd.flags & KEYEVENTF_UNICODE)
1808 msg->wparam = VK_PACKET;
1810 else
1812 unsigned int flags = 0;
1813 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1814 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1815 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1816 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1818 msg->wparam = vkey;
1819 msg->lparam |= flags << 16;
1820 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1823 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1824 queue_hardware_message( desktop, msg, 1 );
1826 return wait;
1829 /* queue a hardware message for a custom type of event */
1830 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1831 const hw_input_t *input )
1833 struct hardware_msg_data *msg_data;
1834 struct message *msg;
1836 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
1837 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1839 free( msg );
1840 return;
1842 memset( msg_data, 0, sizeof(*msg_data) );
1844 msg->type = MSG_HARDWARE;
1845 msg->win = get_user_full_handle( win );
1846 msg->msg = input->hw.msg;
1847 msg->wparam = 0;
1848 msg->lparam = input->hw.lparam;
1849 msg->time = get_tick_count();
1850 msg->result = NULL;
1851 msg->data = msg_data;
1852 msg->data_size = sizeof(*msg_data);
1854 queue_hardware_message( desktop, msg, 1 );
1857 /* check message filter for a hardware message */
1858 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1859 user_handle_t filter_win, unsigned int first, unsigned int last )
1861 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1863 /* we can only test the window for a keyboard message since the
1864 * dest window for a mouse message depends on hittest */
1865 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1866 return 0;
1867 /* the message code is final for a keyboard message, we can simply check it */
1868 return check_msg_filter( msg_code, first, last );
1870 else /* mouse message */
1872 /* we need to check all possible values that the message can have in the end */
1874 if (check_msg_filter( msg_code, first, last )) return 1;
1875 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1877 /* all other messages can become non-client messages */
1878 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1880 /* clicks can become double-clicks or non-client double-clicks */
1881 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1882 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1884 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1885 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1887 return 0;
1892 /* find a hardware message for the given queue */
1893 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1894 unsigned int first, unsigned int last, unsigned int flags,
1895 struct get_message_reply *reply )
1897 struct thread_input *input = thread->queue->input;
1898 struct thread *win_thread;
1899 struct list *ptr;
1900 user_handle_t win;
1901 int clear_bits, got_one = 0;
1902 unsigned int msg_code;
1904 ptr = list_head( &input->msg_list );
1905 if (hw_id)
1907 while (ptr)
1909 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1910 if (msg->unique_id == hw_id) break;
1911 ptr = list_next( &input->msg_list, ptr );
1913 if (!ptr) ptr = list_head( &input->msg_list );
1914 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1917 if (ptr == list_head( &input->msg_list ))
1918 clear_bits = QS_INPUT;
1919 else
1920 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1922 while (ptr)
1924 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1925 struct hardware_msg_data *data = msg->data;
1927 ptr = list_next( &input->msg_list, ptr );
1928 win = find_hardware_message_window( input->desktop, input, msg, &msg_code );
1929 if (!win || !(win_thread = get_window_thread( win )))
1931 /* no window at all, remove it */
1932 update_input_key_state( input->desktop, input->keystate, msg );
1933 list_remove( &msg->entry );
1934 free_message( msg );
1935 continue;
1937 if (win_thread != thread)
1939 if (win_thread->queue->input == input)
1941 /* wake the other thread */
1942 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1943 got_one = 1;
1945 else
1947 /* for another thread input, drop it */
1948 update_input_key_state( input->desktop, input->keystate, msg );
1949 list_remove( &msg->entry );
1950 free_message( msg );
1952 release_object( win_thread );
1953 continue;
1955 release_object( win_thread );
1957 /* if we already got a message for another thread, or if it doesn't
1958 * match the filter we skip it */
1959 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1961 clear_bits &= ~get_hardware_msg_bit( msg );
1962 continue;
1964 /* now we can return it */
1965 if (!msg->unique_id) msg->unique_id = get_unique_id();
1966 reply->type = MSG_HARDWARE;
1967 reply->win = win;
1968 reply->msg = msg_code;
1969 reply->wparam = msg->wparam;
1970 reply->lparam = msg->lparam;
1971 reply->time = msg->time;
1973 data->hw_id = msg->unique_id;
1974 set_reply_data( msg->data, msg->data_size );
1975 if (msg->msg == WM_INPUT && (flags & PM_REMOVE))
1976 release_hardware_message( current->queue, data->hw_id, 1, 0 );
1977 return 1;
1979 /* nothing found, clear the hardware queue bits */
1980 clear_queue_bits( thread->queue, clear_bits );
1981 return 0;
1984 /* increment (or decrement if 'incr' is negative) the queue paint count */
1985 void inc_queue_paint_count( struct thread *thread, int incr )
1987 struct msg_queue *queue = thread->queue;
1989 assert( queue );
1991 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1993 if (queue->paint_count)
1994 set_queue_bits( queue, QS_PAINT );
1995 else
1996 clear_queue_bits( queue, QS_PAINT );
2000 /* remove all messages and timers belonging to a certain window */
2001 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2003 struct msg_queue *queue = thread->queue;
2004 struct list *ptr;
2005 int i;
2007 if (!queue) return;
2009 /* remove timers */
2011 ptr = list_head( &queue->pending_timers );
2012 while (ptr)
2014 struct list *next = list_next( &queue->pending_timers, ptr );
2015 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2016 if (timer->win == win) free_timer( queue, timer );
2017 ptr = next;
2019 ptr = list_head( &queue->expired_timers );
2020 while (ptr)
2022 struct list *next = list_next( &queue->expired_timers, ptr );
2023 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2024 if (timer->win == win) free_timer( queue, timer );
2025 ptr = next;
2028 /* remove messages */
2029 for (i = 0; i < NB_MSG_KINDS; i++)
2031 struct list *ptr, *next;
2033 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2035 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2036 if (msg->win == win)
2038 if (msg->msg == WM_QUIT && !queue->quit_message)
2040 queue->quit_message = 1;
2041 queue->exit_code = msg->wparam;
2043 remove_queue_message( queue, msg, i );
2048 thread_input_cleanup_window( queue, win );
2051 /* post a message to a window; used by socket handling */
2052 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2054 struct message *msg;
2055 struct thread *thread = get_window_thread( win );
2057 if (!thread) return;
2059 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2061 msg->type = MSG_POSTED;
2062 msg->win = get_user_full_handle( win );
2063 msg->msg = message;
2064 msg->wparam = wparam;
2065 msg->lparam = lparam;
2066 msg->time = get_tick_count();
2067 msg->result = NULL;
2068 msg->data = NULL;
2069 msg->data_size = 0;
2071 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2072 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2073 if (message == WM_HOTKEY)
2075 set_queue_bits( thread->queue, QS_HOTKEY );
2076 thread->queue->hotkey_count++;
2079 release_object( thread );
2082 /* post a win event */
2083 void post_win_event( struct thread *thread, unsigned int event,
2084 user_handle_t win, unsigned int object_id,
2085 unsigned int child_id, client_ptr_t hook_proc,
2086 const WCHAR *module, data_size_t module_size,
2087 user_handle_t hook)
2089 struct message *msg;
2091 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2093 struct winevent_msg_data *data;
2095 msg->type = MSG_WINEVENT;
2096 msg->win = get_user_full_handle( win );
2097 msg->msg = event;
2098 msg->wparam = object_id;
2099 msg->lparam = child_id;
2100 msg->time = get_tick_count();
2101 msg->result = NULL;
2103 if ((data = malloc( sizeof(*data) + module_size )))
2105 data->hook = hook;
2106 data->tid = get_thread_id( current );
2107 data->hook_proc = hook_proc;
2108 memcpy( data + 1, module, module_size );
2110 msg->data = data;
2111 msg->data_size = sizeof(*data) + module_size;
2113 if (debug_level > 1)
2114 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2115 get_thread_id(thread), event, win, object_id, child_id );
2116 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2117 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2119 else
2120 free( msg );
2124 /* free all hotkeys on a desktop, optionally filtering by window */
2125 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2127 struct hotkey *hotkey, *hotkey2;
2129 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2131 if (!window || hotkey->win == window)
2133 list_remove( &hotkey->entry );
2134 free( hotkey );
2140 /* check if the thread owning the window is hung */
2141 DECL_HANDLER(is_window_hung)
2143 struct thread *thread;
2145 thread = get_window_thread( req->win );
2146 if (thread)
2148 reply->is_hung = is_queue_hung( thread->queue );
2149 release_object( thread );
2151 else reply->is_hung = 0;
2155 /* get the message queue of the current thread */
2156 DECL_HANDLER(get_msg_queue)
2158 struct msg_queue *queue = get_current_queue();
2160 reply->handle = 0;
2161 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2165 /* set the file descriptor associated to the current thread queue */
2166 DECL_HANDLER(set_queue_fd)
2168 struct msg_queue *queue = get_current_queue();
2169 struct file *file;
2170 int unix_fd;
2172 if (queue->fd) /* fd can only be set once */
2174 set_error( STATUS_ACCESS_DENIED );
2175 return;
2177 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2179 if ((unix_fd = get_file_unix_fd( file )) != -1)
2181 if ((unix_fd = dup( unix_fd )) != -1)
2182 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2183 else
2184 file_set_error();
2186 release_object( file );
2190 /* set the current message queue wakeup mask */
2191 DECL_HANDLER(set_queue_mask)
2193 struct msg_queue *queue = get_current_queue();
2195 if (queue)
2197 queue->wake_mask = req->wake_mask;
2198 queue->changed_mask = req->changed_mask;
2199 reply->wake_bits = queue->wake_bits;
2200 reply->changed_bits = queue->changed_bits;
2201 if (is_signaled( queue ))
2203 /* if skip wait is set, do what would have been done in the subsequent wait */
2204 if (req->skip_wait)
2206 queue->wake_mask = queue->wake_get_msg;
2207 queue->changed_mask = queue->changed_get_msg;
2209 else wake_up( &queue->obj, 0 );
2215 /* get the current message queue status */
2216 DECL_HANDLER(get_queue_status)
2218 struct msg_queue *queue = current->queue;
2219 if (queue)
2221 reply->wake_bits = queue->wake_bits;
2222 reply->changed_bits = queue->changed_bits;
2223 if (req->clear) queue->changed_bits = 0;
2225 else reply->wake_bits = reply->changed_bits = 0;
2229 /* send a message to a thread queue */
2230 DECL_HANDLER(send_message)
2232 struct message *msg;
2233 struct msg_queue *send_queue = get_current_queue();
2234 struct msg_queue *recv_queue = NULL;
2235 struct thread *thread = NULL;
2237 if (!(thread = get_thread_from_id( req->id ))) return;
2239 if (!(recv_queue = thread->queue))
2241 set_error( STATUS_INVALID_PARAMETER );
2242 release_object( thread );
2243 return;
2245 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2247 set_error( STATUS_TIMEOUT );
2248 release_object( thread );
2249 return;
2252 if ((msg = mem_alloc( sizeof(*msg) )))
2254 msg->type = req->type;
2255 msg->win = get_user_full_handle( req->win );
2256 msg->msg = req->msg;
2257 msg->wparam = req->wparam;
2258 msg->lparam = req->lparam;
2259 msg->time = get_tick_count();
2260 msg->result = NULL;
2261 msg->data = NULL;
2262 msg->data_size = get_req_data_size();
2264 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2266 free( msg );
2267 release_object( thread );
2268 return;
2271 switch(msg->type)
2273 case MSG_OTHER_PROCESS:
2274 case MSG_ASCII:
2275 case MSG_UNICODE:
2276 case MSG_CALLBACK:
2277 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2279 free_message( msg );
2280 break;
2282 /* fall through */
2283 case MSG_NOTIFY:
2284 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2285 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2286 break;
2287 case MSG_POSTED:
2288 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2289 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2290 if (msg->msg == WM_HOTKEY)
2292 set_queue_bits( recv_queue, QS_HOTKEY );
2293 recv_queue->hotkey_count++;
2295 break;
2296 case MSG_HARDWARE: /* should use send_hardware_message instead */
2297 case MSG_CALLBACK_RESULT: /* cannot send this one */
2298 case MSG_HOOK_LL: /* generated internally */
2299 default:
2300 set_error( STATUS_INVALID_PARAMETER );
2301 free( msg );
2302 break;
2305 release_object( thread );
2308 /* send a hardware message to a thread queue */
2309 DECL_HANDLER(send_hardware_message)
2311 struct thread *thread = NULL;
2312 struct desktop *desktop;
2313 struct msg_queue *sender = get_current_queue();
2314 data_size_t size = min( 256, get_reply_max_size() );
2316 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2318 if (req->win)
2320 if (!(thread = get_window_thread( req->win ))) return;
2321 if (desktop != thread->queue->input->desktop)
2323 /* don't allow queuing events to a different desktop */
2324 release_object( desktop );
2325 return;
2329 reply->prev_x = desktop->cursor.x;
2330 reply->prev_y = desktop->cursor.y;
2332 switch (req->input.type)
2334 case INPUT_MOUSE:
2335 reply->wait = queue_mouse_message( desktop, req->win, &req->input, req->flags, sender );
2336 break;
2337 case INPUT_KEYBOARD:
2338 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, req->flags, sender );
2339 break;
2340 case INPUT_HARDWARE:
2341 queue_custom_hardware_message( desktop, req->win, &req->input );
2342 break;
2343 default:
2344 set_error( STATUS_INVALID_PARAMETER );
2346 if (thread) release_object( thread );
2348 reply->new_x = desktop->cursor.x;
2349 reply->new_y = desktop->cursor.y;
2350 set_reply_data( desktop->keystate, size );
2351 release_object( desktop );
2354 /* post a quit message to the current queue */
2355 DECL_HANDLER(post_quit_message)
2357 struct msg_queue *queue = get_current_queue();
2359 if (!queue)
2360 return;
2362 queue->quit_message = 1;
2363 queue->exit_code = req->exit_code;
2364 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2367 /* get a message from the current queue */
2368 DECL_HANDLER(get_message)
2370 struct timer *timer;
2371 struct list *ptr;
2372 struct msg_queue *queue = get_current_queue();
2373 user_handle_t get_win = get_user_full_handle( req->get_win );
2374 unsigned int filter = req->flags >> 16;
2376 reply->active_hooks = get_active_hooks();
2378 if (!queue) return;
2379 queue->last_get_msg = current_time;
2380 queue->wake_get_msg = queue->changed_get_msg = 0;
2381 if (!filter) filter = QS_ALLINPUT;
2383 /* first check for sent messages */
2384 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2386 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2387 receive_message( queue, msg, reply );
2388 return;
2391 /* clear changed bits so we can wait on them if we don't find a message */
2392 if (filter & QS_POSTMESSAGE)
2394 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2395 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2397 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2398 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2400 /* then check for posted messages */
2401 if ((filter & QS_POSTMESSAGE) &&
2402 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2403 return;
2405 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2406 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2407 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2408 return;
2410 /* only check for quit messages if not posted messages pending.
2411 * note: the quit message isn't filtered */
2412 if (get_quit_message( queue, req->flags, reply ))
2413 return;
2415 /* then check for any raw hardware message */
2416 if ((filter & QS_INPUT) &&
2417 filter_contains_hw_range( req->get_first, req->get_last ) &&
2418 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2419 return;
2421 /* now check for WM_PAINT */
2422 if ((filter & QS_PAINT) &&
2423 queue->paint_count &&
2424 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2425 (reply->win = find_window_to_repaint( get_win, current )))
2427 reply->type = MSG_POSTED;
2428 reply->msg = WM_PAINT;
2429 reply->wparam = 0;
2430 reply->lparam = 0;
2431 reply->time = get_tick_count();
2432 return;
2435 /* now check for timer */
2436 if ((filter & QS_TIMER) &&
2437 (timer = find_expired_timer( queue, get_win, req->get_first,
2438 req->get_last, (req->flags & PM_REMOVE) )))
2440 reply->type = MSG_POSTED;
2441 reply->win = timer->win;
2442 reply->msg = timer->msg;
2443 reply->wparam = timer->id;
2444 reply->lparam = timer->lparam;
2445 reply->time = get_tick_count();
2446 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2447 set_event( current->process->idle_event );
2448 return;
2451 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2452 queue->wake_mask = queue->wake_get_msg = req->wake_mask;
2453 queue->changed_mask = queue->changed_get_msg = req->changed_mask;
2454 set_error( STATUS_PENDING ); /* FIXME */
2458 /* reply to a sent message */
2459 DECL_HANDLER(reply_message)
2461 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2462 else if (current->queue->recv_result)
2463 reply_message( current->queue, req->result, 0, req->remove,
2464 get_req_data(), get_req_data_size() );
2468 /* accept the current hardware message */
2469 DECL_HANDLER(accept_hardware_message)
2471 if (current->queue)
2472 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
2473 else
2474 set_error( STATUS_ACCESS_DENIED );
2478 /* retrieve the reply for the last message sent */
2479 DECL_HANDLER(get_message_reply)
2481 struct message_result *result;
2482 struct list *entry;
2483 struct msg_queue *queue = current->queue;
2485 if (queue)
2487 set_error( STATUS_PENDING );
2488 reply->result = 0;
2490 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2492 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2493 if (result->replied || req->cancel)
2495 if (result->replied)
2497 reply->result = result->result;
2498 set_error( result->error );
2499 if (result->data)
2501 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2502 set_reply_data_ptr( result->data, data_len );
2503 result->data = NULL;
2504 result->data_size = 0;
2507 remove_result_from_sender( result );
2509 entry = list_head( &queue->send_result );
2510 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2511 else
2513 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2514 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
2518 else set_error( STATUS_ACCESS_DENIED );
2522 /* set a window timer */
2523 DECL_HANDLER(set_win_timer)
2525 struct timer *timer;
2526 struct msg_queue *queue;
2527 struct thread *thread = NULL;
2528 user_handle_t win = 0;
2529 lparam_t id = req->id;
2531 if (req->win)
2533 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2535 set_error( STATUS_INVALID_HANDLE );
2536 return;
2538 if (thread->process != current->process)
2540 release_object( thread );
2541 set_error( STATUS_ACCESS_DENIED );
2542 return;
2544 queue = thread->queue;
2545 /* remove it if it existed already */
2546 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2548 else
2550 queue = get_current_queue();
2551 /* look for a timer with this id */
2552 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2554 /* free and reuse id */
2555 free_timer( queue, timer );
2557 else
2559 /* find a free id for it */
2562 id = queue->next_timer_id;
2563 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2565 while (find_timer( queue, 0, req->msg, id ));
2569 if ((timer = set_timer( queue, req->rate )))
2571 timer->win = win;
2572 timer->msg = req->msg;
2573 timer->id = id;
2574 timer->lparam = req->lparam;
2575 reply->id = id;
2577 if (thread) release_object( thread );
2580 /* kill a window timer */
2581 DECL_HANDLER(kill_win_timer)
2583 struct timer *timer;
2584 struct thread *thread;
2585 user_handle_t win = 0;
2587 if (req->win)
2589 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2591 set_error( STATUS_INVALID_HANDLE );
2592 return;
2594 if (thread->process != current->process)
2596 release_object( thread );
2597 set_error( STATUS_ACCESS_DENIED );
2598 return;
2601 else thread = (struct thread *)grab_object( current );
2603 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2604 free_timer( thread->queue, timer );
2605 else
2606 set_error( STATUS_INVALID_PARAMETER );
2608 release_object( thread );
2611 DECL_HANDLER(register_hotkey)
2613 struct desktop *desktop;
2614 user_handle_t win_handle = req->window;
2615 struct hotkey *hotkey;
2616 struct hotkey *new_hotkey = NULL;
2617 struct thread *thread;
2618 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2620 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2622 if (win_handle)
2624 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2626 release_object( desktop );
2627 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2628 return;
2631 thread = get_window_thread( win_handle );
2632 if (thread) release_object( thread );
2634 if (thread != current)
2636 release_object( desktop );
2637 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2638 return;
2642 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2644 if (req->vkey == hotkey->vkey &&
2645 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2647 release_object( desktop );
2648 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2649 return;
2651 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2652 new_hotkey = hotkey;
2655 if (new_hotkey)
2657 reply->replaced = 1;
2658 reply->flags = new_hotkey->flags;
2659 reply->vkey = new_hotkey->vkey;
2661 else
2663 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2664 if (new_hotkey)
2666 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2667 new_hotkey->queue = current->queue;
2668 new_hotkey->win = win_handle;
2669 new_hotkey->id = req->id;
2673 if (new_hotkey)
2675 new_hotkey->flags = req->flags;
2676 new_hotkey->vkey = req->vkey;
2679 release_object( desktop );
2682 DECL_HANDLER(unregister_hotkey)
2684 struct desktop *desktop;
2685 user_handle_t win_handle = req->window;
2686 struct hotkey *hotkey;
2687 struct thread *thread;
2689 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2691 if (win_handle)
2693 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2695 release_object( desktop );
2696 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2697 return;
2700 thread = get_window_thread( win_handle );
2701 if (thread) release_object( thread );
2703 if (thread != current)
2705 release_object( desktop );
2706 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2707 return;
2711 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2713 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2714 goto found;
2717 release_object( desktop );
2718 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2719 return;
2721 found:
2722 reply->flags = hotkey->flags;
2723 reply->vkey = hotkey->vkey;
2724 list_remove( &hotkey->entry );
2725 free( hotkey );
2726 release_object( desktop );
2729 /* attach (or detach) thread inputs */
2730 DECL_HANDLER(attach_thread_input)
2732 struct thread *thread_from = get_thread_from_id( req->tid_from );
2733 struct thread *thread_to = get_thread_from_id( req->tid_to );
2735 if (!thread_from || !thread_to)
2737 if (thread_from) release_object( thread_from );
2738 if (thread_to) release_object( thread_to );
2739 return;
2741 if (thread_from != thread_to)
2743 if (req->attach) attach_thread_input( thread_from, thread_to );
2744 else
2746 if (thread_from->queue && thread_to->queue &&
2747 thread_from->queue->input == thread_to->queue->input)
2748 detach_thread_input( thread_from );
2749 else
2750 set_error( STATUS_ACCESS_DENIED );
2753 else set_error( STATUS_ACCESS_DENIED );
2754 release_object( thread_from );
2755 release_object( thread_to );
2759 /* get thread input data */
2760 DECL_HANDLER(get_thread_input)
2762 struct thread *thread = NULL;
2763 struct desktop *desktop;
2764 struct thread_input *input;
2766 if (req->tid)
2768 if (!(thread = get_thread_from_id( req->tid ))) return;
2769 if (!(desktop = get_thread_desktop( thread, 0 )))
2771 release_object( thread );
2772 return;
2774 input = thread->queue ? thread->queue->input : NULL;
2776 else
2778 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2779 input = desktop->foreground_input; /* get the foreground thread info */
2782 if (input)
2784 reply->focus = input->focus;
2785 reply->capture = input->capture;
2786 reply->active = input->active;
2787 reply->menu_owner = input->menu_owner;
2788 reply->move_size = input->move_size;
2789 reply->caret = input->caret;
2790 reply->cursor = input->cursor;
2791 reply->show_count = input->cursor_count;
2792 reply->rect = input->caret_rect;
2795 /* foreground window is active window of foreground thread */
2796 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2797 if (thread) release_object( thread );
2798 release_object( desktop );
2802 /* retrieve queue keyboard state for a given thread */
2803 DECL_HANDLER(get_key_state)
2805 struct thread *thread;
2806 struct desktop *desktop;
2807 data_size_t size = min( 256, get_reply_max_size() );
2809 if (!req->tid) /* get global async key state */
2811 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2812 if (req->key >= 0)
2814 reply->state = desktop->keystate[req->key & 0xff];
2815 desktop->keystate[req->key & 0xff] &= ~0x40;
2817 set_reply_data( desktop->keystate, size );
2818 release_object( desktop );
2820 else
2822 if (!(thread = get_thread_from_id( req->tid ))) return;
2823 if (thread->queue)
2825 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2826 set_reply_data( thread->queue->input->keystate, size );
2828 release_object( thread );
2833 /* set queue keyboard state for a given thread */
2834 DECL_HANDLER(set_key_state)
2836 struct thread *thread;
2837 struct desktop *desktop;
2838 data_size_t size = min( 256, get_req_data_size() );
2840 if (!req->tid) /* set global async key state */
2842 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2843 memcpy( desktop->keystate, get_req_data(), size );
2844 release_object( desktop );
2846 else
2848 if (!(thread = get_thread_from_id( req->tid ))) return;
2849 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
2850 if (req->async && (desktop = get_thread_desktop( thread, 0 )))
2852 memcpy( desktop->keystate, get_req_data(), size );
2853 release_object( desktop );
2855 release_object( thread );
2860 /* set the system foreground window */
2861 DECL_HANDLER(set_foreground_window)
2863 struct thread *thread = NULL;
2864 struct desktop *desktop;
2865 struct msg_queue *queue = get_current_queue();
2867 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2868 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
2869 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
2870 reply->send_msg_new = FALSE;
2872 if (is_valid_foreground_window( req->handle ) &&
2873 (thread = get_window_thread( req->handle )) &&
2874 thread->queue->input->desktop == desktop)
2876 set_foreground_input( desktop, thread->queue->input );
2877 reply->send_msg_new = (desktop->foreground_input != queue->input);
2879 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2881 if (thread) release_object( thread );
2882 release_object( desktop );
2886 /* set the current thread focus window */
2887 DECL_HANDLER(set_focus_window)
2889 struct msg_queue *queue = get_current_queue();
2891 reply->previous = 0;
2892 if (queue && check_queue_input_window( queue, req->handle ))
2894 reply->previous = queue->input->focus;
2895 queue->input->focus = get_user_full_handle( req->handle );
2900 /* set the current thread active window */
2901 DECL_HANDLER(set_active_window)
2903 struct msg_queue *queue = get_current_queue();
2905 reply->previous = 0;
2906 if (queue && check_queue_input_window( queue, req->handle ))
2908 if (!req->handle || make_window_active( req->handle ))
2910 reply->previous = queue->input->active;
2911 queue->input->active = get_user_full_handle( req->handle );
2913 else set_error( STATUS_INVALID_HANDLE );
2918 /* set the current thread capture window */
2919 DECL_HANDLER(set_capture_window)
2921 struct msg_queue *queue = get_current_queue();
2923 reply->previous = reply->full_handle = 0;
2924 if (queue && check_queue_input_window( queue, req->handle ))
2926 struct thread_input *input = queue->input;
2928 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2929 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2931 set_error(STATUS_ACCESS_DENIED);
2932 return;
2934 reply->previous = input->capture;
2935 input->capture = get_user_full_handle( req->handle );
2936 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2937 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2938 reply->full_handle = input->capture;
2943 /* Set the current thread caret window */
2944 DECL_HANDLER(set_caret_window)
2946 struct msg_queue *queue = get_current_queue();
2948 reply->previous = 0;
2949 if (queue && check_queue_input_window( queue, req->handle ))
2951 struct thread_input *input = queue->input;
2953 reply->previous = input->caret;
2954 reply->old_rect = input->caret_rect;
2955 reply->old_hide = input->caret_hide;
2956 reply->old_state = input->caret_state;
2958 set_caret_window( input, get_user_full_handle(req->handle) );
2959 input->caret_rect.right = input->caret_rect.left + req->width;
2960 input->caret_rect.bottom = input->caret_rect.top + req->height;
2965 /* Set the current thread caret information */
2966 DECL_HANDLER(set_caret_info)
2968 struct msg_queue *queue = get_current_queue();
2969 struct thread_input *input;
2971 if (!queue) return;
2972 input = queue->input;
2973 reply->full_handle = input->caret;
2974 reply->old_rect = input->caret_rect;
2975 reply->old_hide = input->caret_hide;
2976 reply->old_state = input->caret_state;
2978 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2980 set_error( STATUS_ACCESS_DENIED );
2981 return;
2983 if (req->flags & SET_CARET_POS)
2985 input->caret_rect.right += req->x - input->caret_rect.left;
2986 input->caret_rect.bottom += req->y - input->caret_rect.top;
2987 input->caret_rect.left = req->x;
2988 input->caret_rect.top = req->y;
2990 if (req->flags & SET_CARET_HIDE)
2992 input->caret_hide += req->hide;
2993 if (input->caret_hide < 0) input->caret_hide = 0;
2995 if (req->flags & SET_CARET_STATE)
2997 if (req->state == -1) input->caret_state = !input->caret_state;
2998 else input->caret_state = !!req->state;
3003 /* get the time of the last input event */
3004 DECL_HANDLER(get_last_input_time)
3006 reply->time = last_input_time;
3009 /* set/get the current cursor */
3010 DECL_HANDLER(set_cursor)
3012 struct msg_queue *queue = get_current_queue();
3013 struct thread_input *input;
3015 if (!queue) return;
3016 input = queue->input;
3018 reply->prev_handle = input->cursor;
3019 reply->prev_count = input->cursor_count;
3020 reply->prev_x = input->desktop->cursor.x;
3021 reply->prev_y = input->desktop->cursor.y;
3023 if (req->flags & SET_CURSOR_HANDLE)
3025 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3027 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3028 return;
3030 input->cursor = req->handle;
3032 if (req->flags & SET_CURSOR_COUNT)
3034 queue->cursor_count += req->show_count;
3035 input->cursor_count += req->show_count;
3037 if (req->flags & SET_CURSOR_POS)
3039 set_cursor_pos( input->desktop, req->x, req->y );
3041 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3043 struct desktop *desktop = input->desktop;
3045 /* only the desktop owner can set the message */
3046 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3047 desktop->cursor.clip_msg = req->clip_msg;
3049 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip );
3052 reply->new_x = input->desktop->cursor.x;
3053 reply->new_y = input->desktop->cursor.y;
3054 reply->new_clip = input->desktop->cursor.clip;
3055 reply->last_change = input->desktop->cursor.last_change;
3058 DECL_HANDLER(update_rawinput_devices)
3060 const struct rawinput_device *devices = get_req_data();
3061 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3062 const struct rawinput_device_entry *e;
3063 unsigned int i;
3065 for (i = 0; i < device_count; ++i)
3067 update_rawinput_device(&devices[i]);
3070 e = find_rawinput_device( 1, 2 );
3071 current->process->rawinput_mouse = e ? &e->device : NULL;
3072 e = find_rawinput_device( 1, 6 );
3073 current->process->rawinput_kbd = e ? &e->device : NULL;