d3d11: Create dxgi resource objects for buffers.
[wine.git] / server / queue.c
blob934cf358059b967c334eb69ac50b7e371fcabd88
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_entry *find_rawinput_device( struct process *process, unsigned short usage_page, unsigned short usage )
1537 struct rawinput_device_entry *e;
1539 LIST_FOR_EACH_ENTRY( e, &process->rawinput_devices, struct rawinput_device_entry, entry )
1541 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1542 return e;
1545 return NULL;
1548 static void update_rawinput_device(const struct rawinput_device *device)
1550 struct rawinput_device_entry *e;
1552 if (!(e = find_rawinput_device( current->process, device->usage_page, device->usage )))
1554 if (!(e = mem_alloc( sizeof(*e) ))) return;
1555 list_add_tail( &current->process->rawinput_devices, &e->entry );
1558 if (device->flags & RIDEV_REMOVE)
1560 list_remove( &e->entry );
1561 free( e );
1562 return;
1565 e->device = *device;
1566 e->device.target = get_user_full_handle( e->device.target );
1569 static void prepend_cursor_history( int x, int y, unsigned int time, lparam_t info )
1571 cursor_pos_t *pos = &cursor_history[--cursor_history_latest % ARRAY_SIZE(cursor_history)];
1573 pos->x = x;
1574 pos->y = y;
1575 pos->time = time;
1576 pos->info = info;
1579 /* queue a hardware message into a given thread input */
1580 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1582 user_handle_t win;
1583 struct thread *thread;
1584 struct thread_input *input;
1585 struct hardware_msg_data *msg_data = msg->data;
1586 unsigned int msg_code;
1588 update_input_key_state( desktop, desktop->keystate, msg->msg, msg->wparam );
1589 last_input_time = get_tick_count();
1590 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1592 if (is_keyboard_msg( msg ))
1594 if (queue_hotkey_message( desktop, msg )) return;
1595 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1596 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1597 msg->lparam &= ~(KF_EXTENDED << 16);
1599 else if (msg->msg != WM_INPUT && msg->msg != WM_INPUT_DEVICE_CHANGE)
1601 if (msg->msg == WM_MOUSEMOVE)
1603 prepend_cursor_history( msg->x, msg->y, msg->time, msg_data->info );
1604 if (update_desktop_cursor_pos( desktop, msg->x, msg->y )) always_queue = 1;
1606 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1607 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1608 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1609 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1610 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1611 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1612 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1614 msg->x = desktop->cursor.x;
1615 msg->y = desktop->cursor.y;
1617 if (msg->win && (thread = get_window_thread( msg->win )))
1619 input = thread->queue->input;
1620 release_object( thread );
1622 else input = desktop->foreground_input;
1624 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1625 if (!win || !thread)
1627 if (input) update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1628 free_message( msg );
1629 return;
1631 input = thread->queue->input;
1633 if (win != desktop->cursor.win) always_queue = 1;
1634 desktop->cursor.win = win;
1636 if (!always_queue || merge_message( input, msg )) free_message( msg );
1637 else
1639 msg->unique_id = 0; /* will be set once we return it to the app */
1640 list_add_tail( &input->msg_list, &msg->entry );
1641 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1643 release_object( thread );
1646 /* send the low-level hook message for a given hardware message */
1647 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1648 const hw_input_t *input, struct msg_queue *sender )
1650 struct thread *hook_thread;
1651 struct msg_queue *queue;
1652 struct message *msg;
1653 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1654 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1656 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1657 if (!(queue = hook_thread->queue)) return 0;
1658 if (is_queue_hung( queue )) return 0;
1660 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1662 msg->type = MSG_HOOK_LL;
1663 msg->win = 0;
1664 msg->msg = id;
1665 msg->wparam = hardware_msg->msg;
1666 msg->x = hardware_msg->x;
1667 msg->y = hardware_msg->y;
1668 msg->time = hardware_msg->time;
1669 msg->data_size = hardware_msg->data_size;
1670 msg->result = NULL;
1672 if (input->type == INPUT_KEYBOARD)
1674 unsigned short vkey = input->kbd.vkey;
1675 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1676 msg->lparam = (input->kbd.scan << 16) | vkey;
1678 else msg->lparam = input->mouse.data << 16;
1680 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1681 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1683 free_message( msg );
1684 return 0;
1686 msg->result->hardware_msg = hardware_msg;
1687 msg->result->desktop = (struct desktop *)grab_object( desktop );
1688 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1689 set_queue_bits( queue, QS_SENDMESSAGE );
1690 return 1;
1693 /* get the foreground thread for a desktop and a window receiving input */
1694 static struct thread *get_foreground_thread( struct desktop *desktop, user_handle_t window )
1696 /* if desktop has no foreground process, assume the receiving window is */
1697 if (desktop->foreground_input) return get_window_thread( desktop->foreground_input->focus );
1698 if (window) return get_window_thread( window );
1699 return NULL;
1702 struct rawinput_message
1704 struct thread *foreground;
1705 struct desktop *desktop;
1706 struct hw_msg_source source;
1707 unsigned int time;
1708 unsigned int message;
1709 struct hardware_msg_data data;
1710 const void *hid_report;
1713 /* check if process is supposed to receive a WM_INPUT message and eventually queue it */
1714 static int queue_rawinput_message( struct process* process, void *arg )
1716 const struct rawinput_message* raw_msg = arg;
1717 const struct rawinput_device_entry *entry;
1718 const struct rawinput_device *device = NULL;
1719 struct desktop *target_desktop = NULL, *desktop = NULL;
1720 struct thread *target_thread = NULL, *foreground = NULL;
1721 struct message *msg;
1722 data_size_t report_size;
1723 int wparam = RIM_INPUT;
1725 if (raw_msg->data.rawinput.type == RIM_TYPEMOUSE)
1726 device = process->rawinput_mouse;
1727 else if (raw_msg->data.rawinput.type == RIM_TYPEKEYBOARD)
1728 device = process->rawinput_kbd;
1729 else if ((entry = find_rawinput_device( process, raw_msg->data.rawinput.hid.usage_page, raw_msg->data.rawinput.hid.usage )))
1730 device = &entry->device;
1731 if (!device) return 0;
1733 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && !(device->flags & RIDEV_DEVNOTIFY)) return 0;
1735 if (raw_msg->desktop) desktop = (struct desktop *)grab_object( raw_msg->desktop );
1736 else if (!(desktop = get_desktop_obj( process, process->desktop, 0 ))) goto done;
1738 if (raw_msg->foreground) foreground = (struct thread *)grab_object( raw_msg->foreground );
1739 else if (!(foreground = get_foreground_thread( desktop, 0 ))) goto done;
1741 if (process != foreground->process)
1743 if (raw_msg->message == WM_INPUT && !(device->flags & RIDEV_INPUTSINK)) goto done;
1744 if (!(target_thread = get_window_thread( device->target ))) goto done;
1745 if (!(target_desktop = get_thread_desktop( target_thread, 0 ))) goto done;
1746 if (target_desktop != desktop) goto done;
1747 wparam = RIM_INPUTSINK;
1750 if (raw_msg->data.rawinput.type != RIM_TYPEHID || !raw_msg->hid_report) report_size = 0;
1751 else report_size = raw_msg->data.size - sizeof(raw_msg->data);
1753 if (!(msg = alloc_hardware_message( raw_msg->data.info, raw_msg->source, raw_msg->time, report_size )))
1754 goto done;
1756 msg->win = device->target;
1757 msg->msg = raw_msg->message;
1758 msg->wparam = wparam;
1759 msg->lparam = 0;
1760 memcpy( msg->data, &raw_msg->data, sizeof(raw_msg->data) );
1761 if (report_size) memcpy( (struct hardware_msg_data *)msg->data + 1, raw_msg->hid_report, report_size );
1763 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && raw_msg->data.rawinput.type == RIM_TYPEHID)
1765 msg->wparam = raw_msg->data.rawinput.hid.param;
1766 msg->lparam = raw_msg->data.rawinput.hid.device;
1769 queue_hardware_message( desktop, msg, 1 );
1771 done:
1772 if (target_thread) release_object( target_thread );
1773 if (target_desktop) release_object( target_desktop );
1774 if (foreground) release_object( foreground );
1775 if (desktop) release_object( desktop );
1776 return 0;
1779 /* queue a hardware message for a mouse event */
1780 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1781 unsigned int origin, struct msg_queue *sender )
1783 const struct rawinput_device *device;
1784 struct hardware_msg_data *msg_data;
1785 struct rawinput_message raw_msg;
1786 struct message *msg;
1787 struct thread *foreground;
1788 unsigned int i, time, flags;
1789 struct hw_msg_source source = { IMDT_MOUSE, origin };
1790 int wait = 0, x, y;
1792 static const unsigned int messages[] =
1794 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1795 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1796 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1797 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1798 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1799 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1800 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1801 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1802 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1803 0, /* 0x0200 = unused */
1804 0, /* 0x0400 = unused */
1805 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1806 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1809 desktop->cursor.last_change = get_tick_count();
1810 flags = input->mouse.flags;
1811 time = input->mouse.time;
1812 if (!time) time = desktop->cursor.last_change;
1814 if (flags & MOUSEEVENTF_MOVE)
1816 if (flags & MOUSEEVENTF_ABSOLUTE)
1818 x = input->mouse.x;
1819 y = input->mouse.y;
1820 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1821 x == desktop->cursor.x && y == desktop->cursor.y)
1822 flags &= ~MOUSEEVENTF_MOVE;
1824 else
1826 x = desktop->cursor.x + input->mouse.x;
1827 y = desktop->cursor.y + input->mouse.y;
1830 else
1832 x = desktop->cursor.x;
1833 y = desktop->cursor.y;
1836 if ((foreground = get_foreground_thread( desktop, win )))
1838 memset( &raw_msg, 0, sizeof(raw_msg) );
1839 raw_msg.foreground = foreground;
1840 raw_msg.desktop = desktop;
1841 raw_msg.source = source;
1842 raw_msg.time = time;
1843 raw_msg.message = WM_INPUT;
1845 msg_data = &raw_msg.data;
1846 msg_data->info = input->mouse.info;
1847 msg_data->size = sizeof(*msg_data);
1848 msg_data->flags = flags;
1849 msg_data->rawinput.type = RIM_TYPEMOUSE;
1850 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1851 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1852 msg_data->rawinput.mouse.data = input->mouse.data;
1854 enum_processes( queue_rawinput_message, &raw_msg );
1855 release_object( foreground );
1858 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
1860 update_desktop_mouse_state( desktop, flags, x, y, input->mouse.data << 16 );
1861 return 0;
1864 for (i = 0; i < ARRAY_SIZE( messages ); i++)
1866 if (!messages[i]) continue;
1867 if (!(flags & (1 << i))) continue;
1868 flags &= ~(1 << i);
1870 if (!(msg = alloc_hardware_message( input->mouse.info, source, time, 0 ))) return 0;
1871 msg_data = msg->data;
1873 msg->win = get_user_full_handle( win );
1874 msg->msg = messages[i];
1875 msg->wparam = input->mouse.data << 16;
1876 msg->lparam = 0;
1877 msg->x = x;
1878 msg->y = y;
1879 if (origin == IMO_INJECTED) msg_data->flags = LLMHF_INJECTED;
1881 /* specify a sender only when sending the last message */
1882 if (!(flags & ((1 << ARRAY_SIZE( messages )) - 1)))
1884 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1885 queue_hardware_message( desktop, msg, 0 );
1887 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1888 queue_hardware_message( desktop, msg, 0 );
1890 return wait;
1893 /* queue a hardware message for a keyboard event */
1894 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1895 unsigned int origin, struct msg_queue *sender )
1897 struct hw_msg_source source = { IMDT_KEYBOARD, origin };
1898 const struct rawinput_device *device;
1899 struct hardware_msg_data *msg_data;
1900 struct rawinput_message raw_msg;
1901 struct message *msg;
1902 struct thread *foreground;
1903 unsigned char vkey = input->kbd.vkey;
1904 unsigned int message_code, time;
1905 int wait;
1907 if (!(time = input->kbd.time)) time = get_tick_count();
1909 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1911 switch (vkey)
1913 case VK_MENU:
1914 case VK_LMENU:
1915 case VK_RMENU:
1916 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1917 break;
1918 case VK_CONTROL:
1919 case VK_LCONTROL:
1920 case VK_RCONTROL:
1921 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1922 break;
1923 case VK_SHIFT:
1924 case VK_LSHIFT:
1925 case VK_RSHIFT:
1926 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1927 break;
1931 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1932 switch (vkey)
1934 case VK_LMENU:
1935 case VK_RMENU:
1936 if (input->kbd.flags & KEYEVENTF_KEYUP)
1938 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1939 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1940 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1941 message_code = WM_SYSKEYUP;
1942 desktop->keystate[VK_MENU] &= ~0x02;
1944 else
1946 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1947 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1948 message_code = WM_SYSKEYDOWN;
1949 desktop->keystate[VK_MENU] |= 0x02;
1951 break;
1953 case VK_LCONTROL:
1954 case VK_RCONTROL:
1955 /* send WM_SYSKEYUP on release if Alt still pressed */
1956 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1957 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1958 message_code = WM_SYSKEYUP;
1959 desktop->keystate[VK_MENU] &= ~0x02;
1960 break;
1962 default:
1963 /* send WM_SYSKEY for Alt-anykey and for F10 */
1964 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1965 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1966 /* fall through */
1967 case VK_F10:
1968 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1969 desktop->keystate[VK_MENU] &= ~0x02;
1970 break;
1973 if ((foreground = get_foreground_thread( desktop, win )))
1975 memset( &raw_msg, 0, sizeof(raw_msg) );
1976 raw_msg.foreground = foreground;
1977 raw_msg.desktop = desktop;
1978 raw_msg.source = source;
1979 raw_msg.time = time;
1980 raw_msg.message = WM_INPUT;
1982 msg_data = &raw_msg.data;
1983 msg_data->info = input->kbd.info;
1984 msg_data->size = sizeof(*msg_data);
1985 msg_data->flags = input->kbd.flags;
1986 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1987 msg_data->rawinput.kbd.message = message_code;
1988 msg_data->rawinput.kbd.vkey = vkey;
1989 msg_data->rawinput.kbd.scan = input->kbd.scan;
1991 enum_processes( queue_rawinput_message, &raw_msg );
1992 release_object( foreground );
1995 if ((device = current->process->rawinput_kbd) && (device->flags & RIDEV_NOLEGACY))
1997 update_input_key_state( desktop, desktop->keystate, message_code, vkey );
1998 return 0;
2001 if (!(msg = alloc_hardware_message( input->kbd.info, source, time, 0 ))) return 0;
2002 msg_data = msg->data;
2004 msg->win = get_user_full_handle( win );
2005 msg->msg = message_code;
2006 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
2007 if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
2009 if (input->kbd.flags & KEYEVENTF_UNICODE && !vkey)
2011 msg->wparam = VK_PACKET;
2013 else
2015 unsigned int flags = 0;
2016 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
2017 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
2018 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
2019 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
2021 msg->wparam = vkey;
2022 msg->lparam |= flags << 16;
2023 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
2026 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
2027 queue_hardware_message( desktop, msg, 1 );
2029 return wait;
2032 /* queue a hardware message for a custom type of event */
2033 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
2034 unsigned int origin, const hw_input_t *input )
2036 struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
2037 struct hardware_msg_data *msg_data;
2038 struct rawinput_message raw_msg;
2039 struct message *msg;
2040 data_size_t report_size = 0;
2042 switch (input->hw.msg)
2044 case WM_INPUT:
2045 case WM_INPUT_DEVICE_CHANGE:
2046 memset( &raw_msg, 0, sizeof(raw_msg) );
2047 raw_msg.source = source;
2048 raw_msg.time = get_tick_count();
2049 raw_msg.message = input->hw.msg;
2051 if (input->hw.rawinput.type == RIM_TYPEHID)
2053 raw_msg.hid_report = get_req_data();
2054 report_size = input->hw.rawinput.hid.length * input->hw.rawinput.hid.count;
2057 if (report_size != get_req_data_size())
2059 set_error( STATUS_INVALID_PARAMETER );
2060 return;
2063 msg_data = &raw_msg.data;
2064 msg_data->size = sizeof(*msg_data) + report_size;
2065 msg_data->rawinput = input->hw.rawinput;
2067 enum_processes( queue_rawinput_message, &raw_msg );
2069 if (raw_msg.foreground) release_object( raw_msg.foreground );
2070 return;
2073 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
2075 msg->win = get_user_full_handle( win );
2076 msg->msg = input->hw.msg;
2077 msg->wparam = 0;
2078 msg->lparam = input->hw.lparam;
2079 msg->x = desktop->cursor.x;
2080 msg->y = desktop->cursor.y;
2082 queue_hardware_message( desktop, msg, 1 );
2085 /* check message filter for a hardware message */
2086 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
2087 user_handle_t filter_win, unsigned int first, unsigned int last )
2089 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
2091 /* we can only test the window for a keyboard message since the
2092 * dest window for a mouse message depends on hittest */
2093 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
2094 return 0;
2095 /* the message code is final for a keyboard message, we can simply check it */
2096 return check_msg_filter( msg_code, first, last );
2098 else /* mouse message */
2100 /* we need to check all possible values that the message can have in the end */
2102 if (check_msg_filter( msg_code, first, last )) return 1;
2103 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
2105 /* all other messages can become non-client messages */
2106 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
2108 /* clicks can become double-clicks or non-client double-clicks */
2109 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
2110 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
2112 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2113 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2115 return 0;
2120 /* find a hardware message for the given queue */
2121 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
2122 unsigned int first, unsigned int last, unsigned int flags,
2123 struct get_message_reply *reply )
2125 struct thread_input *input = thread->queue->input;
2126 struct thread *win_thread;
2127 struct list *ptr;
2128 user_handle_t win;
2129 int clear_bits, got_one = 0;
2130 unsigned int msg_code;
2132 ptr = list_head( &input->msg_list );
2133 if (hw_id)
2135 while (ptr)
2137 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2138 if (msg->unique_id == hw_id) break;
2139 ptr = list_next( &input->msg_list, ptr );
2141 if (!ptr) ptr = list_head( &input->msg_list );
2142 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
2145 if (ptr == list_head( &input->msg_list ))
2146 clear_bits = QS_INPUT;
2147 else
2148 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
2150 while (ptr)
2152 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2153 struct hardware_msg_data *data = msg->data;
2155 ptr = list_next( &input->msg_list, ptr );
2156 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
2157 if (!win || !win_thread)
2159 /* no window at all, remove it */
2160 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2161 list_remove( &msg->entry );
2162 free_message( msg );
2163 continue;
2165 if (win_thread != thread)
2167 if (win_thread->queue->input == input)
2169 /* wake the other thread */
2170 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
2171 got_one = 1;
2173 else
2175 /* for another thread input, drop it */
2176 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2177 list_remove( &msg->entry );
2178 free_message( msg );
2180 release_object( win_thread );
2181 continue;
2183 release_object( win_thread );
2185 /* if we already got a message for another thread, or if it doesn't
2186 * match the filter we skip it */
2187 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
2189 clear_bits &= ~get_hardware_msg_bit( msg );
2190 continue;
2193 reply->total = msg->data_size;
2194 if (msg->data_size > get_reply_max_size())
2196 set_error( STATUS_BUFFER_OVERFLOW );
2197 return 1;
2200 /* now we can return it */
2201 if (!msg->unique_id) msg->unique_id = get_unique_id();
2202 reply->type = MSG_HARDWARE;
2203 reply->win = win;
2204 reply->msg = msg_code;
2205 reply->wparam = msg->wparam;
2206 reply->lparam = msg->lparam;
2207 reply->x = msg->x;
2208 reply->y = msg->y;
2209 reply->time = msg->time;
2211 data->hw_id = msg->unique_id;
2212 set_reply_data( msg->data, msg->data_size );
2213 if ((msg->msg == WM_INPUT || msg->msg == WM_INPUT_DEVICE_CHANGE) && (flags & PM_REMOVE))
2214 release_hardware_message( current->queue, data->hw_id );
2215 return 1;
2217 /* nothing found, clear the hardware queue bits */
2218 clear_queue_bits( thread->queue, clear_bits );
2219 return 0;
2222 /* increment (or decrement if 'incr' is negative) the queue paint count */
2223 void inc_queue_paint_count( struct thread *thread, int incr )
2225 struct msg_queue *queue = thread->queue;
2227 assert( queue );
2229 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2231 if (queue->paint_count)
2232 set_queue_bits( queue, QS_PAINT );
2233 else
2234 clear_queue_bits( queue, QS_PAINT );
2238 /* remove all messages and timers belonging to a certain window */
2239 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2241 struct msg_queue *queue = thread->queue;
2242 struct list *ptr;
2243 int i;
2245 if (!queue) return;
2247 /* remove timers */
2249 ptr = list_head( &queue->pending_timers );
2250 while (ptr)
2252 struct list *next = list_next( &queue->pending_timers, ptr );
2253 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2254 if (timer->win == win) free_timer( queue, timer );
2255 ptr = next;
2257 ptr = list_head( &queue->expired_timers );
2258 while (ptr)
2260 struct list *next = list_next( &queue->expired_timers, ptr );
2261 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2262 if (timer->win == win) free_timer( queue, timer );
2263 ptr = next;
2266 /* remove messages */
2267 for (i = 0; i < NB_MSG_KINDS; i++)
2269 struct list *ptr, *next;
2271 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2273 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2274 if (msg->win == win)
2276 if (msg->msg == WM_QUIT && !queue->quit_message)
2278 queue->quit_message = 1;
2279 queue->exit_code = msg->wparam;
2281 remove_queue_message( queue, msg, i );
2286 thread_input_cleanup_window( queue, win );
2289 /* post a message to a window */
2290 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2292 struct message *msg;
2293 struct thread *thread = get_window_thread( win );
2295 if (!thread) return;
2297 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2299 msg->type = MSG_POSTED;
2300 msg->win = get_user_full_handle( win );
2301 msg->msg = message;
2302 msg->wparam = wparam;
2303 msg->lparam = lparam;
2304 msg->result = NULL;
2305 msg->data = NULL;
2306 msg->data_size = 0;
2308 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2310 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2311 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2312 if (message == WM_HOTKEY)
2314 set_queue_bits( thread->queue, QS_HOTKEY );
2315 thread->queue->hotkey_count++;
2318 release_object( thread );
2321 /* send a notify message to a window */
2322 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2324 struct message *msg;
2325 struct thread *thread = get_window_thread( win );
2327 if (!thread) return;
2329 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2331 msg->type = MSG_NOTIFY;
2332 msg->win = get_user_full_handle( win );
2333 msg->msg = message;
2334 msg->wparam = wparam;
2335 msg->lparam = lparam;
2336 msg->result = NULL;
2337 msg->data = NULL;
2338 msg->data_size = 0;
2340 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2342 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2343 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2345 release_object( thread );
2348 /* post a win event */
2349 void post_win_event( struct thread *thread, unsigned int event,
2350 user_handle_t win, unsigned int object_id,
2351 unsigned int child_id, client_ptr_t hook_proc,
2352 const WCHAR *module, data_size_t module_size,
2353 user_handle_t hook)
2355 struct message *msg;
2357 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2359 struct winevent_msg_data *data;
2361 msg->type = MSG_WINEVENT;
2362 msg->win = get_user_full_handle( win );
2363 msg->msg = event;
2364 msg->wparam = object_id;
2365 msg->lparam = child_id;
2366 msg->time = get_tick_count();
2367 msg->result = NULL;
2369 if ((data = malloc( sizeof(*data) + module_size )))
2371 data->hook = hook;
2372 data->tid = get_thread_id( current );
2373 data->hook_proc = hook_proc;
2374 memcpy( data + 1, module, module_size );
2376 msg->data = data;
2377 msg->data_size = sizeof(*data) + module_size;
2379 if (debug_level > 1)
2380 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2381 get_thread_id(thread), event, win, object_id, child_id );
2382 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2383 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2385 else
2386 free( msg );
2390 /* free all hotkeys on a desktop, optionally filtering by window */
2391 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2393 struct hotkey *hotkey, *hotkey2;
2395 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2397 if (!window || hotkey->win == window)
2399 list_remove( &hotkey->entry );
2400 free( hotkey );
2406 /* check if the thread owning the window is hung */
2407 DECL_HANDLER(is_window_hung)
2409 struct thread *thread;
2411 thread = get_window_thread( req->win );
2412 if (thread)
2414 reply->is_hung = is_queue_hung( thread->queue );
2415 release_object( thread );
2417 else reply->is_hung = 0;
2421 /* get the message queue of the current thread */
2422 DECL_HANDLER(get_msg_queue)
2424 struct msg_queue *queue = get_current_queue();
2426 reply->handle = 0;
2427 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2431 /* set the file descriptor associated to the current thread queue */
2432 DECL_HANDLER(set_queue_fd)
2434 struct msg_queue *queue = get_current_queue();
2435 struct file *file;
2436 int unix_fd;
2438 if (queue->fd) /* fd can only be set once */
2440 set_error( STATUS_ACCESS_DENIED );
2441 return;
2443 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2445 if ((unix_fd = get_file_unix_fd( file )) != -1)
2447 if ((unix_fd = dup( unix_fd )) != -1)
2448 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2449 else
2450 file_set_error();
2452 release_object( file );
2456 /* set the current message queue wakeup mask */
2457 DECL_HANDLER(set_queue_mask)
2459 struct msg_queue *queue = get_current_queue();
2461 if (queue)
2463 queue->wake_mask = req->wake_mask;
2464 queue->changed_mask = req->changed_mask;
2465 reply->wake_bits = queue->wake_bits;
2466 reply->changed_bits = queue->changed_bits;
2467 if (is_signaled( queue ))
2469 /* if skip wait is set, do what would have been done in the subsequent wait */
2470 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2471 else wake_up( &queue->obj, 0 );
2477 /* get the current message queue status */
2478 DECL_HANDLER(get_queue_status)
2480 struct msg_queue *queue = current->queue;
2481 if (queue)
2483 reply->wake_bits = queue->wake_bits;
2484 reply->changed_bits = queue->changed_bits;
2485 queue->changed_bits &= ~req->clear_bits;
2487 else reply->wake_bits = reply->changed_bits = 0;
2491 /* send a message to a thread queue */
2492 DECL_HANDLER(send_message)
2494 struct message *msg;
2495 struct msg_queue *send_queue = get_current_queue();
2496 struct msg_queue *recv_queue = NULL;
2497 struct thread *thread = NULL;
2499 if (!(thread = get_thread_from_id( req->id ))) return;
2501 if (!(recv_queue = thread->queue))
2503 set_error( STATUS_INVALID_PARAMETER );
2504 release_object( thread );
2505 return;
2507 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2509 set_error( STATUS_TIMEOUT );
2510 release_object( thread );
2511 return;
2514 if ((msg = mem_alloc( sizeof(*msg) )))
2516 msg->type = req->type;
2517 msg->win = get_user_full_handle( req->win );
2518 msg->msg = req->msg;
2519 msg->wparam = req->wparam;
2520 msg->lparam = req->lparam;
2521 msg->result = NULL;
2522 msg->data = NULL;
2523 msg->data_size = get_req_data_size();
2525 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2527 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2529 free( msg );
2530 release_object( thread );
2531 return;
2534 switch(msg->type)
2536 case MSG_OTHER_PROCESS:
2537 case MSG_ASCII:
2538 case MSG_UNICODE:
2539 case MSG_CALLBACK:
2540 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2542 free_message( msg );
2543 break;
2545 /* fall through */
2546 case MSG_NOTIFY:
2547 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2548 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2549 break;
2550 case MSG_POSTED:
2551 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2552 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2553 if (msg->msg == WM_HOTKEY)
2555 set_queue_bits( recv_queue, QS_HOTKEY );
2556 recv_queue->hotkey_count++;
2558 break;
2559 case MSG_HARDWARE: /* should use send_hardware_message instead */
2560 case MSG_CALLBACK_RESULT: /* cannot send this one */
2561 case MSG_HOOK_LL: /* generated internally */
2562 default:
2563 set_error( STATUS_INVALID_PARAMETER );
2564 free( msg );
2565 break;
2568 release_object( thread );
2571 /* send a hardware message to a thread queue */
2572 DECL_HANDLER(send_hardware_message)
2574 struct thread *thread = NULL;
2575 struct desktop *desktop;
2576 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2577 struct msg_queue *sender = get_current_queue();
2578 data_size_t size = min( 256, get_reply_max_size() );
2580 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2582 if (req->win)
2584 if (!(thread = get_window_thread( req->win ))) return;
2585 if (desktop != thread->queue->input->desktop)
2587 /* don't allow queuing events to a different desktop */
2588 release_object( desktop );
2589 return;
2593 reply->prev_x = desktop->cursor.x;
2594 reply->prev_y = desktop->cursor.y;
2596 switch (req->input.type)
2598 case INPUT_MOUSE:
2599 reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2600 break;
2601 case INPUT_KEYBOARD:
2602 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2603 break;
2604 case INPUT_HARDWARE:
2605 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2606 break;
2607 default:
2608 set_error( STATUS_INVALID_PARAMETER );
2610 if (thread) release_object( thread );
2612 reply->new_x = desktop->cursor.x;
2613 reply->new_y = desktop->cursor.y;
2614 set_reply_data( desktop->keystate, size );
2615 release_object( desktop );
2618 /* post a quit message to the current queue */
2619 DECL_HANDLER(post_quit_message)
2621 struct msg_queue *queue = get_current_queue();
2623 if (!queue)
2624 return;
2626 queue->quit_message = 1;
2627 queue->exit_code = req->exit_code;
2628 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2631 /* get a message from the current queue */
2632 DECL_HANDLER(get_message)
2634 struct timer *timer;
2635 struct list *ptr;
2636 struct msg_queue *queue = get_current_queue();
2637 user_handle_t get_win = get_user_full_handle( req->get_win );
2638 unsigned int filter = req->flags >> 16;
2640 reply->active_hooks = get_active_hooks();
2642 if (get_win && get_win != 1 && get_win != -1 && !get_user_object( get_win, USER_WINDOW ))
2644 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2645 return;
2648 if (!queue) return;
2649 queue->last_get_msg = current_time;
2650 if (!filter) filter = QS_ALLINPUT;
2652 /* first check for sent messages */
2653 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2655 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2656 receive_message( queue, msg, reply );
2657 return;
2660 /* clear changed bits so we can wait on them if we don't find a message */
2661 if (filter & QS_POSTMESSAGE)
2663 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2664 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2666 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2667 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2669 /* then check for posted messages */
2670 if ((filter & QS_POSTMESSAGE) &&
2671 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2672 return;
2674 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2675 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2676 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2677 return;
2679 /* only check for quit messages if not posted messages pending */
2680 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2681 return;
2683 /* then check for any raw hardware message */
2684 if ((filter & QS_INPUT) &&
2685 filter_contains_hw_range( req->get_first, req->get_last ) &&
2686 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2687 return;
2689 /* now check for WM_PAINT */
2690 if ((filter & QS_PAINT) &&
2691 queue->paint_count &&
2692 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2693 (reply->win = find_window_to_repaint( get_win, current )))
2695 reply->type = MSG_POSTED;
2696 reply->msg = WM_PAINT;
2697 reply->wparam = 0;
2698 reply->lparam = 0;
2699 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2700 return;
2703 /* now check for timer */
2704 if ((filter & QS_TIMER) &&
2705 (timer = find_expired_timer( queue, get_win, req->get_first,
2706 req->get_last, (req->flags & PM_REMOVE) )))
2708 reply->type = MSG_POSTED;
2709 reply->win = timer->win;
2710 reply->msg = timer->msg;
2711 reply->wparam = timer->id;
2712 reply->lparam = timer->lparam;
2713 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2714 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2715 set_event( current->process->idle_event );
2716 return;
2719 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2720 queue->wake_mask = req->wake_mask;
2721 queue->changed_mask = req->changed_mask;
2722 set_error( STATUS_PENDING ); /* FIXME */
2726 /* reply to a sent message */
2727 DECL_HANDLER(reply_message)
2729 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2730 else if (current->queue->recv_result)
2731 reply_message( current->queue, req->result, 0, req->remove,
2732 get_req_data(), get_req_data_size() );
2736 /* accept the current hardware message */
2737 DECL_HANDLER(accept_hardware_message)
2739 if (current->queue)
2740 release_hardware_message( current->queue, req->hw_id );
2741 else
2742 set_error( STATUS_ACCESS_DENIED );
2746 /* retrieve the reply for the last message sent */
2747 DECL_HANDLER(get_message_reply)
2749 struct message_result *result;
2750 struct list *entry;
2751 struct msg_queue *queue = current->queue;
2753 if (queue)
2755 set_error( STATUS_PENDING );
2756 reply->result = 0;
2758 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2760 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2761 if (result->replied || req->cancel)
2763 if (result->replied)
2765 reply->result = result->result;
2766 set_error( result->error );
2767 if (result->data)
2769 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2770 set_reply_data_ptr( result->data, data_len );
2771 result->data = NULL;
2772 result->data_size = 0;
2775 remove_result_from_sender( result );
2777 entry = list_head( &queue->send_result );
2778 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2779 else
2781 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2782 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2783 else clear_queue_bits( queue, QS_SMRESULT );
2787 else set_error( STATUS_ACCESS_DENIED );
2791 /* set a window timer */
2792 DECL_HANDLER(set_win_timer)
2794 struct timer *timer;
2795 struct msg_queue *queue;
2796 struct thread *thread = NULL;
2797 user_handle_t win = 0;
2798 lparam_t id = req->id;
2800 if (req->win)
2802 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2804 set_error( STATUS_INVALID_HANDLE );
2805 return;
2807 if (thread->process != current->process)
2809 release_object( thread );
2810 set_error( STATUS_ACCESS_DENIED );
2811 return;
2813 queue = thread->queue;
2814 /* remove it if it existed already */
2815 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2817 else
2819 queue = get_current_queue();
2820 /* look for a timer with this id */
2821 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2823 /* free and reuse id */
2824 free_timer( queue, timer );
2826 else
2828 lparam_t end_id = queue->next_timer_id;
2830 /* find a free id for it */
2831 while (1)
2833 id = queue->next_timer_id;
2834 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2836 if (!find_timer( queue, 0, req->msg, id )) break;
2837 if (queue->next_timer_id == end_id)
2839 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2840 return;
2846 if ((timer = set_timer( queue, req->rate )))
2848 timer->win = win;
2849 timer->msg = req->msg;
2850 timer->id = id;
2851 timer->lparam = req->lparam;
2852 reply->id = id;
2854 if (thread) release_object( thread );
2857 /* kill a window timer */
2858 DECL_HANDLER(kill_win_timer)
2860 struct timer *timer;
2861 struct thread *thread;
2862 user_handle_t win = 0;
2864 if (req->win)
2866 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2868 set_error( STATUS_INVALID_HANDLE );
2869 return;
2871 if (thread->process != current->process)
2873 release_object( thread );
2874 set_error( STATUS_ACCESS_DENIED );
2875 return;
2878 else thread = (struct thread *)grab_object( current );
2880 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2881 free_timer( thread->queue, timer );
2882 else
2883 set_error( STATUS_INVALID_PARAMETER );
2885 release_object( thread );
2888 DECL_HANDLER(register_hotkey)
2890 struct desktop *desktop;
2891 user_handle_t win_handle = req->window;
2892 struct hotkey *hotkey;
2893 struct hotkey *new_hotkey = NULL;
2894 struct thread *thread;
2895 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2897 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2899 if (win_handle)
2901 if (!(win_handle = get_valid_window_handle( win_handle )))
2903 release_object( desktop );
2904 return;
2907 thread = get_window_thread( win_handle );
2908 if (thread) release_object( thread );
2910 if (thread != current)
2912 release_object( desktop );
2913 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2914 return;
2918 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2920 if (req->vkey == hotkey->vkey &&
2921 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2923 release_object( desktop );
2924 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2925 return;
2927 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2928 new_hotkey = hotkey;
2931 if (new_hotkey)
2933 reply->replaced = 1;
2934 reply->flags = new_hotkey->flags;
2935 reply->vkey = new_hotkey->vkey;
2937 else
2939 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2940 if (new_hotkey)
2942 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2943 new_hotkey->queue = current->queue;
2944 new_hotkey->win = win_handle;
2945 new_hotkey->id = req->id;
2949 if (new_hotkey)
2951 new_hotkey->flags = req->flags;
2952 new_hotkey->vkey = req->vkey;
2955 release_object( desktop );
2958 DECL_HANDLER(unregister_hotkey)
2960 struct desktop *desktop;
2961 user_handle_t win_handle = req->window;
2962 struct hotkey *hotkey;
2963 struct thread *thread;
2965 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2967 if (win_handle)
2969 if (!(win_handle = get_valid_window_handle( win_handle )))
2971 release_object( desktop );
2972 return;
2975 thread = get_window_thread( win_handle );
2976 if (thread) release_object( thread );
2978 if (thread != current)
2980 release_object( desktop );
2981 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2982 return;
2986 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2988 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2989 goto found;
2992 release_object( desktop );
2993 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2994 return;
2996 found:
2997 reply->flags = hotkey->flags;
2998 reply->vkey = hotkey->vkey;
2999 list_remove( &hotkey->entry );
3000 free( hotkey );
3001 release_object( desktop );
3004 /* attach (or detach) thread inputs */
3005 DECL_HANDLER(attach_thread_input)
3007 struct thread *thread_from = get_thread_from_id( req->tid_from );
3008 struct thread *thread_to = get_thread_from_id( req->tid_to );
3010 if (!thread_from || !thread_to)
3012 if (thread_from) release_object( thread_from );
3013 if (thread_to) release_object( thread_to );
3014 return;
3016 if (thread_from != thread_to)
3018 if (req->attach)
3020 if ((thread_to->queue || thread_to == current) &&
3021 (thread_from->queue || thread_from == current))
3022 attach_thread_input( thread_from, thread_to );
3023 else
3024 set_error( STATUS_INVALID_PARAMETER );
3026 else
3028 if (thread_from->queue && thread_to->queue &&
3029 thread_from->queue->input == thread_to->queue->input)
3030 detach_thread_input( thread_from );
3031 else
3032 set_error( STATUS_ACCESS_DENIED );
3035 else set_error( STATUS_ACCESS_DENIED );
3036 release_object( thread_from );
3037 release_object( thread_to );
3041 /* get thread input data */
3042 DECL_HANDLER(get_thread_input)
3044 struct thread *thread = NULL;
3045 struct desktop *desktop;
3046 struct thread_input *input;
3048 if (req->tid)
3050 if (!(thread = get_thread_from_id( req->tid ))) return;
3051 if (!(desktop = get_thread_desktop( thread, 0 )))
3053 release_object( thread );
3054 return;
3056 input = thread->queue ? thread->queue->input : NULL;
3058 else
3060 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3061 input = desktop->foreground_input; /* get the foreground thread info */
3064 if (input)
3066 reply->focus = input->focus;
3067 reply->capture = input->capture;
3068 reply->active = input->active;
3069 reply->menu_owner = input->menu_owner;
3070 reply->move_size = input->move_size;
3071 reply->caret = input->caret;
3072 reply->cursor = input->cursor;
3073 reply->show_count = input->cursor_count;
3074 reply->rect = input->caret_rect;
3077 /* foreground window is active window of foreground thread */
3078 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
3079 if (thread) release_object( thread );
3080 release_object( desktop );
3084 /* retrieve queue keyboard state for current thread or global async state */
3085 DECL_HANDLER(get_key_state)
3087 struct desktop *desktop;
3088 data_size_t size = min( 256, get_reply_max_size() );
3090 if (req->async) /* get global async key state */
3092 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3093 if (req->key >= 0)
3095 reply->state = desktop->keystate[req->key & 0xff];
3096 desktop->keystate[req->key & 0xff] &= ~0x40;
3098 set_reply_data( desktop->keystate, size );
3099 release_object( desktop );
3101 else
3103 struct msg_queue *queue = get_current_queue();
3104 unsigned char *keystate = queue->input->keystate;
3105 if (req->key >= 0)
3107 sync_input_keystate( queue->input );
3108 reply->state = keystate[req->key & 0xff];
3110 set_reply_data( keystate, size );
3115 /* set queue keyboard state for current thread */
3116 DECL_HANDLER(set_key_state)
3118 struct desktop *desktop;
3119 struct msg_queue *queue = get_current_queue();
3120 data_size_t size = min( 256, get_req_data_size() );
3122 memcpy( queue->input->keystate, get_req_data(), size );
3123 memcpy( queue->input->desktop_keystate, queue->input->desktop->keystate, 256 );
3124 if (req->async && (desktop = get_thread_desktop( current, 0 )))
3126 memcpy( desktop->keystate, get_req_data(), size );
3127 release_object( desktop );
3132 /* set the system foreground window */
3133 DECL_HANDLER(set_foreground_window)
3135 struct thread *thread = NULL;
3136 struct desktop *desktop;
3137 struct msg_queue *queue = get_current_queue();
3139 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3140 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
3141 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
3142 reply->send_msg_new = FALSE;
3144 if (is_valid_foreground_window( req->handle ) &&
3145 (thread = get_window_thread( req->handle )) &&
3146 thread->queue->input->desktop == desktop)
3148 set_foreground_input( desktop, thread->queue->input );
3149 reply->send_msg_new = (desktop->foreground_input != queue->input);
3151 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
3153 if (thread) release_object( thread );
3154 release_object( desktop );
3158 /* set the current thread focus window */
3159 DECL_HANDLER(set_focus_window)
3161 struct msg_queue *queue = get_current_queue();
3163 reply->previous = 0;
3164 if (queue && check_queue_input_window( queue, req->handle ))
3166 reply->previous = queue->input->focus;
3167 queue->input->focus = get_user_full_handle( req->handle );
3172 /* set the current thread active window */
3173 DECL_HANDLER(set_active_window)
3175 struct msg_queue *queue = get_current_queue();
3177 reply->previous = 0;
3178 if (queue && check_queue_input_window( queue, req->handle ))
3180 if (!req->handle || make_window_active( req->handle ))
3182 reply->previous = queue->input->active;
3183 queue->input->active = get_user_full_handle( req->handle );
3185 else set_error( STATUS_INVALID_HANDLE );
3190 /* set the current thread capture window */
3191 DECL_HANDLER(set_capture_window)
3193 struct msg_queue *queue = get_current_queue();
3195 reply->previous = reply->full_handle = 0;
3196 if (queue && check_queue_input_window( queue, req->handle ))
3198 struct thread_input *input = queue->input;
3200 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
3201 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
3203 set_error(STATUS_ACCESS_DENIED);
3204 return;
3206 reply->previous = input->capture;
3207 input->capture = get_user_full_handle( req->handle );
3208 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
3209 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
3210 reply->full_handle = input->capture;
3215 /* Set the current thread caret window */
3216 DECL_HANDLER(set_caret_window)
3218 struct msg_queue *queue = get_current_queue();
3220 reply->previous = 0;
3221 if (queue && check_queue_input_window( queue, req->handle ))
3223 struct thread_input *input = queue->input;
3225 reply->previous = input->caret;
3226 reply->old_rect = input->caret_rect;
3227 reply->old_hide = input->caret_hide;
3228 reply->old_state = input->caret_state;
3230 set_caret_window( input, get_user_full_handle(req->handle) );
3231 input->caret_rect.right = input->caret_rect.left + req->width;
3232 input->caret_rect.bottom = input->caret_rect.top + req->height;
3237 /* Set the current thread caret information */
3238 DECL_HANDLER(set_caret_info)
3240 struct msg_queue *queue = get_current_queue();
3241 struct thread_input *input;
3243 if (!queue) return;
3244 input = queue->input;
3245 reply->full_handle = input->caret;
3246 reply->old_rect = input->caret_rect;
3247 reply->old_hide = input->caret_hide;
3248 reply->old_state = input->caret_state;
3250 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3252 set_error( STATUS_ACCESS_DENIED );
3253 return;
3255 if (req->flags & SET_CARET_POS)
3257 input->caret_rect.right += req->x - input->caret_rect.left;
3258 input->caret_rect.bottom += req->y - input->caret_rect.top;
3259 input->caret_rect.left = req->x;
3260 input->caret_rect.top = req->y;
3262 if (req->flags & SET_CARET_HIDE)
3264 input->caret_hide += req->hide;
3265 if (input->caret_hide < 0) input->caret_hide = 0;
3267 if (req->flags & SET_CARET_STATE)
3269 switch (req->state)
3271 case CARET_STATE_OFF: input->caret_state = 0; break;
3272 case CARET_STATE_ON: input->caret_state = 1; break;
3273 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3274 case CARET_STATE_ON_IF_MOVED:
3275 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3276 break;
3282 /* get the time of the last input event */
3283 DECL_HANDLER(get_last_input_time)
3285 reply->time = last_input_time;
3288 /* set/get the current cursor */
3289 DECL_HANDLER(set_cursor)
3291 struct msg_queue *queue = get_current_queue();
3292 struct thread_input *input;
3294 if (!queue) return;
3295 input = queue->input;
3297 reply->prev_handle = input->cursor;
3298 reply->prev_count = input->cursor_count;
3299 reply->prev_x = input->desktop->cursor.x;
3300 reply->prev_y = input->desktop->cursor.y;
3302 if (req->flags & SET_CURSOR_HANDLE)
3304 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3306 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3307 return;
3309 input->cursor = req->handle;
3311 if (req->flags & SET_CURSOR_COUNT)
3313 queue->cursor_count += req->show_count;
3314 input->cursor_count += req->show_count;
3316 if (req->flags & SET_CURSOR_POS)
3318 set_cursor_pos( input->desktop, req->x, req->y );
3320 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3322 struct desktop *desktop = input->desktop;
3324 /* only the desktop owner can set the message */
3325 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3326 desktop->cursor.clip_msg = req->clip_msg;
3328 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip, 0 );
3331 reply->new_x = input->desktop->cursor.x;
3332 reply->new_y = input->desktop->cursor.y;
3333 reply->new_clip = input->desktop->cursor.clip;
3334 reply->last_change = input->desktop->cursor.last_change;
3337 /* Get the history of the 64 last cursor positions */
3338 DECL_HANDLER(get_cursor_history)
3340 cursor_pos_t *pos;
3341 unsigned int i, count = min( 64, get_reply_max_size() / sizeof(*pos) );
3343 if ((pos = set_reply_data_size( count * sizeof(*pos) )))
3344 for (i = 0; i < count; i++)
3345 pos[i] = cursor_history[(i + cursor_history_latest) % ARRAY_SIZE(cursor_history)];
3348 DECL_HANDLER(get_rawinput_buffer)
3350 struct thread_input *input = current->queue->input;
3351 data_size_t size = 0, next_size = 0, pos = 0;
3352 struct list *ptr;
3353 char *buf, *tmp;
3354 int count = 0, buf_size = 16 * sizeof(struct hardware_msg_data);
3356 if (!req->buffer_size) buf = NULL;
3357 else if (!(buf = mem_alloc( buf_size ))) return;
3359 ptr = list_head( &input->msg_list );
3360 while (ptr)
3362 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
3363 struct hardware_msg_data *data = msg->data;
3364 data_size_t extra_size = data->size - sizeof(*data);
3366 ptr = list_next( &input->msg_list, ptr );
3367 if (msg->msg != WM_INPUT) continue;
3369 next_size = req->rawinput_size + extra_size;
3370 if (size + next_size > req->buffer_size) break;
3371 if (pos + data->size > get_reply_max_size()) break;
3372 if (pos + data->size > buf_size)
3374 buf_size += buf_size / 2 + extra_size;
3375 if (!(tmp = realloc( buf, buf_size )))
3377 free( buf );
3378 set_error( STATUS_NO_MEMORY );
3379 return;
3381 buf = tmp;
3384 memcpy( buf + pos, data, data->size );
3385 list_remove( &msg->entry );
3386 free_message( msg );
3388 size += next_size;
3389 pos += sizeof(*data);
3390 count++;
3393 reply->next_size = next_size;
3394 reply->count = count;
3395 set_reply_data_ptr( buf, pos );
3398 DECL_HANDLER(update_rawinput_devices)
3400 const struct rawinput_device *devices = get_req_data();
3401 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3402 const struct rawinput_device_entry *e;
3403 unsigned int i;
3405 for (i = 0; i < device_count; ++i)
3407 update_rawinput_device(&devices[i]);
3410 e = find_rawinput_device( current->process, 1, 2 );
3411 current->process->rawinput_mouse = e ? &e->device : NULL;
3412 e = find_rawinput_device( current->process, 1, 6 );
3413 current->process->rawinput_kbd = e ? &e->device : NULL;
3416 DECL_HANDLER(get_rawinput_devices)
3418 struct rawinput_device_entry *e;
3419 struct rawinput_device *devices;
3420 unsigned int i = 0, device_count = list_count( &current->process->rawinput_devices );
3421 unsigned int size = device_count * sizeof(*devices);
3423 reply->device_count = device_count;
3425 /* no buffer provided, nothing else to do */
3426 if (!get_reply_max_size()) return;
3428 if (size > get_reply_max_size())
3429 set_error( STATUS_BUFFER_TOO_SMALL );
3430 else if ((devices = set_reply_data_size( size )))
3432 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
3433 devices[i++] = e->device;