comctl32/tests: Use BOOL type where appropriate.
[wine/wine-gecko.git] / server / queue.c
blobc19bb3ab56dea6661ef7047fe6c97b851664d30f
1 /*
2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "winternl.h"
37 #include "handle.h"
38 #include "file.h"
39 #include "thread.h"
40 #include "process.h"
41 #include "request.h"
42 #include "user.h"
44 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
45 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
47 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
48 #define NB_MSG_KINDS (POST_MESSAGE+1)
51 struct message_result
53 struct list sender_entry; /* entry in sender list */
54 struct message *msg; /* message the result is for */
55 struct message_result *recv_next; /* next in receiver list */
56 struct msg_queue *sender; /* sender queue */
57 struct msg_queue *receiver; /* receiver queue */
58 int replied; /* has it been replied to? */
59 unsigned int error; /* error code to pass back to sender */
60 lparam_t result; /* reply result */
61 struct message *hardware_msg; /* hardware message if low-level hook result */
62 struct desktop *desktop; /* desktop for hardware message */
63 struct message *callback_msg; /* message to queue for callback */
64 void *data; /* message reply data */
65 unsigned int data_size; /* size of message reply data */
66 struct timeout_user *timeout; /* result timeout */
69 struct message
71 struct list entry; /* entry in message list */
72 enum message_type type; /* message type */
73 user_handle_t win; /* window handle */
74 unsigned int msg; /* message code */
75 lparam_t wparam; /* parameters */
76 lparam_t lparam; /* parameters */
77 unsigned int time; /* message time */
78 void *data; /* message data for sent messages */
79 unsigned int data_size; /* size of message data */
80 unsigned int unique_id; /* unique id for nested hw message waits */
81 struct message_result *result; /* result in sender queue */
84 struct timer
86 struct list entry; /* entry in timer list */
87 timeout_t when; /* next expiration */
88 unsigned int rate; /* timer rate in ms */
89 user_handle_t win; /* window handle */
90 unsigned int msg; /* message to post */
91 lparam_t id; /* timer id */
92 lparam_t lparam; /* lparam for message */
95 struct thread_input
97 struct object obj; /* object header */
98 struct desktop *desktop; /* desktop that this thread input belongs to */
99 user_handle_t focus; /* focus window */
100 user_handle_t capture; /* capture window */
101 user_handle_t active; /* active window */
102 user_handle_t menu_owner; /* current menu owner window */
103 user_handle_t move_size; /* current moving/resizing window */
104 user_handle_t caret; /* caret window */
105 rectangle_t caret_rect; /* caret rectangle */
106 int caret_hide; /* caret hide count */
107 int caret_state; /* caret on/off state */
108 user_handle_t cursor; /* current cursor */
109 int cursor_count; /* cursor show count */
110 struct list msg_list; /* list of hardware messages */
111 unsigned char keystate[256]; /* state of each key */
114 struct msg_queue
116 struct object obj; /* object header */
117 struct fd *fd; /* optional file descriptor to poll */
118 unsigned int wake_bits; /* wakeup bits */
119 unsigned int wake_mask; /* wakeup mask */
120 unsigned int changed_bits; /* changed wakeup bits */
121 unsigned int changed_mask; /* changed wakeup mask */
122 int paint_count; /* pending paint messages count */
123 int hotkey_count; /* pending hotkey messages count */
124 int quit_message; /* is there a pending quit message? */
125 int exit_code; /* exit code of pending quit message */
126 int cursor_count; /* per-queue cursor show count */
127 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
128 struct list send_result; /* stack of sent messages waiting for result */
129 struct list callback_result; /* list of callback messages waiting for result */
130 struct message_result *recv_result; /* stack of received messages waiting for result */
131 struct list pending_timers; /* list of pending timers */
132 struct list expired_timers; /* list of expired timers */
133 lparam_t next_timer_id; /* id for the next timer with a 0 window */
134 struct timeout_user *timeout; /* timeout for next timer to expire */
135 struct thread_input *input; /* thread input descriptor */
136 struct hook_table *hooks; /* hook table */
137 timeout_t last_get_msg; /* time of last get message call */
140 struct hotkey
142 struct list entry; /* entry in desktop hotkey list */
143 struct msg_queue *queue; /* queue owning this hotkey */
144 user_handle_t win; /* window handle */
145 int id; /* hotkey id */
146 unsigned int vkey; /* virtual key code */
147 unsigned int flags; /* key modifiers */
150 static void msg_queue_dump( struct object *obj, int verbose );
151 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
152 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
153 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
154 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
155 static void msg_queue_destroy( struct object *obj );
156 static void msg_queue_poll_event( struct fd *fd, int event );
157 static void thread_input_dump( struct object *obj, int verbose );
158 static void thread_input_destroy( struct object *obj );
159 static void timer_callback( void *private );
161 static const struct object_ops msg_queue_ops =
163 sizeof(struct msg_queue), /* size */
164 msg_queue_dump, /* dump */
165 no_get_type, /* get_type */
166 msg_queue_add_queue, /* add_queue */
167 msg_queue_remove_queue, /* remove_queue */
168 msg_queue_signaled, /* signaled */
169 msg_queue_satisfied, /* satisfied */
170 no_signal, /* signal */
171 no_get_fd, /* get_fd */
172 no_map_access, /* map_access */
173 default_get_sd, /* get_sd */
174 default_set_sd, /* set_sd */
175 no_lookup_name, /* lookup_name */
176 no_open_file, /* open_file */
177 no_close_handle, /* close_handle */
178 msg_queue_destroy /* destroy */
181 static const struct fd_ops msg_queue_fd_ops =
183 NULL, /* get_poll_events */
184 msg_queue_poll_event, /* poll_event */
185 NULL, /* flush */
186 NULL, /* get_fd_type */
187 NULL, /* ioctl */
188 NULL, /* queue_async */
189 NULL, /* reselect_async */
190 NULL /* cancel async */
194 static const struct object_ops thread_input_ops =
196 sizeof(struct thread_input), /* size */
197 thread_input_dump, /* dump */
198 no_get_type, /* get_type */
199 no_add_queue, /* add_queue */
200 NULL, /* remove_queue */
201 NULL, /* signaled */
202 NULL, /* satisfied */
203 no_signal, /* signal */
204 no_get_fd, /* get_fd */
205 no_map_access, /* map_access */
206 default_get_sd, /* get_sd */
207 default_set_sd, /* set_sd */
208 no_lookup_name, /* lookup_name */
209 no_open_file, /* open_file */
210 no_close_handle, /* close_handle */
211 thread_input_destroy /* destroy */
214 /* pointer to input structure of foreground thread */
215 static unsigned int last_input_time;
217 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
218 static void free_message( struct message *msg );
220 /* set the caret window in a given thread input */
221 static void set_caret_window( struct thread_input *input, user_handle_t win )
223 if (!win || win != input->caret)
225 input->caret_rect.left = 0;
226 input->caret_rect.top = 0;
227 input->caret_rect.right = 0;
228 input->caret_rect.bottom = 0;
230 input->caret = win;
231 input->caret_hide = 1;
232 input->caret_state = 0;
235 /* create a thread input object */
236 static struct thread_input *create_thread_input( struct thread *thread )
238 struct thread_input *input;
240 if ((input = alloc_object( &thread_input_ops )))
242 input->focus = 0;
243 input->capture = 0;
244 input->active = 0;
245 input->menu_owner = 0;
246 input->move_size = 0;
247 input->cursor = 0;
248 input->cursor_count = 0;
249 list_init( &input->msg_list );
250 set_caret_window( input, 0 );
251 memset( input->keystate, 0, sizeof(input->keystate) );
253 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
255 release_object( input );
256 return NULL;
259 return input;
262 /* create a message queue object */
263 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
265 struct thread_input *new_input = NULL;
266 struct msg_queue *queue;
267 int i;
269 if (!input)
271 if (!(new_input = create_thread_input( thread ))) return NULL;
272 input = new_input;
275 if ((queue = alloc_object( &msg_queue_ops )))
277 queue->fd = NULL;
278 queue->wake_bits = 0;
279 queue->wake_mask = 0;
280 queue->changed_bits = 0;
281 queue->changed_mask = 0;
282 queue->paint_count = 0;
283 queue->hotkey_count = 0;
284 queue->quit_message = 0;
285 queue->cursor_count = 0;
286 queue->recv_result = NULL;
287 queue->next_timer_id = 0x7fff;
288 queue->timeout = NULL;
289 queue->input = (struct thread_input *)grab_object( input );
290 queue->hooks = NULL;
291 queue->last_get_msg = current_time;
292 list_init( &queue->send_result );
293 list_init( &queue->callback_result );
294 list_init( &queue->pending_timers );
295 list_init( &queue->expired_timers );
296 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
298 thread->queue = queue;
300 if (new_input) release_object( new_input );
301 return queue;
304 /* free the message queue of a thread at thread exit */
305 void free_msg_queue( struct thread *thread )
307 remove_thread_hooks( thread );
308 if (!thread->queue) return;
309 release_object( thread->queue );
310 thread->queue = NULL;
313 /* change the thread input data of a given thread */
314 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
316 struct msg_queue *queue = thread->queue;
318 if (!queue)
320 thread->queue = create_msg_queue( thread, new_input );
321 return thread->queue != NULL;
323 if (queue->input)
325 queue->input->cursor_count -= queue->cursor_count;
326 release_object( queue->input );
328 queue->input = (struct thread_input *)grab_object( new_input );
329 new_input->cursor_count += queue->cursor_count;
330 return 1;
333 /* set the cursor position and queue the corresponding mouse message */
334 static void set_cursor_pos( struct desktop *desktop, int x, int y )
336 struct hardware_msg_data *msg_data;
337 struct message *msg;
339 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
340 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
342 free( msg );
343 return;
345 memset( msg_data, 0, sizeof(*msg_data) );
347 msg->type = MSG_HARDWARE;
348 msg->win = 0;
349 msg->msg = WM_MOUSEMOVE;
350 msg->wparam = 0;
351 msg->lparam = 0;
352 msg->time = get_tick_count();
353 msg->result = NULL;
354 msg->data = msg_data;
355 msg->data_size = sizeof(*msg_data);
356 msg_data->x = x;
357 msg_data->y = y;
358 queue_hardware_message( desktop, msg, 1 );
361 /* set the cursor clip rectangle */
362 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect )
364 rectangle_t top_rect;
365 int x, y;
367 get_top_window_rectangle( desktop, &top_rect );
368 if (rect)
370 rectangle_t new_rect = *rect;
371 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
372 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
373 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
374 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
375 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
376 desktop->cursor.clip = new_rect;
378 else desktop->cursor.clip = top_rect;
380 if (desktop->cursor.clip_msg)
381 post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
383 /* warp the mouse to be inside the clip rect */
384 x = min( max( desktop->cursor.x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
385 y = min( max( desktop->cursor.y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
386 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
389 /* change the foreground input and reset the cursor clip rect */
390 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
392 if (desktop->foreground_input == input) return;
393 set_clip_rectangle( desktop, NULL );
394 desktop->foreground_input = input;
397 /* get the hook table for a given thread */
398 struct hook_table *get_queue_hooks( struct thread *thread )
400 if (!thread->queue) return NULL;
401 return thread->queue->hooks;
404 /* set the hook table for a given thread, allocating the queue if needed */
405 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
407 struct msg_queue *queue = thread->queue;
408 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
409 if (queue->hooks) release_object( queue->hooks );
410 queue->hooks = hooks;
413 /* check the queue status */
414 static inline int is_signaled( struct msg_queue *queue )
416 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
419 /* set some queue bits */
420 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
422 queue->wake_bits |= bits;
423 queue->changed_bits |= bits;
424 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
427 /* clear some queue bits */
428 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
430 queue->wake_bits &= ~bits;
431 queue->changed_bits &= ~bits;
434 /* check whether msg is a keyboard message */
435 static inline int is_keyboard_msg( struct message *msg )
437 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
440 /* check if message is matched by the filter */
441 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
443 return (msg >= first && msg <= last);
446 /* check whether a message filter contains at least one potential hardware message */
447 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
449 /* hardware message ranges are (in numerical order):
450 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
451 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
452 * WM_MOUSEFIRST .. WM_MOUSELAST
454 if (last < WM_NCMOUSEFIRST) return 0;
455 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
456 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
457 if (first > WM_MOUSELAST) return 0;
458 return 1;
461 /* get the QS_* bit corresponding to a given hardware message */
462 static inline int get_hardware_msg_bit( struct message *msg )
464 if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
465 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
466 if (is_keyboard_msg( msg )) return QS_KEY;
467 return QS_MOUSEBUTTON;
470 /* get the current thread queue, creating it if needed */
471 static inline struct msg_queue *get_current_queue(void)
473 struct msg_queue *queue = current->queue;
474 if (!queue) queue = create_msg_queue( current, NULL );
475 return queue;
478 /* get a (pseudo-)unique id to tag hardware messages */
479 static inline unsigned int get_unique_id(void)
481 static unsigned int id;
482 if (!++id) id = 1; /* avoid an id of 0 */
483 return id;
486 /* try to merge a message with the last in the list; return 1 if successful */
487 static int merge_message( struct thread_input *input, const struct message *msg )
489 struct message *prev;
490 struct list *ptr;
492 if (msg->msg != WM_MOUSEMOVE) return 0;
493 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
495 prev = LIST_ENTRY( ptr, struct message, entry );
496 if (prev->msg != WM_INPUT) break;
498 if (!ptr) return 0;
499 if (prev->result) return 0;
500 if (prev->win && msg->win && prev->win != msg->win) return 0;
501 if (prev->msg != msg->msg) return 0;
502 if (prev->type != msg->type) return 0;
503 /* now we can merge it */
504 prev->wparam = msg->wparam;
505 prev->lparam = msg->lparam;
506 prev->time = msg->time;
507 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
509 struct hardware_msg_data *prev_data = prev->data;
510 struct hardware_msg_data *msg_data = msg->data;
511 prev_data->x = msg_data->x;
512 prev_data->y = msg_data->y;
513 prev_data->info = msg_data->info;
515 list_remove( ptr );
516 list_add_tail( &input->msg_list, ptr );
517 return 1;
520 /* free a result structure */
521 static void free_result( struct message_result *result )
523 if (result->timeout) remove_timeout_user( result->timeout );
524 free( result->data );
525 if (result->callback_msg) free_message( result->callback_msg );
526 if (result->hardware_msg) free_message( result->hardware_msg );
527 if (result->desktop) release_object( result->desktop );
528 free( result );
531 /* remove the result from the sender list it is on */
532 static inline void remove_result_from_sender( struct message_result *result )
534 assert( result->sender );
536 list_remove( &result->sender_entry );
537 result->sender = NULL;
538 if (!result->receiver) free_result( result );
541 /* store the message result in the appropriate structure */
542 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
544 res->result = result;
545 res->error = error;
546 res->replied = 1;
547 if (res->timeout)
549 remove_timeout_user( res->timeout );
550 res->timeout = NULL;
553 if (res->hardware_msg)
555 if (!error && result) /* rejected by the hook */
556 free_message( res->hardware_msg );
557 else
558 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
560 res->hardware_msg = NULL;
563 if (res->sender)
565 if (res->callback_msg)
567 /* queue the callback message in the sender queue */
568 struct callback_msg_data *data = res->callback_msg->data;
569 data->result = result;
570 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
571 set_queue_bits( res->sender, QS_SENDMESSAGE );
572 res->callback_msg = NULL;
573 remove_result_from_sender( res );
575 else
577 /* wake sender queue if waiting on this result */
578 if (list_head(&res->sender->send_result) == &res->sender_entry)
579 set_queue_bits( res->sender, QS_SMRESULT );
582 else if (!res->receiver) free_result( res );
585 /* free a message when deleting a queue or window */
586 static void free_message( struct message *msg )
588 struct message_result *result = msg->result;
589 if (result)
591 result->msg = NULL;
592 result->receiver = NULL;
593 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
595 free( msg->data );
596 free( msg );
599 /* remove (and free) a message from a message list */
600 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
601 enum message_kind kind )
603 list_remove( &msg->entry );
604 switch(kind)
606 case SEND_MESSAGE:
607 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
608 break;
609 case POST_MESSAGE:
610 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
611 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
612 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
613 clear_queue_bits( queue, QS_HOTKEY );
614 break;
616 free_message( msg );
619 /* message timed out without getting a reply */
620 static void result_timeout( void *private )
622 struct message_result *result = private;
624 assert( !result->replied );
626 result->timeout = NULL;
628 if (result->msg) /* not received yet */
630 struct message *msg = result->msg;
632 result->msg = NULL;
633 msg->result = NULL;
634 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
635 result->receiver = NULL;
637 store_message_result( result, 0, STATUS_TIMEOUT );
640 /* allocate and fill a message result structure */
641 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
642 struct msg_queue *recv_queue,
643 struct message *msg, timeout_t timeout )
645 struct message_result *result = mem_alloc( sizeof(*result) );
646 if (result)
648 result->msg = msg;
649 result->sender = send_queue;
650 result->receiver = recv_queue;
651 result->replied = 0;
652 result->data = NULL;
653 result->data_size = 0;
654 result->timeout = NULL;
655 result->hardware_msg = NULL;
656 result->desktop = NULL;
657 result->callback_msg = NULL;
659 if (msg->type == MSG_CALLBACK)
661 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
663 if (!callback_msg)
665 free( result );
666 return NULL;
668 callback_msg->type = MSG_CALLBACK_RESULT;
669 callback_msg->win = msg->win;
670 callback_msg->msg = msg->msg;
671 callback_msg->wparam = 0;
672 callback_msg->lparam = 0;
673 callback_msg->time = get_tick_count();
674 callback_msg->result = NULL;
675 /* steal the data from the original message */
676 callback_msg->data = msg->data;
677 callback_msg->data_size = msg->data_size;
678 msg->data = NULL;
679 msg->data_size = 0;
681 result->callback_msg = callback_msg;
682 list_add_head( &send_queue->callback_result, &result->sender_entry );
684 else if (send_queue) list_add_head( &send_queue->send_result, &result->sender_entry );
686 if (timeout != TIMEOUT_INFINITE)
687 result->timeout = add_timeout_user( timeout, result_timeout, result );
689 return result;
692 /* receive a message, removing it from the sent queue */
693 static void receive_message( struct msg_queue *queue, struct message *msg,
694 struct get_message_reply *reply )
696 struct message_result *result = msg->result;
698 reply->total = msg->data_size;
699 if (msg->data_size > get_reply_max_size())
701 set_error( STATUS_BUFFER_OVERFLOW );
702 return;
704 reply->type = msg->type;
705 reply->win = msg->win;
706 reply->msg = msg->msg;
707 reply->wparam = msg->wparam;
708 reply->lparam = msg->lparam;
709 reply->time = msg->time;
711 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
713 list_remove( &msg->entry );
714 /* put the result on the receiver result stack */
715 if (result)
717 result->msg = NULL;
718 result->recv_next = queue->recv_result;
719 queue->recv_result = result;
721 free( msg );
722 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
725 /* set the result of the current received message */
726 static void reply_message( struct msg_queue *queue, lparam_t result,
727 unsigned int error, int remove, const void *data, data_size_t len )
729 struct message_result *res = queue->recv_result;
731 if (remove)
733 queue->recv_result = res->recv_next;
734 res->receiver = NULL;
735 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
737 free_result( res );
738 return;
741 if (!res->replied)
743 if (len && (res->data = memdup( data, len ))) res->data_size = len;
744 store_message_result( res, result, error );
748 static int match_window( user_handle_t win, user_handle_t msg_win )
750 if (!win) return 1;
751 if (win == -1 || win == 1) return !msg_win;
752 if (msg_win == win) return 1;
753 return is_child_window( win, msg_win );
756 /* retrieve a posted message */
757 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
758 unsigned int first, unsigned int last, unsigned int flags,
759 struct get_message_reply *reply )
761 struct message *msg;
763 /* check against the filters */
764 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
766 if (!match_window( win, msg->win )) continue;
767 if (!check_msg_filter( msg->msg, first, last )) continue;
768 goto found; /* found one */
770 return 0;
772 /* return it to the app */
773 found:
774 reply->total = msg->data_size;
775 if (msg->data_size > get_reply_max_size())
777 set_error( STATUS_BUFFER_OVERFLOW );
778 return 1;
780 reply->type = msg->type;
781 reply->win = msg->win;
782 reply->msg = msg->msg;
783 reply->wparam = msg->wparam;
784 reply->lparam = msg->lparam;
785 reply->time = msg->time;
787 if (flags & PM_REMOVE)
789 if (msg->data)
791 set_reply_data_ptr( msg->data, msg->data_size );
792 msg->data = NULL;
793 msg->data_size = 0;
795 remove_queue_message( queue, msg, POST_MESSAGE );
797 else if (msg->data) set_reply_data( msg->data, msg->data_size );
799 return 1;
802 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
803 struct get_message_reply *reply )
805 if (queue->quit_message)
807 reply->total = 0;
808 reply->type = MSG_POSTED;
809 reply->win = 0;
810 reply->msg = WM_QUIT;
811 reply->wparam = queue->exit_code;
812 reply->lparam = 0;
813 reply->time = get_tick_count();
815 if (flags & PM_REMOVE)
817 queue->quit_message = 0;
818 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
819 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
821 return 1;
823 else
824 return 0;
827 /* empty a message list and free all the messages */
828 static void empty_msg_list( struct list *list )
830 struct list *ptr;
832 while ((ptr = list_head( list )) != NULL)
834 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
835 list_remove( &msg->entry );
836 free_message( msg );
840 /* cleanup all pending results when deleting a queue */
841 static void cleanup_results( struct msg_queue *queue )
843 struct list *entry;
845 while ((entry = list_head( &queue->send_result )) != NULL)
847 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
850 while ((entry = list_head( &queue->callback_result )) != NULL)
852 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
855 while (queue->recv_result)
856 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
859 /* check if the thread owning the queue is hung (not checking for messages) */
860 static int is_queue_hung( struct msg_queue *queue )
862 struct wait_queue_entry *entry;
864 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
865 return 0; /* less than 5 seconds since last get message -> not hung */
867 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
869 if (get_wait_queue_thread(entry)->queue == queue)
870 return 0; /* thread is waiting on queue -> not hung */
872 return 1;
875 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
877 struct msg_queue *queue = (struct msg_queue *)obj;
878 struct process *process = get_wait_queue_thread(entry)->process;
880 /* a thread can only wait on its own queue */
881 if (get_wait_queue_thread(entry)->queue != queue)
883 set_error( STATUS_ACCESS_DENIED );
884 return 0;
886 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
888 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
889 set_fd_events( queue->fd, POLLIN );
890 add_queue( obj, entry );
891 return 1;
894 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
896 struct msg_queue *queue = (struct msg_queue *)obj;
898 remove_queue( obj, entry );
899 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
900 set_fd_events( queue->fd, 0 );
903 static void msg_queue_dump( struct object *obj, int verbose )
905 struct msg_queue *queue = (struct msg_queue *)obj;
906 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
907 queue->wake_bits, queue->wake_mask );
910 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
912 struct msg_queue *queue = (struct msg_queue *)obj;
913 int ret = 0;
915 if (queue->fd)
917 if ((ret = check_fd_events( queue->fd, POLLIN )))
918 /* stop waiting on select() if we are signaled */
919 set_fd_events( queue->fd, 0 );
920 else if (!list_empty( &obj->wait_queue ))
921 /* restart waiting on poll() if we are no longer signaled */
922 set_fd_events( queue->fd, POLLIN );
924 return ret || is_signaled( queue );
927 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
929 struct msg_queue *queue = (struct msg_queue *)obj;
930 queue->wake_mask = 0;
931 queue->changed_mask = 0;
934 static void msg_queue_destroy( struct object *obj )
936 struct msg_queue *queue = (struct msg_queue *)obj;
937 struct list *ptr;
938 struct hotkey *hotkey, *hotkey2;
939 int i;
941 cleanup_results( queue );
942 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
944 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
946 if (hotkey->queue == queue)
948 list_remove( &hotkey->entry );
949 free( hotkey );
953 while ((ptr = list_head( &queue->pending_timers )))
955 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
956 list_remove( &timer->entry );
957 free( timer );
959 while ((ptr = list_head( &queue->expired_timers )))
961 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
962 list_remove( &timer->entry );
963 free( timer );
965 if (queue->timeout) remove_timeout_user( queue->timeout );
966 queue->input->cursor_count -= queue->cursor_count;
967 release_object( queue->input );
968 if (queue->hooks) release_object( queue->hooks );
969 if (queue->fd) release_object( queue->fd );
972 static void msg_queue_poll_event( struct fd *fd, int event )
974 struct msg_queue *queue = get_fd_user( fd );
975 assert( queue->obj.ops == &msg_queue_ops );
977 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
978 else set_fd_events( queue->fd, 0 );
979 wake_up( &queue->obj, 0 );
982 static void thread_input_dump( struct object *obj, int verbose )
984 struct thread_input *input = (struct thread_input *)obj;
985 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
986 input->focus, input->capture, input->active );
989 static void thread_input_destroy( struct object *obj )
991 struct thread_input *input = (struct thread_input *)obj;
993 empty_msg_list( &input->msg_list );
994 if (input->desktop)
996 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
997 release_object( input->desktop );
1001 /* fix the thread input data when a window is destroyed */
1002 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1004 struct thread_input *input = queue->input;
1006 if (window == input->focus) input->focus = 0;
1007 if (window == input->capture) input->capture = 0;
1008 if (window == input->active) input->active = 0;
1009 if (window == input->menu_owner) input->menu_owner = 0;
1010 if (window == input->move_size) input->move_size = 0;
1011 if (window == input->caret) set_caret_window( input, 0 );
1014 /* check if the specified window can be set in the input data of a given queue */
1015 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1017 struct thread *thread;
1018 int ret = 0;
1020 if (!window) return 1; /* we can always clear the data */
1022 if ((thread = get_window_thread( window )))
1024 ret = (queue->input == thread->queue->input);
1025 if (!ret) set_error( STATUS_ACCESS_DENIED );
1026 release_object( thread );
1028 else set_error( STATUS_INVALID_HANDLE );
1030 return ret;
1033 /* make sure the specified thread has a queue */
1034 int init_thread_queue( struct thread *thread )
1036 if (thread->queue) return 1;
1037 return (create_msg_queue( thread, NULL ) != NULL);
1040 /* attach two thread input data structures */
1041 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1043 struct desktop *desktop;
1044 struct thread_input *input;
1045 int ret;
1047 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1048 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1049 input = (struct thread_input *)grab_object( thread_to->queue->input );
1050 if (input->desktop != desktop)
1052 set_error( STATUS_ACCESS_DENIED );
1053 release_object( input );
1054 release_object( desktop );
1055 return 0;
1057 release_object( desktop );
1059 ret = assign_thread_input( thread_from, input );
1060 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1061 release_object( input );
1062 return ret;
1065 /* detach two thread input data structures */
1066 void detach_thread_input( struct thread *thread_from )
1068 struct thread_input *input;
1070 if ((input = create_thread_input( thread_from )))
1072 assign_thread_input( thread_from, input );
1073 release_object( input );
1078 /* set the next timer to expire */
1079 static void set_next_timer( struct msg_queue *queue )
1081 struct list *ptr;
1083 if (queue->timeout)
1085 remove_timeout_user( queue->timeout );
1086 queue->timeout = NULL;
1088 if ((ptr = list_head( &queue->pending_timers )))
1090 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1091 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1093 /* set/clear QS_TIMER bit */
1094 if (list_empty( &queue->expired_timers ))
1095 clear_queue_bits( queue, QS_TIMER );
1096 else
1097 set_queue_bits( queue, QS_TIMER );
1100 /* find a timer from its window and id */
1101 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1102 unsigned int msg, lparam_t id )
1104 struct list *ptr;
1106 /* we need to search both lists */
1108 LIST_FOR_EACH( ptr, &queue->pending_timers )
1110 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1111 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1113 LIST_FOR_EACH( ptr, &queue->expired_timers )
1115 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1116 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1118 return NULL;
1121 /* callback for the next timer expiration */
1122 static void timer_callback( void *private )
1124 struct msg_queue *queue = private;
1125 struct list *ptr;
1127 queue->timeout = NULL;
1128 /* move on to the next timer */
1129 ptr = list_head( &queue->pending_timers );
1130 list_remove( ptr );
1131 list_add_tail( &queue->expired_timers, ptr );
1132 set_next_timer( queue );
1135 /* link a timer at its rightful place in the queue list */
1136 static void link_timer( struct msg_queue *queue, struct timer *timer )
1138 struct list *ptr;
1140 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1142 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1143 if (t->when >= timer->when) break;
1145 list_add_before( ptr, &timer->entry );
1148 /* remove a timer from the queue timer list and free it */
1149 static void free_timer( struct msg_queue *queue, struct timer *timer )
1151 list_remove( &timer->entry );
1152 free( timer );
1153 set_next_timer( queue );
1156 /* restart an expired timer */
1157 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1159 list_remove( &timer->entry );
1160 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1161 link_timer( queue, timer );
1162 set_next_timer( queue );
1165 /* find an expired timer matching the filtering parameters */
1166 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1167 unsigned int get_first, unsigned int get_last,
1168 int remove )
1170 struct list *ptr;
1172 LIST_FOR_EACH( ptr, &queue->expired_timers )
1174 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1175 if (win && timer->win != win) continue;
1176 if (check_msg_filter( timer->msg, get_first, get_last ))
1178 if (remove) restart_timer( queue, timer );
1179 return timer;
1182 return NULL;
1185 /* add a timer */
1186 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1188 struct timer *timer = mem_alloc( sizeof(*timer) );
1189 if (timer)
1191 timer->rate = max( rate, 1 );
1192 timer->when = current_time + (timeout_t)timer->rate * 10000;
1193 link_timer( queue, timer );
1194 /* check if we replaced the next timer */
1195 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1197 return timer;
1200 /* change the input key state for a given key */
1201 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1203 if (down)
1205 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1206 keystate[key] |= down;
1208 else keystate[key] &= ~0x80;
1211 /* update the input key state for a keyboard message */
1212 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1213 const struct message *msg )
1215 unsigned char key;
1216 int down = 0;
1218 switch (msg->msg)
1220 case WM_LBUTTONDOWN:
1221 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1222 /* fall through */
1223 case WM_LBUTTONUP:
1224 set_input_key_state( keystate, VK_LBUTTON, down );
1225 break;
1226 case WM_MBUTTONDOWN:
1227 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1228 /* fall through */
1229 case WM_MBUTTONUP:
1230 set_input_key_state( keystate, VK_MBUTTON, down );
1231 break;
1232 case WM_RBUTTONDOWN:
1233 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1234 /* fall through */
1235 case WM_RBUTTONUP:
1236 set_input_key_state( keystate, VK_RBUTTON, down );
1237 break;
1238 case WM_XBUTTONDOWN:
1239 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1240 /* fall through */
1241 case WM_XBUTTONUP:
1242 if (msg->wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1243 else if (msg->wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1244 break;
1245 case WM_KEYDOWN:
1246 case WM_SYSKEYDOWN:
1247 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1248 /* fall through */
1249 case WM_KEYUP:
1250 case WM_SYSKEYUP:
1251 key = (unsigned char)msg->wparam;
1252 set_input_key_state( keystate, key, down );
1253 switch(key)
1255 case VK_LCONTROL:
1256 case VK_RCONTROL:
1257 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1258 set_input_key_state( keystate, VK_CONTROL, down );
1259 break;
1260 case VK_LMENU:
1261 case VK_RMENU:
1262 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1263 set_input_key_state( keystate, VK_MENU, down );
1264 break;
1265 case VK_LSHIFT:
1266 case VK_RSHIFT:
1267 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1268 set_input_key_state( keystate, VK_SHIFT, down );
1269 break;
1271 break;
1275 /* release the hardware message currently being processed by the given thread */
1276 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1277 int remove, user_handle_t new_win )
1279 struct thread_input *input = queue->input;
1280 struct message *msg;
1282 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1284 if (msg->unique_id == hw_id) break;
1286 if (&msg->entry == &input->msg_list) return; /* not found */
1288 /* clear the queue bit for that message */
1289 if (remove || new_win)
1291 struct message *other;
1292 int clr_bit;
1294 clr_bit = get_hardware_msg_bit( msg );
1295 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1297 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1299 clr_bit = 0;
1300 break;
1303 if (clr_bit) clear_queue_bits( queue, clr_bit );
1306 if (new_win) /* set the new window */
1308 struct thread *owner = get_window_thread( new_win );
1309 if (owner)
1311 msg->win = new_win;
1312 if (owner->queue->input != input)
1314 list_remove( &msg->entry );
1315 if (merge_message( owner->queue->input, msg ))
1317 free_message( msg );
1318 release_object( owner );
1319 return;
1321 list_add_tail( &owner->queue->input->msg_list, &msg->entry );
1323 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1324 remove = 0;
1325 release_object( owner );
1328 if (remove)
1330 update_input_key_state( input->desktop, input->keystate, msg );
1331 list_remove( &msg->entry );
1332 free_message( msg );
1336 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1338 struct hotkey *hotkey;
1339 unsigned int modifiers = 0;
1341 if (msg->msg != WM_KEYDOWN) return 0;
1343 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1344 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1345 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1346 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1348 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1350 if (hotkey->vkey != msg->wparam) continue;
1351 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1354 return 0;
1356 found:
1357 msg->type = MSG_POSTED;
1358 msg->win = hotkey->win;
1359 msg->msg = WM_HOTKEY;
1360 msg->wparam = hotkey->id;
1361 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1363 free( msg->data );
1364 msg->data = NULL;
1365 msg->data_size = 0;
1367 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1368 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1369 hotkey->queue->hotkey_count++;
1370 return 1;
1373 /* find the window that should receive a given hardware message */
1374 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1375 struct message *msg, unsigned int *msg_code )
1377 struct hardware_msg_data *data = msg->data;
1378 user_handle_t win = 0;
1380 *msg_code = msg->msg;
1381 if (msg->msg == WM_INPUT)
1383 if (!(win = msg->win) && input) win = input->focus;
1385 else if (is_keyboard_msg( msg ))
1387 if (input && !(win = input->focus))
1389 win = input->active;
1390 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1393 else /* mouse message */
1395 if (!input || !(win = input->capture))
1397 if (!(win = msg->win) || !is_window_visible( win ) || is_window_transparent( win ))
1398 win = window_from_point( desktop, data->x, data->y );
1401 return win;
1404 static struct rawinput_device_entry *find_rawinput_device( unsigned short usage_page, unsigned short usage )
1406 struct rawinput_device_entry *e;
1408 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
1410 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1411 return e;
1414 return NULL;
1417 static void update_rawinput_device(const struct rawinput_device *device)
1419 struct rawinput_device_entry *e;
1421 if (!(e = find_rawinput_device( device->usage_page, device->usage )))
1423 if (!(e = mem_alloc( sizeof(*e) ))) return;
1424 list_add_tail( &current->process->rawinput_devices, &e->entry );
1427 if (device->flags & RIDEV_REMOVE)
1429 list_remove( &e->entry );
1430 free( e );
1431 return;
1434 e->device = *device;
1435 e->device.target = get_user_full_handle( e->device.target );
1438 /* queue a hardware message into a given thread input */
1439 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1441 user_handle_t win;
1442 struct thread *thread;
1443 struct thread_input *input;
1444 unsigned int msg_code;
1445 struct hardware_msg_data *data = msg->data;
1447 update_input_key_state( desktop, desktop->keystate, msg );
1448 last_input_time = get_tick_count();
1449 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1451 if (is_keyboard_msg( msg ))
1453 if (queue_hotkey_message( desktop, msg )) return;
1454 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1455 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1456 msg->lparam &= ~(KF_EXTENDED << 16);
1458 else if (msg->msg != WM_INPUT)
1460 if (msg->msg == WM_MOUSEMOVE)
1462 int x = min( max( data->x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
1463 int y = min( max( data->y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
1464 if (desktop->cursor.x != x || desktop->cursor.y != y) always_queue = 1;
1465 desktop->cursor.x = x;
1466 desktop->cursor.y = y;
1467 desktop->cursor.last_change = get_tick_count();
1469 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1470 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1471 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1472 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1473 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1474 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1475 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1477 data->x = desktop->cursor.x;
1478 data->y = desktop->cursor.y;
1480 if (msg->win && (thread = get_window_thread( msg->win )))
1482 input = thread->queue->input;
1483 release_object( thread );
1485 else input = desktop->foreground_input;
1487 win = find_hardware_message_window( desktop, input, msg, &msg_code );
1488 if (!win || !(thread = get_window_thread(win)))
1490 if (input) update_input_key_state( input->desktop, input->keystate, msg );
1491 free_message( msg );
1492 return;
1494 input = thread->queue->input;
1496 if (win != desktop->cursor.win) always_queue = 1;
1497 desktop->cursor.win = win;
1499 if (!always_queue || merge_message( input, msg )) free_message( msg );
1500 else
1502 msg->unique_id = 0; /* will be set once we return it to the app */
1503 list_add_tail( &input->msg_list, &msg->entry );
1504 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1506 release_object( thread );
1509 /* send the low-level hook message for a given hardware message */
1510 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1511 const hw_input_t *input, struct msg_queue *sender )
1513 struct thread *hook_thread;
1514 struct msg_queue *queue;
1515 struct message *msg;
1516 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1517 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1519 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1520 if (!(queue = hook_thread->queue)) return 0;
1521 if (is_queue_hung( queue )) return 0;
1523 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1525 msg->type = MSG_HOOK_LL;
1526 msg->win = 0;
1527 msg->msg = id;
1528 msg->wparam = hardware_msg->msg;
1529 msg->time = hardware_msg->time;
1530 msg->data_size = hardware_msg->data_size;
1531 msg->result = NULL;
1533 if (input->type == INPUT_KEYBOARD)
1535 unsigned short vkey = input->kbd.vkey;
1536 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1537 msg->lparam = (input->kbd.scan << 16) | vkey;
1539 else msg->lparam = input->mouse.data << 16;
1541 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1542 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1544 free_message( msg );
1545 return 0;
1547 msg->result->hardware_msg = hardware_msg;
1548 msg->result->desktop = (struct desktop *)grab_object( desktop );
1549 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1550 set_queue_bits( queue, QS_SENDMESSAGE );
1551 return 1;
1554 /* queue a hardware message for a mouse event */
1555 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1556 unsigned int hook_flags, struct msg_queue *sender )
1558 const struct rawinput_device *device;
1559 struct hardware_msg_data *msg_data;
1560 struct message *msg;
1561 unsigned int i, time, flags;
1562 int wait = 0, x, y;
1564 static const unsigned int messages[] =
1566 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1567 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1568 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1569 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1570 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1571 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1572 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1573 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1574 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1575 0, /* 0x0200 = unused */
1576 0, /* 0x0400 = unused */
1577 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1578 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1581 desktop->cursor.last_change = get_tick_count();
1582 flags = input->mouse.flags;
1583 time = input->mouse.time;
1584 if (!time) time = desktop->cursor.last_change;
1586 if (flags & MOUSEEVENTF_MOVE)
1588 if (flags & MOUSEEVENTF_ABSOLUTE)
1590 x = input->mouse.x;
1591 y = input->mouse.y;
1592 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1593 x == desktop->cursor.x && y == desktop->cursor.y)
1594 flags &= ~MOUSEEVENTF_MOVE;
1596 else
1598 x = desktop->cursor.x + input->mouse.x;
1599 y = desktop->cursor.y + input->mouse.y;
1602 else
1604 x = desktop->cursor.x;
1605 y = desktop->cursor.y;
1608 if ((device = current->process->rawinput_mouse))
1610 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1611 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1613 free( msg );
1614 return 0;
1617 msg->type = MSG_HARDWARE;
1618 msg->win = device->target;
1619 msg->msg = WM_INPUT;
1620 msg->wparam = RIM_INPUT;
1621 msg->lparam = 0;
1622 msg->time = time;
1623 msg->data = msg_data;
1624 msg->data_size = sizeof(*msg_data);
1625 msg->result = NULL;
1627 msg_data->info = input->mouse.info;
1628 msg_data->flags = flags;
1629 msg_data->rawinput.type = RIM_TYPEMOUSE;
1630 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1631 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1632 msg_data->rawinput.mouse.data = input->mouse.data;
1634 queue_hardware_message( desktop, msg, 0 );
1637 for (i = 0; i < sizeof(messages)/sizeof(messages[0]); i++)
1639 if (!messages[i]) continue;
1640 if (!(flags & (1 << i))) continue;
1641 flags &= ~(1 << i);
1643 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1644 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1646 free( msg );
1647 return 0;
1649 memset( msg_data, 0, sizeof(*msg_data) );
1651 msg->type = MSG_HARDWARE;
1652 msg->win = get_user_full_handle( win );
1653 msg->msg = messages[i];
1654 msg->wparam = input->mouse.data << 16;
1655 msg->lparam = 0;
1656 msg->time = time;
1657 msg->result = NULL;
1658 msg->data = msg_data;
1659 msg->data_size = sizeof(*msg_data);
1660 msg_data->x = x;
1661 msg_data->y = y;
1662 msg_data->info = input->mouse.info;
1663 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLMHF_INJECTED;
1665 /* specify a sender only when sending the last message */
1666 if (!(flags & ((1 << sizeof(messages)/sizeof(messages[0])) - 1)))
1668 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1669 queue_hardware_message( desktop, msg, 0 );
1671 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1672 queue_hardware_message( desktop, msg, 0 );
1674 return wait;
1677 /* queue a hardware message for a keyboard event */
1678 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1679 unsigned int hook_flags, struct msg_queue *sender )
1681 const struct rawinput_device *device;
1682 struct hardware_msg_data *msg_data;
1683 struct message *msg;
1684 unsigned char vkey = input->kbd.vkey;
1685 unsigned int message_code, time;
1686 int wait;
1688 if (!(time = input->kbd.time)) time = get_tick_count();
1690 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1692 switch (vkey)
1694 case VK_MENU:
1695 case VK_LMENU:
1696 case VK_RMENU:
1697 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1698 break;
1699 case VK_CONTROL:
1700 case VK_LCONTROL:
1701 case VK_RCONTROL:
1702 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1703 break;
1704 case VK_SHIFT:
1705 case VK_LSHIFT:
1706 case VK_RSHIFT:
1707 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1708 break;
1712 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1713 switch (vkey)
1715 case VK_LMENU:
1716 case VK_RMENU:
1717 if (input->kbd.flags & KEYEVENTF_KEYUP)
1719 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1720 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1721 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1722 message_code = WM_SYSKEYUP;
1723 desktop->keystate[VK_MENU] &= ~0x02;
1725 else
1727 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1728 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1729 message_code = WM_SYSKEYDOWN;
1730 desktop->keystate[VK_MENU] |= 0x02;
1732 break;
1734 case VK_LCONTROL:
1735 case VK_RCONTROL:
1736 /* send WM_SYSKEYUP on release if Alt still pressed */
1737 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1738 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1739 message_code = WM_SYSKEYUP;
1740 desktop->keystate[VK_MENU] &= ~0x02;
1741 break;
1743 default:
1744 /* send WM_SYSKEY for Alt-anykey and for F10 */
1745 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1746 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1747 /* fall through */
1748 case VK_F10:
1749 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1750 desktop->keystate[VK_MENU] &= ~0x02;
1751 break;
1754 if ((device = current->process->rawinput_kbd))
1756 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1757 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1759 free( msg );
1760 return 0;
1763 msg->type = MSG_HARDWARE;
1764 msg->win = device->target;
1765 msg->msg = WM_INPUT;
1766 msg->wparam = RIM_INPUT;
1767 msg->lparam = 0;
1768 msg->time = time;
1769 msg->data = msg_data;
1770 msg->data_size = sizeof(*msg_data);
1771 msg->result = NULL;
1773 msg_data->info = input->kbd.info;
1774 msg_data->flags = input->kbd.flags;
1775 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1776 msg_data->rawinput.kbd.message = message_code;
1777 msg_data->rawinput.kbd.vkey = vkey;
1778 msg_data->rawinput.kbd.scan = input->kbd.scan;
1780 queue_hardware_message( desktop, msg, 0 );
1783 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1784 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1786 free( msg );
1787 return 0;
1789 memset( msg_data, 0, sizeof(*msg_data) );
1791 msg->type = MSG_HARDWARE;
1792 msg->win = get_user_full_handle( win );
1793 msg->msg = message_code;
1794 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1795 msg->time = time;
1796 msg->result = NULL;
1797 msg->data = msg_data;
1798 msg->data_size = sizeof(*msg_data);
1799 msg_data->info = input->kbd.info;
1800 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLKHF_INJECTED;
1802 if (input->kbd.flags & KEYEVENTF_UNICODE)
1804 msg->wparam = VK_PACKET;
1806 else
1808 unsigned int flags = 0;
1809 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1810 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1811 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1812 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1814 msg->wparam = vkey;
1815 msg->lparam |= flags << 16;
1816 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1819 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1820 queue_hardware_message( desktop, msg, 1 );
1822 return wait;
1825 /* queue a hardware message for a custom type of event */
1826 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1827 const hw_input_t *input )
1829 struct hardware_msg_data *msg_data;
1830 struct message *msg;
1832 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
1833 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1835 free( msg );
1836 return;
1838 memset( msg_data, 0, sizeof(*msg_data) );
1840 msg->type = MSG_HARDWARE;
1841 msg->win = get_user_full_handle( win );
1842 msg->msg = input->hw.msg;
1843 msg->wparam = 0;
1844 msg->lparam = input->hw.lparam;
1845 msg->time = get_tick_count();
1846 msg->result = NULL;
1847 msg->data = msg_data;
1848 msg->data_size = sizeof(*msg_data);
1850 queue_hardware_message( desktop, msg, 1 );
1853 /* check message filter for a hardware message */
1854 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1855 user_handle_t filter_win, unsigned int first, unsigned int last )
1857 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1859 /* we can only test the window for a keyboard message since the
1860 * dest window for a mouse message depends on hittest */
1861 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1862 return 0;
1863 /* the message code is final for a keyboard message, we can simply check it */
1864 return check_msg_filter( msg_code, first, last );
1866 else /* mouse message */
1868 /* we need to check all possible values that the message can have in the end */
1870 if (check_msg_filter( msg_code, first, last )) return 1;
1871 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1873 /* all other messages can become non-client messages */
1874 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1876 /* clicks can become double-clicks or non-client double-clicks */
1877 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1878 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1880 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1881 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1883 return 0;
1888 /* find a hardware message for the given queue */
1889 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1890 unsigned int first, unsigned int last, unsigned int flags,
1891 struct get_message_reply *reply )
1893 struct thread_input *input = thread->queue->input;
1894 struct thread *win_thread;
1895 struct list *ptr;
1896 user_handle_t win;
1897 int clear_bits, got_one = 0;
1898 unsigned int msg_code;
1900 ptr = list_head( &input->msg_list );
1901 if (hw_id)
1903 while (ptr)
1905 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1906 if (msg->unique_id == hw_id) break;
1907 ptr = list_next( &input->msg_list, ptr );
1909 if (!ptr) ptr = list_head( &input->msg_list );
1910 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1913 if (ptr == list_head( &input->msg_list ))
1914 clear_bits = QS_INPUT;
1915 else
1916 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1918 while (ptr)
1920 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1921 struct hardware_msg_data *data = msg->data;
1923 ptr = list_next( &input->msg_list, ptr );
1924 win = find_hardware_message_window( input->desktop, input, msg, &msg_code );
1925 if (!win || !(win_thread = get_window_thread( win )))
1927 /* no window at all, remove it */
1928 update_input_key_state( input->desktop, input->keystate, msg );
1929 list_remove( &msg->entry );
1930 free_message( msg );
1931 continue;
1933 if (win_thread != thread)
1935 if (win_thread->queue->input == input)
1937 /* wake the other thread */
1938 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1939 got_one = 1;
1941 else
1943 /* for another thread input, drop it */
1944 update_input_key_state( input->desktop, input->keystate, msg );
1945 list_remove( &msg->entry );
1946 free_message( msg );
1948 release_object( win_thread );
1949 continue;
1951 release_object( win_thread );
1953 /* if we already got a message for another thread, or if it doesn't
1954 * match the filter we skip it */
1955 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1957 clear_bits &= ~get_hardware_msg_bit( msg );
1958 continue;
1960 /* now we can return it */
1961 if (!msg->unique_id) msg->unique_id = get_unique_id();
1962 reply->type = MSG_HARDWARE;
1963 reply->win = win;
1964 reply->msg = msg_code;
1965 reply->wparam = msg->wparam;
1966 reply->lparam = msg->lparam;
1967 reply->time = msg->time;
1969 data->hw_id = msg->unique_id;
1970 set_reply_data( msg->data, msg->data_size );
1971 if (msg->msg == WM_INPUT && (flags & PM_REMOVE))
1972 release_hardware_message( current->queue, data->hw_id, 1, 0 );
1973 return 1;
1975 /* nothing found, clear the hardware queue bits */
1976 clear_queue_bits( thread->queue, clear_bits );
1977 return 0;
1980 /* increment (or decrement if 'incr' is negative) the queue paint count */
1981 void inc_queue_paint_count( struct thread *thread, int incr )
1983 struct msg_queue *queue = thread->queue;
1985 assert( queue );
1987 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1989 if (queue->paint_count)
1990 set_queue_bits( queue, QS_PAINT );
1991 else
1992 clear_queue_bits( queue, QS_PAINT );
1996 /* remove all messages and timers belonging to a certain window */
1997 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1999 struct msg_queue *queue = thread->queue;
2000 struct list *ptr;
2001 int i;
2003 if (!queue) return;
2005 /* remove timers */
2007 ptr = list_head( &queue->pending_timers );
2008 while (ptr)
2010 struct list *next = list_next( &queue->pending_timers, ptr );
2011 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2012 if (timer->win == win) free_timer( queue, timer );
2013 ptr = next;
2015 ptr = list_head( &queue->expired_timers );
2016 while (ptr)
2018 struct list *next = list_next( &queue->expired_timers, ptr );
2019 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2020 if (timer->win == win) free_timer( queue, timer );
2021 ptr = next;
2024 /* remove messages */
2025 for (i = 0; i < NB_MSG_KINDS; i++)
2027 struct list *ptr, *next;
2029 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2031 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2032 if (msg->win == win)
2034 if (msg->msg == WM_QUIT && !queue->quit_message)
2036 queue->quit_message = 1;
2037 queue->exit_code = msg->wparam;
2039 remove_queue_message( queue, msg, i );
2044 thread_input_cleanup_window( queue, win );
2047 /* post a message to a window; used by socket handling */
2048 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2050 struct message *msg;
2051 struct thread *thread = get_window_thread( win );
2053 if (!thread) return;
2055 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2057 msg->type = MSG_POSTED;
2058 msg->win = get_user_full_handle( win );
2059 msg->msg = message;
2060 msg->wparam = wparam;
2061 msg->lparam = lparam;
2062 msg->time = get_tick_count();
2063 msg->result = NULL;
2064 msg->data = NULL;
2065 msg->data_size = 0;
2067 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2068 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2069 if (message == WM_HOTKEY)
2071 set_queue_bits( thread->queue, QS_HOTKEY );
2072 thread->queue->hotkey_count++;
2075 release_object( thread );
2078 /* post a win event */
2079 void post_win_event( struct thread *thread, unsigned int event,
2080 user_handle_t win, unsigned int object_id,
2081 unsigned int child_id, client_ptr_t hook_proc,
2082 const WCHAR *module, data_size_t module_size,
2083 user_handle_t hook)
2085 struct message *msg;
2087 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2089 struct winevent_msg_data *data;
2091 msg->type = MSG_WINEVENT;
2092 msg->win = get_user_full_handle( win );
2093 msg->msg = event;
2094 msg->wparam = object_id;
2095 msg->lparam = child_id;
2096 msg->time = get_tick_count();
2097 msg->result = NULL;
2099 if ((data = malloc( sizeof(*data) + module_size )))
2101 data->hook = hook;
2102 data->tid = get_thread_id( current );
2103 data->hook_proc = hook_proc;
2104 memcpy( data + 1, module, module_size );
2106 msg->data = data;
2107 msg->data_size = sizeof(*data) + module_size;
2109 if (debug_level > 1)
2110 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2111 get_thread_id(thread), event, win, object_id, child_id );
2112 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2113 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2115 else
2116 free( msg );
2120 /* free all hotkeys on a desktop, optionally filtering by window */
2121 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2123 struct hotkey *hotkey, *hotkey2;
2125 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2127 if (!window || hotkey->win == window)
2129 list_remove( &hotkey->entry );
2130 free( hotkey );
2136 /* check if the thread owning the window is hung */
2137 DECL_HANDLER(is_window_hung)
2139 struct thread *thread;
2141 thread = get_window_thread( req->win );
2142 if (thread)
2144 reply->is_hung = is_queue_hung( thread->queue );
2145 release_object( thread );
2147 else reply->is_hung = 0;
2151 /* get the message queue of the current thread */
2152 DECL_HANDLER(get_msg_queue)
2154 struct msg_queue *queue = get_current_queue();
2156 reply->handle = 0;
2157 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2161 /* set the file descriptor associated to the current thread queue */
2162 DECL_HANDLER(set_queue_fd)
2164 struct msg_queue *queue = get_current_queue();
2165 struct file *file;
2166 int unix_fd;
2168 if (queue->fd) /* fd can only be set once */
2170 set_error( STATUS_ACCESS_DENIED );
2171 return;
2173 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2175 if ((unix_fd = get_file_unix_fd( file )) != -1)
2177 if ((unix_fd = dup( unix_fd )) != -1)
2178 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2179 else
2180 file_set_error();
2182 release_object( file );
2186 /* set the current message queue wakeup mask */
2187 DECL_HANDLER(set_queue_mask)
2189 struct msg_queue *queue = get_current_queue();
2191 if (queue)
2193 queue->wake_mask = req->wake_mask;
2194 queue->changed_mask = req->changed_mask;
2195 reply->wake_bits = queue->wake_bits;
2196 reply->changed_bits = queue->changed_bits;
2197 if (is_signaled( queue ))
2199 /* if skip wait is set, do what would have been done in the subsequent wait */
2200 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2201 else wake_up( &queue->obj, 0 );
2207 /* get the current message queue status */
2208 DECL_HANDLER(get_queue_status)
2210 struct msg_queue *queue = current->queue;
2211 if (queue)
2213 reply->wake_bits = queue->wake_bits;
2214 reply->changed_bits = queue->changed_bits;
2215 if (req->clear) queue->changed_bits = 0;
2217 else reply->wake_bits = reply->changed_bits = 0;
2221 /* send a message to a thread queue */
2222 DECL_HANDLER(send_message)
2224 struct message *msg;
2225 struct msg_queue *send_queue = get_current_queue();
2226 struct msg_queue *recv_queue = NULL;
2227 struct thread *thread = NULL;
2229 if (!(thread = get_thread_from_id( req->id ))) return;
2231 if (!(recv_queue = thread->queue))
2233 set_error( STATUS_INVALID_PARAMETER );
2234 release_object( thread );
2235 return;
2237 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2239 set_error( STATUS_TIMEOUT );
2240 release_object( thread );
2241 return;
2244 if ((msg = mem_alloc( sizeof(*msg) )))
2246 msg->type = req->type;
2247 msg->win = get_user_full_handle( req->win );
2248 msg->msg = req->msg;
2249 msg->wparam = req->wparam;
2250 msg->lparam = req->lparam;
2251 msg->time = get_tick_count();
2252 msg->result = NULL;
2253 msg->data = NULL;
2254 msg->data_size = get_req_data_size();
2256 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2258 free( msg );
2259 release_object( thread );
2260 return;
2263 switch(msg->type)
2265 case MSG_OTHER_PROCESS:
2266 case MSG_ASCII:
2267 case MSG_UNICODE:
2268 case MSG_CALLBACK:
2269 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2271 free_message( msg );
2272 break;
2274 /* fall through */
2275 case MSG_NOTIFY:
2276 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2277 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2278 break;
2279 case MSG_POSTED:
2280 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2281 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2282 if (msg->msg == WM_HOTKEY)
2284 set_queue_bits( recv_queue, QS_HOTKEY );
2285 recv_queue->hotkey_count++;
2287 break;
2288 case MSG_HARDWARE: /* should use send_hardware_message instead */
2289 case MSG_CALLBACK_RESULT: /* cannot send this one */
2290 case MSG_HOOK_LL: /* generated internally */
2291 default:
2292 set_error( STATUS_INVALID_PARAMETER );
2293 free( msg );
2294 break;
2297 release_object( thread );
2300 /* send a hardware message to a thread queue */
2301 DECL_HANDLER(send_hardware_message)
2303 struct thread *thread = NULL;
2304 struct desktop *desktop;
2305 struct msg_queue *sender = get_current_queue();
2306 data_size_t size = min( 256, get_reply_max_size() );
2308 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2310 if (req->win)
2312 if (!(thread = get_window_thread( req->win ))) return;
2313 if (desktop != thread->queue->input->desktop)
2315 /* don't allow queuing events to a different desktop */
2316 release_object( desktop );
2317 return;
2321 reply->prev_x = desktop->cursor.x;
2322 reply->prev_y = desktop->cursor.y;
2324 switch (req->input.type)
2326 case INPUT_MOUSE:
2327 reply->wait = queue_mouse_message( desktop, req->win, &req->input, req->flags, sender );
2328 break;
2329 case INPUT_KEYBOARD:
2330 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, req->flags, sender );
2331 break;
2332 case INPUT_HARDWARE:
2333 queue_custom_hardware_message( desktop, req->win, &req->input );
2334 break;
2335 default:
2336 set_error( STATUS_INVALID_PARAMETER );
2338 if (thread) release_object( thread );
2340 reply->new_x = desktop->cursor.x;
2341 reply->new_y = desktop->cursor.y;
2342 set_reply_data( desktop->keystate, size );
2343 release_object( desktop );
2346 /* post a quit message to the current queue */
2347 DECL_HANDLER(post_quit_message)
2349 struct msg_queue *queue = get_current_queue();
2351 if (!queue)
2352 return;
2354 queue->quit_message = 1;
2355 queue->exit_code = req->exit_code;
2356 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2359 /* get a message from the current queue */
2360 DECL_HANDLER(get_message)
2362 struct timer *timer;
2363 struct list *ptr;
2364 struct msg_queue *queue = get_current_queue();
2365 user_handle_t get_win = get_user_full_handle( req->get_win );
2366 unsigned int filter = req->flags >> 16;
2368 reply->active_hooks = get_active_hooks();
2370 if (!queue) return;
2371 queue->last_get_msg = current_time;
2372 if (!filter) filter = QS_ALLINPUT;
2374 /* first check for sent messages */
2375 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2377 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2378 receive_message( queue, msg, reply );
2379 return;
2382 /* clear changed bits so we can wait on them if we don't find a message */
2383 if (filter & QS_POSTMESSAGE)
2385 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2386 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2388 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2389 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2391 /* then check for posted messages */
2392 if ((filter & QS_POSTMESSAGE) &&
2393 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2394 return;
2396 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2397 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2398 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2399 return;
2401 /* only check for quit messages if not posted messages pending.
2402 * note: the quit message isn't filtered */
2403 if (get_quit_message( queue, req->flags, reply ))
2404 return;
2406 /* then check for any raw hardware message */
2407 if ((filter & QS_INPUT) &&
2408 filter_contains_hw_range( req->get_first, req->get_last ) &&
2409 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2410 return;
2412 /* now check for WM_PAINT */
2413 if ((filter & QS_PAINT) &&
2414 queue->paint_count &&
2415 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2416 (reply->win = find_window_to_repaint( get_win, current )))
2418 reply->type = MSG_POSTED;
2419 reply->msg = WM_PAINT;
2420 reply->wparam = 0;
2421 reply->lparam = 0;
2422 reply->time = get_tick_count();
2423 return;
2426 /* now check for timer */
2427 if ((filter & QS_TIMER) &&
2428 (timer = find_expired_timer( queue, get_win, req->get_first,
2429 req->get_last, (req->flags & PM_REMOVE) )))
2431 reply->type = MSG_POSTED;
2432 reply->win = timer->win;
2433 reply->msg = timer->msg;
2434 reply->wparam = timer->id;
2435 reply->lparam = timer->lparam;
2436 reply->time = get_tick_count();
2437 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2438 set_event( current->process->idle_event );
2439 return;
2442 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2443 queue->wake_mask = req->wake_mask;
2444 queue->changed_mask = req->changed_mask;
2445 set_error( STATUS_PENDING ); /* FIXME */
2449 /* reply to a sent message */
2450 DECL_HANDLER(reply_message)
2452 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2453 else if (current->queue->recv_result)
2454 reply_message( current->queue, req->result, 0, req->remove,
2455 get_req_data(), get_req_data_size() );
2459 /* accept the current hardware message */
2460 DECL_HANDLER(accept_hardware_message)
2462 if (current->queue)
2463 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
2464 else
2465 set_error( STATUS_ACCESS_DENIED );
2469 /* retrieve the reply for the last message sent */
2470 DECL_HANDLER(get_message_reply)
2472 struct message_result *result;
2473 struct list *entry;
2474 struct msg_queue *queue = current->queue;
2476 if (queue)
2478 set_error( STATUS_PENDING );
2479 reply->result = 0;
2481 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2483 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2484 if (result->replied || req->cancel)
2486 if (result->replied)
2488 reply->result = result->result;
2489 set_error( result->error );
2490 if (result->data)
2492 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2493 set_reply_data_ptr( result->data, data_len );
2494 result->data = NULL;
2495 result->data_size = 0;
2498 remove_result_from_sender( result );
2500 entry = list_head( &queue->send_result );
2501 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2502 else
2504 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2505 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
2509 else set_error( STATUS_ACCESS_DENIED );
2513 /* set a window timer */
2514 DECL_HANDLER(set_win_timer)
2516 struct timer *timer;
2517 struct msg_queue *queue;
2518 struct thread *thread = NULL;
2519 user_handle_t win = 0;
2520 lparam_t id = req->id;
2522 if (req->win)
2524 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2526 set_error( STATUS_INVALID_HANDLE );
2527 return;
2529 if (thread->process != current->process)
2531 release_object( thread );
2532 set_error( STATUS_ACCESS_DENIED );
2533 return;
2535 queue = thread->queue;
2536 /* remove it if it existed already */
2537 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2539 else
2541 queue = get_current_queue();
2542 /* look for a timer with this id */
2543 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2545 /* free and reuse id */
2546 free_timer( queue, timer );
2548 else
2550 /* find a free id for it */
2553 id = queue->next_timer_id;
2554 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2556 while (find_timer( queue, 0, req->msg, id ));
2560 if ((timer = set_timer( queue, req->rate )))
2562 timer->win = win;
2563 timer->msg = req->msg;
2564 timer->id = id;
2565 timer->lparam = req->lparam;
2566 reply->id = id;
2568 if (thread) release_object( thread );
2571 /* kill a window timer */
2572 DECL_HANDLER(kill_win_timer)
2574 struct timer *timer;
2575 struct thread *thread;
2576 user_handle_t win = 0;
2578 if (req->win)
2580 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2582 set_error( STATUS_INVALID_HANDLE );
2583 return;
2585 if (thread->process != current->process)
2587 release_object( thread );
2588 set_error( STATUS_ACCESS_DENIED );
2589 return;
2592 else thread = (struct thread *)grab_object( current );
2594 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2595 free_timer( thread->queue, timer );
2596 else
2597 set_error( STATUS_INVALID_PARAMETER );
2599 release_object( thread );
2602 DECL_HANDLER(register_hotkey)
2604 struct desktop *desktop;
2605 user_handle_t win_handle = req->window;
2606 struct hotkey *hotkey;
2607 struct hotkey *new_hotkey = NULL;
2608 struct thread *thread;
2609 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2611 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2613 if (win_handle)
2615 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2617 release_object( desktop );
2618 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2619 return;
2622 thread = get_window_thread( win_handle );
2623 if (thread) release_object( thread );
2625 if (thread != current)
2627 release_object( desktop );
2628 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2629 return;
2633 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2635 if (req->vkey == hotkey->vkey &&
2636 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2638 release_object( desktop );
2639 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2640 return;
2642 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2643 new_hotkey = hotkey;
2646 if (new_hotkey)
2648 reply->replaced = 1;
2649 reply->flags = new_hotkey->flags;
2650 reply->vkey = new_hotkey->vkey;
2652 else
2654 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2655 if (new_hotkey)
2657 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2658 new_hotkey->queue = current->queue;
2659 new_hotkey->win = win_handle;
2660 new_hotkey->id = req->id;
2664 if (new_hotkey)
2666 new_hotkey->flags = req->flags;
2667 new_hotkey->vkey = req->vkey;
2670 release_object( desktop );
2673 DECL_HANDLER(unregister_hotkey)
2675 struct desktop *desktop;
2676 user_handle_t win_handle = req->window;
2677 struct hotkey *hotkey;
2678 struct thread *thread;
2680 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2682 if (win_handle)
2684 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2686 release_object( desktop );
2687 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2688 return;
2691 thread = get_window_thread( win_handle );
2692 if (thread) release_object( thread );
2694 if (thread != current)
2696 release_object( desktop );
2697 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2698 return;
2702 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2704 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2705 goto found;
2708 release_object( desktop );
2709 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2710 return;
2712 found:
2713 reply->flags = hotkey->flags;
2714 reply->vkey = hotkey->vkey;
2715 list_remove( &hotkey->entry );
2716 free( hotkey );
2717 release_object( desktop );
2720 /* attach (or detach) thread inputs */
2721 DECL_HANDLER(attach_thread_input)
2723 struct thread *thread_from = get_thread_from_id( req->tid_from );
2724 struct thread *thread_to = get_thread_from_id( req->tid_to );
2726 if (!thread_from || !thread_to)
2728 if (thread_from) release_object( thread_from );
2729 if (thread_to) release_object( thread_to );
2730 return;
2732 if (thread_from != thread_to)
2734 if (req->attach) attach_thread_input( thread_from, thread_to );
2735 else
2737 if (thread_from->queue && thread_to->queue &&
2738 thread_from->queue->input == thread_to->queue->input)
2739 detach_thread_input( thread_from );
2740 else
2741 set_error( STATUS_ACCESS_DENIED );
2744 else set_error( STATUS_ACCESS_DENIED );
2745 release_object( thread_from );
2746 release_object( thread_to );
2750 /* get thread input data */
2751 DECL_HANDLER(get_thread_input)
2753 struct thread *thread = NULL;
2754 struct desktop *desktop;
2755 struct thread_input *input;
2757 if (req->tid)
2759 if (!(thread = get_thread_from_id( req->tid ))) return;
2760 if (!(desktop = get_thread_desktop( thread, 0 )))
2762 release_object( thread );
2763 return;
2765 input = thread->queue ? thread->queue->input : NULL;
2767 else
2769 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2770 input = desktop->foreground_input; /* get the foreground thread info */
2773 if (input)
2775 reply->focus = input->focus;
2776 reply->capture = input->capture;
2777 reply->active = input->active;
2778 reply->menu_owner = input->menu_owner;
2779 reply->move_size = input->move_size;
2780 reply->caret = input->caret;
2781 reply->cursor = input->cursor;
2782 reply->show_count = input->cursor_count;
2783 reply->rect = input->caret_rect;
2786 /* foreground window is active window of foreground thread */
2787 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2788 if (thread) release_object( thread );
2789 release_object( desktop );
2793 /* retrieve queue keyboard state for a given thread */
2794 DECL_HANDLER(get_key_state)
2796 struct thread *thread;
2797 struct desktop *desktop;
2798 data_size_t size = min( 256, get_reply_max_size() );
2800 if (!req->tid) /* get global async key state */
2802 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2803 if (req->key >= 0)
2805 reply->state = desktop->keystate[req->key & 0xff];
2806 desktop->keystate[req->key & 0xff] &= ~0x40;
2808 set_reply_data( desktop->keystate, size );
2809 release_object( desktop );
2811 else
2813 if (!(thread = get_thread_from_id( req->tid ))) return;
2814 if (thread->queue)
2816 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2817 set_reply_data( thread->queue->input->keystate, size );
2819 release_object( thread );
2824 /* set queue keyboard state for a given thread */
2825 DECL_HANDLER(set_key_state)
2827 struct thread *thread;
2828 struct desktop *desktop;
2829 data_size_t size = min( 256, get_req_data_size() );
2831 if (!req->tid) /* set global async key state */
2833 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2834 memcpy( desktop->keystate, get_req_data(), size );
2835 release_object( desktop );
2837 else
2839 if (!(thread = get_thread_from_id( req->tid ))) return;
2840 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
2841 if (req->async && (desktop = get_thread_desktop( thread, 0 )))
2843 memcpy( desktop->keystate, get_req_data(), size );
2844 release_object( desktop );
2846 release_object( thread );
2851 /* set the system foreground window */
2852 DECL_HANDLER(set_foreground_window)
2854 struct thread *thread = NULL;
2855 struct desktop *desktop;
2856 struct msg_queue *queue = get_current_queue();
2858 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2859 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
2860 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
2861 reply->send_msg_new = FALSE;
2863 if (is_top_level_window( req->handle ) &&
2864 ((thread = get_window_thread( req->handle ))) &&
2865 (thread->queue->input->desktop == desktop))
2867 set_foreground_input( desktop, thread->queue->input );
2868 reply->send_msg_new = (desktop->foreground_input != queue->input);
2870 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2872 if (thread) release_object( thread );
2873 release_object( desktop );
2877 /* set the current thread focus window */
2878 DECL_HANDLER(set_focus_window)
2880 struct msg_queue *queue = get_current_queue();
2882 reply->previous = 0;
2883 if (queue && check_queue_input_window( queue, req->handle ))
2885 reply->previous = queue->input->focus;
2886 queue->input->focus = get_user_full_handle( req->handle );
2891 /* set the current thread active window */
2892 DECL_HANDLER(set_active_window)
2894 struct msg_queue *queue = get_current_queue();
2896 reply->previous = 0;
2897 if (queue && check_queue_input_window( queue, req->handle ))
2899 if (!req->handle || make_window_active( req->handle ))
2901 reply->previous = queue->input->active;
2902 queue->input->active = get_user_full_handle( req->handle );
2904 else set_error( STATUS_INVALID_HANDLE );
2909 /* set the current thread capture window */
2910 DECL_HANDLER(set_capture_window)
2912 struct msg_queue *queue = get_current_queue();
2914 reply->previous = reply->full_handle = 0;
2915 if (queue && check_queue_input_window( queue, req->handle ))
2917 struct thread_input *input = queue->input;
2919 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2920 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2922 set_error(STATUS_ACCESS_DENIED);
2923 return;
2925 reply->previous = input->capture;
2926 input->capture = get_user_full_handle( req->handle );
2927 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2928 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2929 reply->full_handle = input->capture;
2934 /* Set the current thread caret window */
2935 DECL_HANDLER(set_caret_window)
2937 struct msg_queue *queue = get_current_queue();
2939 reply->previous = 0;
2940 if (queue && check_queue_input_window( queue, req->handle ))
2942 struct thread_input *input = queue->input;
2944 reply->previous = input->caret;
2945 reply->old_rect = input->caret_rect;
2946 reply->old_hide = input->caret_hide;
2947 reply->old_state = input->caret_state;
2949 set_caret_window( input, get_user_full_handle(req->handle) );
2950 input->caret_rect.right = input->caret_rect.left + req->width;
2951 input->caret_rect.bottom = input->caret_rect.top + req->height;
2956 /* Set the current thread caret information */
2957 DECL_HANDLER(set_caret_info)
2959 struct msg_queue *queue = get_current_queue();
2960 struct thread_input *input;
2962 if (!queue) return;
2963 input = queue->input;
2964 reply->full_handle = input->caret;
2965 reply->old_rect = input->caret_rect;
2966 reply->old_hide = input->caret_hide;
2967 reply->old_state = input->caret_state;
2969 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2971 set_error( STATUS_ACCESS_DENIED );
2972 return;
2974 if (req->flags & SET_CARET_POS)
2976 input->caret_rect.right += req->x - input->caret_rect.left;
2977 input->caret_rect.bottom += req->y - input->caret_rect.top;
2978 input->caret_rect.left = req->x;
2979 input->caret_rect.top = req->y;
2981 if (req->flags & SET_CARET_HIDE)
2983 input->caret_hide += req->hide;
2984 if (input->caret_hide < 0) input->caret_hide = 0;
2986 if (req->flags & SET_CARET_STATE)
2988 if (req->state == -1) input->caret_state = !input->caret_state;
2989 else input->caret_state = !!req->state;
2994 /* get the time of the last input event */
2995 DECL_HANDLER(get_last_input_time)
2997 reply->time = last_input_time;
3000 /* set/get the current cursor */
3001 DECL_HANDLER(set_cursor)
3003 struct msg_queue *queue = get_current_queue();
3004 struct thread_input *input;
3006 if (!queue) return;
3007 input = queue->input;
3009 reply->prev_handle = input->cursor;
3010 reply->prev_count = input->cursor_count;
3011 reply->prev_x = input->desktop->cursor.x;
3012 reply->prev_y = input->desktop->cursor.y;
3014 if (req->flags & SET_CURSOR_HANDLE)
3016 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3018 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3019 return;
3021 input->cursor = req->handle;
3023 if (req->flags & SET_CURSOR_COUNT)
3025 queue->cursor_count += req->show_count;
3026 input->cursor_count += req->show_count;
3028 if (req->flags & SET_CURSOR_POS)
3030 set_cursor_pos( input->desktop, req->x, req->y );
3032 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3034 struct desktop *desktop = input->desktop;
3036 /* only the desktop owner can set the message */
3037 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3038 desktop->cursor.clip_msg = req->clip_msg;
3040 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip );
3043 reply->new_x = input->desktop->cursor.x;
3044 reply->new_y = input->desktop->cursor.y;
3045 reply->new_clip = input->desktop->cursor.clip;
3046 reply->last_change = input->desktop->cursor.last_change;
3049 DECL_HANDLER(update_rawinput_devices)
3051 const struct rawinput_device *devices = get_req_data();
3052 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3053 const struct rawinput_device_entry *e;
3054 unsigned int i;
3056 for (i = 0; i < device_count; ++i)
3058 update_rawinput_device(&devices[i]);
3061 e = find_rawinput_device( 1, 2 );
3062 current->process->rawinput_mouse = e ? &e->device : NULL;
3063 e = find_rawinput_device( 1, 6 );
3064 current->process->rawinput_kbd = e ? &e->device : NULL;