winejoystick: Fix a crash on accessing a CFArray past its end due to an off-by-one...
[wine/multimedia.git] / server / queue.c
blob3a321cd16cf27950c0ad5ca4ce553215ababe54d
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>
28 #ifdef HAVE_POLL_H
29 # include <poll.h>
30 #endif
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "winternl.h"
40 #include "handle.h"
41 #include "file.h"
42 #include "thread.h"
43 #include "process.h"
44 #include "request.h"
45 #include "user.h"
47 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
48 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
50 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
51 #define NB_MSG_KINDS (POST_MESSAGE+1)
54 struct message_result
56 struct list sender_entry; /* entry in sender list */
57 struct message *msg; /* message the result is for */
58 struct message_result *recv_next; /* next in receiver list */
59 struct msg_queue *sender; /* sender queue */
60 struct msg_queue *receiver; /* receiver queue */
61 int replied; /* has it been replied to? */
62 unsigned int error; /* error code to pass back to sender */
63 lparam_t result; /* reply result */
64 struct message *hardware_msg; /* hardware message if low-level hook result */
65 struct desktop *desktop; /* desktop for hardware message */
66 struct message *callback_msg; /* message to queue for callback */
67 void *data; /* message reply data */
68 unsigned int data_size; /* size of message reply data */
69 struct timeout_user *timeout; /* result timeout */
72 struct message
74 struct list entry; /* entry in message list */
75 enum message_type type; /* message type */
76 user_handle_t win; /* window handle */
77 unsigned int msg; /* message code */
78 lparam_t wparam; /* parameters */
79 lparam_t lparam; /* parameters */
80 unsigned int time; /* message time */
81 void *data; /* message data for sent messages */
82 unsigned int data_size; /* size of message data */
83 unsigned int unique_id; /* unique id for nested hw message waits */
84 struct message_result *result; /* result in sender queue */
87 struct timer
89 struct list entry; /* entry in timer list */
90 timeout_t when; /* next expiration */
91 unsigned int rate; /* timer rate in ms */
92 user_handle_t win; /* window handle */
93 unsigned int msg; /* message to post */
94 lparam_t id; /* timer id */
95 lparam_t lparam; /* lparam for message */
98 struct thread_input
100 struct object obj; /* object header */
101 struct desktop *desktop; /* desktop that this thread input belongs to */
102 user_handle_t focus; /* focus window */
103 user_handle_t capture; /* capture window */
104 user_handle_t active; /* active window */
105 user_handle_t menu_owner; /* current menu owner window */
106 user_handle_t move_size; /* current moving/resizing window */
107 user_handle_t caret; /* caret window */
108 rectangle_t caret_rect; /* caret rectangle */
109 int caret_hide; /* caret hide count */
110 int caret_state; /* caret on/off state */
111 user_handle_t cursor; /* current cursor */
112 int cursor_count; /* cursor show count */
113 struct list msg_list; /* list of hardware messages */
114 unsigned char keystate[256]; /* state of each key */
117 struct msg_queue
119 struct object obj; /* object header */
120 struct fd *fd; /* optional file descriptor to poll */
121 unsigned int wake_bits; /* wakeup bits */
122 unsigned int wake_mask; /* wakeup mask */
123 unsigned int changed_bits; /* changed wakeup bits */
124 unsigned int changed_mask; /* changed wakeup mask */
125 int paint_count; /* pending paint messages count */
126 int hotkey_count; /* pending hotkey messages count */
127 int quit_message; /* is there a pending quit message? */
128 int exit_code; /* exit code of pending quit message */
129 int cursor_count; /* per-queue cursor show count */
130 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
131 struct list send_result; /* stack of sent messages waiting for result */
132 struct list callback_result; /* list of callback messages waiting for result */
133 struct message_result *recv_result; /* stack of received messages waiting for result */
134 struct list pending_timers; /* list of pending timers */
135 struct list expired_timers; /* list of expired timers */
136 lparam_t next_timer_id; /* id for the next timer with a 0 window */
137 struct timeout_user *timeout; /* timeout for next timer to expire */
138 struct thread_input *input; /* thread input descriptor */
139 struct hook_table *hooks; /* hook table */
140 timeout_t last_get_msg; /* time of last get message call */
143 struct hotkey
145 struct list entry; /* entry in desktop hotkey list */
146 struct msg_queue *queue; /* queue owning this hotkey */
147 user_handle_t win; /* window handle */
148 int id; /* hotkey id */
149 unsigned int vkey; /* virtual key code */
150 unsigned int flags; /* key modifiers */
153 static void msg_queue_dump( struct object *obj, int verbose );
154 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
155 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
156 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
157 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
158 static void msg_queue_destroy( struct object *obj );
159 static void msg_queue_poll_event( struct fd *fd, int event );
160 static void thread_input_dump( struct object *obj, int verbose );
161 static void thread_input_destroy( struct object *obj );
162 static void timer_callback( void *private );
164 static const struct object_ops msg_queue_ops =
166 sizeof(struct msg_queue), /* size */
167 msg_queue_dump, /* dump */
168 no_get_type, /* get_type */
169 msg_queue_add_queue, /* add_queue */
170 msg_queue_remove_queue, /* remove_queue */
171 msg_queue_signaled, /* signaled */
172 msg_queue_satisfied, /* satisfied */
173 no_signal, /* signal */
174 no_get_fd, /* get_fd */
175 no_map_access, /* map_access */
176 default_get_sd, /* get_sd */
177 default_set_sd, /* set_sd */
178 no_lookup_name, /* lookup_name */
179 no_open_file, /* open_file */
180 no_close_handle, /* close_handle */
181 msg_queue_destroy /* destroy */
184 static const struct fd_ops msg_queue_fd_ops =
186 NULL, /* get_poll_events */
187 msg_queue_poll_event, /* poll_event */
188 NULL, /* flush */
189 NULL, /* get_fd_type */
190 NULL, /* ioctl */
191 NULL, /* queue_async */
192 NULL, /* reselect_async */
193 NULL /* cancel async */
197 static const struct object_ops thread_input_ops =
199 sizeof(struct thread_input), /* size */
200 thread_input_dump, /* dump */
201 no_get_type, /* get_type */
202 no_add_queue, /* add_queue */
203 NULL, /* remove_queue */
204 NULL, /* signaled */
205 NULL, /* satisfied */
206 no_signal, /* signal */
207 no_get_fd, /* get_fd */
208 no_map_access, /* map_access */
209 default_get_sd, /* get_sd */
210 default_set_sd, /* set_sd */
211 no_lookup_name, /* lookup_name */
212 no_open_file, /* open_file */
213 no_close_handle, /* close_handle */
214 thread_input_destroy /* destroy */
217 /* pointer to input structure of foreground thread */
218 static unsigned int last_input_time;
220 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
221 static void free_message( struct message *msg );
223 /* set the caret window in a given thread input */
224 static void set_caret_window( struct thread_input *input, user_handle_t win )
226 if (!win || win != input->caret)
228 input->caret_rect.left = 0;
229 input->caret_rect.top = 0;
230 input->caret_rect.right = 0;
231 input->caret_rect.bottom = 0;
233 input->caret = win;
234 input->caret_hide = 1;
235 input->caret_state = 0;
238 /* create a thread input object */
239 static struct thread_input *create_thread_input( struct thread *thread )
241 struct thread_input *input;
243 if ((input = alloc_object( &thread_input_ops )))
245 input->focus = 0;
246 input->capture = 0;
247 input->active = 0;
248 input->menu_owner = 0;
249 input->move_size = 0;
250 input->cursor = 0;
251 input->cursor_count = 0;
252 list_init( &input->msg_list );
253 set_caret_window( input, 0 );
254 memset( input->keystate, 0, sizeof(input->keystate) );
256 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
258 release_object( input );
259 return NULL;
262 return input;
265 /* create a message queue object */
266 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
268 struct thread_input *new_input = NULL;
269 struct msg_queue *queue;
270 int i;
272 if (!input)
274 if (!(new_input = create_thread_input( thread ))) return NULL;
275 input = new_input;
278 if ((queue = alloc_object( &msg_queue_ops )))
280 queue->fd = NULL;
281 queue->wake_bits = 0;
282 queue->wake_mask = 0;
283 queue->changed_bits = 0;
284 queue->changed_mask = 0;
285 queue->paint_count = 0;
286 queue->hotkey_count = 0;
287 queue->quit_message = 0;
288 queue->cursor_count = 0;
289 queue->recv_result = NULL;
290 queue->next_timer_id = 0x7fff;
291 queue->timeout = NULL;
292 queue->input = (struct thread_input *)grab_object( input );
293 queue->hooks = NULL;
294 queue->last_get_msg = current_time;
295 list_init( &queue->send_result );
296 list_init( &queue->callback_result );
297 list_init( &queue->pending_timers );
298 list_init( &queue->expired_timers );
299 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
301 thread->queue = queue;
303 if (new_input) release_object( new_input );
304 return queue;
307 /* free the message queue of a thread at thread exit */
308 void free_msg_queue( struct thread *thread )
310 remove_thread_hooks( thread );
311 if (!thread->queue) return;
312 release_object( thread->queue );
313 thread->queue = NULL;
316 /* change the thread input data of a given thread */
317 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
319 struct msg_queue *queue = thread->queue;
321 if (!queue)
323 thread->queue = create_msg_queue( thread, new_input );
324 return thread->queue != NULL;
326 if (queue->input)
328 queue->input->cursor_count -= queue->cursor_count;
329 release_object( queue->input );
331 queue->input = (struct thread_input *)grab_object( new_input );
332 new_input->cursor_count += queue->cursor_count;
333 return 1;
336 /* set the cursor position and queue the corresponding mouse message */
337 static void set_cursor_pos( struct desktop *desktop, int x, int y )
339 struct hardware_msg_data *msg_data;
340 struct message *msg;
342 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
343 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
345 free( msg );
346 return;
348 memset( msg_data, 0, sizeof(*msg_data) );
350 msg->type = MSG_HARDWARE;
351 msg->win = 0;
352 msg->msg = WM_MOUSEMOVE;
353 msg->wparam = 0;
354 msg->lparam = 0;
355 msg->time = get_tick_count();
356 msg->result = NULL;
357 msg->data = msg_data;
358 msg->data_size = sizeof(*msg_data);
359 msg_data->x = x;
360 msg_data->y = y;
361 queue_hardware_message( desktop, msg, 1 );
364 /* set the cursor clip rectangle */
365 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect )
367 rectangle_t top_rect;
368 int x, y;
370 get_top_window_rectangle( desktop, &top_rect );
371 if (rect)
373 rectangle_t new_rect = *rect;
374 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
375 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
376 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
377 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
378 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
379 desktop->cursor.clip = new_rect;
381 else desktop->cursor.clip = top_rect;
383 if (desktop->cursor.clip_msg)
384 post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
386 /* warp the mouse to be inside the clip rect */
387 x = min( max( desktop->cursor.x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
388 y = min( max( desktop->cursor.y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
389 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
392 /* change the foreground input and reset the cursor clip rect */
393 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
395 if (desktop->foreground_input == input) return;
396 set_clip_rectangle( desktop, NULL );
397 desktop->foreground_input = input;
400 /* get the hook table for a given thread */
401 struct hook_table *get_queue_hooks( struct thread *thread )
403 if (!thread->queue) return NULL;
404 return thread->queue->hooks;
407 /* set the hook table for a given thread, allocating the queue if needed */
408 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
410 struct msg_queue *queue = thread->queue;
411 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
412 if (queue->hooks) release_object( queue->hooks );
413 queue->hooks = hooks;
416 /* check the queue status */
417 static inline int is_signaled( struct msg_queue *queue )
419 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
422 /* set some queue bits */
423 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
425 queue->wake_bits |= bits;
426 queue->changed_bits |= bits;
427 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
430 /* clear some queue bits */
431 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
433 queue->wake_bits &= ~bits;
434 queue->changed_bits &= ~bits;
437 /* check whether msg is a keyboard message */
438 static inline int is_keyboard_msg( struct message *msg )
440 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
443 /* check if message is matched by the filter */
444 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
446 return (msg >= first && msg <= last);
449 /* check whether a message filter contains at least one potential hardware message */
450 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
452 /* hardware message ranges are (in numerical order):
453 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
454 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
455 * WM_MOUSEFIRST .. WM_MOUSELAST
457 if (last < WM_NCMOUSEFIRST) return 0;
458 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
459 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
460 if (first > WM_MOUSELAST) return 0;
461 return 1;
464 /* get the QS_* bit corresponding to a given hardware message */
465 static inline int get_hardware_msg_bit( struct message *msg )
467 if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
468 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
469 if (is_keyboard_msg( msg )) return QS_KEY;
470 return QS_MOUSEBUTTON;
473 /* get the current thread queue, creating it if needed */
474 static inline struct msg_queue *get_current_queue(void)
476 struct msg_queue *queue = current->queue;
477 if (!queue) queue = create_msg_queue( current, NULL );
478 return queue;
481 /* get a (pseudo-)unique id to tag hardware messages */
482 static inline unsigned int get_unique_id(void)
484 static unsigned int id;
485 if (!++id) id = 1; /* avoid an id of 0 */
486 return id;
489 /* try to merge a message with the last in the list; return 1 if successful */
490 static int merge_message( struct thread_input *input, const struct message *msg )
492 struct message *prev;
493 struct list *ptr;
495 if (msg->msg != WM_MOUSEMOVE) return 0;
496 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
498 prev = LIST_ENTRY( ptr, struct message, entry );
499 if (prev->msg != WM_INPUT) break;
501 if (!ptr) return 0;
502 if (prev->result) return 0;
503 if (prev->win && msg->win && prev->win != msg->win) return 0;
504 if (prev->msg != msg->msg) return 0;
505 if (prev->type != msg->type) return 0;
506 /* now we can merge it */
507 prev->wparam = msg->wparam;
508 prev->lparam = msg->lparam;
509 prev->time = msg->time;
510 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
512 struct hardware_msg_data *prev_data = prev->data;
513 struct hardware_msg_data *msg_data = msg->data;
514 prev_data->x = msg_data->x;
515 prev_data->y = msg_data->y;
516 prev_data->info = msg_data->info;
518 list_remove( ptr );
519 list_add_tail( &input->msg_list, ptr );
520 return 1;
523 /* free a result structure */
524 static void free_result( struct message_result *result )
526 if (result->timeout) remove_timeout_user( result->timeout );
527 free( result->data );
528 if (result->callback_msg) free_message( result->callback_msg );
529 if (result->hardware_msg) free_message( result->hardware_msg );
530 if (result->desktop) release_object( result->desktop );
531 free( result );
534 /* remove the result from the sender list it is on */
535 static inline void remove_result_from_sender( struct message_result *result )
537 assert( result->sender );
539 list_remove( &result->sender_entry );
540 result->sender = NULL;
541 if (!result->receiver) free_result( result );
544 /* store the message result in the appropriate structure */
545 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
547 res->result = result;
548 res->error = error;
549 res->replied = 1;
550 if (res->timeout)
552 remove_timeout_user( res->timeout );
553 res->timeout = NULL;
556 if (res->hardware_msg)
558 if (!error && result) /* rejected by the hook */
559 free_message( res->hardware_msg );
560 else
561 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
563 res->hardware_msg = NULL;
566 if (res->sender)
568 if (res->callback_msg)
570 /* queue the callback message in the sender queue */
571 struct callback_msg_data *data = res->callback_msg->data;
572 data->result = result;
573 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
574 set_queue_bits( res->sender, QS_SENDMESSAGE );
575 res->callback_msg = NULL;
576 remove_result_from_sender( res );
578 else
580 /* wake sender queue if waiting on this result */
581 if (list_head(&res->sender->send_result) == &res->sender_entry)
582 set_queue_bits( res->sender, QS_SMRESULT );
585 else if (!res->receiver) free_result( res );
588 /* free a message when deleting a queue or window */
589 static void free_message( struct message *msg )
591 struct message_result *result = msg->result;
592 if (result)
594 result->msg = NULL;
595 result->receiver = NULL;
596 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
598 free( msg->data );
599 free( msg );
602 /* remove (and free) a message from a message list */
603 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
604 enum message_kind kind )
606 list_remove( &msg->entry );
607 switch(kind)
609 case SEND_MESSAGE:
610 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
611 break;
612 case POST_MESSAGE:
613 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
614 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
615 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
616 clear_queue_bits( queue, QS_HOTKEY );
617 break;
619 free_message( msg );
622 /* message timed out without getting a reply */
623 static void result_timeout( void *private )
625 struct message_result *result = private;
627 assert( !result->replied );
629 result->timeout = NULL;
631 if (result->msg) /* not received yet */
633 struct message *msg = result->msg;
635 result->msg = NULL;
636 msg->result = NULL;
637 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
638 result->receiver = NULL;
640 store_message_result( result, 0, STATUS_TIMEOUT );
643 /* allocate and fill a message result structure */
644 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
645 struct msg_queue *recv_queue,
646 struct message *msg, timeout_t timeout )
648 struct message_result *result = mem_alloc( sizeof(*result) );
649 if (result)
651 result->msg = msg;
652 result->sender = send_queue;
653 result->receiver = recv_queue;
654 result->replied = 0;
655 result->data = NULL;
656 result->data_size = 0;
657 result->timeout = NULL;
658 result->hardware_msg = NULL;
659 result->desktop = NULL;
660 result->callback_msg = NULL;
662 if (msg->type == MSG_CALLBACK)
664 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
666 if (!callback_msg)
668 free( result );
669 return NULL;
671 callback_msg->type = MSG_CALLBACK_RESULT;
672 callback_msg->win = msg->win;
673 callback_msg->msg = msg->msg;
674 callback_msg->wparam = 0;
675 callback_msg->lparam = 0;
676 callback_msg->time = get_tick_count();
677 callback_msg->result = NULL;
678 /* steal the data from the original message */
679 callback_msg->data = msg->data;
680 callback_msg->data_size = msg->data_size;
681 msg->data = NULL;
682 msg->data_size = 0;
684 result->callback_msg = callback_msg;
685 list_add_head( &send_queue->callback_result, &result->sender_entry );
687 else if (send_queue)
689 list_add_head( &send_queue->send_result, &result->sender_entry );
690 clear_queue_bits( send_queue, QS_SMRESULT );
693 if (timeout != TIMEOUT_INFINITE)
694 result->timeout = add_timeout_user( timeout, result_timeout, result );
696 return result;
699 /* receive a message, removing it from the sent queue */
700 static void receive_message( struct msg_queue *queue, struct message *msg,
701 struct get_message_reply *reply )
703 struct message_result *result = msg->result;
705 reply->total = msg->data_size;
706 if (msg->data_size > get_reply_max_size())
708 set_error( STATUS_BUFFER_OVERFLOW );
709 return;
711 reply->type = msg->type;
712 reply->win = msg->win;
713 reply->msg = msg->msg;
714 reply->wparam = msg->wparam;
715 reply->lparam = msg->lparam;
716 reply->time = msg->time;
718 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
720 list_remove( &msg->entry );
721 /* put the result on the receiver result stack */
722 if (result)
724 result->msg = NULL;
725 result->recv_next = queue->recv_result;
726 queue->recv_result = result;
728 free( msg );
729 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
732 /* set the result of the current received message */
733 static void reply_message( struct msg_queue *queue, lparam_t result,
734 unsigned int error, int remove, const void *data, data_size_t len )
736 struct message_result *res = queue->recv_result;
738 if (remove)
740 queue->recv_result = res->recv_next;
741 res->receiver = NULL;
742 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
744 free_result( res );
745 return;
748 if (!res->replied)
750 if (len && (res->data = memdup( data, len ))) res->data_size = len;
751 store_message_result( res, result, error );
755 static int match_window( user_handle_t win, user_handle_t msg_win )
757 if (!win) return 1;
758 if (win == -1 || win == 1) return !msg_win;
759 if (msg_win == win) return 1;
760 return is_child_window( win, msg_win );
763 /* retrieve a posted message */
764 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
765 unsigned int first, unsigned int last, unsigned int flags,
766 struct get_message_reply *reply )
768 struct message *msg;
770 /* check against the filters */
771 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
773 if (!match_window( win, msg->win )) continue;
774 if (!check_msg_filter( msg->msg, first, last )) continue;
775 goto found; /* found one */
777 return 0;
779 /* return it to the app */
780 found:
781 reply->total = msg->data_size;
782 if (msg->data_size > get_reply_max_size())
784 set_error( STATUS_BUFFER_OVERFLOW );
785 return 1;
787 reply->type = msg->type;
788 reply->win = msg->win;
789 reply->msg = msg->msg;
790 reply->wparam = msg->wparam;
791 reply->lparam = msg->lparam;
792 reply->time = msg->time;
794 if (flags & PM_REMOVE)
796 if (msg->data)
798 set_reply_data_ptr( msg->data, msg->data_size );
799 msg->data = NULL;
800 msg->data_size = 0;
802 remove_queue_message( queue, msg, POST_MESSAGE );
804 else if (msg->data) set_reply_data( msg->data, msg->data_size );
806 return 1;
809 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
810 struct get_message_reply *reply )
812 if (queue->quit_message)
814 reply->total = 0;
815 reply->type = MSG_POSTED;
816 reply->win = 0;
817 reply->msg = WM_QUIT;
818 reply->wparam = queue->exit_code;
819 reply->lparam = 0;
820 reply->time = get_tick_count();
822 if (flags & PM_REMOVE)
824 queue->quit_message = 0;
825 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
826 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
828 return 1;
830 else
831 return 0;
834 /* empty a message list and free all the messages */
835 static void empty_msg_list( struct list *list )
837 struct list *ptr;
839 while ((ptr = list_head( list )) != NULL)
841 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
842 list_remove( &msg->entry );
843 free_message( msg );
847 /* cleanup all pending results when deleting a queue */
848 static void cleanup_results( struct msg_queue *queue )
850 struct list *entry;
852 while ((entry = list_head( &queue->send_result )) != NULL)
854 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
857 while ((entry = list_head( &queue->callback_result )) != NULL)
859 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
862 while (queue->recv_result)
863 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
866 /* check if the thread owning the queue is hung (not checking for messages) */
867 static int is_queue_hung( struct msg_queue *queue )
869 struct wait_queue_entry *entry;
871 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
872 return 0; /* less than 5 seconds since last get message -> not hung */
874 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
876 if (get_wait_queue_thread(entry)->queue == queue)
877 return 0; /* thread is waiting on queue -> not hung */
879 return 1;
882 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
884 struct msg_queue *queue = (struct msg_queue *)obj;
885 struct process *process = get_wait_queue_thread(entry)->process;
887 /* a thread can only wait on its own queue */
888 if (get_wait_queue_thread(entry)->queue != queue)
890 set_error( STATUS_ACCESS_DENIED );
891 return 0;
893 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
895 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
896 set_fd_events( queue->fd, POLLIN );
897 add_queue( obj, entry );
898 return 1;
901 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
903 struct msg_queue *queue = (struct msg_queue *)obj;
905 remove_queue( obj, entry );
906 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
907 set_fd_events( queue->fd, 0 );
910 static void msg_queue_dump( struct object *obj, int verbose )
912 struct msg_queue *queue = (struct msg_queue *)obj;
913 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
914 queue->wake_bits, queue->wake_mask );
917 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
919 struct msg_queue *queue = (struct msg_queue *)obj;
920 int ret = 0;
922 if (queue->fd)
924 if ((ret = check_fd_events( queue->fd, POLLIN )))
925 /* stop waiting on select() if we are signaled */
926 set_fd_events( queue->fd, 0 );
927 else if (!list_empty( &obj->wait_queue ))
928 /* restart waiting on poll() if we are no longer signaled */
929 set_fd_events( queue->fd, POLLIN );
931 return ret || is_signaled( queue );
934 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
936 struct msg_queue *queue = (struct msg_queue *)obj;
937 queue->wake_mask = 0;
938 queue->changed_mask = 0;
941 static void msg_queue_destroy( struct object *obj )
943 struct msg_queue *queue = (struct msg_queue *)obj;
944 struct list *ptr;
945 struct hotkey *hotkey, *hotkey2;
946 int i;
948 cleanup_results( queue );
949 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
951 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
953 if (hotkey->queue == queue)
955 list_remove( &hotkey->entry );
956 free( hotkey );
960 while ((ptr = list_head( &queue->pending_timers )))
962 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
963 list_remove( &timer->entry );
964 free( timer );
966 while ((ptr = list_head( &queue->expired_timers )))
968 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
969 list_remove( &timer->entry );
970 free( timer );
972 if (queue->timeout) remove_timeout_user( queue->timeout );
973 queue->input->cursor_count -= queue->cursor_count;
974 release_object( queue->input );
975 if (queue->hooks) release_object( queue->hooks );
976 if (queue->fd) release_object( queue->fd );
979 static void msg_queue_poll_event( struct fd *fd, int event )
981 struct msg_queue *queue = get_fd_user( fd );
982 assert( queue->obj.ops == &msg_queue_ops );
984 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
985 else set_fd_events( queue->fd, 0 );
986 wake_up( &queue->obj, 0 );
989 static void thread_input_dump( struct object *obj, int verbose )
991 struct thread_input *input = (struct thread_input *)obj;
992 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
993 input->focus, input->capture, input->active );
996 static void thread_input_destroy( struct object *obj )
998 struct thread_input *input = (struct thread_input *)obj;
1000 empty_msg_list( &input->msg_list );
1001 if (input->desktop)
1003 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
1004 release_object( input->desktop );
1008 /* fix the thread input data when a window is destroyed */
1009 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1011 struct thread_input *input = queue->input;
1013 if (window == input->focus) input->focus = 0;
1014 if (window == input->capture) input->capture = 0;
1015 if (window == input->active) input->active = 0;
1016 if (window == input->menu_owner) input->menu_owner = 0;
1017 if (window == input->move_size) input->move_size = 0;
1018 if (window == input->caret) set_caret_window( input, 0 );
1021 /* check if the specified window can be set in the input data of a given queue */
1022 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1024 struct thread *thread;
1025 int ret = 0;
1027 if (!window) return 1; /* we can always clear the data */
1029 if ((thread = get_window_thread( window )))
1031 ret = (queue->input == thread->queue->input);
1032 if (!ret) set_error( STATUS_ACCESS_DENIED );
1033 release_object( thread );
1035 else set_error( STATUS_INVALID_HANDLE );
1037 return ret;
1040 /* make sure the specified thread has a queue */
1041 int init_thread_queue( struct thread *thread )
1043 if (thread->queue) return 1;
1044 return (create_msg_queue( thread, NULL ) != NULL);
1047 /* attach two thread input data structures */
1048 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1050 struct desktop *desktop;
1051 struct thread_input *input;
1052 int ret;
1054 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1055 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1056 input = (struct thread_input *)grab_object( thread_to->queue->input );
1057 if (input->desktop != desktop)
1059 set_error( STATUS_ACCESS_DENIED );
1060 release_object( input );
1061 release_object( desktop );
1062 return 0;
1064 release_object( desktop );
1066 ret = assign_thread_input( thread_from, input );
1067 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1068 release_object( input );
1069 return ret;
1072 /* detach two thread input data structures */
1073 void detach_thread_input( struct thread *thread_from )
1075 struct thread_input *input;
1077 if ((input = create_thread_input( thread_from )))
1079 assign_thread_input( thread_from, input );
1080 release_object( input );
1085 /* set the next timer to expire */
1086 static void set_next_timer( struct msg_queue *queue )
1088 struct list *ptr;
1090 if (queue->timeout)
1092 remove_timeout_user( queue->timeout );
1093 queue->timeout = NULL;
1095 if ((ptr = list_head( &queue->pending_timers )))
1097 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1098 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1100 /* set/clear QS_TIMER bit */
1101 if (list_empty( &queue->expired_timers ))
1102 clear_queue_bits( queue, QS_TIMER );
1103 else
1104 set_queue_bits( queue, QS_TIMER );
1107 /* find a timer from its window and id */
1108 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1109 unsigned int msg, lparam_t id )
1111 struct list *ptr;
1113 /* we need to search both lists */
1115 LIST_FOR_EACH( ptr, &queue->pending_timers )
1117 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1118 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1120 LIST_FOR_EACH( ptr, &queue->expired_timers )
1122 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1123 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1125 return NULL;
1128 /* callback for the next timer expiration */
1129 static void timer_callback( void *private )
1131 struct msg_queue *queue = private;
1132 struct list *ptr;
1134 queue->timeout = NULL;
1135 /* move on to the next timer */
1136 ptr = list_head( &queue->pending_timers );
1137 list_remove( ptr );
1138 list_add_tail( &queue->expired_timers, ptr );
1139 set_next_timer( queue );
1142 /* link a timer at its rightful place in the queue list */
1143 static void link_timer( struct msg_queue *queue, struct timer *timer )
1145 struct list *ptr;
1147 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1149 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1150 if (t->when >= timer->when) break;
1152 list_add_before( ptr, &timer->entry );
1155 /* remove a timer from the queue timer list and free it */
1156 static void free_timer( struct msg_queue *queue, struct timer *timer )
1158 list_remove( &timer->entry );
1159 free( timer );
1160 set_next_timer( queue );
1163 /* restart an expired timer */
1164 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1166 list_remove( &timer->entry );
1167 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1168 link_timer( queue, timer );
1169 set_next_timer( queue );
1172 /* find an expired timer matching the filtering parameters */
1173 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1174 unsigned int get_first, unsigned int get_last,
1175 int remove )
1177 struct list *ptr;
1179 LIST_FOR_EACH( ptr, &queue->expired_timers )
1181 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1182 if (win && timer->win != win) continue;
1183 if (check_msg_filter( timer->msg, get_first, get_last ))
1185 if (remove) restart_timer( queue, timer );
1186 return timer;
1189 return NULL;
1192 /* add a timer */
1193 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1195 struct timer *timer = mem_alloc( sizeof(*timer) );
1196 if (timer)
1198 timer->rate = max( rate, 1 );
1199 timer->when = current_time + (timeout_t)timer->rate * 10000;
1200 link_timer( queue, timer );
1201 /* check if we replaced the next timer */
1202 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1204 return timer;
1207 /* change the input key state for a given key */
1208 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1210 if (down)
1212 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1213 keystate[key] |= down;
1215 else keystate[key] &= ~0x80;
1218 /* update the input key state for a keyboard message */
1219 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1220 const struct message *msg )
1222 unsigned char key;
1223 int down = 0;
1225 switch (msg->msg)
1227 case WM_LBUTTONDOWN:
1228 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1229 /* fall through */
1230 case WM_LBUTTONUP:
1231 set_input_key_state( keystate, VK_LBUTTON, down );
1232 break;
1233 case WM_MBUTTONDOWN:
1234 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1235 /* fall through */
1236 case WM_MBUTTONUP:
1237 set_input_key_state( keystate, VK_MBUTTON, down );
1238 break;
1239 case WM_RBUTTONDOWN:
1240 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1241 /* fall through */
1242 case WM_RBUTTONUP:
1243 set_input_key_state( keystate, VK_RBUTTON, down );
1244 break;
1245 case WM_XBUTTONDOWN:
1246 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1247 /* fall through */
1248 case WM_XBUTTONUP:
1249 if (msg->wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1250 else if (msg->wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1251 break;
1252 case WM_KEYDOWN:
1253 case WM_SYSKEYDOWN:
1254 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1255 /* fall through */
1256 case WM_KEYUP:
1257 case WM_SYSKEYUP:
1258 key = (unsigned char)msg->wparam;
1259 set_input_key_state( keystate, key, down );
1260 switch(key)
1262 case VK_LCONTROL:
1263 case VK_RCONTROL:
1264 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1265 set_input_key_state( keystate, VK_CONTROL, down );
1266 break;
1267 case VK_LMENU:
1268 case VK_RMENU:
1269 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1270 set_input_key_state( keystate, VK_MENU, down );
1271 break;
1272 case VK_LSHIFT:
1273 case VK_RSHIFT:
1274 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1275 set_input_key_state( keystate, VK_SHIFT, down );
1276 break;
1278 break;
1282 /* release the hardware message currently being processed by the given thread */
1283 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1284 int remove )
1286 struct thread_input *input = queue->input;
1287 struct message *msg;
1289 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1291 if (msg->unique_id == hw_id) break;
1293 if (&msg->entry == &input->msg_list) return; /* not found */
1295 /* clear the queue bit for that message */
1296 if (remove)
1298 struct message *other;
1299 int clr_bit;
1301 clr_bit = get_hardware_msg_bit( msg );
1302 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1304 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1306 clr_bit = 0;
1307 break;
1310 if (clr_bit) clear_queue_bits( queue, clr_bit );
1312 update_input_key_state( input->desktop, input->keystate, msg );
1313 list_remove( &msg->entry );
1314 free_message( msg );
1318 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1320 struct hotkey *hotkey;
1321 unsigned int modifiers = 0;
1323 if (msg->msg != WM_KEYDOWN) return 0;
1325 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1326 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1327 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1328 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1330 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1332 if (hotkey->vkey != msg->wparam) continue;
1333 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1336 return 0;
1338 found:
1339 msg->type = MSG_POSTED;
1340 msg->win = hotkey->win;
1341 msg->msg = WM_HOTKEY;
1342 msg->wparam = hotkey->id;
1343 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1345 free( msg->data );
1346 msg->data = NULL;
1347 msg->data_size = 0;
1349 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1350 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1351 hotkey->queue->hotkey_count++;
1352 return 1;
1355 /* find the window that should receive a given hardware message */
1356 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1357 struct message *msg, unsigned int *msg_code,
1358 struct thread **thread )
1360 struct hardware_msg_data *data = msg->data;
1361 user_handle_t win = 0;
1363 *thread = NULL;
1364 *msg_code = msg->msg;
1365 if (msg->msg == WM_INPUT)
1367 if (!(win = msg->win) && input) win = input->focus;
1369 else if (is_keyboard_msg( msg ))
1371 if (input && !(win = input->focus))
1373 win = input->active;
1374 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1377 else if (!input || !(win = input->capture)) /* mouse message */
1379 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1380 else win = shallow_window_from_point( desktop, data->x, data->y );
1382 *thread = window_thread_from_point( win, data->x, data->y );
1385 if (!*thread)
1386 *thread = get_window_thread( win );
1387 return win;
1390 static struct rawinput_device_entry *find_rawinput_device( unsigned short usage_page, unsigned short usage )
1392 struct rawinput_device_entry *e;
1394 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
1396 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1397 return e;
1400 return NULL;
1403 static void update_rawinput_device(const struct rawinput_device *device)
1405 struct rawinput_device_entry *e;
1407 if (!(e = find_rawinput_device( device->usage_page, device->usage )))
1409 if (!(e = mem_alloc( sizeof(*e) ))) return;
1410 list_add_tail( &current->process->rawinput_devices, &e->entry );
1413 if (device->flags & RIDEV_REMOVE)
1415 list_remove( &e->entry );
1416 free( e );
1417 return;
1420 e->device = *device;
1421 e->device.target = get_user_full_handle( e->device.target );
1424 /* queue a hardware message into a given thread input */
1425 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1427 user_handle_t win;
1428 struct thread *thread;
1429 struct thread_input *input;
1430 unsigned int msg_code;
1431 struct hardware_msg_data *data = msg->data;
1433 update_input_key_state( desktop, desktop->keystate, msg );
1434 last_input_time = get_tick_count();
1435 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1437 if (is_keyboard_msg( msg ))
1439 if (queue_hotkey_message( desktop, msg )) return;
1440 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1441 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1442 msg->lparam &= ~(KF_EXTENDED << 16);
1444 else if (msg->msg != WM_INPUT)
1446 if (msg->msg == WM_MOUSEMOVE)
1448 int x = min( max( data->x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
1449 int y = min( max( data->y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
1450 if (desktop->cursor.x != x || desktop->cursor.y != y) always_queue = 1;
1451 desktop->cursor.x = x;
1452 desktop->cursor.y = y;
1453 desktop->cursor.last_change = get_tick_count();
1455 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1456 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1457 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1458 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1459 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1460 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1461 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1463 data->x = desktop->cursor.x;
1464 data->y = desktop->cursor.y;
1466 if (msg->win && (thread = get_window_thread( msg->win )))
1468 input = thread->queue->input;
1469 release_object( thread );
1471 else input = desktop->foreground_input;
1473 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1474 if (!win || !thread)
1476 if (input) update_input_key_state( input->desktop, input->keystate, msg );
1477 free_message( msg );
1478 return;
1480 input = thread->queue->input;
1482 if (win != desktop->cursor.win) always_queue = 1;
1483 desktop->cursor.win = win;
1485 if (!always_queue || merge_message( input, msg )) free_message( msg );
1486 else
1488 msg->unique_id = 0; /* will be set once we return it to the app */
1489 list_add_tail( &input->msg_list, &msg->entry );
1490 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1492 release_object( thread );
1495 /* send the low-level hook message for a given hardware message */
1496 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1497 const hw_input_t *input, struct msg_queue *sender )
1499 struct thread *hook_thread;
1500 struct msg_queue *queue;
1501 struct message *msg;
1502 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1503 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1505 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1506 if (!(queue = hook_thread->queue)) return 0;
1507 if (is_queue_hung( queue )) return 0;
1509 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1511 msg->type = MSG_HOOK_LL;
1512 msg->win = 0;
1513 msg->msg = id;
1514 msg->wparam = hardware_msg->msg;
1515 msg->time = hardware_msg->time;
1516 msg->data_size = hardware_msg->data_size;
1517 msg->result = NULL;
1519 if (input->type == INPUT_KEYBOARD)
1521 unsigned short vkey = input->kbd.vkey;
1522 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1523 msg->lparam = (input->kbd.scan << 16) | vkey;
1525 else msg->lparam = input->mouse.data << 16;
1527 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1528 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1530 free_message( msg );
1531 return 0;
1533 msg->result->hardware_msg = hardware_msg;
1534 msg->result->desktop = (struct desktop *)grab_object( desktop );
1535 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1536 set_queue_bits( queue, QS_SENDMESSAGE );
1537 return 1;
1540 /* queue a hardware message for a mouse event */
1541 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1542 unsigned int hook_flags, struct msg_queue *sender )
1544 const struct rawinput_device *device;
1545 struct hardware_msg_data *msg_data;
1546 struct message *msg;
1547 unsigned int i, time, flags;
1548 int wait = 0, x, y;
1550 static const unsigned int messages[] =
1552 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1553 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1554 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1555 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1556 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1557 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1558 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1559 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1560 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1561 0, /* 0x0200 = unused */
1562 0, /* 0x0400 = unused */
1563 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1564 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1567 desktop->cursor.last_change = get_tick_count();
1568 flags = input->mouse.flags;
1569 time = input->mouse.time;
1570 if (!time) time = desktop->cursor.last_change;
1572 if (flags & MOUSEEVENTF_MOVE)
1574 if (flags & MOUSEEVENTF_ABSOLUTE)
1576 x = input->mouse.x;
1577 y = input->mouse.y;
1578 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1579 x == desktop->cursor.x && y == desktop->cursor.y)
1580 flags &= ~MOUSEEVENTF_MOVE;
1582 else
1584 x = desktop->cursor.x + input->mouse.x;
1585 y = desktop->cursor.y + input->mouse.y;
1588 else
1590 x = desktop->cursor.x;
1591 y = desktop->cursor.y;
1594 if ((device = current->process->rawinput_mouse))
1596 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1597 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1599 free( msg );
1600 return 0;
1603 msg->type = MSG_HARDWARE;
1604 msg->win = device->target;
1605 msg->msg = WM_INPUT;
1606 msg->wparam = RIM_INPUT;
1607 msg->lparam = 0;
1608 msg->time = time;
1609 msg->data = msg_data;
1610 msg->data_size = sizeof(*msg_data);
1611 msg->result = NULL;
1613 msg_data->info = input->mouse.info;
1614 msg_data->flags = flags;
1615 msg_data->rawinput.type = RIM_TYPEMOUSE;
1616 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1617 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1618 msg_data->rawinput.mouse.data = input->mouse.data;
1620 queue_hardware_message( desktop, msg, 0 );
1623 for (i = 0; i < sizeof(messages)/sizeof(messages[0]); i++)
1625 if (!messages[i]) continue;
1626 if (!(flags & (1 << i))) continue;
1627 flags &= ~(1 << i);
1629 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1630 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1632 free( msg );
1633 return 0;
1635 memset( msg_data, 0, sizeof(*msg_data) );
1637 msg->type = MSG_HARDWARE;
1638 msg->win = get_user_full_handle( win );
1639 msg->msg = messages[i];
1640 msg->wparam = input->mouse.data << 16;
1641 msg->lparam = 0;
1642 msg->time = time;
1643 msg->result = NULL;
1644 msg->data = msg_data;
1645 msg->data_size = sizeof(*msg_data);
1646 msg_data->x = x;
1647 msg_data->y = y;
1648 msg_data->info = input->mouse.info;
1649 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLMHF_INJECTED;
1651 /* specify a sender only when sending the last message */
1652 if (!(flags & ((1 << sizeof(messages)/sizeof(messages[0])) - 1)))
1654 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1655 queue_hardware_message( desktop, msg, 0 );
1657 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1658 queue_hardware_message( desktop, msg, 0 );
1660 return wait;
1663 /* queue a hardware message for a keyboard event */
1664 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1665 unsigned int hook_flags, struct msg_queue *sender )
1667 const struct rawinput_device *device;
1668 struct hardware_msg_data *msg_data;
1669 struct message *msg;
1670 unsigned char vkey = input->kbd.vkey;
1671 unsigned int message_code, time;
1672 int wait;
1674 if (!(time = input->kbd.time)) time = get_tick_count();
1676 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1678 switch (vkey)
1680 case VK_MENU:
1681 case VK_LMENU:
1682 case VK_RMENU:
1683 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1684 break;
1685 case VK_CONTROL:
1686 case VK_LCONTROL:
1687 case VK_RCONTROL:
1688 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1689 break;
1690 case VK_SHIFT:
1691 case VK_LSHIFT:
1692 case VK_RSHIFT:
1693 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1694 break;
1698 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1699 switch (vkey)
1701 case VK_LMENU:
1702 case VK_RMENU:
1703 if (input->kbd.flags & KEYEVENTF_KEYUP)
1705 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1706 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1707 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1708 message_code = WM_SYSKEYUP;
1709 desktop->keystate[VK_MENU] &= ~0x02;
1711 else
1713 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1714 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1715 message_code = WM_SYSKEYDOWN;
1716 desktop->keystate[VK_MENU] |= 0x02;
1718 break;
1720 case VK_LCONTROL:
1721 case VK_RCONTROL:
1722 /* send WM_SYSKEYUP on release if Alt still pressed */
1723 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1724 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1725 message_code = WM_SYSKEYUP;
1726 desktop->keystate[VK_MENU] &= ~0x02;
1727 break;
1729 default:
1730 /* send WM_SYSKEY for Alt-anykey and for F10 */
1731 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1732 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1733 /* fall through */
1734 case VK_F10:
1735 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1736 desktop->keystate[VK_MENU] &= ~0x02;
1737 break;
1740 if ((device = current->process->rawinput_kbd))
1742 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1743 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1745 free( msg );
1746 return 0;
1749 msg->type = MSG_HARDWARE;
1750 msg->win = device->target;
1751 msg->msg = WM_INPUT;
1752 msg->wparam = RIM_INPUT;
1753 msg->lparam = 0;
1754 msg->time = time;
1755 msg->data = msg_data;
1756 msg->data_size = sizeof(*msg_data);
1757 msg->result = NULL;
1759 msg_data->info = input->kbd.info;
1760 msg_data->flags = input->kbd.flags;
1761 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1762 msg_data->rawinput.kbd.message = message_code;
1763 msg_data->rawinput.kbd.vkey = vkey;
1764 msg_data->rawinput.kbd.scan = input->kbd.scan;
1766 queue_hardware_message( desktop, msg, 0 );
1769 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1770 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1772 free( msg );
1773 return 0;
1775 memset( msg_data, 0, sizeof(*msg_data) );
1777 msg->type = MSG_HARDWARE;
1778 msg->win = get_user_full_handle( win );
1779 msg->msg = message_code;
1780 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1781 msg->time = time;
1782 msg->result = NULL;
1783 msg->data = msg_data;
1784 msg->data_size = sizeof(*msg_data);
1785 msg_data->info = input->kbd.info;
1786 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLKHF_INJECTED;
1788 if (input->kbd.flags & KEYEVENTF_UNICODE)
1790 msg->wparam = VK_PACKET;
1792 else
1794 unsigned int flags = 0;
1795 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1796 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1797 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1798 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1800 msg->wparam = vkey;
1801 msg->lparam |= flags << 16;
1802 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1805 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1806 queue_hardware_message( desktop, msg, 1 );
1808 return wait;
1811 /* queue a hardware message for a custom type of event */
1812 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1813 const hw_input_t *input )
1815 struct hardware_msg_data *msg_data;
1816 struct message *msg;
1818 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
1819 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1821 free( msg );
1822 return;
1824 memset( msg_data, 0, sizeof(*msg_data) );
1826 msg->type = MSG_HARDWARE;
1827 msg->win = get_user_full_handle( win );
1828 msg->msg = input->hw.msg;
1829 msg->wparam = 0;
1830 msg->lparam = input->hw.lparam;
1831 msg->time = get_tick_count();
1832 msg->result = NULL;
1833 msg->data = msg_data;
1834 msg->data_size = sizeof(*msg_data);
1836 queue_hardware_message( desktop, msg, 1 );
1839 /* check message filter for a hardware message */
1840 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1841 user_handle_t filter_win, unsigned int first, unsigned int last )
1843 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1845 /* we can only test the window for a keyboard message since the
1846 * dest window for a mouse message depends on hittest */
1847 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1848 return 0;
1849 /* the message code is final for a keyboard message, we can simply check it */
1850 return check_msg_filter( msg_code, first, last );
1852 else /* mouse message */
1854 /* we need to check all possible values that the message can have in the end */
1856 if (check_msg_filter( msg_code, first, last )) return 1;
1857 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1859 /* all other messages can become non-client messages */
1860 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1862 /* clicks can become double-clicks or non-client double-clicks */
1863 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1864 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1866 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1867 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1869 return 0;
1874 /* find a hardware message for the given queue */
1875 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1876 unsigned int first, unsigned int last, unsigned int flags,
1877 struct get_message_reply *reply )
1879 struct thread_input *input = thread->queue->input;
1880 struct thread *win_thread;
1881 struct list *ptr;
1882 user_handle_t win;
1883 int clear_bits, got_one = 0;
1884 unsigned int msg_code;
1886 ptr = list_head( &input->msg_list );
1887 if (hw_id)
1889 while (ptr)
1891 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1892 if (msg->unique_id == hw_id) break;
1893 ptr = list_next( &input->msg_list, ptr );
1895 if (!ptr) ptr = list_head( &input->msg_list );
1896 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1899 if (ptr == list_head( &input->msg_list ))
1900 clear_bits = QS_INPUT;
1901 else
1902 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1904 while (ptr)
1906 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1907 struct hardware_msg_data *data = msg->data;
1909 ptr = list_next( &input->msg_list, ptr );
1910 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
1911 if (!win || !win_thread)
1913 /* no window at all, remove it */
1914 update_input_key_state( input->desktop, input->keystate, msg );
1915 list_remove( &msg->entry );
1916 free_message( msg );
1917 continue;
1919 if (win_thread != thread)
1921 if (win_thread->queue->input == input)
1923 /* wake the other thread */
1924 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1925 got_one = 1;
1927 else
1929 /* for another thread input, drop it */
1930 update_input_key_state( input->desktop, input->keystate, msg );
1931 list_remove( &msg->entry );
1932 free_message( msg );
1934 release_object( win_thread );
1935 continue;
1937 release_object( win_thread );
1939 /* if we already got a message for another thread, or if it doesn't
1940 * match the filter we skip it */
1941 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1943 clear_bits &= ~get_hardware_msg_bit( msg );
1944 continue;
1946 /* now we can return it */
1947 if (!msg->unique_id) msg->unique_id = get_unique_id();
1948 reply->type = MSG_HARDWARE;
1949 reply->win = win;
1950 reply->msg = msg_code;
1951 reply->wparam = msg->wparam;
1952 reply->lparam = msg->lparam;
1953 reply->time = msg->time;
1955 data->hw_id = msg->unique_id;
1956 set_reply_data( msg->data, msg->data_size );
1957 if (msg->msg == WM_INPUT && (flags & PM_REMOVE))
1958 release_hardware_message( current->queue, data->hw_id, 1 );
1959 return 1;
1961 /* nothing found, clear the hardware queue bits */
1962 clear_queue_bits( thread->queue, clear_bits );
1963 return 0;
1966 /* increment (or decrement if 'incr' is negative) the queue paint count */
1967 void inc_queue_paint_count( struct thread *thread, int incr )
1969 struct msg_queue *queue = thread->queue;
1971 assert( queue );
1973 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1975 if (queue->paint_count)
1976 set_queue_bits( queue, QS_PAINT );
1977 else
1978 clear_queue_bits( queue, QS_PAINT );
1982 /* remove all messages and timers belonging to a certain window */
1983 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1985 struct msg_queue *queue = thread->queue;
1986 struct list *ptr;
1987 int i;
1989 if (!queue) return;
1991 /* remove timers */
1993 ptr = list_head( &queue->pending_timers );
1994 while (ptr)
1996 struct list *next = list_next( &queue->pending_timers, ptr );
1997 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1998 if (timer->win == win) free_timer( queue, timer );
1999 ptr = next;
2001 ptr = list_head( &queue->expired_timers );
2002 while (ptr)
2004 struct list *next = list_next( &queue->expired_timers, ptr );
2005 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2006 if (timer->win == win) free_timer( queue, timer );
2007 ptr = next;
2010 /* remove messages */
2011 for (i = 0; i < NB_MSG_KINDS; i++)
2013 struct list *ptr, *next;
2015 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2017 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2018 if (msg->win == win)
2020 if (msg->msg == WM_QUIT && !queue->quit_message)
2022 queue->quit_message = 1;
2023 queue->exit_code = msg->wparam;
2025 remove_queue_message( queue, msg, i );
2030 thread_input_cleanup_window( queue, win );
2033 /* post a message to a window; used by socket handling */
2034 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2036 struct message *msg;
2037 struct thread *thread = get_window_thread( win );
2039 if (!thread) return;
2041 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2043 msg->type = MSG_POSTED;
2044 msg->win = get_user_full_handle( win );
2045 msg->msg = message;
2046 msg->wparam = wparam;
2047 msg->lparam = lparam;
2048 msg->time = get_tick_count();
2049 msg->result = NULL;
2050 msg->data = NULL;
2051 msg->data_size = 0;
2053 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2054 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2055 if (message == WM_HOTKEY)
2057 set_queue_bits( thread->queue, QS_HOTKEY );
2058 thread->queue->hotkey_count++;
2061 release_object( thread );
2064 /* post a win event */
2065 void post_win_event( struct thread *thread, unsigned int event,
2066 user_handle_t win, unsigned int object_id,
2067 unsigned int child_id, client_ptr_t hook_proc,
2068 const WCHAR *module, data_size_t module_size,
2069 user_handle_t hook)
2071 struct message *msg;
2073 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2075 struct winevent_msg_data *data;
2077 msg->type = MSG_WINEVENT;
2078 msg->win = get_user_full_handle( win );
2079 msg->msg = event;
2080 msg->wparam = object_id;
2081 msg->lparam = child_id;
2082 msg->time = get_tick_count();
2083 msg->result = NULL;
2085 if ((data = malloc( sizeof(*data) + module_size )))
2087 data->hook = hook;
2088 data->tid = get_thread_id( current );
2089 data->hook_proc = hook_proc;
2090 memcpy( data + 1, module, module_size );
2092 msg->data = data;
2093 msg->data_size = sizeof(*data) + module_size;
2095 if (debug_level > 1)
2096 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2097 get_thread_id(thread), event, win, object_id, child_id );
2098 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2099 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2101 else
2102 free( msg );
2106 /* free all hotkeys on a desktop, optionally filtering by window */
2107 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2109 struct hotkey *hotkey, *hotkey2;
2111 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2113 if (!window || hotkey->win == window)
2115 list_remove( &hotkey->entry );
2116 free( hotkey );
2122 /* check if the thread owning the window is hung */
2123 DECL_HANDLER(is_window_hung)
2125 struct thread *thread;
2127 thread = get_window_thread( req->win );
2128 if (thread)
2130 reply->is_hung = is_queue_hung( thread->queue );
2131 release_object( thread );
2133 else reply->is_hung = 0;
2137 /* get the message queue of the current thread */
2138 DECL_HANDLER(get_msg_queue)
2140 struct msg_queue *queue = get_current_queue();
2142 reply->handle = 0;
2143 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2147 /* set the file descriptor associated to the current thread queue */
2148 DECL_HANDLER(set_queue_fd)
2150 struct msg_queue *queue = get_current_queue();
2151 struct file *file;
2152 int unix_fd;
2154 if (queue->fd) /* fd can only be set once */
2156 set_error( STATUS_ACCESS_DENIED );
2157 return;
2159 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2161 if ((unix_fd = get_file_unix_fd( file )) != -1)
2163 if ((unix_fd = dup( unix_fd )) != -1)
2164 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2165 else
2166 file_set_error();
2168 release_object( file );
2172 /* set the current message queue wakeup mask */
2173 DECL_HANDLER(set_queue_mask)
2175 struct msg_queue *queue = get_current_queue();
2177 if (queue)
2179 queue->wake_mask = req->wake_mask;
2180 queue->changed_mask = req->changed_mask;
2181 reply->wake_bits = queue->wake_bits;
2182 reply->changed_bits = queue->changed_bits;
2183 if (is_signaled( queue ))
2185 /* if skip wait is set, do what would have been done in the subsequent wait */
2186 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2187 else wake_up( &queue->obj, 0 );
2193 /* get the current message queue status */
2194 DECL_HANDLER(get_queue_status)
2196 struct msg_queue *queue = current->queue;
2197 if (queue)
2199 reply->wake_bits = queue->wake_bits;
2200 reply->changed_bits = queue->changed_bits;
2201 if (req->clear) queue->changed_bits = 0;
2203 else reply->wake_bits = reply->changed_bits = 0;
2207 /* send a message to a thread queue */
2208 DECL_HANDLER(send_message)
2210 struct message *msg;
2211 struct msg_queue *send_queue = get_current_queue();
2212 struct msg_queue *recv_queue = NULL;
2213 struct thread *thread = NULL;
2215 if (!(thread = get_thread_from_id( req->id ))) return;
2217 if (!(recv_queue = thread->queue))
2219 set_error( STATUS_INVALID_PARAMETER );
2220 release_object( thread );
2221 return;
2223 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2225 set_error( STATUS_TIMEOUT );
2226 release_object( thread );
2227 return;
2230 if ((msg = mem_alloc( sizeof(*msg) )))
2232 msg->type = req->type;
2233 msg->win = get_user_full_handle( req->win );
2234 msg->msg = req->msg;
2235 msg->wparam = req->wparam;
2236 msg->lparam = req->lparam;
2237 msg->time = get_tick_count();
2238 msg->result = NULL;
2239 msg->data = NULL;
2240 msg->data_size = get_req_data_size();
2242 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2244 free( msg );
2245 release_object( thread );
2246 return;
2249 switch(msg->type)
2251 case MSG_OTHER_PROCESS:
2252 case MSG_ASCII:
2253 case MSG_UNICODE:
2254 case MSG_CALLBACK:
2255 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2257 free_message( msg );
2258 break;
2260 /* fall through */
2261 case MSG_NOTIFY:
2262 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2263 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2264 break;
2265 case MSG_POSTED:
2266 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2267 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2268 if (msg->msg == WM_HOTKEY)
2270 set_queue_bits( recv_queue, QS_HOTKEY );
2271 recv_queue->hotkey_count++;
2273 break;
2274 case MSG_HARDWARE: /* should use send_hardware_message instead */
2275 case MSG_CALLBACK_RESULT: /* cannot send this one */
2276 case MSG_HOOK_LL: /* generated internally */
2277 default:
2278 set_error( STATUS_INVALID_PARAMETER );
2279 free( msg );
2280 break;
2283 release_object( thread );
2286 /* send a hardware message to a thread queue */
2287 DECL_HANDLER(send_hardware_message)
2289 struct thread *thread = NULL;
2290 struct desktop *desktop;
2291 struct msg_queue *sender = get_current_queue();
2292 data_size_t size = min( 256, get_reply_max_size() );
2294 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2296 if (req->win)
2298 if (!(thread = get_window_thread( req->win ))) return;
2299 if (desktop != thread->queue->input->desktop)
2301 /* don't allow queuing events to a different desktop */
2302 release_object( desktop );
2303 return;
2307 reply->prev_x = desktop->cursor.x;
2308 reply->prev_y = desktop->cursor.y;
2310 switch (req->input.type)
2312 case INPUT_MOUSE:
2313 reply->wait = queue_mouse_message( desktop, req->win, &req->input, req->flags, sender );
2314 break;
2315 case INPUT_KEYBOARD:
2316 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, req->flags, sender );
2317 break;
2318 case INPUT_HARDWARE:
2319 queue_custom_hardware_message( desktop, req->win, &req->input );
2320 break;
2321 default:
2322 set_error( STATUS_INVALID_PARAMETER );
2324 if (thread) release_object( thread );
2326 reply->new_x = desktop->cursor.x;
2327 reply->new_y = desktop->cursor.y;
2328 set_reply_data( desktop->keystate, size );
2329 release_object( desktop );
2332 /* post a quit message to the current queue */
2333 DECL_HANDLER(post_quit_message)
2335 struct msg_queue *queue = get_current_queue();
2337 if (!queue)
2338 return;
2340 queue->quit_message = 1;
2341 queue->exit_code = req->exit_code;
2342 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2345 /* get a message from the current queue */
2346 DECL_HANDLER(get_message)
2348 struct timer *timer;
2349 struct list *ptr;
2350 struct msg_queue *queue = get_current_queue();
2351 user_handle_t get_win = get_user_full_handle( req->get_win );
2352 unsigned int filter = req->flags >> 16;
2354 reply->active_hooks = get_active_hooks();
2356 if (!queue) return;
2357 queue->last_get_msg = current_time;
2358 if (!filter) filter = QS_ALLINPUT;
2360 /* first check for sent messages */
2361 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2363 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2364 receive_message( queue, msg, reply );
2365 return;
2368 /* clear changed bits so we can wait on them if we don't find a message */
2369 if (filter & QS_POSTMESSAGE)
2371 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2372 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2374 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2375 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2377 /* then check for posted messages */
2378 if ((filter & QS_POSTMESSAGE) &&
2379 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2380 return;
2382 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2383 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2384 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2385 return;
2387 /* only check for quit messages if not posted messages pending */
2388 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2389 return;
2391 /* then check for any raw hardware message */
2392 if ((filter & QS_INPUT) &&
2393 filter_contains_hw_range( req->get_first, req->get_last ) &&
2394 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2395 return;
2397 /* now check for WM_PAINT */
2398 if ((filter & QS_PAINT) &&
2399 queue->paint_count &&
2400 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2401 (reply->win = find_window_to_repaint( get_win, current )))
2403 reply->type = MSG_POSTED;
2404 reply->msg = WM_PAINT;
2405 reply->wparam = 0;
2406 reply->lparam = 0;
2407 reply->time = get_tick_count();
2408 return;
2411 /* now check for timer */
2412 if ((filter & QS_TIMER) &&
2413 (timer = find_expired_timer( queue, get_win, req->get_first,
2414 req->get_last, (req->flags & PM_REMOVE) )))
2416 reply->type = MSG_POSTED;
2417 reply->win = timer->win;
2418 reply->msg = timer->msg;
2419 reply->wparam = timer->id;
2420 reply->lparam = timer->lparam;
2421 reply->time = get_tick_count();
2422 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2423 set_event( current->process->idle_event );
2424 return;
2427 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2428 queue->wake_mask = req->wake_mask;
2429 queue->changed_mask = req->changed_mask;
2430 set_error( STATUS_PENDING ); /* FIXME */
2434 /* reply to a sent message */
2435 DECL_HANDLER(reply_message)
2437 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2438 else if (current->queue->recv_result)
2439 reply_message( current->queue, req->result, 0, req->remove,
2440 get_req_data(), get_req_data_size() );
2444 /* accept the current hardware message */
2445 DECL_HANDLER(accept_hardware_message)
2447 if (current->queue)
2448 release_hardware_message( current->queue, req->hw_id, req->remove );
2449 else
2450 set_error( STATUS_ACCESS_DENIED );
2454 /* retrieve the reply for the last message sent */
2455 DECL_HANDLER(get_message_reply)
2457 struct message_result *result;
2458 struct list *entry;
2459 struct msg_queue *queue = current->queue;
2461 if (queue)
2463 set_error( STATUS_PENDING );
2464 reply->result = 0;
2466 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2468 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2469 if (result->replied || req->cancel)
2471 if (result->replied)
2473 reply->result = result->result;
2474 set_error( result->error );
2475 if (result->data)
2477 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2478 set_reply_data_ptr( result->data, data_len );
2479 result->data = NULL;
2480 result->data_size = 0;
2483 remove_result_from_sender( result );
2485 entry = list_head( &queue->send_result );
2486 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2487 else
2489 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2490 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2491 else clear_queue_bits( queue, QS_SMRESULT );
2495 else set_error( STATUS_ACCESS_DENIED );
2499 /* set a window timer */
2500 DECL_HANDLER(set_win_timer)
2502 struct timer *timer;
2503 struct msg_queue *queue;
2504 struct thread *thread = NULL;
2505 user_handle_t win = 0;
2506 lparam_t id = req->id;
2508 if (req->win)
2510 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2512 set_error( STATUS_INVALID_HANDLE );
2513 return;
2515 if (thread->process != current->process)
2517 release_object( thread );
2518 set_error( STATUS_ACCESS_DENIED );
2519 return;
2521 queue = thread->queue;
2522 /* remove it if it existed already */
2523 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2525 else
2527 queue = get_current_queue();
2528 /* look for a timer with this id */
2529 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2531 /* free and reuse id */
2532 free_timer( queue, timer );
2534 else
2536 /* find a free id for it */
2539 id = queue->next_timer_id;
2540 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2542 while (find_timer( queue, 0, req->msg, id ));
2546 if ((timer = set_timer( queue, req->rate )))
2548 timer->win = win;
2549 timer->msg = req->msg;
2550 timer->id = id;
2551 timer->lparam = req->lparam;
2552 reply->id = id;
2554 if (thread) release_object( thread );
2557 /* kill a window timer */
2558 DECL_HANDLER(kill_win_timer)
2560 struct timer *timer;
2561 struct thread *thread;
2562 user_handle_t win = 0;
2564 if (req->win)
2566 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2568 set_error( STATUS_INVALID_HANDLE );
2569 return;
2571 if (thread->process != current->process)
2573 release_object( thread );
2574 set_error( STATUS_ACCESS_DENIED );
2575 return;
2578 else thread = (struct thread *)grab_object( current );
2580 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2581 free_timer( thread->queue, timer );
2582 else
2583 set_error( STATUS_INVALID_PARAMETER );
2585 release_object( thread );
2588 DECL_HANDLER(register_hotkey)
2590 struct desktop *desktop;
2591 user_handle_t win_handle = req->window;
2592 struct hotkey *hotkey;
2593 struct hotkey *new_hotkey = NULL;
2594 struct thread *thread;
2595 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2597 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2599 if (win_handle)
2601 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2603 release_object( desktop );
2604 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2605 return;
2608 thread = get_window_thread( win_handle );
2609 if (thread) release_object( thread );
2611 if (thread != current)
2613 release_object( desktop );
2614 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2615 return;
2619 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2621 if (req->vkey == hotkey->vkey &&
2622 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2624 release_object( desktop );
2625 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2626 return;
2628 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2629 new_hotkey = hotkey;
2632 if (new_hotkey)
2634 reply->replaced = 1;
2635 reply->flags = new_hotkey->flags;
2636 reply->vkey = new_hotkey->vkey;
2638 else
2640 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2641 if (new_hotkey)
2643 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2644 new_hotkey->queue = current->queue;
2645 new_hotkey->win = win_handle;
2646 new_hotkey->id = req->id;
2650 if (new_hotkey)
2652 new_hotkey->flags = req->flags;
2653 new_hotkey->vkey = req->vkey;
2656 release_object( desktop );
2659 DECL_HANDLER(unregister_hotkey)
2661 struct desktop *desktop;
2662 user_handle_t win_handle = req->window;
2663 struct hotkey *hotkey;
2664 struct thread *thread;
2666 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2668 if (win_handle)
2670 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2672 release_object( desktop );
2673 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2674 return;
2677 thread = get_window_thread( win_handle );
2678 if (thread) release_object( thread );
2680 if (thread != current)
2682 release_object( desktop );
2683 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2684 return;
2688 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2690 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2691 goto found;
2694 release_object( desktop );
2695 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2696 return;
2698 found:
2699 reply->flags = hotkey->flags;
2700 reply->vkey = hotkey->vkey;
2701 list_remove( &hotkey->entry );
2702 free( hotkey );
2703 release_object( desktop );
2706 /* attach (or detach) thread inputs */
2707 DECL_HANDLER(attach_thread_input)
2709 struct thread *thread_from = get_thread_from_id( req->tid_from );
2710 struct thread *thread_to = get_thread_from_id( req->tid_to );
2712 if (!thread_from || !thread_to)
2714 if (thread_from) release_object( thread_from );
2715 if (thread_to) release_object( thread_to );
2716 return;
2718 if (thread_from != thread_to)
2720 if (req->attach) attach_thread_input( thread_from, thread_to );
2721 else
2723 if (thread_from->queue && thread_to->queue &&
2724 thread_from->queue->input == thread_to->queue->input)
2725 detach_thread_input( thread_from );
2726 else
2727 set_error( STATUS_ACCESS_DENIED );
2730 else set_error( STATUS_ACCESS_DENIED );
2731 release_object( thread_from );
2732 release_object( thread_to );
2736 /* get thread input data */
2737 DECL_HANDLER(get_thread_input)
2739 struct thread *thread = NULL;
2740 struct desktop *desktop;
2741 struct thread_input *input;
2743 if (req->tid)
2745 if (!(thread = get_thread_from_id( req->tid ))) return;
2746 if (!(desktop = get_thread_desktop( thread, 0 )))
2748 release_object( thread );
2749 return;
2751 input = thread->queue ? thread->queue->input : NULL;
2753 else
2755 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2756 input = desktop->foreground_input; /* get the foreground thread info */
2759 if (input)
2761 reply->focus = input->focus;
2762 reply->capture = input->capture;
2763 reply->active = input->active;
2764 reply->menu_owner = input->menu_owner;
2765 reply->move_size = input->move_size;
2766 reply->caret = input->caret;
2767 reply->cursor = input->cursor;
2768 reply->show_count = input->cursor_count;
2769 reply->rect = input->caret_rect;
2772 /* foreground window is active window of foreground thread */
2773 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2774 if (thread) release_object( thread );
2775 release_object( desktop );
2779 /* retrieve queue keyboard state for a given thread */
2780 DECL_HANDLER(get_key_state)
2782 struct thread *thread;
2783 struct desktop *desktop;
2784 data_size_t size = min( 256, get_reply_max_size() );
2786 if (!req->tid) /* get global async key state */
2788 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2789 if (req->key >= 0)
2791 reply->state = desktop->keystate[req->key & 0xff];
2792 desktop->keystate[req->key & 0xff] &= ~0x40;
2794 set_reply_data( desktop->keystate, size );
2795 release_object( desktop );
2797 else
2799 if (!(thread = get_thread_from_id( req->tid ))) return;
2800 if (thread->queue)
2802 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2803 set_reply_data( thread->queue->input->keystate, size );
2805 release_object( thread );
2810 /* set queue keyboard state for a given thread */
2811 DECL_HANDLER(set_key_state)
2813 struct thread *thread;
2814 struct desktop *desktop;
2815 data_size_t size = min( 256, get_req_data_size() );
2817 if (!req->tid) /* set global async key state */
2819 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2820 memcpy( desktop->keystate, get_req_data(), size );
2821 release_object( desktop );
2823 else
2825 if (!(thread = get_thread_from_id( req->tid ))) return;
2826 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
2827 if (req->async && (desktop = get_thread_desktop( thread, 0 )))
2829 memcpy( desktop->keystate, get_req_data(), size );
2830 release_object( desktop );
2832 release_object( thread );
2837 /* set the system foreground window */
2838 DECL_HANDLER(set_foreground_window)
2840 struct thread *thread = NULL;
2841 struct desktop *desktop;
2842 struct msg_queue *queue = get_current_queue();
2844 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2845 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
2846 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
2847 reply->send_msg_new = FALSE;
2849 if (is_valid_foreground_window( req->handle ) &&
2850 (thread = get_window_thread( req->handle )) &&
2851 thread->queue->input->desktop == desktop)
2853 set_foreground_input( desktop, thread->queue->input );
2854 reply->send_msg_new = (desktop->foreground_input != queue->input);
2856 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2858 if (thread) release_object( thread );
2859 release_object( desktop );
2863 /* set the current thread focus window */
2864 DECL_HANDLER(set_focus_window)
2866 struct msg_queue *queue = get_current_queue();
2868 reply->previous = 0;
2869 if (queue && check_queue_input_window( queue, req->handle ))
2871 reply->previous = queue->input->focus;
2872 queue->input->focus = get_user_full_handle( req->handle );
2877 /* set the current thread active window */
2878 DECL_HANDLER(set_active_window)
2880 struct msg_queue *queue = get_current_queue();
2882 reply->previous = 0;
2883 if (queue && check_queue_input_window( queue, req->handle ))
2885 if (!req->handle || make_window_active( req->handle ))
2887 reply->previous = queue->input->active;
2888 queue->input->active = get_user_full_handle( req->handle );
2890 else set_error( STATUS_INVALID_HANDLE );
2895 /* set the current thread capture window */
2896 DECL_HANDLER(set_capture_window)
2898 struct msg_queue *queue = get_current_queue();
2900 reply->previous = reply->full_handle = 0;
2901 if (queue && check_queue_input_window( queue, req->handle ))
2903 struct thread_input *input = queue->input;
2905 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2906 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2908 set_error(STATUS_ACCESS_DENIED);
2909 return;
2911 reply->previous = input->capture;
2912 input->capture = get_user_full_handle( req->handle );
2913 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2914 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2915 reply->full_handle = input->capture;
2920 /* Set the current thread caret window */
2921 DECL_HANDLER(set_caret_window)
2923 struct msg_queue *queue = get_current_queue();
2925 reply->previous = 0;
2926 if (queue && check_queue_input_window( queue, req->handle ))
2928 struct thread_input *input = queue->input;
2930 reply->previous = input->caret;
2931 reply->old_rect = input->caret_rect;
2932 reply->old_hide = input->caret_hide;
2933 reply->old_state = input->caret_state;
2935 set_caret_window( input, get_user_full_handle(req->handle) );
2936 input->caret_rect.right = input->caret_rect.left + req->width;
2937 input->caret_rect.bottom = input->caret_rect.top + req->height;
2942 /* Set the current thread caret information */
2943 DECL_HANDLER(set_caret_info)
2945 struct msg_queue *queue = get_current_queue();
2946 struct thread_input *input;
2948 if (!queue) return;
2949 input = queue->input;
2950 reply->full_handle = input->caret;
2951 reply->old_rect = input->caret_rect;
2952 reply->old_hide = input->caret_hide;
2953 reply->old_state = input->caret_state;
2955 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2957 set_error( STATUS_ACCESS_DENIED );
2958 return;
2960 if (req->flags & SET_CARET_POS)
2962 input->caret_rect.right += req->x - input->caret_rect.left;
2963 input->caret_rect.bottom += req->y - input->caret_rect.top;
2964 input->caret_rect.left = req->x;
2965 input->caret_rect.top = req->y;
2967 if (req->flags & SET_CARET_HIDE)
2969 input->caret_hide += req->hide;
2970 if (input->caret_hide < 0) input->caret_hide = 0;
2972 if (req->flags & SET_CARET_STATE)
2974 if (req->state == -1) input->caret_state = !input->caret_state;
2975 else input->caret_state = !!req->state;
2980 /* get the time of the last input event */
2981 DECL_HANDLER(get_last_input_time)
2983 reply->time = last_input_time;
2986 /* set/get the current cursor */
2987 DECL_HANDLER(set_cursor)
2989 struct msg_queue *queue = get_current_queue();
2990 struct thread_input *input;
2992 if (!queue) return;
2993 input = queue->input;
2995 reply->prev_handle = input->cursor;
2996 reply->prev_count = input->cursor_count;
2997 reply->prev_x = input->desktop->cursor.x;
2998 reply->prev_y = input->desktop->cursor.y;
3000 if (req->flags & SET_CURSOR_HANDLE)
3002 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3004 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3005 return;
3007 input->cursor = req->handle;
3009 if (req->flags & SET_CURSOR_COUNT)
3011 queue->cursor_count += req->show_count;
3012 input->cursor_count += req->show_count;
3014 if (req->flags & SET_CURSOR_POS)
3016 set_cursor_pos( input->desktop, req->x, req->y );
3018 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3020 struct desktop *desktop = input->desktop;
3022 /* only the desktop owner can set the message */
3023 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3024 desktop->cursor.clip_msg = req->clip_msg;
3026 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip );
3029 reply->new_x = input->desktop->cursor.x;
3030 reply->new_y = input->desktop->cursor.y;
3031 reply->new_clip = input->desktop->cursor.clip;
3032 reply->last_change = input->desktop->cursor.last_change;
3035 DECL_HANDLER(update_rawinput_devices)
3037 const struct rawinput_device *devices = get_req_data();
3038 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3039 const struct rawinput_device_entry *e;
3040 unsigned int i;
3042 for (i = 0; i < device_count; ++i)
3044 update_rawinput_device(&devices[i]);
3047 e = find_rawinput_device( 1, 2 );
3048 current->process->rawinput_mouse = e ? &e->device : NULL;
3049 e = find_rawinput_device( 1, 6 );
3050 current->process->rawinput_kbd = e ? &e->device : NULL;