ntdll: Add RtlDosPathNameToRelativeNtPathName_U.
[wine.git] / server / queue.c
blob01fe8d6c060840b29023685f7be9a1214e53c500
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 );
2047 if (raw_msg.foreground) release_object( raw_msg.foreground );
2048 return;
2051 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
2053 msg->win = get_user_full_handle( win );
2054 msg->msg = input->hw.msg;
2055 msg->wparam = 0;
2056 msg->lparam = input->hw.lparam;
2057 msg->x = desktop->cursor.x;
2058 msg->y = desktop->cursor.y;
2060 queue_hardware_message( desktop, msg, 1 );
2063 /* check message filter for a hardware message */
2064 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
2065 user_handle_t filter_win, unsigned int first, unsigned int last )
2067 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
2069 /* we can only test the window for a keyboard message since the
2070 * dest window for a mouse message depends on hittest */
2071 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
2072 return 0;
2073 /* the message code is final for a keyboard message, we can simply check it */
2074 return check_msg_filter( msg_code, first, last );
2076 else /* mouse message */
2078 /* we need to check all possible values that the message can have in the end */
2080 if (check_msg_filter( msg_code, first, last )) return 1;
2081 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
2083 /* all other messages can become non-client messages */
2084 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
2086 /* clicks can become double-clicks or non-client double-clicks */
2087 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
2088 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
2090 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2091 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2093 return 0;
2098 /* find a hardware message for the given queue */
2099 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
2100 unsigned int first, unsigned int last, unsigned int flags,
2101 struct get_message_reply *reply )
2103 struct thread_input *input = thread->queue->input;
2104 struct thread *win_thread;
2105 struct list *ptr;
2106 user_handle_t win;
2107 int clear_bits, got_one = 0;
2108 unsigned int msg_code;
2110 ptr = list_head( &input->msg_list );
2111 if (hw_id)
2113 while (ptr)
2115 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2116 if (msg->unique_id == hw_id) break;
2117 ptr = list_next( &input->msg_list, ptr );
2119 if (!ptr) ptr = list_head( &input->msg_list );
2120 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
2123 if (ptr == list_head( &input->msg_list ))
2124 clear_bits = QS_INPUT;
2125 else
2126 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
2128 while (ptr)
2130 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2131 struct hardware_msg_data *data = msg->data;
2133 ptr = list_next( &input->msg_list, ptr );
2134 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
2135 if (!win || !win_thread)
2137 /* no window at all, remove it */
2138 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2139 list_remove( &msg->entry );
2140 free_message( msg );
2141 continue;
2143 if (win_thread != thread)
2145 if (win_thread->queue->input == input)
2147 /* wake the other thread */
2148 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
2149 got_one = 1;
2151 else
2153 /* for another thread input, drop it */
2154 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2155 list_remove( &msg->entry );
2156 free_message( msg );
2158 release_object( win_thread );
2159 continue;
2161 release_object( win_thread );
2163 /* if we already got a message for another thread, or if it doesn't
2164 * match the filter we skip it */
2165 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
2167 clear_bits &= ~get_hardware_msg_bit( msg );
2168 continue;
2171 reply->total = msg->data_size;
2172 if (msg->data_size > get_reply_max_size())
2174 set_error( STATUS_BUFFER_OVERFLOW );
2175 return 1;
2178 /* now we can return it */
2179 if (!msg->unique_id) msg->unique_id = get_unique_id();
2180 reply->type = MSG_HARDWARE;
2181 reply->win = win;
2182 reply->msg = msg_code;
2183 reply->wparam = msg->wparam;
2184 reply->lparam = msg->lparam;
2185 reply->x = msg->x;
2186 reply->y = msg->y;
2187 reply->time = msg->time;
2189 data->hw_id = msg->unique_id;
2190 set_reply_data( msg->data, msg->data_size );
2191 if ((msg->msg == WM_INPUT || msg->msg == WM_INPUT_DEVICE_CHANGE) && (flags & PM_REMOVE))
2192 release_hardware_message( current->queue, data->hw_id );
2193 return 1;
2195 /* nothing found, clear the hardware queue bits */
2196 clear_queue_bits( thread->queue, clear_bits );
2197 return 0;
2200 /* increment (or decrement if 'incr' is negative) the queue paint count */
2201 void inc_queue_paint_count( struct thread *thread, int incr )
2203 struct msg_queue *queue = thread->queue;
2205 assert( queue );
2207 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2209 if (queue->paint_count)
2210 set_queue_bits( queue, QS_PAINT );
2211 else
2212 clear_queue_bits( queue, QS_PAINT );
2216 /* remove all messages and timers belonging to a certain window */
2217 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2219 struct msg_queue *queue = thread->queue;
2220 struct list *ptr;
2221 int i;
2223 if (!queue) return;
2225 /* remove timers */
2227 ptr = list_head( &queue->pending_timers );
2228 while (ptr)
2230 struct list *next = list_next( &queue->pending_timers, ptr );
2231 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2232 if (timer->win == win) free_timer( queue, timer );
2233 ptr = next;
2235 ptr = list_head( &queue->expired_timers );
2236 while (ptr)
2238 struct list *next = list_next( &queue->expired_timers, ptr );
2239 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2240 if (timer->win == win) free_timer( queue, timer );
2241 ptr = next;
2244 /* remove messages */
2245 for (i = 0; i < NB_MSG_KINDS; i++)
2247 struct list *ptr, *next;
2249 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2251 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2252 if (msg->win == win)
2254 if (msg->msg == WM_QUIT && !queue->quit_message)
2256 queue->quit_message = 1;
2257 queue->exit_code = msg->wparam;
2259 remove_queue_message( queue, msg, i );
2264 thread_input_cleanup_window( queue, win );
2267 /* post a message to a window */
2268 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2270 struct message *msg;
2271 struct thread *thread = get_window_thread( win );
2273 if (!thread) return;
2275 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2277 msg->type = MSG_POSTED;
2278 msg->win = get_user_full_handle( win );
2279 msg->msg = message;
2280 msg->wparam = wparam;
2281 msg->lparam = lparam;
2282 msg->result = NULL;
2283 msg->data = NULL;
2284 msg->data_size = 0;
2286 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2288 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2289 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2290 if (message == WM_HOTKEY)
2292 set_queue_bits( thread->queue, QS_HOTKEY );
2293 thread->queue->hotkey_count++;
2296 release_object( thread );
2299 /* send a notify message to a window */
2300 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2302 struct message *msg;
2303 struct thread *thread = get_window_thread( win );
2305 if (!thread) return;
2307 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2309 msg->type = MSG_NOTIFY;
2310 msg->win = get_user_full_handle( win );
2311 msg->msg = message;
2312 msg->wparam = wparam;
2313 msg->lparam = lparam;
2314 msg->result = NULL;
2315 msg->data = NULL;
2316 msg->data_size = 0;
2318 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2320 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2321 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2323 release_object( thread );
2326 /* post a win event */
2327 void post_win_event( struct thread *thread, unsigned int event,
2328 user_handle_t win, unsigned int object_id,
2329 unsigned int child_id, client_ptr_t hook_proc,
2330 const WCHAR *module, data_size_t module_size,
2331 user_handle_t hook)
2333 struct message *msg;
2335 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2337 struct winevent_msg_data *data;
2339 msg->type = MSG_WINEVENT;
2340 msg->win = get_user_full_handle( win );
2341 msg->msg = event;
2342 msg->wparam = object_id;
2343 msg->lparam = child_id;
2344 msg->time = get_tick_count();
2345 msg->result = NULL;
2347 if ((data = malloc( sizeof(*data) + module_size )))
2349 data->hook = hook;
2350 data->tid = get_thread_id( current );
2351 data->hook_proc = hook_proc;
2352 memcpy( data + 1, module, module_size );
2354 msg->data = data;
2355 msg->data_size = sizeof(*data) + module_size;
2357 if (debug_level > 1)
2358 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2359 get_thread_id(thread), event, win, object_id, child_id );
2360 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2361 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2363 else
2364 free( msg );
2368 /* free all hotkeys on a desktop, optionally filtering by window */
2369 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2371 struct hotkey *hotkey, *hotkey2;
2373 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2375 if (!window || hotkey->win == window)
2377 list_remove( &hotkey->entry );
2378 free( hotkey );
2384 /* check if the thread owning the window is hung */
2385 DECL_HANDLER(is_window_hung)
2387 struct thread *thread;
2389 thread = get_window_thread( req->win );
2390 if (thread)
2392 reply->is_hung = is_queue_hung( thread->queue );
2393 release_object( thread );
2395 else reply->is_hung = 0;
2399 /* get the message queue of the current thread */
2400 DECL_HANDLER(get_msg_queue)
2402 struct msg_queue *queue = get_current_queue();
2404 reply->handle = 0;
2405 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2409 /* set the file descriptor associated to the current thread queue */
2410 DECL_HANDLER(set_queue_fd)
2412 struct msg_queue *queue = get_current_queue();
2413 struct file *file;
2414 int unix_fd;
2416 if (queue->fd) /* fd can only be set once */
2418 set_error( STATUS_ACCESS_DENIED );
2419 return;
2421 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2423 if ((unix_fd = get_file_unix_fd( file )) != -1)
2425 if ((unix_fd = dup( unix_fd )) != -1)
2426 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2427 else
2428 file_set_error();
2430 release_object( file );
2434 /* set the current message queue wakeup mask */
2435 DECL_HANDLER(set_queue_mask)
2437 struct msg_queue *queue = get_current_queue();
2439 if (queue)
2441 queue->wake_mask = req->wake_mask;
2442 queue->changed_mask = req->changed_mask;
2443 reply->wake_bits = queue->wake_bits;
2444 reply->changed_bits = queue->changed_bits;
2445 if (is_signaled( queue ))
2447 /* if skip wait is set, do what would have been done in the subsequent wait */
2448 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2449 else wake_up( &queue->obj, 0 );
2455 /* get the current message queue status */
2456 DECL_HANDLER(get_queue_status)
2458 struct msg_queue *queue = current->queue;
2459 if (queue)
2461 reply->wake_bits = queue->wake_bits;
2462 reply->changed_bits = queue->changed_bits;
2463 queue->changed_bits &= ~req->clear_bits;
2465 else reply->wake_bits = reply->changed_bits = 0;
2469 /* send a message to a thread queue */
2470 DECL_HANDLER(send_message)
2472 struct message *msg;
2473 struct msg_queue *send_queue = get_current_queue();
2474 struct msg_queue *recv_queue = NULL;
2475 struct thread *thread = NULL;
2477 if (!(thread = get_thread_from_id( req->id ))) return;
2479 if (!(recv_queue = thread->queue))
2481 set_error( STATUS_INVALID_PARAMETER );
2482 release_object( thread );
2483 return;
2485 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2487 set_error( STATUS_TIMEOUT );
2488 release_object( thread );
2489 return;
2492 if ((msg = mem_alloc( sizeof(*msg) )))
2494 msg->type = req->type;
2495 msg->win = get_user_full_handle( req->win );
2496 msg->msg = req->msg;
2497 msg->wparam = req->wparam;
2498 msg->lparam = req->lparam;
2499 msg->result = NULL;
2500 msg->data = NULL;
2501 msg->data_size = get_req_data_size();
2503 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2505 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2507 free( msg );
2508 release_object( thread );
2509 return;
2512 switch(msg->type)
2514 case MSG_OTHER_PROCESS:
2515 case MSG_ASCII:
2516 case MSG_UNICODE:
2517 case MSG_CALLBACK:
2518 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2520 free_message( msg );
2521 break;
2523 /* fall through */
2524 case MSG_NOTIFY:
2525 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2526 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2527 break;
2528 case MSG_POSTED:
2529 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2530 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2531 if (msg->msg == WM_HOTKEY)
2533 set_queue_bits( recv_queue, QS_HOTKEY );
2534 recv_queue->hotkey_count++;
2536 break;
2537 case MSG_HARDWARE: /* should use send_hardware_message instead */
2538 case MSG_CALLBACK_RESULT: /* cannot send this one */
2539 case MSG_HOOK_LL: /* generated internally */
2540 default:
2541 set_error( STATUS_INVALID_PARAMETER );
2542 free( msg );
2543 break;
2546 release_object( thread );
2549 /* send a hardware message to a thread queue */
2550 DECL_HANDLER(send_hardware_message)
2552 struct thread *thread = NULL;
2553 struct desktop *desktop;
2554 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2555 struct msg_queue *sender = get_current_queue();
2556 data_size_t size = min( 256, get_reply_max_size() );
2558 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2560 if (req->win)
2562 if (!(thread = get_window_thread( req->win ))) return;
2563 if (desktop != thread->queue->input->desktop)
2565 /* don't allow queuing events to a different desktop */
2566 release_object( desktop );
2567 return;
2571 reply->prev_x = desktop->cursor.x;
2572 reply->prev_y = desktop->cursor.y;
2574 switch (req->input.type)
2576 case INPUT_MOUSE:
2577 reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2578 break;
2579 case INPUT_KEYBOARD:
2580 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2581 break;
2582 case INPUT_HARDWARE:
2583 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2584 break;
2585 default:
2586 set_error( STATUS_INVALID_PARAMETER );
2588 if (thread) release_object( thread );
2590 reply->new_x = desktop->cursor.x;
2591 reply->new_y = desktop->cursor.y;
2592 set_reply_data( desktop->keystate, size );
2593 release_object( desktop );
2596 /* post a quit message to the current queue */
2597 DECL_HANDLER(post_quit_message)
2599 struct msg_queue *queue = get_current_queue();
2601 if (!queue)
2602 return;
2604 queue->quit_message = 1;
2605 queue->exit_code = req->exit_code;
2606 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2609 /* get a message from the current queue */
2610 DECL_HANDLER(get_message)
2612 struct timer *timer;
2613 struct list *ptr;
2614 struct msg_queue *queue = get_current_queue();
2615 user_handle_t get_win = get_user_full_handle( req->get_win );
2616 unsigned int filter = req->flags >> 16;
2618 reply->active_hooks = get_active_hooks();
2620 if (get_win && get_win != 1 && get_win != -1 && !get_user_object( get_win, USER_WINDOW ))
2622 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2623 return;
2626 if (!queue) return;
2627 queue->last_get_msg = current_time;
2628 if (!filter) filter = QS_ALLINPUT;
2630 /* first check for sent messages */
2631 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2633 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2634 receive_message( queue, msg, reply );
2635 return;
2638 /* clear changed bits so we can wait on them if we don't find a message */
2639 if (filter & QS_POSTMESSAGE)
2641 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2642 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2644 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2645 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2647 /* then check for posted messages */
2648 if ((filter & QS_POSTMESSAGE) &&
2649 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2650 return;
2652 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2653 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2654 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2655 return;
2657 /* only check for quit messages if not posted messages pending */
2658 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2659 return;
2661 /* then check for any raw hardware message */
2662 if ((filter & QS_INPUT) &&
2663 filter_contains_hw_range( req->get_first, req->get_last ) &&
2664 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2665 return;
2667 /* now check for WM_PAINT */
2668 if ((filter & QS_PAINT) &&
2669 queue->paint_count &&
2670 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2671 (reply->win = find_window_to_repaint( get_win, current )))
2673 reply->type = MSG_POSTED;
2674 reply->msg = WM_PAINT;
2675 reply->wparam = 0;
2676 reply->lparam = 0;
2677 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2678 return;
2681 /* now check for timer */
2682 if ((filter & QS_TIMER) &&
2683 (timer = find_expired_timer( queue, get_win, req->get_first,
2684 req->get_last, (req->flags & PM_REMOVE) )))
2686 reply->type = MSG_POSTED;
2687 reply->win = timer->win;
2688 reply->msg = timer->msg;
2689 reply->wparam = timer->id;
2690 reply->lparam = timer->lparam;
2691 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2692 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2693 set_event( current->process->idle_event );
2694 return;
2697 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2698 queue->wake_mask = req->wake_mask;
2699 queue->changed_mask = req->changed_mask;
2700 set_error( STATUS_PENDING ); /* FIXME */
2704 /* reply to a sent message */
2705 DECL_HANDLER(reply_message)
2707 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2708 else if (current->queue->recv_result)
2709 reply_message( current->queue, req->result, 0, req->remove,
2710 get_req_data(), get_req_data_size() );
2714 /* accept the current hardware message */
2715 DECL_HANDLER(accept_hardware_message)
2717 if (current->queue)
2718 release_hardware_message( current->queue, req->hw_id );
2719 else
2720 set_error( STATUS_ACCESS_DENIED );
2724 /* retrieve the reply for the last message sent */
2725 DECL_HANDLER(get_message_reply)
2727 struct message_result *result;
2728 struct list *entry;
2729 struct msg_queue *queue = current->queue;
2731 if (queue)
2733 set_error( STATUS_PENDING );
2734 reply->result = 0;
2736 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2738 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2739 if (result->replied || req->cancel)
2741 if (result->replied)
2743 reply->result = result->result;
2744 set_error( result->error );
2745 if (result->data)
2747 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2748 set_reply_data_ptr( result->data, data_len );
2749 result->data = NULL;
2750 result->data_size = 0;
2753 remove_result_from_sender( result );
2755 entry = list_head( &queue->send_result );
2756 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2757 else
2759 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2760 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2761 else clear_queue_bits( queue, QS_SMRESULT );
2765 else set_error( STATUS_ACCESS_DENIED );
2769 /* set a window timer */
2770 DECL_HANDLER(set_win_timer)
2772 struct timer *timer;
2773 struct msg_queue *queue;
2774 struct thread *thread = NULL;
2775 user_handle_t win = 0;
2776 lparam_t id = req->id;
2778 if (req->win)
2780 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2782 set_error( STATUS_INVALID_HANDLE );
2783 return;
2785 if (thread->process != current->process)
2787 release_object( thread );
2788 set_error( STATUS_ACCESS_DENIED );
2789 return;
2791 queue = thread->queue;
2792 /* remove it if it existed already */
2793 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2795 else
2797 queue = get_current_queue();
2798 /* look for a timer with this id */
2799 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2801 /* free and reuse id */
2802 free_timer( queue, timer );
2804 else
2806 lparam_t end_id = queue->next_timer_id;
2808 /* find a free id for it */
2809 while (1)
2811 id = queue->next_timer_id;
2812 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2814 if (!find_timer( queue, 0, req->msg, id )) break;
2815 if (queue->next_timer_id == end_id)
2817 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2818 return;
2824 if ((timer = set_timer( queue, req->rate )))
2826 timer->win = win;
2827 timer->msg = req->msg;
2828 timer->id = id;
2829 timer->lparam = req->lparam;
2830 reply->id = id;
2832 if (thread) release_object( thread );
2835 /* kill a window timer */
2836 DECL_HANDLER(kill_win_timer)
2838 struct timer *timer;
2839 struct thread *thread;
2840 user_handle_t win = 0;
2842 if (req->win)
2844 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2846 set_error( STATUS_INVALID_HANDLE );
2847 return;
2849 if (thread->process != current->process)
2851 release_object( thread );
2852 set_error( STATUS_ACCESS_DENIED );
2853 return;
2856 else thread = (struct thread *)grab_object( current );
2858 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2859 free_timer( thread->queue, timer );
2860 else
2861 set_error( STATUS_INVALID_PARAMETER );
2863 release_object( thread );
2866 DECL_HANDLER(register_hotkey)
2868 struct desktop *desktop;
2869 user_handle_t win_handle = req->window;
2870 struct hotkey *hotkey;
2871 struct hotkey *new_hotkey = NULL;
2872 struct thread *thread;
2873 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2875 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2877 if (win_handle)
2879 if (!(win_handle = get_valid_window_handle( win_handle )))
2881 release_object( desktop );
2882 return;
2885 thread = get_window_thread( win_handle );
2886 if (thread) release_object( thread );
2888 if (thread != current)
2890 release_object( desktop );
2891 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2892 return;
2896 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2898 if (req->vkey == hotkey->vkey &&
2899 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2901 release_object( desktop );
2902 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2903 return;
2905 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2906 new_hotkey = hotkey;
2909 if (new_hotkey)
2911 reply->replaced = 1;
2912 reply->flags = new_hotkey->flags;
2913 reply->vkey = new_hotkey->vkey;
2915 else
2917 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2918 if (new_hotkey)
2920 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2921 new_hotkey->queue = current->queue;
2922 new_hotkey->win = win_handle;
2923 new_hotkey->id = req->id;
2927 if (new_hotkey)
2929 new_hotkey->flags = req->flags;
2930 new_hotkey->vkey = req->vkey;
2933 release_object( desktop );
2936 DECL_HANDLER(unregister_hotkey)
2938 struct desktop *desktop;
2939 user_handle_t win_handle = req->window;
2940 struct hotkey *hotkey;
2941 struct thread *thread;
2943 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2945 if (win_handle)
2947 if (!(win_handle = get_valid_window_handle( win_handle )))
2949 release_object( desktop );
2950 return;
2953 thread = get_window_thread( win_handle );
2954 if (thread) release_object( thread );
2956 if (thread != current)
2958 release_object( desktop );
2959 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2960 return;
2964 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2966 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2967 goto found;
2970 release_object( desktop );
2971 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2972 return;
2974 found:
2975 reply->flags = hotkey->flags;
2976 reply->vkey = hotkey->vkey;
2977 list_remove( &hotkey->entry );
2978 free( hotkey );
2979 release_object( desktop );
2982 /* attach (or detach) thread inputs */
2983 DECL_HANDLER(attach_thread_input)
2985 struct thread *thread_from = get_thread_from_id( req->tid_from );
2986 struct thread *thread_to = get_thread_from_id( req->tid_to );
2988 if (!thread_from || !thread_to)
2990 if (thread_from) release_object( thread_from );
2991 if (thread_to) release_object( thread_to );
2992 return;
2994 if (thread_from != thread_to)
2996 if (req->attach)
2998 if ((thread_to->queue || thread_to == current) &&
2999 (thread_from->queue || thread_from == current))
3000 attach_thread_input( thread_from, thread_to );
3001 else
3002 set_error( STATUS_INVALID_PARAMETER );
3004 else
3006 if (thread_from->queue && thread_to->queue &&
3007 thread_from->queue->input == thread_to->queue->input)
3008 detach_thread_input( thread_from );
3009 else
3010 set_error( STATUS_ACCESS_DENIED );
3013 else set_error( STATUS_ACCESS_DENIED );
3014 release_object( thread_from );
3015 release_object( thread_to );
3019 /* get thread input data */
3020 DECL_HANDLER(get_thread_input)
3022 struct thread *thread = NULL;
3023 struct desktop *desktop;
3024 struct thread_input *input;
3026 if (req->tid)
3028 if (!(thread = get_thread_from_id( req->tid ))) return;
3029 if (!(desktop = get_thread_desktop( thread, 0 )))
3031 release_object( thread );
3032 return;
3034 input = thread->queue ? thread->queue->input : NULL;
3036 else
3038 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3039 input = desktop->foreground_input; /* get the foreground thread info */
3042 if (input)
3044 reply->focus = input->focus;
3045 reply->capture = input->capture;
3046 reply->active = input->active;
3047 reply->menu_owner = input->menu_owner;
3048 reply->move_size = input->move_size;
3049 reply->caret = input->caret;
3050 reply->cursor = input->cursor;
3051 reply->show_count = input->cursor_count;
3052 reply->rect = input->caret_rect;
3055 /* foreground window is active window of foreground thread */
3056 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
3057 if (thread) release_object( thread );
3058 release_object( desktop );
3062 /* retrieve queue keyboard state for current thread or global async state */
3063 DECL_HANDLER(get_key_state)
3065 struct desktop *desktop;
3066 data_size_t size = min( 256, get_reply_max_size() );
3068 if (req->async) /* get global async key state */
3070 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3071 if (req->key >= 0)
3073 reply->state = desktop->keystate[req->key & 0xff];
3074 desktop->keystate[req->key & 0xff] &= ~0x40;
3076 set_reply_data( desktop->keystate, size );
3077 release_object( desktop );
3079 else
3081 struct msg_queue *queue = get_current_queue();
3082 unsigned char *keystate = queue->input->keystate;
3083 if (req->key >= 0)
3085 sync_input_keystate( queue->input );
3086 reply->state = keystate[req->key & 0xff];
3088 set_reply_data( keystate, size );
3093 /* set queue keyboard state for current thread */
3094 DECL_HANDLER(set_key_state)
3096 struct desktop *desktop;
3097 struct msg_queue *queue = get_current_queue();
3098 data_size_t size = min( 256, get_req_data_size() );
3100 memcpy( queue->input->keystate, get_req_data(), size );
3101 memcpy( queue->input->desktop_keystate, queue->input->desktop->keystate, 256 );
3102 if (req->async && (desktop = get_thread_desktop( current, 0 )))
3104 memcpy( desktop->keystate, get_req_data(), size );
3105 release_object( desktop );
3110 /* set the system foreground window */
3111 DECL_HANDLER(set_foreground_window)
3113 struct thread *thread = NULL;
3114 struct desktop *desktop;
3115 struct msg_queue *queue = get_current_queue();
3117 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3118 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
3119 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
3120 reply->send_msg_new = FALSE;
3122 if (is_valid_foreground_window( req->handle ) &&
3123 (thread = get_window_thread( req->handle )) &&
3124 thread->queue->input->desktop == desktop)
3126 set_foreground_input( desktop, thread->queue->input );
3127 reply->send_msg_new = (desktop->foreground_input != queue->input);
3129 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
3131 if (thread) release_object( thread );
3132 release_object( desktop );
3136 /* set the current thread focus window */
3137 DECL_HANDLER(set_focus_window)
3139 struct msg_queue *queue = get_current_queue();
3141 reply->previous = 0;
3142 if (queue && check_queue_input_window( queue, req->handle ))
3144 reply->previous = queue->input->focus;
3145 queue->input->focus = get_user_full_handle( req->handle );
3150 /* set the current thread active window */
3151 DECL_HANDLER(set_active_window)
3153 struct msg_queue *queue = get_current_queue();
3155 reply->previous = 0;
3156 if (queue && check_queue_input_window( queue, req->handle ))
3158 if (!req->handle || make_window_active( req->handle ))
3160 reply->previous = queue->input->active;
3161 queue->input->active = get_user_full_handle( req->handle );
3163 else set_error( STATUS_INVALID_HANDLE );
3168 /* set the current thread capture window */
3169 DECL_HANDLER(set_capture_window)
3171 struct msg_queue *queue = get_current_queue();
3173 reply->previous = reply->full_handle = 0;
3174 if (queue && check_queue_input_window( queue, req->handle ))
3176 struct thread_input *input = queue->input;
3178 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
3179 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
3181 set_error(STATUS_ACCESS_DENIED);
3182 return;
3184 reply->previous = input->capture;
3185 input->capture = get_user_full_handle( req->handle );
3186 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
3187 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
3188 reply->full_handle = input->capture;
3193 /* Set the current thread caret window */
3194 DECL_HANDLER(set_caret_window)
3196 struct msg_queue *queue = get_current_queue();
3198 reply->previous = 0;
3199 if (queue && check_queue_input_window( queue, req->handle ))
3201 struct thread_input *input = queue->input;
3203 reply->previous = input->caret;
3204 reply->old_rect = input->caret_rect;
3205 reply->old_hide = input->caret_hide;
3206 reply->old_state = input->caret_state;
3208 set_caret_window( input, get_user_full_handle(req->handle) );
3209 input->caret_rect.right = input->caret_rect.left + req->width;
3210 input->caret_rect.bottom = input->caret_rect.top + req->height;
3215 /* Set the current thread caret information */
3216 DECL_HANDLER(set_caret_info)
3218 struct msg_queue *queue = get_current_queue();
3219 struct thread_input *input;
3221 if (!queue) return;
3222 input = queue->input;
3223 reply->full_handle = input->caret;
3224 reply->old_rect = input->caret_rect;
3225 reply->old_hide = input->caret_hide;
3226 reply->old_state = input->caret_state;
3228 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3230 set_error( STATUS_ACCESS_DENIED );
3231 return;
3233 if (req->flags & SET_CARET_POS)
3235 input->caret_rect.right += req->x - input->caret_rect.left;
3236 input->caret_rect.bottom += req->y - input->caret_rect.top;
3237 input->caret_rect.left = req->x;
3238 input->caret_rect.top = req->y;
3240 if (req->flags & SET_CARET_HIDE)
3242 input->caret_hide += req->hide;
3243 if (input->caret_hide < 0) input->caret_hide = 0;
3245 if (req->flags & SET_CARET_STATE)
3247 switch (req->state)
3249 case CARET_STATE_OFF: input->caret_state = 0; break;
3250 case CARET_STATE_ON: input->caret_state = 1; break;
3251 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3252 case CARET_STATE_ON_IF_MOVED:
3253 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3254 break;
3260 /* get the time of the last input event */
3261 DECL_HANDLER(get_last_input_time)
3263 reply->time = last_input_time;
3266 /* set/get the current cursor */
3267 DECL_HANDLER(set_cursor)
3269 struct msg_queue *queue = get_current_queue();
3270 struct thread_input *input;
3272 if (!queue) return;
3273 input = queue->input;
3275 reply->prev_handle = input->cursor;
3276 reply->prev_count = input->cursor_count;
3277 reply->prev_x = input->desktop->cursor.x;
3278 reply->prev_y = input->desktop->cursor.y;
3280 if (req->flags & SET_CURSOR_HANDLE)
3282 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3284 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3285 return;
3287 input->cursor = req->handle;
3289 if (req->flags & SET_CURSOR_COUNT)
3291 queue->cursor_count += req->show_count;
3292 input->cursor_count += req->show_count;
3294 if (req->flags & SET_CURSOR_POS)
3296 set_cursor_pos( input->desktop, req->x, req->y );
3298 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3300 struct desktop *desktop = input->desktop;
3302 /* only the desktop owner can set the message */
3303 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3304 desktop->cursor.clip_msg = req->clip_msg;
3306 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip, 0 );
3309 reply->new_x = input->desktop->cursor.x;
3310 reply->new_y = input->desktop->cursor.y;
3311 reply->new_clip = input->desktop->cursor.clip;
3312 reply->last_change = input->desktop->cursor.last_change;
3315 /* Get the history of the 64 last cursor positions */
3316 DECL_HANDLER(get_cursor_history)
3318 cursor_pos_t *pos;
3319 unsigned int i, count = min( 64, get_reply_max_size() / sizeof(*pos) );
3321 if ((pos = set_reply_data_size( count * sizeof(*pos) )))
3322 for (i = 0; i < count; i++)
3323 pos[i] = cursor_history[(i + cursor_history_latest) % ARRAY_SIZE(cursor_history)];
3326 DECL_HANDLER(get_rawinput_buffer)
3328 struct thread_input *input = current->queue->input;
3329 data_size_t size = 0, next_size = 0, pos = 0;
3330 struct list *ptr;
3331 char *buf, *tmp;
3332 int count = 0, buf_size = 16 * sizeof(struct hardware_msg_data);
3334 if (!req->buffer_size) buf = NULL;
3335 else if (!(buf = mem_alloc( buf_size ))) return;
3337 ptr = list_head( &input->msg_list );
3338 while (ptr)
3340 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
3341 struct hardware_msg_data *data = msg->data;
3342 data_size_t extra_size = data->size - sizeof(*data);
3344 ptr = list_next( &input->msg_list, ptr );
3345 if (msg->msg != WM_INPUT) continue;
3347 next_size = req->rawinput_size + extra_size;
3348 if (size + next_size > req->buffer_size) break;
3349 if (pos + data->size > get_reply_max_size()) break;
3350 if (pos + data->size > buf_size)
3352 buf_size += buf_size / 2 + extra_size;
3353 if (!(tmp = realloc( buf, buf_size )))
3355 free( buf );
3356 set_error( STATUS_NO_MEMORY );
3357 return;
3359 buf = tmp;
3362 memcpy( buf + pos, data, data->size );
3363 list_remove( &msg->entry );
3364 free_message( msg );
3366 size += next_size;
3367 pos += sizeof(*data);
3368 count++;
3371 reply->next_size = next_size;
3372 reply->count = count;
3373 set_reply_data_ptr( buf, pos );
3376 DECL_HANDLER(update_rawinput_devices)
3378 const struct rawinput_device *tmp, *devices = get_req_data();
3379 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3380 size_t size = device_count * sizeof(*devices);
3381 struct process *process = current->process;
3383 if (!size)
3385 process->rawinput_device_count = 0;
3386 process->rawinput_mouse = NULL;
3387 process->rawinput_kbd = NULL;
3388 return;
3391 if (!(tmp = realloc( process->rawinput_devices, size )))
3393 set_error( STATUS_NO_MEMORY );
3394 return;
3396 process->rawinput_devices = (struct rawinput_device *)tmp;
3397 process->rawinput_device_count = device_count;
3398 memcpy( process->rawinput_devices, devices, size );
3400 process->rawinput_mouse = find_rawinput_device( process, 1, 2 );
3401 process->rawinput_kbd = find_rawinput_device( process, 1, 6 );