gdiplus: Skip resampling when drawing a bitmap with no scaling/rotation.
[wine/multimedia.git] / server / queue.c
blob601629ecd77ca05b3a854f8a64efe131d726a1a3
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, user_handle_t new_win )
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 || new_win)
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 );
1313 if (new_win) /* set the new window */
1315 struct thread *owner = get_window_thread( new_win );
1316 if (owner)
1318 msg->win = new_win;
1319 if (owner->queue->input != input)
1321 list_remove( &msg->entry );
1322 if (merge_message( owner->queue->input, msg ))
1324 free_message( msg );
1325 release_object( owner );
1326 return;
1328 list_add_tail( &owner->queue->input->msg_list, &msg->entry );
1330 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1331 remove = 0;
1332 release_object( owner );
1335 if (remove)
1337 update_input_key_state( input->desktop, input->keystate, msg );
1338 list_remove( &msg->entry );
1339 free_message( msg );
1343 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1345 struct hotkey *hotkey;
1346 unsigned int modifiers = 0;
1348 if (msg->msg != WM_KEYDOWN) return 0;
1350 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1351 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1352 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1353 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1355 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1357 if (hotkey->vkey != msg->wparam) continue;
1358 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1361 return 0;
1363 found:
1364 msg->type = MSG_POSTED;
1365 msg->win = hotkey->win;
1366 msg->msg = WM_HOTKEY;
1367 msg->wparam = hotkey->id;
1368 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1370 free( msg->data );
1371 msg->data = NULL;
1372 msg->data_size = 0;
1374 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1375 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1376 hotkey->queue->hotkey_count++;
1377 return 1;
1380 /* find the window that should receive a given hardware message */
1381 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1382 struct message *msg, unsigned int *msg_code )
1384 struct hardware_msg_data *data = msg->data;
1385 user_handle_t win = 0;
1387 *msg_code = msg->msg;
1388 if (msg->msg == WM_INPUT)
1390 if (!(win = msg->win) && input) win = input->focus;
1392 else if (is_keyboard_msg( msg ))
1394 if (input && !(win = input->focus))
1396 win = input->active;
1397 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1400 else /* mouse message */
1402 if (!input || !(win = input->capture))
1404 if (!(win = msg->win) || !is_window_visible( win ) || is_window_transparent( win ))
1405 win = window_from_point( desktop, data->x, data->y );
1408 return win;
1411 static struct rawinput_device_entry *find_rawinput_device( unsigned short usage_page, unsigned short usage )
1413 struct rawinput_device_entry *e;
1415 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
1417 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1418 return e;
1421 return NULL;
1424 static void update_rawinput_device(const struct rawinput_device *device)
1426 struct rawinput_device_entry *e;
1428 if (!(e = find_rawinput_device( device->usage_page, device->usage )))
1430 if (!(e = mem_alloc( sizeof(*e) ))) return;
1431 list_add_tail( &current->process->rawinput_devices, &e->entry );
1434 if (device->flags & RIDEV_REMOVE)
1436 list_remove( &e->entry );
1437 free( e );
1438 return;
1441 e->device = *device;
1442 e->device.target = get_user_full_handle( e->device.target );
1445 /* queue a hardware message into a given thread input */
1446 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1448 user_handle_t win;
1449 struct thread *thread;
1450 struct thread_input *input;
1451 unsigned int msg_code;
1452 struct hardware_msg_data *data = msg->data;
1454 update_input_key_state( desktop, desktop->keystate, msg );
1455 last_input_time = get_tick_count();
1456 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1458 if (is_keyboard_msg( msg ))
1460 if (queue_hotkey_message( desktop, msg )) return;
1461 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1462 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1463 msg->lparam &= ~(KF_EXTENDED << 16);
1465 else if (msg->msg != WM_INPUT)
1467 if (msg->msg == WM_MOUSEMOVE)
1469 int x = min( max( data->x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
1470 int y = min( max( data->y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
1471 if (desktop->cursor.x != x || desktop->cursor.y != y) always_queue = 1;
1472 desktop->cursor.x = x;
1473 desktop->cursor.y = y;
1474 desktop->cursor.last_change = get_tick_count();
1476 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1477 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1478 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1479 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1480 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1481 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1482 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1484 data->x = desktop->cursor.x;
1485 data->y = desktop->cursor.y;
1487 if (msg->win && (thread = get_window_thread( msg->win )))
1489 input = thread->queue->input;
1490 release_object( thread );
1492 else input = desktop->foreground_input;
1494 win = find_hardware_message_window( desktop, input, msg, &msg_code );
1495 if (!win || !(thread = get_window_thread(win)))
1497 if (input) update_input_key_state( input->desktop, input->keystate, msg );
1498 free_message( msg );
1499 return;
1501 input = thread->queue->input;
1503 if (win != desktop->cursor.win) always_queue = 1;
1504 desktop->cursor.win = win;
1506 if (!always_queue || merge_message( input, msg )) free_message( msg );
1507 else
1509 msg->unique_id = 0; /* will be set once we return it to the app */
1510 list_add_tail( &input->msg_list, &msg->entry );
1511 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1513 release_object( thread );
1516 /* send the low-level hook message for a given hardware message */
1517 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1518 const hw_input_t *input, struct msg_queue *sender )
1520 struct thread *hook_thread;
1521 struct msg_queue *queue;
1522 struct message *msg;
1523 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1524 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1526 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1527 if (!(queue = hook_thread->queue)) return 0;
1528 if (is_queue_hung( queue )) return 0;
1530 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1532 msg->type = MSG_HOOK_LL;
1533 msg->win = 0;
1534 msg->msg = id;
1535 msg->wparam = hardware_msg->msg;
1536 msg->time = hardware_msg->time;
1537 msg->data_size = hardware_msg->data_size;
1538 msg->result = NULL;
1540 if (input->type == INPUT_KEYBOARD)
1542 unsigned short vkey = input->kbd.vkey;
1543 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1544 msg->lparam = (input->kbd.scan << 16) | vkey;
1546 else msg->lparam = input->mouse.data << 16;
1548 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1549 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1551 free_message( msg );
1552 return 0;
1554 msg->result->hardware_msg = hardware_msg;
1555 msg->result->desktop = (struct desktop *)grab_object( desktop );
1556 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1557 set_queue_bits( queue, QS_SENDMESSAGE );
1558 return 1;
1561 /* queue a hardware message for a mouse event */
1562 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1563 unsigned int hook_flags, struct msg_queue *sender )
1565 const struct rawinput_device *device;
1566 struct hardware_msg_data *msg_data;
1567 struct message *msg;
1568 unsigned int i, time, flags;
1569 int wait = 0, x, y;
1571 static const unsigned int messages[] =
1573 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1574 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1575 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1576 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1577 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1578 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1579 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1580 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1581 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1582 0, /* 0x0200 = unused */
1583 0, /* 0x0400 = unused */
1584 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1585 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1588 desktop->cursor.last_change = get_tick_count();
1589 flags = input->mouse.flags;
1590 time = input->mouse.time;
1591 if (!time) time = desktop->cursor.last_change;
1593 if (flags & MOUSEEVENTF_MOVE)
1595 if (flags & MOUSEEVENTF_ABSOLUTE)
1597 x = input->mouse.x;
1598 y = input->mouse.y;
1599 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1600 x == desktop->cursor.x && y == desktop->cursor.y)
1601 flags &= ~MOUSEEVENTF_MOVE;
1603 else
1605 x = desktop->cursor.x + input->mouse.x;
1606 y = desktop->cursor.y + input->mouse.y;
1609 else
1611 x = desktop->cursor.x;
1612 y = desktop->cursor.y;
1615 if ((device = current->process->rawinput_mouse))
1617 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1618 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1620 free( msg );
1621 return 0;
1624 msg->type = MSG_HARDWARE;
1625 msg->win = device->target;
1626 msg->msg = WM_INPUT;
1627 msg->wparam = RIM_INPUT;
1628 msg->lparam = 0;
1629 msg->time = time;
1630 msg->data = msg_data;
1631 msg->data_size = sizeof(*msg_data);
1632 msg->result = NULL;
1634 msg_data->info = input->mouse.info;
1635 msg_data->flags = flags;
1636 msg_data->rawinput.type = RIM_TYPEMOUSE;
1637 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1638 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1639 msg_data->rawinput.mouse.data = input->mouse.data;
1641 queue_hardware_message( desktop, msg, 0 );
1644 for (i = 0; i < sizeof(messages)/sizeof(messages[0]); i++)
1646 if (!messages[i]) continue;
1647 if (!(flags & (1 << i))) continue;
1648 flags &= ~(1 << i);
1650 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1651 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1653 free( msg );
1654 return 0;
1656 memset( msg_data, 0, sizeof(*msg_data) );
1658 msg->type = MSG_HARDWARE;
1659 msg->win = get_user_full_handle( win );
1660 msg->msg = messages[i];
1661 msg->wparam = input->mouse.data << 16;
1662 msg->lparam = 0;
1663 msg->time = time;
1664 msg->result = NULL;
1665 msg->data = msg_data;
1666 msg->data_size = sizeof(*msg_data);
1667 msg_data->x = x;
1668 msg_data->y = y;
1669 msg_data->info = input->mouse.info;
1670 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLMHF_INJECTED;
1672 /* specify a sender only when sending the last message */
1673 if (!(flags & ((1 << sizeof(messages)/sizeof(messages[0])) - 1)))
1675 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1676 queue_hardware_message( desktop, msg, 0 );
1678 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1679 queue_hardware_message( desktop, msg, 0 );
1681 return wait;
1684 /* queue a hardware message for a keyboard event */
1685 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1686 unsigned int hook_flags, struct msg_queue *sender )
1688 const struct rawinput_device *device;
1689 struct hardware_msg_data *msg_data;
1690 struct message *msg;
1691 unsigned char vkey = input->kbd.vkey;
1692 unsigned int message_code, time;
1693 int wait;
1695 if (!(time = input->kbd.time)) time = get_tick_count();
1697 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1699 switch (vkey)
1701 case VK_MENU:
1702 case VK_LMENU:
1703 case VK_RMENU:
1704 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1705 break;
1706 case VK_CONTROL:
1707 case VK_LCONTROL:
1708 case VK_RCONTROL:
1709 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1710 break;
1711 case VK_SHIFT:
1712 case VK_LSHIFT:
1713 case VK_RSHIFT:
1714 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1715 break;
1719 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1720 switch (vkey)
1722 case VK_LMENU:
1723 case VK_RMENU:
1724 if (input->kbd.flags & KEYEVENTF_KEYUP)
1726 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1727 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1728 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1729 message_code = WM_SYSKEYUP;
1730 desktop->keystate[VK_MENU] &= ~0x02;
1732 else
1734 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1735 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1736 message_code = WM_SYSKEYDOWN;
1737 desktop->keystate[VK_MENU] |= 0x02;
1739 break;
1741 case VK_LCONTROL:
1742 case VK_RCONTROL:
1743 /* send WM_SYSKEYUP on release if Alt still pressed */
1744 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1745 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1746 message_code = WM_SYSKEYUP;
1747 desktop->keystate[VK_MENU] &= ~0x02;
1748 break;
1750 default:
1751 /* send WM_SYSKEY for Alt-anykey and for F10 */
1752 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1753 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1754 /* fall through */
1755 case VK_F10:
1756 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1757 desktop->keystate[VK_MENU] &= ~0x02;
1758 break;
1761 if ((device = current->process->rawinput_kbd))
1763 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1764 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1766 free( msg );
1767 return 0;
1770 msg->type = MSG_HARDWARE;
1771 msg->win = device->target;
1772 msg->msg = WM_INPUT;
1773 msg->wparam = RIM_INPUT;
1774 msg->lparam = 0;
1775 msg->time = time;
1776 msg->data = msg_data;
1777 msg->data_size = sizeof(*msg_data);
1778 msg->result = NULL;
1780 msg_data->info = input->kbd.info;
1781 msg_data->flags = input->kbd.flags;
1782 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1783 msg_data->rawinput.kbd.message = message_code;
1784 msg_data->rawinput.kbd.vkey = vkey;
1785 msg_data->rawinput.kbd.scan = input->kbd.scan;
1787 queue_hardware_message( desktop, msg, 0 );
1790 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1791 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1793 free( msg );
1794 return 0;
1796 memset( msg_data, 0, sizeof(*msg_data) );
1798 msg->type = MSG_HARDWARE;
1799 msg->win = get_user_full_handle( win );
1800 msg->msg = message_code;
1801 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1802 msg->time = time;
1803 msg->result = NULL;
1804 msg->data = msg_data;
1805 msg->data_size = sizeof(*msg_data);
1806 msg_data->info = input->kbd.info;
1807 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLKHF_INJECTED;
1809 if (input->kbd.flags & KEYEVENTF_UNICODE)
1811 msg->wparam = VK_PACKET;
1813 else
1815 unsigned int flags = 0;
1816 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1817 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1818 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1819 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1821 msg->wparam = vkey;
1822 msg->lparam |= flags << 16;
1823 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1826 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1827 queue_hardware_message( desktop, msg, 1 );
1829 return wait;
1832 /* queue a hardware message for a custom type of event */
1833 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1834 const hw_input_t *input )
1836 struct hardware_msg_data *msg_data;
1837 struct message *msg;
1839 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
1840 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1842 free( msg );
1843 return;
1845 memset( msg_data, 0, sizeof(*msg_data) );
1847 msg->type = MSG_HARDWARE;
1848 msg->win = get_user_full_handle( win );
1849 msg->msg = input->hw.msg;
1850 msg->wparam = 0;
1851 msg->lparam = input->hw.lparam;
1852 msg->time = get_tick_count();
1853 msg->result = NULL;
1854 msg->data = msg_data;
1855 msg->data_size = sizeof(*msg_data);
1857 queue_hardware_message( desktop, msg, 1 );
1860 /* check message filter for a hardware message */
1861 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1862 user_handle_t filter_win, unsigned int first, unsigned int last )
1864 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1866 /* we can only test the window for a keyboard message since the
1867 * dest window for a mouse message depends on hittest */
1868 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1869 return 0;
1870 /* the message code is final for a keyboard message, we can simply check it */
1871 return check_msg_filter( msg_code, first, last );
1873 else /* mouse message */
1875 /* we need to check all possible values that the message can have in the end */
1877 if (check_msg_filter( msg_code, first, last )) return 1;
1878 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1880 /* all other messages can become non-client messages */
1881 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1883 /* clicks can become double-clicks or non-client double-clicks */
1884 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1885 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1887 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1888 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1890 return 0;
1895 /* find a hardware message for the given queue */
1896 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1897 unsigned int first, unsigned int last, unsigned int flags,
1898 struct get_message_reply *reply )
1900 struct thread_input *input = thread->queue->input;
1901 struct thread *win_thread;
1902 struct list *ptr;
1903 user_handle_t win;
1904 int clear_bits, got_one = 0;
1905 unsigned int msg_code;
1907 ptr = list_head( &input->msg_list );
1908 if (hw_id)
1910 while (ptr)
1912 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1913 if (msg->unique_id == hw_id) break;
1914 ptr = list_next( &input->msg_list, ptr );
1916 if (!ptr) ptr = list_head( &input->msg_list );
1917 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1920 if (ptr == list_head( &input->msg_list ))
1921 clear_bits = QS_INPUT;
1922 else
1923 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1925 while (ptr)
1927 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1928 struct hardware_msg_data *data = msg->data;
1930 ptr = list_next( &input->msg_list, ptr );
1931 win = find_hardware_message_window( input->desktop, input, msg, &msg_code );
1932 if (!win || !(win_thread = get_window_thread( win )))
1934 /* no window at all, remove it */
1935 update_input_key_state( input->desktop, input->keystate, msg );
1936 list_remove( &msg->entry );
1937 free_message( msg );
1938 continue;
1940 if (win_thread != thread)
1942 if (win_thread->queue->input == input)
1944 /* wake the other thread */
1945 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1946 got_one = 1;
1948 else
1950 /* for another thread input, drop it */
1951 update_input_key_state( input->desktop, input->keystate, msg );
1952 list_remove( &msg->entry );
1953 free_message( msg );
1955 release_object( win_thread );
1956 continue;
1958 release_object( win_thread );
1960 /* if we already got a message for another thread, or if it doesn't
1961 * match the filter we skip it */
1962 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1964 clear_bits &= ~get_hardware_msg_bit( msg );
1965 continue;
1967 /* now we can return it */
1968 if (!msg->unique_id) msg->unique_id = get_unique_id();
1969 reply->type = MSG_HARDWARE;
1970 reply->win = win;
1971 reply->msg = msg_code;
1972 reply->wparam = msg->wparam;
1973 reply->lparam = msg->lparam;
1974 reply->time = msg->time;
1976 data->hw_id = msg->unique_id;
1977 set_reply_data( msg->data, msg->data_size );
1978 if (msg->msg == WM_INPUT && (flags & PM_REMOVE))
1979 release_hardware_message( current->queue, data->hw_id, 1, 0 );
1980 return 1;
1982 /* nothing found, clear the hardware queue bits */
1983 clear_queue_bits( thread->queue, clear_bits );
1984 return 0;
1987 /* increment (or decrement if 'incr' is negative) the queue paint count */
1988 void inc_queue_paint_count( struct thread *thread, int incr )
1990 struct msg_queue *queue = thread->queue;
1992 assert( queue );
1994 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1996 if (queue->paint_count)
1997 set_queue_bits( queue, QS_PAINT );
1998 else
1999 clear_queue_bits( queue, QS_PAINT );
2003 /* remove all messages and timers belonging to a certain window */
2004 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2006 struct msg_queue *queue = thread->queue;
2007 struct list *ptr;
2008 int i;
2010 if (!queue) return;
2012 /* remove timers */
2014 ptr = list_head( &queue->pending_timers );
2015 while (ptr)
2017 struct list *next = list_next( &queue->pending_timers, ptr );
2018 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2019 if (timer->win == win) free_timer( queue, timer );
2020 ptr = next;
2022 ptr = list_head( &queue->expired_timers );
2023 while (ptr)
2025 struct list *next = list_next( &queue->expired_timers, ptr );
2026 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2027 if (timer->win == win) free_timer( queue, timer );
2028 ptr = next;
2031 /* remove messages */
2032 for (i = 0; i < NB_MSG_KINDS; i++)
2034 struct list *ptr, *next;
2036 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2038 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2039 if (msg->win == win)
2041 if (msg->msg == WM_QUIT && !queue->quit_message)
2043 queue->quit_message = 1;
2044 queue->exit_code = msg->wparam;
2046 remove_queue_message( queue, msg, i );
2051 thread_input_cleanup_window( queue, win );
2054 /* post a message to a window; used by socket handling */
2055 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2057 struct message *msg;
2058 struct thread *thread = get_window_thread( win );
2060 if (!thread) return;
2062 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2064 msg->type = MSG_POSTED;
2065 msg->win = get_user_full_handle( win );
2066 msg->msg = message;
2067 msg->wparam = wparam;
2068 msg->lparam = lparam;
2069 msg->time = get_tick_count();
2070 msg->result = NULL;
2071 msg->data = NULL;
2072 msg->data_size = 0;
2074 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2075 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2076 if (message == WM_HOTKEY)
2078 set_queue_bits( thread->queue, QS_HOTKEY );
2079 thread->queue->hotkey_count++;
2082 release_object( thread );
2085 /* post a win event */
2086 void post_win_event( struct thread *thread, unsigned int event,
2087 user_handle_t win, unsigned int object_id,
2088 unsigned int child_id, client_ptr_t hook_proc,
2089 const WCHAR *module, data_size_t module_size,
2090 user_handle_t hook)
2092 struct message *msg;
2094 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2096 struct winevent_msg_data *data;
2098 msg->type = MSG_WINEVENT;
2099 msg->win = get_user_full_handle( win );
2100 msg->msg = event;
2101 msg->wparam = object_id;
2102 msg->lparam = child_id;
2103 msg->time = get_tick_count();
2104 msg->result = NULL;
2106 if ((data = malloc( sizeof(*data) + module_size )))
2108 data->hook = hook;
2109 data->tid = get_thread_id( current );
2110 data->hook_proc = hook_proc;
2111 memcpy( data + 1, module, module_size );
2113 msg->data = data;
2114 msg->data_size = sizeof(*data) + module_size;
2116 if (debug_level > 1)
2117 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2118 get_thread_id(thread), event, win, object_id, child_id );
2119 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2120 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2122 else
2123 free( msg );
2127 /* free all hotkeys on a desktop, optionally filtering by window */
2128 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2130 struct hotkey *hotkey, *hotkey2;
2132 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2134 if (!window || hotkey->win == window)
2136 list_remove( &hotkey->entry );
2137 free( hotkey );
2143 /* check if the thread owning the window is hung */
2144 DECL_HANDLER(is_window_hung)
2146 struct thread *thread;
2148 thread = get_window_thread( req->win );
2149 if (thread)
2151 reply->is_hung = is_queue_hung( thread->queue );
2152 release_object( thread );
2154 else reply->is_hung = 0;
2158 /* get the message queue of the current thread */
2159 DECL_HANDLER(get_msg_queue)
2161 struct msg_queue *queue = get_current_queue();
2163 reply->handle = 0;
2164 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2168 /* set the file descriptor associated to the current thread queue */
2169 DECL_HANDLER(set_queue_fd)
2171 struct msg_queue *queue = get_current_queue();
2172 struct file *file;
2173 int unix_fd;
2175 if (queue->fd) /* fd can only be set once */
2177 set_error( STATUS_ACCESS_DENIED );
2178 return;
2180 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2182 if ((unix_fd = get_file_unix_fd( file )) != -1)
2184 if ((unix_fd = dup( unix_fd )) != -1)
2185 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2186 else
2187 file_set_error();
2189 release_object( file );
2193 /* set the current message queue wakeup mask */
2194 DECL_HANDLER(set_queue_mask)
2196 struct msg_queue *queue = get_current_queue();
2198 if (queue)
2200 queue->wake_mask = req->wake_mask;
2201 queue->changed_mask = req->changed_mask;
2202 reply->wake_bits = queue->wake_bits;
2203 reply->changed_bits = queue->changed_bits;
2204 if (is_signaled( queue ))
2206 /* if skip wait is set, do what would have been done in the subsequent wait */
2207 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2208 else wake_up( &queue->obj, 0 );
2214 /* get the current message queue status */
2215 DECL_HANDLER(get_queue_status)
2217 struct msg_queue *queue = current->queue;
2218 if (queue)
2220 reply->wake_bits = queue->wake_bits;
2221 reply->changed_bits = queue->changed_bits;
2222 if (req->clear) queue->changed_bits = 0;
2224 else reply->wake_bits = reply->changed_bits = 0;
2228 /* send a message to a thread queue */
2229 DECL_HANDLER(send_message)
2231 struct message *msg;
2232 struct msg_queue *send_queue = get_current_queue();
2233 struct msg_queue *recv_queue = NULL;
2234 struct thread *thread = NULL;
2236 if (!(thread = get_thread_from_id( req->id ))) return;
2238 if (!(recv_queue = thread->queue))
2240 set_error( STATUS_INVALID_PARAMETER );
2241 release_object( thread );
2242 return;
2244 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2246 set_error( STATUS_TIMEOUT );
2247 release_object( thread );
2248 return;
2251 if ((msg = mem_alloc( sizeof(*msg) )))
2253 msg->type = req->type;
2254 msg->win = get_user_full_handle( req->win );
2255 msg->msg = req->msg;
2256 msg->wparam = req->wparam;
2257 msg->lparam = req->lparam;
2258 msg->time = get_tick_count();
2259 msg->result = NULL;
2260 msg->data = NULL;
2261 msg->data_size = get_req_data_size();
2263 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2265 free( msg );
2266 release_object( thread );
2267 return;
2270 switch(msg->type)
2272 case MSG_OTHER_PROCESS:
2273 case MSG_ASCII:
2274 case MSG_UNICODE:
2275 case MSG_CALLBACK:
2276 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2278 free_message( msg );
2279 break;
2281 /* fall through */
2282 case MSG_NOTIFY:
2283 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2284 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2285 break;
2286 case MSG_POSTED:
2287 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2288 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2289 if (msg->msg == WM_HOTKEY)
2291 set_queue_bits( recv_queue, QS_HOTKEY );
2292 recv_queue->hotkey_count++;
2294 break;
2295 case MSG_HARDWARE: /* should use send_hardware_message instead */
2296 case MSG_CALLBACK_RESULT: /* cannot send this one */
2297 case MSG_HOOK_LL: /* generated internally */
2298 default:
2299 set_error( STATUS_INVALID_PARAMETER );
2300 free( msg );
2301 break;
2304 release_object( thread );
2307 /* send a hardware message to a thread queue */
2308 DECL_HANDLER(send_hardware_message)
2310 struct thread *thread = NULL;
2311 struct desktop *desktop;
2312 struct msg_queue *sender = get_current_queue();
2313 data_size_t size = min( 256, get_reply_max_size() );
2315 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2317 if (req->win)
2319 if (!(thread = get_window_thread( req->win ))) return;
2320 if (desktop != thread->queue->input->desktop)
2322 /* don't allow queuing events to a different desktop */
2323 release_object( desktop );
2324 return;
2328 reply->prev_x = desktop->cursor.x;
2329 reply->prev_y = desktop->cursor.y;
2331 switch (req->input.type)
2333 case INPUT_MOUSE:
2334 reply->wait = queue_mouse_message( desktop, req->win, &req->input, req->flags, sender );
2335 break;
2336 case INPUT_KEYBOARD:
2337 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, req->flags, sender );
2338 break;
2339 case INPUT_HARDWARE:
2340 queue_custom_hardware_message( desktop, req->win, &req->input );
2341 break;
2342 default:
2343 set_error( STATUS_INVALID_PARAMETER );
2345 if (thread) release_object( thread );
2347 reply->new_x = desktop->cursor.x;
2348 reply->new_y = desktop->cursor.y;
2349 set_reply_data( desktop->keystate, size );
2350 release_object( desktop );
2353 /* post a quit message to the current queue */
2354 DECL_HANDLER(post_quit_message)
2356 struct msg_queue *queue = get_current_queue();
2358 if (!queue)
2359 return;
2361 queue->quit_message = 1;
2362 queue->exit_code = req->exit_code;
2363 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2366 /* get a message from the current queue */
2367 DECL_HANDLER(get_message)
2369 struct timer *timer;
2370 struct list *ptr;
2371 struct msg_queue *queue = get_current_queue();
2372 user_handle_t get_win = get_user_full_handle( req->get_win );
2373 unsigned int filter = req->flags >> 16;
2375 reply->active_hooks = get_active_hooks();
2377 if (!queue) return;
2378 queue->last_get_msg = current_time;
2379 if (!filter) filter = QS_ALLINPUT;
2381 /* first check for sent messages */
2382 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2384 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2385 receive_message( queue, msg, reply );
2386 return;
2389 /* clear changed bits so we can wait on them if we don't find a message */
2390 if (filter & QS_POSTMESSAGE)
2392 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2393 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2395 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2396 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2398 /* then check for posted messages */
2399 if ((filter & QS_POSTMESSAGE) &&
2400 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2401 return;
2403 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2404 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2405 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2406 return;
2408 /* only check for quit messages if not posted messages pending.
2409 * note: the quit message isn't filtered */
2410 if (get_quit_message( queue, req->flags, reply ))
2411 return;
2413 /* then check for any raw hardware message */
2414 if ((filter & QS_INPUT) &&
2415 filter_contains_hw_range( req->get_first, req->get_last ) &&
2416 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2417 return;
2419 /* now check for WM_PAINT */
2420 if ((filter & QS_PAINT) &&
2421 queue->paint_count &&
2422 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2423 (reply->win = find_window_to_repaint( get_win, current )))
2425 reply->type = MSG_POSTED;
2426 reply->msg = WM_PAINT;
2427 reply->wparam = 0;
2428 reply->lparam = 0;
2429 reply->time = get_tick_count();
2430 return;
2433 /* now check for timer */
2434 if ((filter & QS_TIMER) &&
2435 (timer = find_expired_timer( queue, get_win, req->get_first,
2436 req->get_last, (req->flags & PM_REMOVE) )))
2438 reply->type = MSG_POSTED;
2439 reply->win = timer->win;
2440 reply->msg = timer->msg;
2441 reply->wparam = timer->id;
2442 reply->lparam = timer->lparam;
2443 reply->time = get_tick_count();
2444 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2445 set_event( current->process->idle_event );
2446 return;
2449 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2450 queue->wake_mask = req->wake_mask;
2451 queue->changed_mask = req->changed_mask;
2452 set_error( STATUS_PENDING ); /* FIXME */
2456 /* reply to a sent message */
2457 DECL_HANDLER(reply_message)
2459 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2460 else if (current->queue->recv_result)
2461 reply_message( current->queue, req->result, 0, req->remove,
2462 get_req_data(), get_req_data_size() );
2466 /* accept the current hardware message */
2467 DECL_HANDLER(accept_hardware_message)
2469 if (current->queue)
2470 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
2471 else
2472 set_error( STATUS_ACCESS_DENIED );
2476 /* retrieve the reply for the last message sent */
2477 DECL_HANDLER(get_message_reply)
2479 struct message_result *result;
2480 struct list *entry;
2481 struct msg_queue *queue = current->queue;
2483 if (queue)
2485 set_error( STATUS_PENDING );
2486 reply->result = 0;
2488 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2490 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2491 if (result->replied || req->cancel)
2493 if (result->replied)
2495 reply->result = result->result;
2496 set_error( result->error );
2497 if (result->data)
2499 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2500 set_reply_data_ptr( result->data, data_len );
2501 result->data = NULL;
2502 result->data_size = 0;
2505 remove_result_from_sender( result );
2507 entry = list_head( &queue->send_result );
2508 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2509 else
2511 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2512 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2513 else clear_queue_bits( queue, QS_SMRESULT );
2517 else set_error( STATUS_ACCESS_DENIED );
2521 /* set a window timer */
2522 DECL_HANDLER(set_win_timer)
2524 struct timer *timer;
2525 struct msg_queue *queue;
2526 struct thread *thread = NULL;
2527 user_handle_t win = 0;
2528 lparam_t id = req->id;
2530 if (req->win)
2532 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2534 set_error( STATUS_INVALID_HANDLE );
2535 return;
2537 if (thread->process != current->process)
2539 release_object( thread );
2540 set_error( STATUS_ACCESS_DENIED );
2541 return;
2543 queue = thread->queue;
2544 /* remove it if it existed already */
2545 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2547 else
2549 queue = get_current_queue();
2550 /* look for a timer with this id */
2551 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2553 /* free and reuse id */
2554 free_timer( queue, timer );
2556 else
2558 /* find a free id for it */
2561 id = queue->next_timer_id;
2562 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2564 while (find_timer( queue, 0, req->msg, id ));
2568 if ((timer = set_timer( queue, req->rate )))
2570 timer->win = win;
2571 timer->msg = req->msg;
2572 timer->id = id;
2573 timer->lparam = req->lparam;
2574 reply->id = id;
2576 if (thread) release_object( thread );
2579 /* kill a window timer */
2580 DECL_HANDLER(kill_win_timer)
2582 struct timer *timer;
2583 struct thread *thread;
2584 user_handle_t win = 0;
2586 if (req->win)
2588 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2590 set_error( STATUS_INVALID_HANDLE );
2591 return;
2593 if (thread->process != current->process)
2595 release_object( thread );
2596 set_error( STATUS_ACCESS_DENIED );
2597 return;
2600 else thread = (struct thread *)grab_object( current );
2602 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2603 free_timer( thread->queue, timer );
2604 else
2605 set_error( STATUS_INVALID_PARAMETER );
2607 release_object( thread );
2610 DECL_HANDLER(register_hotkey)
2612 struct desktop *desktop;
2613 user_handle_t win_handle = req->window;
2614 struct hotkey *hotkey;
2615 struct hotkey *new_hotkey = NULL;
2616 struct thread *thread;
2617 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2619 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2621 if (win_handle)
2623 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2625 release_object( desktop );
2626 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2627 return;
2630 thread = get_window_thread( win_handle );
2631 if (thread) release_object( thread );
2633 if (thread != current)
2635 release_object( desktop );
2636 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2637 return;
2641 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2643 if (req->vkey == hotkey->vkey &&
2644 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2646 release_object( desktop );
2647 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2648 return;
2650 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2651 new_hotkey = hotkey;
2654 if (new_hotkey)
2656 reply->replaced = 1;
2657 reply->flags = new_hotkey->flags;
2658 reply->vkey = new_hotkey->vkey;
2660 else
2662 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2663 if (new_hotkey)
2665 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2666 new_hotkey->queue = current->queue;
2667 new_hotkey->win = win_handle;
2668 new_hotkey->id = req->id;
2672 if (new_hotkey)
2674 new_hotkey->flags = req->flags;
2675 new_hotkey->vkey = req->vkey;
2678 release_object( desktop );
2681 DECL_HANDLER(unregister_hotkey)
2683 struct desktop *desktop;
2684 user_handle_t win_handle = req->window;
2685 struct hotkey *hotkey;
2686 struct thread *thread;
2688 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2690 if (win_handle)
2692 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2694 release_object( desktop );
2695 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2696 return;
2699 thread = get_window_thread( win_handle );
2700 if (thread) release_object( thread );
2702 if (thread != current)
2704 release_object( desktop );
2705 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2706 return;
2710 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2712 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2713 goto found;
2716 release_object( desktop );
2717 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2718 return;
2720 found:
2721 reply->flags = hotkey->flags;
2722 reply->vkey = hotkey->vkey;
2723 list_remove( &hotkey->entry );
2724 free( hotkey );
2725 release_object( desktop );
2728 /* attach (or detach) thread inputs */
2729 DECL_HANDLER(attach_thread_input)
2731 struct thread *thread_from = get_thread_from_id( req->tid_from );
2732 struct thread *thread_to = get_thread_from_id( req->tid_to );
2734 if (!thread_from || !thread_to)
2736 if (thread_from) release_object( thread_from );
2737 if (thread_to) release_object( thread_to );
2738 return;
2740 if (thread_from != thread_to)
2742 if (req->attach) attach_thread_input( thread_from, thread_to );
2743 else
2745 if (thread_from->queue && thread_to->queue &&
2746 thread_from->queue->input == thread_to->queue->input)
2747 detach_thread_input( thread_from );
2748 else
2749 set_error( STATUS_ACCESS_DENIED );
2752 else set_error( STATUS_ACCESS_DENIED );
2753 release_object( thread_from );
2754 release_object( thread_to );
2758 /* get thread input data */
2759 DECL_HANDLER(get_thread_input)
2761 struct thread *thread = NULL;
2762 struct desktop *desktop;
2763 struct thread_input *input;
2765 if (req->tid)
2767 if (!(thread = get_thread_from_id( req->tid ))) return;
2768 if (!(desktop = get_thread_desktop( thread, 0 )))
2770 release_object( thread );
2771 return;
2773 input = thread->queue ? thread->queue->input : NULL;
2775 else
2777 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2778 input = desktop->foreground_input; /* get the foreground thread info */
2781 if (input)
2783 reply->focus = input->focus;
2784 reply->capture = input->capture;
2785 reply->active = input->active;
2786 reply->menu_owner = input->menu_owner;
2787 reply->move_size = input->move_size;
2788 reply->caret = input->caret;
2789 reply->cursor = input->cursor;
2790 reply->show_count = input->cursor_count;
2791 reply->rect = input->caret_rect;
2794 /* foreground window is active window of foreground thread */
2795 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2796 if (thread) release_object( thread );
2797 release_object( desktop );
2801 /* retrieve queue keyboard state for a given thread */
2802 DECL_HANDLER(get_key_state)
2804 struct thread *thread;
2805 struct desktop *desktop;
2806 data_size_t size = min( 256, get_reply_max_size() );
2808 if (!req->tid) /* get global async key state */
2810 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2811 if (req->key >= 0)
2813 reply->state = desktop->keystate[req->key & 0xff];
2814 desktop->keystate[req->key & 0xff] &= ~0x40;
2816 set_reply_data( desktop->keystate, size );
2817 release_object( desktop );
2819 else
2821 if (!(thread = get_thread_from_id( req->tid ))) return;
2822 if (thread->queue)
2824 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2825 set_reply_data( thread->queue->input->keystate, size );
2827 release_object( thread );
2832 /* set queue keyboard state for a given thread */
2833 DECL_HANDLER(set_key_state)
2835 struct thread *thread;
2836 struct desktop *desktop;
2837 data_size_t size = min( 256, get_req_data_size() );
2839 if (!req->tid) /* set global async key state */
2841 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2842 memcpy( desktop->keystate, get_req_data(), size );
2843 release_object( desktop );
2845 else
2847 if (!(thread = get_thread_from_id( req->tid ))) return;
2848 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
2849 if (req->async && (desktop = get_thread_desktop( thread, 0 )))
2851 memcpy( desktop->keystate, get_req_data(), size );
2852 release_object( desktop );
2854 release_object( thread );
2859 /* set the system foreground window */
2860 DECL_HANDLER(set_foreground_window)
2862 struct thread *thread = NULL;
2863 struct desktop *desktop;
2864 struct msg_queue *queue = get_current_queue();
2866 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2867 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
2868 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
2869 reply->send_msg_new = FALSE;
2871 if (is_valid_foreground_window( req->handle ) &&
2872 (thread = get_window_thread( req->handle )) &&
2873 thread->queue->input->desktop == desktop)
2875 set_foreground_input( desktop, thread->queue->input );
2876 reply->send_msg_new = (desktop->foreground_input != queue->input);
2878 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2880 if (thread) release_object( thread );
2881 release_object( desktop );
2885 /* set the current thread focus window */
2886 DECL_HANDLER(set_focus_window)
2888 struct msg_queue *queue = get_current_queue();
2890 reply->previous = 0;
2891 if (queue && check_queue_input_window( queue, req->handle ))
2893 reply->previous = queue->input->focus;
2894 queue->input->focus = get_user_full_handle( req->handle );
2899 /* set the current thread active window */
2900 DECL_HANDLER(set_active_window)
2902 struct msg_queue *queue = get_current_queue();
2904 reply->previous = 0;
2905 if (queue && check_queue_input_window( queue, req->handle ))
2907 if (!req->handle || make_window_active( req->handle ))
2909 reply->previous = queue->input->active;
2910 queue->input->active = get_user_full_handle( req->handle );
2912 else set_error( STATUS_INVALID_HANDLE );
2917 /* set the current thread capture window */
2918 DECL_HANDLER(set_capture_window)
2920 struct msg_queue *queue = get_current_queue();
2922 reply->previous = reply->full_handle = 0;
2923 if (queue && check_queue_input_window( queue, req->handle ))
2925 struct thread_input *input = queue->input;
2927 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2928 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2930 set_error(STATUS_ACCESS_DENIED);
2931 return;
2933 reply->previous = input->capture;
2934 input->capture = get_user_full_handle( req->handle );
2935 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2936 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2937 reply->full_handle = input->capture;
2942 /* Set the current thread caret window */
2943 DECL_HANDLER(set_caret_window)
2945 struct msg_queue *queue = get_current_queue();
2947 reply->previous = 0;
2948 if (queue && check_queue_input_window( queue, req->handle ))
2950 struct thread_input *input = queue->input;
2952 reply->previous = input->caret;
2953 reply->old_rect = input->caret_rect;
2954 reply->old_hide = input->caret_hide;
2955 reply->old_state = input->caret_state;
2957 set_caret_window( input, get_user_full_handle(req->handle) );
2958 input->caret_rect.right = input->caret_rect.left + req->width;
2959 input->caret_rect.bottom = input->caret_rect.top + req->height;
2964 /* Set the current thread caret information */
2965 DECL_HANDLER(set_caret_info)
2967 struct msg_queue *queue = get_current_queue();
2968 struct thread_input *input;
2970 if (!queue) return;
2971 input = queue->input;
2972 reply->full_handle = input->caret;
2973 reply->old_rect = input->caret_rect;
2974 reply->old_hide = input->caret_hide;
2975 reply->old_state = input->caret_state;
2977 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2979 set_error( STATUS_ACCESS_DENIED );
2980 return;
2982 if (req->flags & SET_CARET_POS)
2984 input->caret_rect.right += req->x - input->caret_rect.left;
2985 input->caret_rect.bottom += req->y - input->caret_rect.top;
2986 input->caret_rect.left = req->x;
2987 input->caret_rect.top = req->y;
2989 if (req->flags & SET_CARET_HIDE)
2991 input->caret_hide += req->hide;
2992 if (input->caret_hide < 0) input->caret_hide = 0;
2994 if (req->flags & SET_CARET_STATE)
2996 if (req->state == -1) input->caret_state = !input->caret_state;
2997 else input->caret_state = !!req->state;
3002 /* get the time of the last input event */
3003 DECL_HANDLER(get_last_input_time)
3005 reply->time = last_input_time;
3008 /* set/get the current cursor */
3009 DECL_HANDLER(set_cursor)
3011 struct msg_queue *queue = get_current_queue();
3012 struct thread_input *input;
3014 if (!queue) return;
3015 input = queue->input;
3017 reply->prev_handle = input->cursor;
3018 reply->prev_count = input->cursor_count;
3019 reply->prev_x = input->desktop->cursor.x;
3020 reply->prev_y = input->desktop->cursor.y;
3022 if (req->flags & SET_CURSOR_HANDLE)
3024 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3026 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3027 return;
3029 input->cursor = req->handle;
3031 if (req->flags & SET_CURSOR_COUNT)
3033 queue->cursor_count += req->show_count;
3034 input->cursor_count += req->show_count;
3036 if (req->flags & SET_CURSOR_POS)
3038 set_cursor_pos( input->desktop, req->x, req->y );
3040 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3042 struct desktop *desktop = input->desktop;
3044 /* only the desktop owner can set the message */
3045 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3046 desktop->cursor.clip_msg = req->clip_msg;
3048 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip );
3051 reply->new_x = input->desktop->cursor.x;
3052 reply->new_y = input->desktop->cursor.y;
3053 reply->new_clip = input->desktop->cursor.clip;
3054 reply->last_change = input->desktop->cursor.last_change;
3057 DECL_HANDLER(update_rawinput_devices)
3059 const struct rawinput_device *devices = get_req_data();
3060 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3061 const struct rawinput_device_entry *e;
3062 unsigned int i;
3064 for (i = 0; i < device_count; ++i)
3066 update_rawinput_device(&devices[i]);
3069 e = find_rawinput_device( 1, 2 );
3070 current->process->rawinput_mouse = e ? &e->device : NULL;
3071 e = find_rawinput_device( 1, 6 );
3072 current->process->rawinput_kbd = e ? &e->device : NULL;