shell32: Implement IFolderView2_{Get, Set}CurrentFolderFlags().
[wine.git] / server / queue.c
blob7320dddc6b78fbb2dac8d6c7f2fc0d2444d7930b
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"
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <poll.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winternl.h"
38 #include "handle.h"
39 #include "file.h"
40 #include "thread.h"
41 #include "process.h"
42 #include "request.h"
43 #include "user.h"
45 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
46 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
48 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
49 #define NB_MSG_KINDS (POST_MESSAGE+1)
52 struct message_result
54 struct list sender_entry; /* entry in sender list */
55 struct message *msg; /* message the result is for */
56 struct message_result *recv_next; /* next in receiver list */
57 struct msg_queue *sender; /* sender queue */
58 struct msg_queue *receiver; /* receiver queue */
59 int replied; /* has it been replied to? */
60 unsigned int error; /* error code to pass back to sender */
61 lparam_t result; /* reply result */
62 struct message *hardware_msg; /* hardware message if low-level hook result */
63 struct desktop *desktop; /* desktop for hardware message */
64 struct message *callback_msg; /* message to queue for callback */
65 void *data; /* message reply data */
66 unsigned int data_size; /* size of message reply data */
67 struct timeout_user *timeout; /* result timeout */
70 struct message
72 struct list entry; /* entry in message list */
73 enum message_type type; /* message type */
74 user_handle_t win; /* window handle */
75 unsigned int msg; /* message code */
76 lparam_t wparam; /* parameters */
77 lparam_t lparam; /* parameters */
78 int x; /* message position */
79 int y;
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 abstime_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 */
115 unsigned char desktop_keystate[256]; /* desktop keystate when keystate was synced */
116 int keystate_lock; /* keystate is locked */
119 struct msg_queue
121 struct object obj; /* object header */
122 struct fd *fd; /* optional file descriptor to poll */
123 unsigned int wake_bits; /* wakeup bits */
124 unsigned int wake_mask; /* wakeup mask */
125 unsigned int changed_bits; /* changed wakeup bits */
126 unsigned int changed_mask; /* changed wakeup mask */
127 int paint_count; /* pending paint messages count */
128 int hotkey_count; /* pending hotkey messages count */
129 int quit_message; /* is there a pending quit message? */
130 int exit_code; /* exit code of pending quit message */
131 int cursor_count; /* per-queue cursor show count */
132 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
133 struct list send_result; /* stack of sent messages waiting for result */
134 struct list callback_result; /* list of callback messages waiting for result */
135 struct message_result *recv_result; /* stack of received messages waiting for result */
136 struct list pending_timers; /* list of pending timers */
137 struct list expired_timers; /* list of expired timers */
138 lparam_t next_timer_id; /* id for the next timer with a 0 window */
139 struct timeout_user *timeout; /* timeout for next timer to expire */
140 struct thread_input *input; /* thread input descriptor */
141 struct hook_table *hooks; /* hook table */
142 timeout_t last_get_msg; /* time of last get message call */
143 int keystate_lock; /* owns an input keystate lock */
146 struct hotkey
148 struct list entry; /* entry in desktop hotkey list */
149 struct msg_queue *queue; /* queue owning this hotkey */
150 user_handle_t win; /* window handle */
151 int id; /* hotkey id */
152 unsigned int vkey; /* virtual key code */
153 unsigned int flags; /* key modifiers */
156 static void msg_queue_dump( struct object *obj, int verbose );
157 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
158 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
159 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
160 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
161 static void msg_queue_destroy( struct object *obj );
162 static void msg_queue_poll_event( struct fd *fd, int event );
163 static void thread_input_dump( struct object *obj, int verbose );
164 static void thread_input_destroy( struct object *obj );
165 static void timer_callback( void *private );
167 static const struct object_ops msg_queue_ops =
169 sizeof(struct msg_queue), /* size */
170 &no_type, /* type */
171 msg_queue_dump, /* dump */
172 msg_queue_add_queue, /* add_queue */
173 msg_queue_remove_queue, /* remove_queue */
174 msg_queue_signaled, /* signaled */
175 msg_queue_satisfied, /* satisfied */
176 no_signal, /* signal */
177 no_get_fd, /* get_fd */
178 default_map_access, /* map_access */
179 default_get_sd, /* get_sd */
180 default_set_sd, /* set_sd */
181 no_get_full_name, /* get_full_name */
182 no_lookup_name, /* lookup_name */
183 no_link_name, /* link_name */
184 NULL, /* unlink_name */
185 no_open_file, /* open_file */
186 no_kernel_obj_list, /* get_kernel_obj_list */
187 no_close_handle, /* close_handle */
188 msg_queue_destroy /* destroy */
191 static const struct fd_ops msg_queue_fd_ops =
193 NULL, /* get_poll_events */
194 msg_queue_poll_event, /* poll_event */
195 NULL, /* flush */
196 NULL, /* get_fd_type */
197 NULL, /* ioctl */
198 NULL, /* queue_async */
199 NULL, /* reselect_async */
200 NULL /* cancel async */
204 static const struct object_ops thread_input_ops =
206 sizeof(struct thread_input), /* size */
207 &no_type, /* type */
208 thread_input_dump, /* dump */
209 no_add_queue, /* add_queue */
210 NULL, /* remove_queue */
211 NULL, /* signaled */
212 NULL, /* satisfied */
213 no_signal, /* signal */
214 no_get_fd, /* get_fd */
215 default_map_access, /* map_access */
216 default_get_sd, /* get_sd */
217 default_set_sd, /* set_sd */
218 no_get_full_name, /* get_full_name */
219 no_lookup_name, /* lookup_name */
220 no_link_name, /* link_name */
221 NULL, /* unlink_name */
222 no_open_file, /* open_file */
223 no_kernel_obj_list, /* get_kernel_obj_list */
224 no_close_handle, /* close_handle */
225 thread_input_destroy /* destroy */
228 /* pointer to input structure of foreground thread */
229 static unsigned int last_input_time;
231 static cursor_pos_t cursor_history[64];
232 static unsigned int cursor_history_latest;
234 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
235 static void free_message( struct message *msg );
237 /* set the caret window in a given thread input */
238 static void set_caret_window( struct thread_input *input, user_handle_t win )
240 if (!win || win != input->caret)
242 input->caret_rect.left = 0;
243 input->caret_rect.top = 0;
244 input->caret_rect.right = 0;
245 input->caret_rect.bottom = 0;
247 input->caret = win;
248 input->caret_hide = 1;
249 input->caret_state = 0;
252 /* create a thread input object */
253 static struct thread_input *create_thread_input( struct thread *thread )
255 struct thread_input *input;
257 if ((input = alloc_object( &thread_input_ops )))
259 input->focus = 0;
260 input->capture = 0;
261 input->active = 0;
262 input->menu_owner = 0;
263 input->move_size = 0;
264 input->cursor = 0;
265 input->cursor_count = 0;
266 list_init( &input->msg_list );
267 set_caret_window( input, 0 );
268 memset( input->keystate, 0, sizeof(input->keystate) );
269 input->keystate_lock = 0;
271 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
273 release_object( input );
274 return NULL;
276 memcpy( input->desktop_keystate, input->desktop->keystate, sizeof(input->desktop_keystate) );
278 return input;
281 /* create a message queue object */
282 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
284 struct thread_input *new_input = NULL;
285 struct msg_queue *queue;
286 int i;
288 if (!input)
290 if (!(new_input = create_thread_input( thread ))) return NULL;
291 input = new_input;
294 if ((queue = alloc_object( &msg_queue_ops )))
296 queue->fd = NULL;
297 queue->wake_bits = 0;
298 queue->wake_mask = 0;
299 queue->changed_bits = 0;
300 queue->changed_mask = 0;
301 queue->paint_count = 0;
302 queue->hotkey_count = 0;
303 queue->quit_message = 0;
304 queue->cursor_count = 0;
305 queue->recv_result = NULL;
306 queue->next_timer_id = 0x7fff;
307 queue->timeout = NULL;
308 queue->input = (struct thread_input *)grab_object( input );
309 queue->hooks = NULL;
310 queue->last_get_msg = current_time;
311 queue->keystate_lock = 0;
312 list_init( &queue->send_result );
313 list_init( &queue->callback_result );
314 list_init( &queue->pending_timers );
315 list_init( &queue->expired_timers );
316 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
318 thread->queue = queue;
320 if (new_input) release_object( new_input );
321 return queue;
324 /* free the message queue of a thread at thread exit */
325 void free_msg_queue( struct thread *thread )
327 remove_thread_hooks( thread );
328 if (!thread->queue) return;
329 release_object( thread->queue );
330 thread->queue = NULL;
333 /* synchronize thread input keystate with the desktop */
334 static void sync_input_keystate( struct thread_input *input )
336 int i;
337 if (!input->desktop || input->keystate_lock) return;
338 for (i = 0; i < sizeof(input->keystate); ++i)
340 if (input->desktop_keystate[i] == input->desktop->keystate[i]) continue;
341 input->keystate[i] = input->desktop_keystate[i] = input->desktop->keystate[i];
345 /* locks thread input keystate to prevent synchronization */
346 static void lock_input_keystate( struct thread_input *input )
348 input->keystate_lock++;
351 /* unlock the thread input keystate and synchronize it again */
352 static void unlock_input_keystate( struct thread_input *input )
354 input->keystate_lock--;
355 if (!input->keystate_lock) sync_input_keystate( input );
358 /* change the thread input data of a given thread */
359 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
361 struct msg_queue *queue = thread->queue;
363 if (!queue)
365 thread->queue = create_msg_queue( thread, new_input );
366 return thread->queue != NULL;
368 if (queue->input)
370 queue->input->cursor_count -= queue->cursor_count;
371 if (queue->keystate_lock) unlock_input_keystate( queue->input );
372 release_object( queue->input );
374 queue->input = (struct thread_input *)grab_object( new_input );
375 if (queue->keystate_lock) lock_input_keystate( queue->input );
376 new_input->cursor_count += queue->cursor_count;
377 return 1;
380 /* allocate a hardware message and its data */
381 static struct message *alloc_hardware_message( lparam_t info, struct hw_msg_source source,
382 unsigned int time, data_size_t extra_size )
384 struct hardware_msg_data *msg_data;
385 struct message *msg;
387 if (!(msg = mem_alloc( sizeof(*msg) ))) return NULL;
388 if (!(msg_data = mem_alloc( sizeof(*msg_data) + extra_size )))
390 free( msg );
391 return NULL;
393 memset( msg, 0, sizeof(*msg) );
394 msg->type = MSG_HARDWARE;
395 msg->time = time;
396 msg->data = msg_data;
397 msg->data_size = sizeof(*msg_data) + extra_size;
399 memset( msg_data, 0, sizeof(*msg_data) + extra_size );
400 msg_data->info = info;
401 msg_data->size = msg->data_size;
402 msg_data->source = source;
403 return msg;
406 static int update_desktop_cursor_pos( struct desktop *desktop, int x, int y )
408 int updated;
410 x = max( min( x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
411 y = max( min( y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
412 updated = (desktop->cursor.x != x || desktop->cursor.y != y);
413 desktop->cursor.x = x;
414 desktop->cursor.y = y;
415 desktop->cursor.last_change = get_tick_count();
417 return updated;
420 /* set the cursor position and queue the corresponding mouse message */
421 static void set_cursor_pos( struct desktop *desktop, int x, int y )
423 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
424 const struct rawinput_device *device;
425 struct message *msg;
427 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
429 update_desktop_cursor_pos( desktop, x, y );
430 return;
433 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
435 msg->msg = WM_MOUSEMOVE;
436 msg->x = x;
437 msg->y = y;
438 queue_hardware_message( desktop, msg, 1 );
441 /* retrieve default position and time for synthesized messages */
442 static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
444 struct desktop *desktop = queue->input->desktop;
446 *x = desktop->cursor.x;
447 *y = desktop->cursor.y;
448 *time = get_tick_count();
451 /* set the cursor clip rectangle */
452 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect, int send_clip_msg )
454 rectangle_t top_rect;
455 int x, y;
457 get_top_window_rectangle( desktop, &top_rect );
458 if (rect)
460 rectangle_t new_rect = *rect;
461 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
462 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
463 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
464 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
465 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
466 desktop->cursor.clip = new_rect;
468 else desktop->cursor.clip = top_rect;
470 if (desktop->cursor.clip_msg && send_clip_msg)
471 post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
473 /* warp the mouse to be inside the clip rect */
474 x = max( min( desktop->cursor.x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
475 y = max( min( desktop->cursor.y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
476 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
479 /* change the foreground input and reset the cursor clip rect */
480 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
482 if (desktop->foreground_input == input) return;
483 set_clip_rectangle( desktop, NULL, 1 );
484 desktop->foreground_input = input;
487 /* get the hook table for a given thread */
488 struct hook_table *get_queue_hooks( struct thread *thread )
490 if (!thread->queue) return NULL;
491 return thread->queue->hooks;
494 /* set the hook table for a given thread, allocating the queue if needed */
495 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
497 struct msg_queue *queue = thread->queue;
498 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
499 if (queue->hooks) release_object( queue->hooks );
500 queue->hooks = hooks;
503 /* check the queue status */
504 static inline int is_signaled( struct msg_queue *queue )
506 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
509 /* set some queue bits */
510 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
512 if (bits & (QS_KEY | QS_MOUSEBUTTON))
514 if (!queue->keystate_lock) lock_input_keystate( queue->input );
515 queue->keystate_lock = 1;
517 queue->wake_bits |= bits;
518 queue->changed_bits |= bits;
519 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
522 /* clear some queue bits */
523 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
525 queue->wake_bits &= ~bits;
526 queue->changed_bits &= ~bits;
527 if (!(queue->wake_bits & (QS_KEY | QS_MOUSEBUTTON)))
529 if (queue->keystate_lock) unlock_input_keystate( queue->input );
530 queue->keystate_lock = 0;
534 /* check whether msg is a keyboard message */
535 static inline int is_keyboard_msg( struct message *msg )
537 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
540 /* check if message is matched by the filter */
541 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
543 return (msg >= first && msg <= last);
546 /* check whether a message filter contains at least one potential hardware message */
547 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
549 /* hardware message ranges are (in numerical order):
550 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
551 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
552 * WM_MOUSEFIRST .. WM_MOUSELAST
554 if (last < WM_NCMOUSEFIRST) return 0;
555 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
556 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
557 if (first > WM_MOUSELAST) return 0;
558 return 1;
561 /* get the QS_* bit corresponding to a given hardware message */
562 static inline int get_hardware_msg_bit( struct message *msg )
564 if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
565 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
566 if (is_keyboard_msg( msg )) return QS_KEY;
567 return QS_MOUSEBUTTON;
570 /* get the current thread queue, creating it if needed */
571 static inline struct msg_queue *get_current_queue(void)
573 struct msg_queue *queue = current->queue;
574 if (!queue) queue = create_msg_queue( current, NULL );
575 return queue;
578 /* get a (pseudo-)unique id to tag hardware messages */
579 static inline unsigned int get_unique_id(void)
581 static unsigned int id;
582 if (!++id) id = 1; /* avoid an id of 0 */
583 return id;
586 /* try to merge a message with the last in the list; return 1 if successful */
587 static int merge_message( struct thread_input *input, const struct message *msg )
589 struct message *prev;
590 struct list *ptr;
592 if (msg->msg != WM_MOUSEMOVE) return 0;
593 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
595 prev = LIST_ENTRY( ptr, struct message, entry );
596 if (prev->msg != WM_INPUT) break;
598 if (!ptr) return 0;
599 if (prev->result) return 0;
600 if (prev->win && msg->win && prev->win != msg->win) return 0;
601 if (prev->msg != msg->msg) return 0;
602 if (prev->type != msg->type) return 0;
603 /* now we can merge it */
604 prev->wparam = msg->wparam;
605 prev->lparam = msg->lparam;
606 prev->x = msg->x;
607 prev->y = msg->y;
608 prev->time = msg->time;
609 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
611 struct hardware_msg_data *prev_data = prev->data;
612 struct hardware_msg_data *msg_data = msg->data;
613 prev_data->info = msg_data->info;
615 list_remove( ptr );
616 list_add_tail( &input->msg_list, ptr );
617 return 1;
620 /* free a result structure */
621 static void free_result( struct message_result *result )
623 if (result->timeout) remove_timeout_user( result->timeout );
624 free( result->data );
625 if (result->callback_msg) free_message( result->callback_msg );
626 if (result->hardware_msg) free_message( result->hardware_msg );
627 if (result->desktop) release_object( result->desktop );
628 free( result );
631 /* remove the result from the sender list it is on */
632 static inline void remove_result_from_sender( struct message_result *result )
634 assert( result->sender );
636 list_remove( &result->sender_entry );
637 result->sender = NULL;
638 if (!result->receiver) free_result( result );
641 /* store the message result in the appropriate structure */
642 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
644 res->result = result;
645 res->error = error;
646 res->replied = 1;
647 if (res->timeout)
649 remove_timeout_user( res->timeout );
650 res->timeout = NULL;
653 if (res->hardware_msg)
655 if (!error && result) /* rejected by the hook */
656 free_message( res->hardware_msg );
657 else
658 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
660 res->hardware_msg = NULL;
663 if (res->sender)
665 if (res->callback_msg)
667 /* queue the callback message in the sender queue */
668 struct callback_msg_data *data = res->callback_msg->data;
669 data->result = result;
670 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
671 set_queue_bits( res->sender, QS_SENDMESSAGE );
672 res->callback_msg = NULL;
673 remove_result_from_sender( res );
675 else
677 /* wake sender queue if waiting on this result */
678 if (list_head(&res->sender->send_result) == &res->sender_entry)
679 set_queue_bits( res->sender, QS_SMRESULT );
682 else if (!res->receiver) free_result( res );
685 /* free a message when deleting a queue or window */
686 static void free_message( struct message *msg )
688 struct message_result *result = msg->result;
689 if (result)
691 result->msg = NULL;
692 result->receiver = NULL;
693 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
695 free( msg->data );
696 free( msg );
699 /* remove (and free) a message from a message list */
700 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
701 enum message_kind kind )
703 list_remove( &msg->entry );
704 switch(kind)
706 case SEND_MESSAGE:
707 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
708 break;
709 case POST_MESSAGE:
710 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
711 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
712 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
713 clear_queue_bits( queue, QS_HOTKEY );
714 break;
716 free_message( msg );
719 /* message timed out without getting a reply */
720 static void result_timeout( void *private )
722 struct message_result *result = private;
724 assert( !result->replied );
726 result->timeout = NULL;
728 if (result->msg) /* not received yet */
730 struct message *msg = result->msg;
732 result->msg = NULL;
733 msg->result = NULL;
734 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
735 result->receiver = NULL;
737 store_message_result( result, 0, STATUS_TIMEOUT );
740 /* allocate and fill a message result structure */
741 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
742 struct msg_queue *recv_queue,
743 struct message *msg, timeout_t timeout )
745 struct message_result *result = mem_alloc( sizeof(*result) );
746 if (result)
748 result->msg = msg;
749 result->sender = send_queue;
750 result->receiver = recv_queue;
751 result->replied = 0;
752 result->data = NULL;
753 result->data_size = 0;
754 result->timeout = NULL;
755 result->hardware_msg = NULL;
756 result->desktop = NULL;
757 result->callback_msg = NULL;
759 if (msg->type == MSG_CALLBACK)
761 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
763 if (!callback_msg)
765 free( result );
766 return NULL;
768 callback_msg->type = MSG_CALLBACK_RESULT;
769 callback_msg->win = msg->win;
770 callback_msg->msg = msg->msg;
771 callback_msg->wparam = 0;
772 callback_msg->lparam = 0;
773 callback_msg->time = get_tick_count();
774 callback_msg->result = NULL;
775 /* steal the data from the original message */
776 callback_msg->data = msg->data;
777 callback_msg->data_size = msg->data_size;
778 msg->data = NULL;
779 msg->data_size = 0;
781 result->callback_msg = callback_msg;
782 list_add_head( &send_queue->callback_result, &result->sender_entry );
784 else if (send_queue)
786 list_add_head( &send_queue->send_result, &result->sender_entry );
787 clear_queue_bits( send_queue, QS_SMRESULT );
790 if (timeout != TIMEOUT_INFINITE)
791 result->timeout = add_timeout_user( timeout, result_timeout, result );
793 return result;
796 /* receive a message, removing it from the sent queue */
797 static void receive_message( struct msg_queue *queue, struct message *msg,
798 struct get_message_reply *reply )
800 struct message_result *result = msg->result;
802 reply->total = msg->data_size;
803 if (msg->data_size > get_reply_max_size())
805 set_error( STATUS_BUFFER_OVERFLOW );
806 return;
808 reply->type = msg->type;
809 reply->win = msg->win;
810 reply->msg = msg->msg;
811 reply->wparam = msg->wparam;
812 reply->lparam = msg->lparam;
813 reply->x = msg->x;
814 reply->y = msg->y;
815 reply->time = msg->time;
817 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
819 list_remove( &msg->entry );
820 /* put the result on the receiver result stack */
821 if (result)
823 result->msg = NULL;
824 result->recv_next = queue->recv_result;
825 queue->recv_result = result;
827 free( msg );
828 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
831 /* set the result of the current received message */
832 static void reply_message( struct msg_queue *queue, lparam_t result,
833 unsigned int error, int remove, const void *data, data_size_t len )
835 struct message_result *res = queue->recv_result;
837 if (remove)
839 queue->recv_result = res->recv_next;
840 res->receiver = NULL;
841 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
843 free_result( res );
844 return;
847 if (!res->replied)
849 if (len && (res->data = memdup( data, len ))) res->data_size = len;
850 store_message_result( res, result, error );
854 static int match_window( user_handle_t win, user_handle_t msg_win )
856 if (!win) return 1;
857 if (win == -1 || win == 1) return !msg_win;
858 if (msg_win == win) return 1;
859 return is_child_window( win, msg_win );
862 /* retrieve a posted message */
863 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
864 unsigned int first, unsigned int last, unsigned int flags,
865 struct get_message_reply *reply )
867 struct message *msg;
869 /* check against the filters */
870 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
872 if (!match_window( win, msg->win )) continue;
873 if (!check_msg_filter( msg->msg, first, last )) continue;
874 goto found; /* found one */
876 return 0;
878 /* return it to the app */
879 found:
880 reply->total = msg->data_size;
881 if (msg->data_size > get_reply_max_size())
883 set_error( STATUS_BUFFER_OVERFLOW );
884 return 1;
886 reply->type = msg->type;
887 reply->win = msg->win;
888 reply->msg = msg->msg;
889 reply->wparam = msg->wparam;
890 reply->lparam = msg->lparam;
891 reply->x = msg->x;
892 reply->y = msg->y;
893 reply->time = msg->time;
895 if (flags & PM_REMOVE)
897 if (msg->data)
899 set_reply_data_ptr( msg->data, msg->data_size );
900 msg->data = NULL;
901 msg->data_size = 0;
903 remove_queue_message( queue, msg, POST_MESSAGE );
905 else if (msg->data) set_reply_data( msg->data, msg->data_size );
907 return 1;
910 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
911 struct get_message_reply *reply )
913 if (queue->quit_message)
915 reply->total = 0;
916 reply->type = MSG_POSTED;
917 reply->win = 0;
918 reply->msg = WM_QUIT;
919 reply->wparam = queue->exit_code;
920 reply->lparam = 0;
922 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
924 if (flags & PM_REMOVE)
926 queue->quit_message = 0;
927 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
928 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
930 return 1;
932 else
933 return 0;
936 /* empty a message list and free all the messages */
937 static void empty_msg_list( struct list *list )
939 struct list *ptr;
941 while ((ptr = list_head( list )) != NULL)
943 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
944 list_remove( &msg->entry );
945 free_message( msg );
949 /* cleanup all pending results when deleting a queue */
950 static void cleanup_results( struct msg_queue *queue )
952 struct list *entry;
954 while ((entry = list_head( &queue->send_result )) != NULL)
956 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
959 while ((entry = list_head( &queue->callback_result )) != NULL)
961 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
964 while (queue->recv_result)
965 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
968 /* check if the thread owning the queue is hung (not checking for messages) */
969 static int is_queue_hung( struct msg_queue *queue )
971 struct wait_queue_entry *entry;
973 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
974 return 0; /* less than 5 seconds since last get message -> not hung */
976 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
978 if (get_wait_queue_thread(entry)->queue == queue)
979 return 0; /* thread is waiting on queue -> not hung */
981 return 1;
984 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
986 struct msg_queue *queue = (struct msg_queue *)obj;
987 struct process *process = get_wait_queue_thread(entry)->process;
989 /* a thread can only wait on its own queue */
990 if (get_wait_queue_thread(entry)->queue != queue)
992 set_error( STATUS_ACCESS_DENIED );
993 return 0;
995 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
997 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
998 set_fd_events( queue->fd, POLLIN );
999 add_queue( obj, entry );
1000 return 1;
1003 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
1005 struct msg_queue *queue = (struct msg_queue *)obj;
1007 remove_queue( obj, entry );
1008 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
1009 set_fd_events( queue->fd, 0 );
1012 static void msg_queue_dump( struct object *obj, int verbose )
1014 struct msg_queue *queue = (struct msg_queue *)obj;
1015 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
1016 queue->wake_bits, queue->wake_mask );
1019 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
1021 struct msg_queue *queue = (struct msg_queue *)obj;
1022 int ret = 0;
1024 if (queue->fd)
1026 if ((ret = check_fd_events( queue->fd, POLLIN )))
1027 /* stop waiting on select() if we are signaled */
1028 set_fd_events( queue->fd, 0 );
1029 else if (!list_empty( &obj->wait_queue ))
1030 /* restart waiting on poll() if we are no longer signaled */
1031 set_fd_events( queue->fd, POLLIN );
1033 return ret || is_signaled( queue );
1036 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
1038 struct msg_queue *queue = (struct msg_queue *)obj;
1039 queue->wake_mask = 0;
1040 queue->changed_mask = 0;
1043 static void msg_queue_destroy( struct object *obj )
1045 struct msg_queue *queue = (struct msg_queue *)obj;
1046 struct list *ptr;
1047 struct hotkey *hotkey, *hotkey2;
1048 int i;
1050 cleanup_results( queue );
1051 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
1053 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
1055 if (hotkey->queue == queue)
1057 list_remove( &hotkey->entry );
1058 free( hotkey );
1062 while ((ptr = list_head( &queue->pending_timers )))
1064 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1065 list_remove( &timer->entry );
1066 free( timer );
1068 while ((ptr = list_head( &queue->expired_timers )))
1070 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1071 list_remove( &timer->entry );
1072 free( timer );
1074 if (queue->timeout) remove_timeout_user( queue->timeout );
1075 queue->input->cursor_count -= queue->cursor_count;
1076 if (queue->keystate_lock) unlock_input_keystate( queue->input );
1077 release_object( queue->input );
1078 if (queue->hooks) release_object( queue->hooks );
1079 if (queue->fd) release_object( queue->fd );
1082 static void msg_queue_poll_event( struct fd *fd, int event )
1084 struct msg_queue *queue = get_fd_user( fd );
1085 assert( queue->obj.ops == &msg_queue_ops );
1087 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1088 else set_fd_events( queue->fd, 0 );
1089 wake_up( &queue->obj, 0 );
1092 static void thread_input_dump( struct object *obj, int verbose )
1094 struct thread_input *input = (struct thread_input *)obj;
1095 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
1096 input->focus, input->capture, input->active );
1099 static void thread_input_destroy( struct object *obj )
1101 struct thread_input *input = (struct thread_input *)obj;
1103 empty_msg_list( &input->msg_list );
1104 if (input->desktop)
1106 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
1107 release_object( input->desktop );
1111 /* fix the thread input data when a window is destroyed */
1112 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1114 struct thread_input *input = queue->input;
1116 if (window == input->focus) input->focus = 0;
1117 if (window == input->capture) input->capture = 0;
1118 if (window == input->active) input->active = 0;
1119 if (window == input->menu_owner) input->menu_owner = 0;
1120 if (window == input->move_size) input->move_size = 0;
1121 if (window == input->caret) set_caret_window( input, 0 );
1124 /* check if the specified window can be set in the input data of a given queue */
1125 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1127 struct thread *thread;
1128 int ret = 0;
1130 if (!window) return 1; /* we can always clear the data */
1132 if ((thread = get_window_thread( window )))
1134 ret = (queue->input == thread->queue->input);
1135 if (!ret) set_error( STATUS_ACCESS_DENIED );
1136 release_object( thread );
1138 else set_error( STATUS_INVALID_HANDLE );
1140 return ret;
1143 /* make sure the specified thread has a queue */
1144 int init_thread_queue( struct thread *thread )
1146 if (thread->queue) return 1;
1147 return (create_msg_queue( thread, NULL ) != NULL);
1150 /* attach two thread input data structures */
1151 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1153 struct desktop *desktop;
1154 struct thread_input *input;
1155 int ret;
1157 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1158 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1159 input = (struct thread_input *)grab_object( thread_to->queue->input );
1160 if (input->desktop != desktop)
1162 set_error( STATUS_ACCESS_DENIED );
1163 release_object( input );
1164 release_object( desktop );
1165 return 0;
1167 release_object( desktop );
1169 if (thread_from->queue)
1171 if (!input->focus) input->focus = thread_from->queue->input->focus;
1172 if (!input->active) input->active = thread_from->queue->input->active;
1175 ret = assign_thread_input( thread_from, input );
1176 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1177 release_object( input );
1178 return ret;
1181 /* detach two thread input data structures */
1182 void detach_thread_input( struct thread *thread_from )
1184 struct thread *thread;
1185 struct thread_input *input, *old_input = thread_from->queue->input;
1187 if ((input = create_thread_input( thread_from )))
1189 if (old_input->focus && (thread = get_window_thread( old_input->focus )))
1191 if (thread == thread_from)
1193 input->focus = old_input->focus;
1194 old_input->focus = 0;
1196 release_object( thread );
1198 if (old_input->active && (thread = get_window_thread( old_input->active )))
1200 if (thread == thread_from)
1202 input->active = old_input->active;
1203 old_input->active = 0;
1205 release_object( thread );
1207 assign_thread_input( thread_from, input );
1208 release_object( input );
1213 /* set the next timer to expire */
1214 static void set_next_timer( struct msg_queue *queue )
1216 struct list *ptr;
1218 if (queue->timeout)
1220 remove_timeout_user( queue->timeout );
1221 queue->timeout = NULL;
1223 if ((ptr = list_head( &queue->pending_timers )))
1225 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1226 queue->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, queue );
1228 /* set/clear QS_TIMER bit */
1229 if (list_empty( &queue->expired_timers ))
1230 clear_queue_bits( queue, QS_TIMER );
1231 else
1232 set_queue_bits( queue, QS_TIMER );
1235 /* find a timer from its window and id */
1236 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1237 unsigned int msg, lparam_t id )
1239 struct list *ptr;
1241 /* we need to search both lists */
1243 LIST_FOR_EACH( ptr, &queue->pending_timers )
1245 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1246 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1248 LIST_FOR_EACH( ptr, &queue->expired_timers )
1250 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1251 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1253 return NULL;
1256 /* callback for the next timer expiration */
1257 static void timer_callback( void *private )
1259 struct msg_queue *queue = private;
1260 struct list *ptr;
1262 queue->timeout = NULL;
1263 /* move on to the next timer */
1264 ptr = list_head( &queue->pending_timers );
1265 list_remove( ptr );
1266 list_add_tail( &queue->expired_timers, ptr );
1267 set_next_timer( queue );
1270 /* link a timer at its rightful place in the queue list */
1271 static void link_timer( struct msg_queue *queue, struct timer *timer )
1273 struct list *ptr;
1275 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1277 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1278 if (t->when <= timer->when) break;
1280 list_add_before( ptr, &timer->entry );
1283 /* remove a timer from the queue timer list and free it */
1284 static void free_timer( struct msg_queue *queue, struct timer *timer )
1286 list_remove( &timer->entry );
1287 free( timer );
1288 set_next_timer( queue );
1291 /* restart an expired timer */
1292 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1294 list_remove( &timer->entry );
1295 while (-timer->when <= monotonic_time) timer->when -= (timeout_t)timer->rate * 10000;
1296 link_timer( queue, timer );
1297 set_next_timer( queue );
1300 /* find an expired timer matching the filtering parameters */
1301 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1302 unsigned int get_first, unsigned int get_last,
1303 int remove )
1305 struct list *ptr;
1307 LIST_FOR_EACH( ptr, &queue->expired_timers )
1309 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1310 if (win && timer->win != win) continue;
1311 if (check_msg_filter( timer->msg, get_first, get_last ))
1313 if (remove) restart_timer( queue, timer );
1314 return timer;
1317 return NULL;
1320 /* add a timer */
1321 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1323 struct timer *timer = mem_alloc( sizeof(*timer) );
1324 if (timer)
1326 timer->rate = max( rate, 1 );
1327 timer->when = -monotonic_time - (timeout_t)timer->rate * 10000;
1328 link_timer( queue, timer );
1329 /* check if we replaced the next timer */
1330 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1332 return timer;
1335 /* change the input key state for a given key */
1336 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1338 if (down)
1340 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1341 keystate[key] |= down;
1343 else keystate[key] &= ~0x80;
1346 /* update the input key state for a keyboard message */
1347 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1348 unsigned int msg, lparam_t wparam )
1350 unsigned char key;
1351 int down = 0;
1353 switch (msg)
1355 case WM_LBUTTONDOWN:
1356 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1357 /* fall through */
1358 case WM_LBUTTONUP:
1359 set_input_key_state( keystate, VK_LBUTTON, down );
1360 break;
1361 case WM_MBUTTONDOWN:
1362 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1363 /* fall through */
1364 case WM_MBUTTONUP:
1365 set_input_key_state( keystate, VK_MBUTTON, down );
1366 break;
1367 case WM_RBUTTONDOWN:
1368 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1369 /* fall through */
1370 case WM_RBUTTONUP:
1371 set_input_key_state( keystate, VK_RBUTTON, down );
1372 break;
1373 case WM_XBUTTONDOWN:
1374 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1375 /* fall through */
1376 case WM_XBUTTONUP:
1377 if (wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1378 else if (wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1379 break;
1380 case WM_KEYDOWN:
1381 case WM_SYSKEYDOWN:
1382 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1383 /* fall through */
1384 case WM_KEYUP:
1385 case WM_SYSKEYUP:
1386 key = (unsigned char)wparam;
1387 set_input_key_state( keystate, key, down );
1388 switch(key)
1390 case VK_LCONTROL:
1391 case VK_RCONTROL:
1392 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1393 set_input_key_state( keystate, VK_CONTROL, down );
1394 break;
1395 case VK_LMENU:
1396 case VK_RMENU:
1397 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1398 set_input_key_state( keystate, VK_MENU, down );
1399 break;
1400 case VK_LSHIFT:
1401 case VK_RSHIFT:
1402 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1403 set_input_key_state( keystate, VK_SHIFT, down );
1404 break;
1406 break;
1410 /* update the desktop key state according to a mouse message flags */
1411 static void update_desktop_mouse_state( struct desktop *desktop, unsigned int flags,
1412 int x, int y, lparam_t wparam )
1414 if (flags & MOUSEEVENTF_MOVE)
1415 update_desktop_cursor_pos( desktop, x, y );
1416 if (flags & MOUSEEVENTF_LEFTDOWN)
1417 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONDOWN, wparam );
1418 if (flags & MOUSEEVENTF_LEFTUP)
1419 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONUP, wparam );
1420 if (flags & MOUSEEVENTF_RIGHTDOWN)
1421 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONDOWN, wparam );
1422 if (flags & MOUSEEVENTF_RIGHTUP)
1423 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONUP, wparam );
1424 if (flags & MOUSEEVENTF_MIDDLEDOWN)
1425 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONDOWN, wparam );
1426 if (flags & MOUSEEVENTF_MIDDLEUP)
1427 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONUP, wparam );
1428 if (flags & MOUSEEVENTF_XDOWN)
1429 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONDOWN, wparam );
1430 if (flags & MOUSEEVENTF_XUP)
1431 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONUP, wparam );
1434 /* release the hardware message currently being processed by the given thread */
1435 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id )
1437 struct thread_input *input = queue->input;
1438 struct message *msg, *other;
1439 int clr_bit;
1441 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1443 if (msg->unique_id == hw_id) break;
1445 if (&msg->entry == &input->msg_list) return; /* not found */
1447 /* clear the queue bit for that message */
1448 clr_bit = get_hardware_msg_bit( msg );
1449 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1451 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1453 clr_bit = 0;
1454 break;
1457 if (clr_bit) clear_queue_bits( queue, clr_bit );
1459 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1460 list_remove( &msg->entry );
1461 free_message( msg );
1464 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1466 struct hotkey *hotkey;
1467 unsigned int modifiers = 0;
1469 if (msg->msg != WM_KEYDOWN && msg->msg != WM_SYSKEYDOWN) return 0;
1471 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1472 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1473 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1474 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1476 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1478 if (hotkey->vkey != msg->wparam) continue;
1479 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1482 return 0;
1484 found:
1485 msg->type = MSG_POSTED;
1486 msg->win = hotkey->win;
1487 msg->msg = WM_HOTKEY;
1488 msg->wparam = hotkey->id;
1489 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1491 free( msg->data );
1492 msg->data = NULL;
1493 msg->data_size = 0;
1495 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1496 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1497 hotkey->queue->hotkey_count++;
1498 return 1;
1501 /* find the window that should receive a given hardware message */
1502 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1503 struct message *msg, unsigned int *msg_code,
1504 struct thread **thread )
1506 user_handle_t win = 0;
1508 *thread = NULL;
1509 *msg_code = msg->msg;
1510 if (msg->msg == WM_INPUT || msg->msg == WM_INPUT_DEVICE_CHANGE)
1512 if (!(win = msg->win) && input) win = input->focus;
1514 else if (is_keyboard_msg( msg ))
1516 if (input && !(win = input->focus))
1518 win = input->active;
1519 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1522 else if (!input || !(win = input->capture)) /* mouse message */
1524 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1525 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1527 *thread = window_thread_from_point( win, msg->x, msg->y );
1530 if (!*thread)
1531 *thread = get_window_thread( win );
1532 return win;
1535 static struct rawinput_device *find_rawinput_device( struct process *process, unsigned short usage_page, unsigned short usage )
1537 struct rawinput_device *device, *end;
1539 for (device = process->rawinput_devices, end = device + process->rawinput_device_count; device != end; device++)
1541 if (device->usage_page != usage_page || device->usage != usage) continue;
1542 return device;
1545 return NULL;
1548 static void prepend_cursor_history( int x, int y, unsigned int time, lparam_t info )
1550 cursor_pos_t *pos = &cursor_history[--cursor_history_latest % ARRAY_SIZE(cursor_history)];
1552 pos->x = x;
1553 pos->y = y;
1554 pos->time = time;
1555 pos->info = info;
1558 /* queue a hardware message into a given thread input */
1559 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1561 user_handle_t win;
1562 struct thread *thread;
1563 struct thread_input *input;
1564 struct hardware_msg_data *msg_data = msg->data;
1565 unsigned int msg_code;
1567 update_input_key_state( desktop, desktop->keystate, msg->msg, msg->wparam );
1568 last_input_time = get_tick_count();
1569 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1571 if (is_keyboard_msg( msg ))
1573 if (queue_hotkey_message( desktop, msg )) return;
1574 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1575 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1576 msg->lparam &= ~(KF_EXTENDED << 16);
1578 else if (msg->msg != WM_INPUT && msg->msg != WM_INPUT_DEVICE_CHANGE)
1580 if (msg->msg == WM_MOUSEMOVE)
1582 prepend_cursor_history( msg->x, msg->y, msg->time, msg_data->info );
1583 if (update_desktop_cursor_pos( desktop, msg->x, msg->y )) always_queue = 1;
1585 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1586 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1587 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1588 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1589 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1590 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1591 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1593 msg->x = desktop->cursor.x;
1594 msg->y = desktop->cursor.y;
1596 if (msg->win && (thread = get_window_thread( msg->win )))
1598 input = thread->queue->input;
1599 release_object( thread );
1601 else input = desktop->foreground_input;
1603 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1604 if (!win || !thread)
1606 if (input) update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1607 free_message( msg );
1608 return;
1610 input = thread->queue->input;
1612 if (win != desktop->cursor.win) always_queue = 1;
1613 desktop->cursor.win = win;
1615 if (!always_queue || merge_message( input, msg )) free_message( msg );
1616 else
1618 msg->unique_id = 0; /* will be set once we return it to the app */
1619 list_add_tail( &input->msg_list, &msg->entry );
1620 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1622 release_object( thread );
1625 /* send the low-level hook message for a given hardware message */
1626 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1627 const hw_input_t *input, struct msg_queue *sender )
1629 struct thread *hook_thread;
1630 struct msg_queue *queue;
1631 struct message *msg;
1632 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1633 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1635 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1636 if (!(queue = hook_thread->queue)) return 0;
1637 if (is_queue_hung( queue )) return 0;
1639 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1641 msg->type = MSG_HOOK_LL;
1642 msg->win = 0;
1643 msg->msg = id;
1644 msg->wparam = hardware_msg->msg;
1645 msg->x = hardware_msg->x;
1646 msg->y = hardware_msg->y;
1647 msg->time = hardware_msg->time;
1648 msg->data_size = hardware_msg->data_size;
1649 msg->result = NULL;
1651 if (input->type == INPUT_KEYBOARD)
1653 unsigned short vkey = input->kbd.vkey;
1654 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1655 msg->lparam = (input->kbd.scan << 16) | vkey;
1657 else msg->lparam = input->mouse.data << 16;
1659 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1660 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1662 free_message( msg );
1663 return 0;
1665 msg->result->hardware_msg = hardware_msg;
1666 msg->result->desktop = (struct desktop *)grab_object( desktop );
1667 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1668 set_queue_bits( queue, QS_SENDMESSAGE );
1669 return 1;
1672 /* get the foreground thread for a desktop and a window receiving input */
1673 static struct thread *get_foreground_thread( struct desktop *desktop, user_handle_t window )
1675 /* if desktop has no foreground process, assume the receiving window is */
1676 if (desktop->foreground_input) return get_window_thread( desktop->foreground_input->focus );
1677 if (window) return get_window_thread( window );
1678 return NULL;
1681 struct rawinput_message
1683 struct thread *foreground;
1684 struct desktop *desktop;
1685 struct hw_msg_source source;
1686 unsigned int time;
1687 unsigned int message;
1688 struct hardware_msg_data data;
1689 const void *hid_report;
1692 /* check if process is supposed to receive a WM_INPUT message and eventually queue it */
1693 static int queue_rawinput_message( struct process* process, void *arg )
1695 const struct rawinput_message* raw_msg = arg;
1696 const struct rawinput_device *device = NULL;
1697 struct desktop *target_desktop = NULL, *desktop = NULL;
1698 struct thread *target_thread = NULL, *foreground = NULL;
1699 struct message *msg;
1700 data_size_t report_size;
1701 int wparam = RIM_INPUT;
1703 if (raw_msg->data.rawinput.type == RIM_TYPEMOUSE)
1704 device = process->rawinput_mouse;
1705 else if (raw_msg->data.rawinput.type == RIM_TYPEKEYBOARD)
1706 device = process->rawinput_kbd;
1707 else
1708 device = find_rawinput_device( process, raw_msg->data.rawinput.hid.usage_page, raw_msg->data.rawinput.hid.usage );
1709 if (!device) return 0;
1711 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && !(device->flags & RIDEV_DEVNOTIFY)) return 0;
1713 if (raw_msg->desktop) desktop = (struct desktop *)grab_object( raw_msg->desktop );
1714 else if (!(desktop = get_desktop_obj( process, process->desktop, 0 ))) goto done;
1716 if (raw_msg->foreground) foreground = (struct thread *)grab_object( raw_msg->foreground );
1717 else if (!(foreground = get_foreground_thread( desktop, 0 ))) goto done;
1719 if (process != foreground->process)
1721 if (raw_msg->message == WM_INPUT && !(device->flags & RIDEV_INPUTSINK)) goto done;
1722 if (!(target_thread = get_window_thread( device->target ))) goto done;
1723 if (!(target_desktop = get_thread_desktop( target_thread, 0 ))) goto done;
1724 if (target_desktop != desktop) goto done;
1725 wparam = RIM_INPUTSINK;
1728 if (raw_msg->data.rawinput.type != RIM_TYPEHID || !raw_msg->hid_report) report_size = 0;
1729 else report_size = raw_msg->data.size - sizeof(raw_msg->data);
1731 if (!(msg = alloc_hardware_message( raw_msg->data.info, raw_msg->source, raw_msg->time, report_size )))
1732 goto done;
1734 msg->win = device->target;
1735 msg->msg = raw_msg->message;
1736 msg->wparam = wparam;
1737 msg->lparam = 0;
1738 memcpy( msg->data, &raw_msg->data, sizeof(raw_msg->data) );
1739 if (report_size) memcpy( (struct hardware_msg_data *)msg->data + 1, raw_msg->hid_report, report_size );
1741 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && raw_msg->data.rawinput.type == RIM_TYPEHID)
1743 msg->wparam = raw_msg->data.rawinput.hid.param;
1744 msg->lparam = raw_msg->data.rawinput.hid.device;
1747 queue_hardware_message( desktop, msg, 1 );
1749 done:
1750 if (target_thread) release_object( target_thread );
1751 if (target_desktop) release_object( target_desktop );
1752 if (foreground) release_object( foreground );
1753 if (desktop) release_object( desktop );
1754 return 0;
1757 /* queue a hardware message for a mouse event */
1758 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1759 unsigned int origin, struct msg_queue *sender )
1761 const struct rawinput_device *device;
1762 struct hardware_msg_data *msg_data;
1763 struct rawinput_message raw_msg;
1764 struct message *msg;
1765 struct thread *foreground;
1766 unsigned int i, time, flags;
1767 struct hw_msg_source source = { IMDT_MOUSE, origin };
1768 int wait = 0, x, y;
1770 static const unsigned int messages[] =
1772 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1773 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1774 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1775 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1776 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1777 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1778 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1779 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1780 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1781 0, /* 0x0200 = unused */
1782 0, /* 0x0400 = unused */
1783 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1784 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1787 desktop->cursor.last_change = get_tick_count();
1788 flags = input->mouse.flags;
1789 time = input->mouse.time;
1790 if (!time) time = desktop->cursor.last_change;
1792 if (flags & MOUSEEVENTF_MOVE)
1794 if (flags & MOUSEEVENTF_ABSOLUTE)
1796 x = input->mouse.x;
1797 y = input->mouse.y;
1798 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1799 x == desktop->cursor.x && y == desktop->cursor.y)
1800 flags &= ~MOUSEEVENTF_MOVE;
1802 else
1804 x = desktop->cursor.x + input->mouse.x;
1805 y = desktop->cursor.y + input->mouse.y;
1808 else
1810 x = desktop->cursor.x;
1811 y = desktop->cursor.y;
1814 if ((foreground = get_foreground_thread( desktop, win )))
1816 memset( &raw_msg, 0, sizeof(raw_msg) );
1817 raw_msg.foreground = foreground;
1818 raw_msg.desktop = desktop;
1819 raw_msg.source = source;
1820 raw_msg.time = time;
1821 raw_msg.message = WM_INPUT;
1823 msg_data = &raw_msg.data;
1824 msg_data->info = input->mouse.info;
1825 msg_data->size = sizeof(*msg_data);
1826 msg_data->flags = flags;
1827 msg_data->rawinput.type = RIM_TYPEMOUSE;
1828 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1829 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1830 msg_data->rawinput.mouse.data = input->mouse.data;
1832 enum_processes( queue_rawinput_message, &raw_msg );
1833 release_object( foreground );
1836 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
1838 update_desktop_mouse_state( desktop, flags, x, y, input->mouse.data << 16 );
1839 return 0;
1842 for (i = 0; i < ARRAY_SIZE( messages ); i++)
1844 if (!messages[i]) continue;
1845 if (!(flags & (1 << i))) continue;
1846 flags &= ~(1 << i);
1848 if (!(msg = alloc_hardware_message( input->mouse.info, source, time, 0 ))) return 0;
1849 msg_data = msg->data;
1851 msg->win = get_user_full_handle( win );
1852 msg->msg = messages[i];
1853 msg->wparam = input->mouse.data << 16;
1854 msg->lparam = 0;
1855 msg->x = x;
1856 msg->y = y;
1857 if (origin == IMO_INJECTED) msg_data->flags = LLMHF_INJECTED;
1859 /* specify a sender only when sending the last message */
1860 if (!(flags & ((1 << ARRAY_SIZE( messages )) - 1)))
1862 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1863 queue_hardware_message( desktop, msg, 0 );
1865 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1866 queue_hardware_message( desktop, msg, 0 );
1868 return wait;
1871 /* queue a hardware message for a keyboard event */
1872 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1873 unsigned int origin, struct msg_queue *sender )
1875 struct hw_msg_source source = { IMDT_KEYBOARD, origin };
1876 const struct rawinput_device *device;
1877 struct hardware_msg_data *msg_data;
1878 struct rawinput_message raw_msg;
1879 struct message *msg;
1880 struct thread *foreground;
1881 unsigned char vkey = input->kbd.vkey;
1882 unsigned int message_code, time;
1883 int wait;
1885 if (!(time = input->kbd.time)) time = get_tick_count();
1887 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1889 switch (vkey)
1891 case VK_MENU:
1892 case VK_LMENU:
1893 case VK_RMENU:
1894 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1895 break;
1896 case VK_CONTROL:
1897 case VK_LCONTROL:
1898 case VK_RCONTROL:
1899 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1900 break;
1901 case VK_SHIFT:
1902 case VK_LSHIFT:
1903 case VK_RSHIFT:
1904 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1905 break;
1909 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1910 switch (vkey)
1912 case VK_LMENU:
1913 case VK_RMENU:
1914 if (input->kbd.flags & KEYEVENTF_KEYUP)
1916 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1917 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1918 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1919 message_code = WM_SYSKEYUP;
1920 desktop->keystate[VK_MENU] &= ~0x02;
1922 else
1924 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1925 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1926 message_code = WM_SYSKEYDOWN;
1927 desktop->keystate[VK_MENU] |= 0x02;
1929 break;
1931 case VK_LCONTROL:
1932 case VK_RCONTROL:
1933 /* send WM_SYSKEYUP on release if Alt still pressed */
1934 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1935 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1936 message_code = WM_SYSKEYUP;
1937 desktop->keystate[VK_MENU] &= ~0x02;
1938 break;
1940 default:
1941 /* send WM_SYSKEY for Alt-anykey and for F10 */
1942 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1943 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1944 /* fall through */
1945 case VK_F10:
1946 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1947 desktop->keystate[VK_MENU] &= ~0x02;
1948 break;
1951 if ((foreground = get_foreground_thread( desktop, win )))
1953 memset( &raw_msg, 0, sizeof(raw_msg) );
1954 raw_msg.foreground = foreground;
1955 raw_msg.desktop = desktop;
1956 raw_msg.source = source;
1957 raw_msg.time = time;
1958 raw_msg.message = WM_INPUT;
1960 msg_data = &raw_msg.data;
1961 msg_data->info = input->kbd.info;
1962 msg_data->size = sizeof(*msg_data);
1963 msg_data->flags = input->kbd.flags;
1964 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1965 msg_data->rawinput.kbd.message = message_code;
1966 msg_data->rawinput.kbd.vkey = vkey;
1967 msg_data->rawinput.kbd.scan = input->kbd.scan;
1969 enum_processes( queue_rawinput_message, &raw_msg );
1970 release_object( foreground );
1973 if ((device = current->process->rawinput_kbd) && (device->flags & RIDEV_NOLEGACY))
1975 update_input_key_state( desktop, desktop->keystate, message_code, vkey );
1976 return 0;
1979 if (!(msg = alloc_hardware_message( input->kbd.info, source, time, 0 ))) return 0;
1980 msg_data = msg->data;
1982 msg->win = get_user_full_handle( win );
1983 msg->msg = message_code;
1984 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1985 if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
1987 if (input->kbd.flags & KEYEVENTF_UNICODE && !vkey)
1989 msg->wparam = VK_PACKET;
1991 else
1993 unsigned int flags = 0;
1994 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1995 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1996 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1997 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1999 msg->wparam = vkey;
2000 msg->lparam |= flags << 16;
2001 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
2004 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
2005 queue_hardware_message( desktop, msg, 1 );
2007 return wait;
2010 /* queue a hardware message for a custom type of event */
2011 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
2012 unsigned int origin, const hw_input_t *input )
2014 struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
2015 struct hardware_msg_data *msg_data;
2016 struct rawinput_message raw_msg;
2017 struct message *msg;
2018 data_size_t report_size = 0;
2020 switch (input->hw.msg)
2022 case WM_INPUT:
2023 case WM_INPUT_DEVICE_CHANGE:
2024 memset( &raw_msg, 0, sizeof(raw_msg) );
2025 raw_msg.source = source;
2026 raw_msg.time = get_tick_count();
2027 raw_msg.message = input->hw.msg;
2029 if (input->hw.rawinput.type == RIM_TYPEHID)
2031 raw_msg.hid_report = get_req_data();
2032 report_size = input->hw.rawinput.hid.length * input->hw.rawinput.hid.count;
2035 if (report_size != get_req_data_size())
2037 set_error( STATUS_INVALID_PARAMETER );
2038 return;
2041 msg_data = &raw_msg.data;
2042 msg_data->size = sizeof(*msg_data) + report_size;
2043 msg_data->rawinput = input->hw.rawinput;
2045 enum_processes( queue_rawinput_message, &raw_msg );
2046 return;
2049 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
2051 msg->win = get_user_full_handle( win );
2052 msg->msg = input->hw.msg;
2053 msg->wparam = 0;
2054 msg->lparam = input->hw.lparam;
2055 msg->x = desktop->cursor.x;
2056 msg->y = desktop->cursor.y;
2058 queue_hardware_message( desktop, msg, 1 );
2061 /* check message filter for a hardware message */
2062 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
2063 user_handle_t filter_win, unsigned int first, unsigned int last )
2065 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
2067 /* we can only test the window for a keyboard message since the
2068 * dest window for a mouse message depends on hittest */
2069 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
2070 return 0;
2071 /* the message code is final for a keyboard message, we can simply check it */
2072 return check_msg_filter( msg_code, first, last );
2074 else /* mouse message */
2076 /* we need to check all possible values that the message can have in the end */
2078 if (check_msg_filter( msg_code, first, last )) return 1;
2079 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
2081 /* all other messages can become non-client messages */
2082 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
2084 /* clicks can become double-clicks or non-client double-clicks */
2085 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
2086 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
2088 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2089 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2091 return 0;
2096 /* find a hardware message for the given queue */
2097 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
2098 unsigned int first, unsigned int last, unsigned int flags,
2099 struct get_message_reply *reply )
2101 struct thread_input *input = thread->queue->input;
2102 struct thread *win_thread;
2103 struct list *ptr;
2104 user_handle_t win;
2105 int clear_bits, got_one = 0;
2106 unsigned int msg_code;
2108 ptr = list_head( &input->msg_list );
2109 if (hw_id)
2111 while (ptr)
2113 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2114 if (msg->unique_id == hw_id) break;
2115 ptr = list_next( &input->msg_list, ptr );
2117 if (!ptr) ptr = list_head( &input->msg_list );
2118 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
2121 if (ptr == list_head( &input->msg_list ))
2122 clear_bits = QS_INPUT;
2123 else
2124 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
2126 while (ptr)
2128 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2129 struct hardware_msg_data *data = msg->data;
2131 ptr = list_next( &input->msg_list, ptr );
2132 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
2133 if (!win || !win_thread)
2135 /* no window at all, remove it */
2136 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2137 list_remove( &msg->entry );
2138 free_message( msg );
2139 continue;
2141 if (win_thread != thread)
2143 if (win_thread->queue->input == input)
2145 /* wake the other thread */
2146 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
2147 got_one = 1;
2149 else
2151 /* for another thread input, drop it */
2152 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2153 list_remove( &msg->entry );
2154 free_message( msg );
2156 release_object( win_thread );
2157 continue;
2159 release_object( win_thread );
2161 /* if we already got a message for another thread, or if it doesn't
2162 * match the filter we skip it */
2163 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
2165 clear_bits &= ~get_hardware_msg_bit( msg );
2166 continue;
2169 reply->total = msg->data_size;
2170 if (msg->data_size > get_reply_max_size())
2172 set_error( STATUS_BUFFER_OVERFLOW );
2173 return 1;
2176 /* now we can return it */
2177 if (!msg->unique_id) msg->unique_id = get_unique_id();
2178 reply->type = MSG_HARDWARE;
2179 reply->win = win;
2180 reply->msg = msg_code;
2181 reply->wparam = msg->wparam;
2182 reply->lparam = msg->lparam;
2183 reply->x = msg->x;
2184 reply->y = msg->y;
2185 reply->time = msg->time;
2187 data->hw_id = msg->unique_id;
2188 set_reply_data( msg->data, msg->data_size );
2189 if ((msg->msg == WM_INPUT || msg->msg == WM_INPUT_DEVICE_CHANGE) && (flags & PM_REMOVE))
2190 release_hardware_message( current->queue, data->hw_id );
2191 return 1;
2193 /* nothing found, clear the hardware queue bits */
2194 clear_queue_bits( thread->queue, clear_bits );
2195 return 0;
2198 /* increment (or decrement if 'incr' is negative) the queue paint count */
2199 void inc_queue_paint_count( struct thread *thread, int incr )
2201 struct msg_queue *queue = thread->queue;
2203 assert( queue );
2205 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2207 if (queue->paint_count)
2208 set_queue_bits( queue, QS_PAINT );
2209 else
2210 clear_queue_bits( queue, QS_PAINT );
2214 /* remove all messages and timers belonging to a certain window */
2215 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2217 struct msg_queue *queue = thread->queue;
2218 struct list *ptr;
2219 int i;
2221 if (!queue) return;
2223 /* remove timers */
2225 ptr = list_head( &queue->pending_timers );
2226 while (ptr)
2228 struct list *next = list_next( &queue->pending_timers, ptr );
2229 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2230 if (timer->win == win) free_timer( queue, timer );
2231 ptr = next;
2233 ptr = list_head( &queue->expired_timers );
2234 while (ptr)
2236 struct list *next = list_next( &queue->expired_timers, ptr );
2237 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2238 if (timer->win == win) free_timer( queue, timer );
2239 ptr = next;
2242 /* remove messages */
2243 for (i = 0; i < NB_MSG_KINDS; i++)
2245 struct list *ptr, *next;
2247 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2249 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2250 if (msg->win == win)
2252 if (msg->msg == WM_QUIT && !queue->quit_message)
2254 queue->quit_message = 1;
2255 queue->exit_code = msg->wparam;
2257 remove_queue_message( queue, msg, i );
2262 thread_input_cleanup_window( queue, win );
2265 /* post a message to a window */
2266 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2268 struct message *msg;
2269 struct thread *thread = get_window_thread( win );
2271 if (!thread) return;
2273 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2275 msg->type = MSG_POSTED;
2276 msg->win = get_user_full_handle( win );
2277 msg->msg = message;
2278 msg->wparam = wparam;
2279 msg->lparam = lparam;
2280 msg->result = NULL;
2281 msg->data = NULL;
2282 msg->data_size = 0;
2284 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2286 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2287 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2288 if (message == WM_HOTKEY)
2290 set_queue_bits( thread->queue, QS_HOTKEY );
2291 thread->queue->hotkey_count++;
2294 release_object( thread );
2297 /* send a notify message to a window */
2298 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2300 struct message *msg;
2301 struct thread *thread = get_window_thread( win );
2303 if (!thread) return;
2305 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2307 msg->type = MSG_NOTIFY;
2308 msg->win = get_user_full_handle( win );
2309 msg->msg = message;
2310 msg->wparam = wparam;
2311 msg->lparam = lparam;
2312 msg->result = NULL;
2313 msg->data = NULL;
2314 msg->data_size = 0;
2316 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2318 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2319 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2321 release_object( thread );
2324 /* post a win event */
2325 void post_win_event( struct thread *thread, unsigned int event,
2326 user_handle_t win, unsigned int object_id,
2327 unsigned int child_id, client_ptr_t hook_proc,
2328 const WCHAR *module, data_size_t module_size,
2329 user_handle_t hook)
2331 struct message *msg;
2333 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2335 struct winevent_msg_data *data;
2337 msg->type = MSG_WINEVENT;
2338 msg->win = get_user_full_handle( win );
2339 msg->msg = event;
2340 msg->wparam = object_id;
2341 msg->lparam = child_id;
2342 msg->time = get_tick_count();
2343 msg->result = NULL;
2345 if ((data = malloc( sizeof(*data) + module_size )))
2347 data->hook = hook;
2348 data->tid = get_thread_id( current );
2349 data->hook_proc = hook_proc;
2350 memcpy( data + 1, module, module_size );
2352 msg->data = data;
2353 msg->data_size = sizeof(*data) + module_size;
2355 if (debug_level > 1)
2356 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2357 get_thread_id(thread), event, win, object_id, child_id );
2358 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2359 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2361 else
2362 free( msg );
2366 /* free all hotkeys on a desktop, optionally filtering by window */
2367 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2369 struct hotkey *hotkey, *hotkey2;
2371 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2373 if (!window || hotkey->win == window)
2375 list_remove( &hotkey->entry );
2376 free( hotkey );
2382 /* check if the thread owning the window is hung */
2383 DECL_HANDLER(is_window_hung)
2385 struct thread *thread;
2387 thread = get_window_thread( req->win );
2388 if (thread)
2390 reply->is_hung = is_queue_hung( thread->queue );
2391 release_object( thread );
2393 else reply->is_hung = 0;
2397 /* get the message queue of the current thread */
2398 DECL_HANDLER(get_msg_queue)
2400 struct msg_queue *queue = get_current_queue();
2402 reply->handle = 0;
2403 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2407 /* set the file descriptor associated to the current thread queue */
2408 DECL_HANDLER(set_queue_fd)
2410 struct msg_queue *queue = get_current_queue();
2411 struct file *file;
2412 int unix_fd;
2414 if (queue->fd) /* fd can only be set once */
2416 set_error( STATUS_ACCESS_DENIED );
2417 return;
2419 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2421 if ((unix_fd = get_file_unix_fd( file )) != -1)
2423 if ((unix_fd = dup( unix_fd )) != -1)
2424 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2425 else
2426 file_set_error();
2428 release_object( file );
2432 /* set the current message queue wakeup mask */
2433 DECL_HANDLER(set_queue_mask)
2435 struct msg_queue *queue = get_current_queue();
2437 if (queue)
2439 queue->wake_mask = req->wake_mask;
2440 queue->changed_mask = req->changed_mask;
2441 reply->wake_bits = queue->wake_bits;
2442 reply->changed_bits = queue->changed_bits;
2443 if (is_signaled( queue ))
2445 /* if skip wait is set, do what would have been done in the subsequent wait */
2446 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2447 else wake_up( &queue->obj, 0 );
2453 /* get the current message queue status */
2454 DECL_HANDLER(get_queue_status)
2456 struct msg_queue *queue = current->queue;
2457 if (queue)
2459 reply->wake_bits = queue->wake_bits;
2460 reply->changed_bits = queue->changed_bits;
2461 queue->changed_bits &= ~req->clear_bits;
2463 else reply->wake_bits = reply->changed_bits = 0;
2467 /* send a message to a thread queue */
2468 DECL_HANDLER(send_message)
2470 struct message *msg;
2471 struct msg_queue *send_queue = get_current_queue();
2472 struct msg_queue *recv_queue = NULL;
2473 struct thread *thread = NULL;
2475 if (!(thread = get_thread_from_id( req->id ))) return;
2477 if (!(recv_queue = thread->queue))
2479 set_error( STATUS_INVALID_PARAMETER );
2480 release_object( thread );
2481 return;
2483 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2485 set_error( STATUS_TIMEOUT );
2486 release_object( thread );
2487 return;
2490 if ((msg = mem_alloc( sizeof(*msg) )))
2492 msg->type = req->type;
2493 msg->win = get_user_full_handle( req->win );
2494 msg->msg = req->msg;
2495 msg->wparam = req->wparam;
2496 msg->lparam = req->lparam;
2497 msg->result = NULL;
2498 msg->data = NULL;
2499 msg->data_size = get_req_data_size();
2501 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2503 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2505 free( msg );
2506 release_object( thread );
2507 return;
2510 switch(msg->type)
2512 case MSG_OTHER_PROCESS:
2513 case MSG_ASCII:
2514 case MSG_UNICODE:
2515 case MSG_CALLBACK:
2516 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2518 free_message( msg );
2519 break;
2521 /* fall through */
2522 case MSG_NOTIFY:
2523 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2524 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2525 break;
2526 case MSG_POSTED:
2527 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2528 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2529 if (msg->msg == WM_HOTKEY)
2531 set_queue_bits( recv_queue, QS_HOTKEY );
2532 recv_queue->hotkey_count++;
2534 break;
2535 case MSG_HARDWARE: /* should use send_hardware_message instead */
2536 case MSG_CALLBACK_RESULT: /* cannot send this one */
2537 case MSG_HOOK_LL: /* generated internally */
2538 default:
2539 set_error( STATUS_INVALID_PARAMETER );
2540 free( msg );
2541 break;
2544 release_object( thread );
2547 /* send a hardware message to a thread queue */
2548 DECL_HANDLER(send_hardware_message)
2550 struct thread *thread = NULL;
2551 struct desktop *desktop;
2552 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2553 struct msg_queue *sender = get_current_queue();
2555 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2557 if (req->win)
2559 if (!(thread = get_window_thread( req->win ))) return;
2560 if (desktop != thread->queue->input->desktop)
2562 /* don't allow queuing events to a different desktop */
2563 release_object( desktop );
2564 return;
2568 reply->prev_x = desktop->cursor.x;
2569 reply->prev_y = desktop->cursor.y;
2571 switch (req->input.type)
2573 case INPUT_MOUSE:
2574 reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2575 break;
2576 case INPUT_KEYBOARD:
2577 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2578 break;
2579 case INPUT_HARDWARE:
2580 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2581 break;
2582 default:
2583 set_error( STATUS_INVALID_PARAMETER );
2585 if (thread) release_object( thread );
2587 reply->new_x = desktop->cursor.x;
2588 reply->new_y = desktop->cursor.y;
2589 release_object( desktop );
2592 /* post a quit message to the current queue */
2593 DECL_HANDLER(post_quit_message)
2595 struct msg_queue *queue = get_current_queue();
2597 if (!queue)
2598 return;
2600 queue->quit_message = 1;
2601 queue->exit_code = req->exit_code;
2602 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2605 /* get a message from the current queue */
2606 DECL_HANDLER(get_message)
2608 struct timer *timer;
2609 struct list *ptr;
2610 struct msg_queue *queue = get_current_queue();
2611 user_handle_t get_win = get_user_full_handle( req->get_win );
2612 unsigned int filter = req->flags >> 16;
2614 reply->active_hooks = get_active_hooks();
2616 if (get_win && get_win != 1 && get_win != -1 && !get_user_object( get_win, USER_WINDOW ))
2618 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2619 return;
2622 if (!queue) return;
2623 queue->last_get_msg = current_time;
2624 if (!filter) filter = QS_ALLINPUT;
2626 /* first check for sent messages */
2627 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2629 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2630 receive_message( queue, msg, reply );
2631 return;
2634 /* clear changed bits so we can wait on them if we don't find a message */
2635 if (filter & QS_POSTMESSAGE)
2637 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2638 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2640 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2641 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2643 /* then check for posted messages */
2644 if ((filter & QS_POSTMESSAGE) &&
2645 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2646 return;
2648 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2649 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2650 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2651 return;
2653 /* only check for quit messages if not posted messages pending */
2654 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2655 return;
2657 /* then check for any raw hardware message */
2658 if ((filter & QS_INPUT) &&
2659 filter_contains_hw_range( req->get_first, req->get_last ) &&
2660 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2661 return;
2663 /* now check for WM_PAINT */
2664 if ((filter & QS_PAINT) &&
2665 queue->paint_count &&
2666 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2667 (reply->win = find_window_to_repaint( get_win, current )))
2669 reply->type = MSG_POSTED;
2670 reply->msg = WM_PAINT;
2671 reply->wparam = 0;
2672 reply->lparam = 0;
2673 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2674 return;
2677 /* now check for timer */
2678 if ((filter & QS_TIMER) &&
2679 (timer = find_expired_timer( queue, get_win, req->get_first,
2680 req->get_last, (req->flags & PM_REMOVE) )))
2682 reply->type = MSG_POSTED;
2683 reply->win = timer->win;
2684 reply->msg = timer->msg;
2685 reply->wparam = timer->id;
2686 reply->lparam = timer->lparam;
2687 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2688 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2689 set_event( current->process->idle_event );
2690 return;
2693 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2694 queue->wake_mask = req->wake_mask;
2695 queue->changed_mask = req->changed_mask;
2696 set_error( STATUS_PENDING ); /* FIXME */
2700 /* reply to a sent message */
2701 DECL_HANDLER(reply_message)
2703 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2704 else if (current->queue->recv_result)
2705 reply_message( current->queue, req->result, 0, req->remove,
2706 get_req_data(), get_req_data_size() );
2710 /* accept the current hardware message */
2711 DECL_HANDLER(accept_hardware_message)
2713 if (current->queue)
2714 release_hardware_message( current->queue, req->hw_id );
2715 else
2716 set_error( STATUS_ACCESS_DENIED );
2720 /* retrieve the reply for the last message sent */
2721 DECL_HANDLER(get_message_reply)
2723 struct message_result *result;
2724 struct list *entry;
2725 struct msg_queue *queue = current->queue;
2727 if (queue)
2729 set_error( STATUS_PENDING );
2730 reply->result = 0;
2732 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2734 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2735 if (result->replied || req->cancel)
2737 if (result->replied)
2739 reply->result = result->result;
2740 set_error( result->error );
2741 if (result->data)
2743 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2744 set_reply_data_ptr( result->data, data_len );
2745 result->data = NULL;
2746 result->data_size = 0;
2749 remove_result_from_sender( result );
2751 entry = list_head( &queue->send_result );
2752 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2753 else
2755 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2756 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2757 else clear_queue_bits( queue, QS_SMRESULT );
2761 else set_error( STATUS_ACCESS_DENIED );
2765 /* set a window timer */
2766 DECL_HANDLER(set_win_timer)
2768 struct timer *timer;
2769 struct msg_queue *queue;
2770 struct thread *thread = NULL;
2771 user_handle_t win = 0;
2772 lparam_t id = req->id;
2774 if (req->win)
2776 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2778 set_error( STATUS_INVALID_HANDLE );
2779 return;
2781 if (thread->process != current->process)
2783 release_object( thread );
2784 set_error( STATUS_ACCESS_DENIED );
2785 return;
2787 queue = thread->queue;
2788 /* remove it if it existed already */
2789 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2791 else
2793 queue = get_current_queue();
2794 /* look for a timer with this id */
2795 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2797 /* free and reuse id */
2798 free_timer( queue, timer );
2800 else
2802 lparam_t end_id = queue->next_timer_id;
2804 /* find a free id for it */
2805 while (1)
2807 id = queue->next_timer_id;
2808 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2810 if (!find_timer( queue, 0, req->msg, id )) break;
2811 if (queue->next_timer_id == end_id)
2813 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2814 return;
2820 if ((timer = set_timer( queue, req->rate )))
2822 timer->win = win;
2823 timer->msg = req->msg;
2824 timer->id = id;
2825 timer->lparam = req->lparam;
2826 reply->id = id;
2828 if (thread) release_object( thread );
2831 /* kill a window timer */
2832 DECL_HANDLER(kill_win_timer)
2834 struct timer *timer;
2835 struct thread *thread;
2836 user_handle_t win = 0;
2838 if (req->win)
2840 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2842 set_error( STATUS_INVALID_HANDLE );
2843 return;
2845 if (thread->process != current->process)
2847 release_object( thread );
2848 set_error( STATUS_ACCESS_DENIED );
2849 return;
2852 else thread = (struct thread *)grab_object( current );
2854 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2855 free_timer( thread->queue, timer );
2856 else
2857 set_error( STATUS_INVALID_PARAMETER );
2859 release_object( thread );
2862 DECL_HANDLER(register_hotkey)
2864 struct desktop *desktop;
2865 user_handle_t win_handle = req->window;
2866 struct hotkey *hotkey;
2867 struct hotkey *new_hotkey = NULL;
2868 struct thread *thread;
2869 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2871 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2873 if (win_handle)
2875 if (!(win_handle = get_valid_window_handle( win_handle )))
2877 release_object( desktop );
2878 return;
2881 thread = get_window_thread( win_handle );
2882 if (thread) release_object( thread );
2884 if (thread != current)
2886 release_object( desktop );
2887 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2888 return;
2892 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2894 if (req->vkey == hotkey->vkey &&
2895 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2897 release_object( desktop );
2898 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2899 return;
2901 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2902 new_hotkey = hotkey;
2905 if (new_hotkey)
2907 reply->replaced = 1;
2908 reply->flags = new_hotkey->flags;
2909 reply->vkey = new_hotkey->vkey;
2911 else
2913 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2914 if (new_hotkey)
2916 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2917 new_hotkey->queue = current->queue;
2918 new_hotkey->win = win_handle;
2919 new_hotkey->id = req->id;
2923 if (new_hotkey)
2925 new_hotkey->flags = req->flags;
2926 new_hotkey->vkey = req->vkey;
2929 release_object( desktop );
2932 DECL_HANDLER(unregister_hotkey)
2934 struct desktop *desktop;
2935 user_handle_t win_handle = req->window;
2936 struct hotkey *hotkey;
2937 struct thread *thread;
2939 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2941 if (win_handle)
2943 if (!(win_handle = get_valid_window_handle( win_handle )))
2945 release_object( desktop );
2946 return;
2949 thread = get_window_thread( win_handle );
2950 if (thread) release_object( thread );
2952 if (thread != current)
2954 release_object( desktop );
2955 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2956 return;
2960 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2962 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2963 goto found;
2966 release_object( desktop );
2967 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2968 return;
2970 found:
2971 reply->flags = hotkey->flags;
2972 reply->vkey = hotkey->vkey;
2973 list_remove( &hotkey->entry );
2974 free( hotkey );
2975 release_object( desktop );
2978 /* attach (or detach) thread inputs */
2979 DECL_HANDLER(attach_thread_input)
2981 struct thread *thread_from = get_thread_from_id( req->tid_from );
2982 struct thread *thread_to = get_thread_from_id( req->tid_to );
2984 if (!thread_from || !thread_to)
2986 if (thread_from) release_object( thread_from );
2987 if (thread_to) release_object( thread_to );
2988 return;
2990 if (thread_from != thread_to)
2992 if (req->attach)
2994 if ((thread_to->queue || thread_to == current) &&
2995 (thread_from->queue || thread_from == current))
2996 attach_thread_input( thread_from, thread_to );
2997 else
2998 set_error( STATUS_INVALID_PARAMETER );
3000 else
3002 if (thread_from->queue && thread_to->queue &&
3003 thread_from->queue->input == thread_to->queue->input)
3004 detach_thread_input( thread_from );
3005 else
3006 set_error( STATUS_ACCESS_DENIED );
3009 else set_error( STATUS_ACCESS_DENIED );
3010 release_object( thread_from );
3011 release_object( thread_to );
3015 /* get thread input data */
3016 DECL_HANDLER(get_thread_input)
3018 struct thread *thread = NULL;
3019 struct desktop *desktop;
3020 struct thread_input *input;
3022 if (req->tid)
3024 if (!(thread = get_thread_from_id( req->tid ))) return;
3025 if (!(desktop = get_thread_desktop( thread, 0 )))
3027 release_object( thread );
3028 return;
3030 input = thread->queue ? thread->queue->input : NULL;
3032 else
3034 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3035 input = desktop->foreground_input; /* get the foreground thread info */
3038 if (input)
3040 reply->focus = input->focus;
3041 reply->capture = input->capture;
3042 reply->active = input->active;
3043 reply->menu_owner = input->menu_owner;
3044 reply->move_size = input->move_size;
3045 reply->caret = input->caret;
3046 reply->cursor = input->cursor;
3047 reply->show_count = input->cursor_count;
3048 reply->rect = input->caret_rect;
3051 /* foreground window is active window of foreground thread */
3052 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
3053 if (thread) release_object( thread );
3054 release_object( desktop );
3058 /* retrieve queue keyboard state for current thread or global async state */
3059 DECL_HANDLER(get_key_state)
3061 struct desktop *desktop;
3062 data_size_t size = min( 256, get_reply_max_size() );
3064 if (req->async) /* get global async key state */
3066 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3067 if (req->key >= 0)
3069 reply->state = desktop->keystate[req->key & 0xff];
3070 desktop->keystate[req->key & 0xff] &= ~0x40;
3072 set_reply_data( desktop->keystate, size );
3073 release_object( desktop );
3075 else
3077 struct msg_queue *queue = get_current_queue();
3078 unsigned char *keystate = queue->input->keystate;
3079 if (req->key >= 0)
3081 sync_input_keystate( queue->input );
3082 reply->state = keystate[req->key & 0xff];
3084 set_reply_data( keystate, size );
3089 /* set queue keyboard state for current thread */
3090 DECL_HANDLER(set_key_state)
3092 struct desktop *desktop;
3093 struct msg_queue *queue = get_current_queue();
3094 data_size_t size = min( 256, get_req_data_size() );
3096 memcpy( queue->input->keystate, get_req_data(), size );
3097 memcpy( queue->input->desktop_keystate, queue->input->desktop->keystate, 256 );
3098 if (req->async && (desktop = get_thread_desktop( current, 0 )))
3100 memcpy( desktop->keystate, get_req_data(), size );
3101 release_object( desktop );
3106 /* set the system foreground window */
3107 DECL_HANDLER(set_foreground_window)
3109 struct thread *thread = NULL;
3110 struct desktop *desktop;
3111 struct msg_queue *queue = get_current_queue();
3113 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3114 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
3115 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
3116 reply->send_msg_new = FALSE;
3118 if (is_valid_foreground_window( req->handle ) &&
3119 (thread = get_window_thread( req->handle )) &&
3120 thread->queue->input->desktop == desktop)
3122 set_foreground_input( desktop, thread->queue->input );
3123 reply->send_msg_new = (desktop->foreground_input != queue->input);
3125 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
3127 if (thread) release_object( thread );
3128 release_object( desktop );
3132 /* set the current thread focus window */
3133 DECL_HANDLER(set_focus_window)
3135 struct msg_queue *queue = get_current_queue();
3137 reply->previous = 0;
3138 if (queue && check_queue_input_window( queue, req->handle ))
3140 reply->previous = queue->input->focus;
3141 queue->input->focus = get_user_full_handle( req->handle );
3146 /* set the current thread active window */
3147 DECL_HANDLER(set_active_window)
3149 struct msg_queue *queue = get_current_queue();
3151 reply->previous = 0;
3152 if (queue && check_queue_input_window( queue, req->handle ))
3154 if (!req->handle || make_window_active( req->handle ))
3156 reply->previous = queue->input->active;
3157 queue->input->active = get_user_full_handle( req->handle );
3159 else set_error( STATUS_INVALID_HANDLE );
3164 /* set the current thread capture window */
3165 DECL_HANDLER(set_capture_window)
3167 struct msg_queue *queue = get_current_queue();
3169 reply->previous = reply->full_handle = 0;
3170 if (queue && check_queue_input_window( queue, req->handle ))
3172 struct thread_input *input = queue->input;
3174 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
3175 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
3177 set_error(STATUS_ACCESS_DENIED);
3178 return;
3180 reply->previous = input->capture;
3181 input->capture = get_user_full_handle( req->handle );
3182 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
3183 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
3184 reply->full_handle = input->capture;
3189 /* Set the current thread caret window */
3190 DECL_HANDLER(set_caret_window)
3192 struct msg_queue *queue = get_current_queue();
3194 reply->previous = 0;
3195 if (queue && check_queue_input_window( queue, req->handle ))
3197 struct thread_input *input = queue->input;
3199 reply->previous = input->caret;
3200 reply->old_rect = input->caret_rect;
3201 reply->old_hide = input->caret_hide;
3202 reply->old_state = input->caret_state;
3204 set_caret_window( input, get_user_full_handle(req->handle) );
3205 input->caret_rect.right = input->caret_rect.left + req->width;
3206 input->caret_rect.bottom = input->caret_rect.top + req->height;
3211 /* Set the current thread caret information */
3212 DECL_HANDLER(set_caret_info)
3214 struct msg_queue *queue = get_current_queue();
3215 struct thread_input *input;
3217 if (!queue) return;
3218 input = queue->input;
3219 reply->full_handle = input->caret;
3220 reply->old_rect = input->caret_rect;
3221 reply->old_hide = input->caret_hide;
3222 reply->old_state = input->caret_state;
3224 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3226 set_error( STATUS_ACCESS_DENIED );
3227 return;
3229 if (req->flags & SET_CARET_POS)
3231 input->caret_rect.right += req->x - input->caret_rect.left;
3232 input->caret_rect.bottom += req->y - input->caret_rect.top;
3233 input->caret_rect.left = req->x;
3234 input->caret_rect.top = req->y;
3236 if (req->flags & SET_CARET_HIDE)
3238 input->caret_hide += req->hide;
3239 if (input->caret_hide < 0) input->caret_hide = 0;
3241 if (req->flags & SET_CARET_STATE)
3243 switch (req->state)
3245 case CARET_STATE_OFF: input->caret_state = 0; break;
3246 case CARET_STATE_ON: input->caret_state = 1; break;
3247 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3248 case CARET_STATE_ON_IF_MOVED:
3249 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3250 break;
3256 /* get the time of the last input event */
3257 DECL_HANDLER(get_last_input_time)
3259 reply->time = last_input_time;
3262 /* set/get the current cursor */
3263 DECL_HANDLER(set_cursor)
3265 struct msg_queue *queue = get_current_queue();
3266 struct thread_input *input;
3268 if (!queue) return;
3269 input = queue->input;
3271 reply->prev_handle = input->cursor;
3272 reply->prev_count = input->cursor_count;
3273 reply->prev_x = input->desktop->cursor.x;
3274 reply->prev_y = input->desktop->cursor.y;
3276 if (req->flags & SET_CURSOR_HANDLE)
3278 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3280 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3281 return;
3283 input->cursor = req->handle;
3285 if (req->flags & SET_CURSOR_COUNT)
3287 queue->cursor_count += req->show_count;
3288 input->cursor_count += req->show_count;
3290 if (req->flags & SET_CURSOR_POS)
3292 set_cursor_pos( input->desktop, req->x, req->y );
3294 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3296 struct desktop *desktop = input->desktop;
3298 /* only the desktop owner can set the message */
3299 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3300 desktop->cursor.clip_msg = req->clip_msg;
3302 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip, 0 );
3305 reply->new_x = input->desktop->cursor.x;
3306 reply->new_y = input->desktop->cursor.y;
3307 reply->new_clip = input->desktop->cursor.clip;
3308 reply->last_change = input->desktop->cursor.last_change;
3311 /* Get the history of the 64 last cursor positions */
3312 DECL_HANDLER(get_cursor_history)
3314 cursor_pos_t *pos;
3315 unsigned int i, count = min( 64, get_reply_max_size() / sizeof(*pos) );
3317 if ((pos = set_reply_data_size( count * sizeof(*pos) )))
3318 for (i = 0; i < count; i++)
3319 pos[i] = cursor_history[(i + cursor_history_latest) % ARRAY_SIZE(cursor_history)];
3322 DECL_HANDLER(get_rawinput_buffer)
3324 struct thread_input *input = current->queue->input;
3325 data_size_t size = 0, next_size = 0, pos = 0;
3326 struct list *ptr;
3327 char *buf, *tmp;
3328 int count = 0, buf_size = 16 * sizeof(struct hardware_msg_data);
3330 if (!req->buffer_size) buf = NULL;
3331 else if (!(buf = mem_alloc( buf_size ))) return;
3333 ptr = list_head( &input->msg_list );
3334 while (ptr)
3336 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
3337 struct hardware_msg_data *data = msg->data;
3338 data_size_t extra_size = data->size - sizeof(*data);
3340 ptr = list_next( &input->msg_list, ptr );
3341 if (msg->msg != WM_INPUT) continue;
3343 next_size = req->rawinput_size + extra_size;
3344 if (size + next_size > req->buffer_size) break;
3345 if (pos + data->size > get_reply_max_size()) break;
3346 if (pos + data->size > buf_size)
3348 buf_size += buf_size / 2 + extra_size;
3349 if (!(tmp = realloc( buf, buf_size )))
3351 free( buf );
3352 set_error( STATUS_NO_MEMORY );
3353 return;
3355 buf = tmp;
3358 memcpy( buf + pos, data, data->size );
3359 list_remove( &msg->entry );
3360 free_message( msg );
3362 size += next_size;
3363 pos += sizeof(*data) + extra_size;
3364 count++;
3367 reply->next_size = next_size;
3368 reply->count = count;
3369 set_reply_data_ptr( buf, pos );
3372 DECL_HANDLER(update_rawinput_devices)
3374 const struct rawinput_device *tmp, *devices = get_req_data();
3375 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3376 size_t size = device_count * sizeof(*devices);
3377 struct process *process = current->process;
3379 if (!size)
3381 process->rawinput_device_count = 0;
3382 process->rawinput_mouse = NULL;
3383 process->rawinput_kbd = NULL;
3384 return;
3387 if (!(tmp = realloc( process->rawinput_devices, size )))
3389 set_error( STATUS_NO_MEMORY );
3390 return;
3392 process->rawinput_devices = (struct rawinput_device *)tmp;
3393 process->rawinput_device_count = device_count;
3394 memcpy( process->rawinput_devices, devices, size );
3396 process->rawinput_mouse = find_rawinput_device( process, 1, 2 );
3397 process->rawinput_kbd = find_rawinput_device( process, 1, 6 );