mshtml: Traverse and unlink FormElements using the dispex.
[wine.git] / server / queue.c
blobfcc946ff0cb45de3fe03175979fc521e79f97946
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"
37 #include "ntuser.h"
39 #include "handle.h"
40 #include "file.h"
41 #include "thread.h"
42 #include "process.h"
43 #include "request.h"
44 #include "user.h"
46 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
47 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
49 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
50 #define NB_MSG_KINDS (POST_MESSAGE+1)
53 struct message_result
55 struct list sender_entry; /* entry in sender list */
56 struct message *msg; /* message the result is for */
57 struct message_result *recv_next; /* next in receiver list */
58 struct msg_queue *sender; /* sender queue */
59 struct msg_queue *receiver; /* receiver queue */
60 int replied; /* has it been replied to? */
61 unsigned int error; /* error code to pass back to sender */
62 lparam_t result; /* reply result */
63 struct message *hardware_msg; /* hardware message if low-level hook result */
64 struct desktop *desktop; /* desktop for hardware message */
65 struct message *callback_msg; /* message to queue for callback */
66 void *data; /* message reply data */
67 unsigned int data_size; /* size of message reply data */
68 struct timeout_user *timeout; /* result timeout */
71 struct message
73 struct list entry; /* entry in message list */
74 enum message_type type; /* message type */
75 user_handle_t win; /* window handle */
76 unsigned int msg; /* message code */
77 lparam_t wparam; /* parameters */
78 lparam_t lparam; /* parameters */
79 int x; /* message position */
80 int y;
81 unsigned int time; /* message time */
82 void *data; /* message data for sent messages */
83 unsigned int data_size; /* size of message data */
84 unsigned int unique_id; /* unique id for nested hw message waits */
85 struct message_result *result; /* result in sender queue */
88 struct timer
90 struct list entry; /* entry in timer list */
91 abstime_t when; /* next expiration */
92 unsigned int rate; /* timer rate in ms */
93 user_handle_t win; /* window handle */
94 unsigned int msg; /* message to post */
95 lparam_t id; /* timer id */
96 lparam_t lparam; /* lparam for message */
99 struct thread_input
101 struct object obj; /* object header */
102 struct desktop *desktop; /* desktop that this thread input belongs to */
103 user_handle_t focus; /* focus window */
104 user_handle_t capture; /* capture window */
105 user_handle_t active; /* active window */
106 user_handle_t menu_owner; /* current menu owner window */
107 user_handle_t move_size; /* current moving/resizing window */
108 user_handle_t caret; /* caret window */
109 rectangle_t caret_rect; /* caret rectangle */
110 int caret_hide; /* caret hide count */
111 int caret_state; /* caret on/off state */
112 user_handle_t cursor; /* current cursor */
113 int cursor_count; /* cursor show count */
114 struct list msg_list; /* list of hardware messages */
115 unsigned char keystate[256]; /* state of each key */
116 unsigned char desktop_keystate[256]; /* desktop keystate when keystate was synced */
117 int keystate_lock; /* keystate is locked */
120 struct msg_queue
122 struct object obj; /* object header */
123 struct fd *fd; /* optional file descriptor to poll */
124 unsigned int wake_bits; /* wakeup bits */
125 unsigned int wake_mask; /* wakeup mask */
126 unsigned int changed_bits; /* changed wakeup bits */
127 unsigned int changed_mask; /* changed wakeup mask */
128 int paint_count; /* pending paint messages count */
129 int hotkey_count; /* pending hotkey messages count */
130 int quit_message; /* is there a pending quit message? */
131 int exit_code; /* exit code of pending quit message */
132 int cursor_count; /* per-queue cursor show count */
133 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
134 struct list send_result; /* stack of sent messages waiting for result */
135 struct list callback_result; /* list of callback messages waiting for result */
136 struct message_result *recv_result; /* stack of received messages waiting for result */
137 struct list pending_timers; /* list of pending timers */
138 struct list expired_timers; /* list of expired timers */
139 lparam_t next_timer_id; /* id for the next timer with a 0 window */
140 struct timeout_user *timeout; /* timeout for next timer to expire */
141 struct thread_input *input; /* thread input descriptor */
142 struct hook_table *hooks; /* hook table */
143 timeout_t last_get_msg; /* time of last get message call */
144 int keystate_lock; /* owns an input keystate lock */
147 struct hotkey
149 struct list entry; /* entry in desktop hotkey list */
150 struct msg_queue *queue; /* queue owning this hotkey */
151 user_handle_t win; /* window handle */
152 int id; /* hotkey id */
153 unsigned int vkey; /* virtual key code */
154 unsigned int flags; /* key modifiers */
157 static void msg_queue_dump( struct object *obj, int verbose );
158 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
159 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
160 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
161 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
162 static void msg_queue_destroy( struct object *obj );
163 static void msg_queue_poll_event( struct fd *fd, int event );
164 static void thread_input_dump( struct object *obj, int verbose );
165 static void thread_input_destroy( struct object *obj );
166 static void timer_callback( void *private );
168 static const struct object_ops msg_queue_ops =
170 sizeof(struct msg_queue), /* size */
171 &no_type, /* type */
172 msg_queue_dump, /* dump */
173 msg_queue_add_queue, /* add_queue */
174 msg_queue_remove_queue, /* remove_queue */
175 msg_queue_signaled, /* signaled */
176 msg_queue_satisfied, /* satisfied */
177 no_signal, /* signal */
178 no_get_fd, /* get_fd */
179 default_map_access, /* map_access */
180 default_get_sd, /* get_sd */
181 default_set_sd, /* set_sd */
182 no_get_full_name, /* get_full_name */
183 no_lookup_name, /* lookup_name */
184 no_link_name, /* link_name */
185 NULL, /* unlink_name */
186 no_open_file, /* open_file */
187 no_kernel_obj_list, /* get_kernel_obj_list */
188 no_close_handle, /* close_handle */
189 msg_queue_destroy /* destroy */
192 static const struct fd_ops msg_queue_fd_ops =
194 NULL, /* get_poll_events */
195 msg_queue_poll_event, /* poll_event */
196 NULL, /* flush */
197 NULL, /* get_fd_type */
198 NULL, /* ioctl */
199 NULL, /* queue_async */
200 NULL, /* reselect_async */
201 NULL /* cancel async */
205 static const struct object_ops thread_input_ops =
207 sizeof(struct thread_input), /* size */
208 &no_type, /* type */
209 thread_input_dump, /* dump */
210 no_add_queue, /* add_queue */
211 NULL, /* remove_queue */
212 NULL, /* signaled */
213 NULL, /* satisfied */
214 no_signal, /* signal */
215 no_get_fd, /* get_fd */
216 default_map_access, /* map_access */
217 default_get_sd, /* get_sd */
218 default_set_sd, /* set_sd */
219 no_get_full_name, /* get_full_name */
220 no_lookup_name, /* lookup_name */
221 no_link_name, /* link_name */
222 NULL, /* unlink_name */
223 no_open_file, /* open_file */
224 no_kernel_obj_list, /* get_kernel_obj_list */
225 no_close_handle, /* close_handle */
226 thread_input_destroy /* destroy */
229 /* pointer to input structure of foreground thread */
230 static unsigned int last_input_time;
232 static cursor_pos_t cursor_history[64];
233 static unsigned int cursor_history_latest;
235 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
236 static void free_message( struct message *msg );
238 /* set the caret window in a given thread input */
239 static void set_caret_window( struct thread_input *input, user_handle_t win )
241 if (!win || win != input->caret)
243 input->caret_rect.left = 0;
244 input->caret_rect.top = 0;
245 input->caret_rect.right = 0;
246 input->caret_rect.bottom = 0;
248 input->caret = win;
249 input->caret_hide = 1;
250 input->caret_state = 0;
253 /* create a thread input object */
254 static struct thread_input *create_thread_input( struct thread *thread )
256 struct thread_input *input;
258 if ((input = alloc_object( &thread_input_ops )))
260 input->focus = 0;
261 input->capture = 0;
262 input->active = 0;
263 input->menu_owner = 0;
264 input->move_size = 0;
265 input->cursor = 0;
266 input->cursor_count = 0;
267 list_init( &input->msg_list );
268 set_caret_window( input, 0 );
269 memset( input->keystate, 0, sizeof(input->keystate) );
270 input->keystate_lock = 0;
272 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
274 release_object( input );
275 return NULL;
277 memcpy( input->desktop_keystate, input->desktop->keystate, sizeof(input->desktop_keystate) );
279 return input;
282 /* create a message queue object */
283 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
285 struct thread_input *new_input = NULL;
286 struct msg_queue *queue;
287 int i;
289 if (!input)
291 if (!(new_input = create_thread_input( thread ))) return NULL;
292 input = new_input;
295 if ((queue = alloc_object( &msg_queue_ops )))
297 queue->fd = NULL;
298 queue->wake_bits = 0;
299 queue->wake_mask = 0;
300 queue->changed_bits = 0;
301 queue->changed_mask = 0;
302 queue->paint_count = 0;
303 queue->hotkey_count = 0;
304 queue->quit_message = 0;
305 queue->cursor_count = 0;
306 queue->recv_result = NULL;
307 queue->next_timer_id = 0x7fff;
308 queue->timeout = NULL;
309 queue->input = (struct thread_input *)grab_object( input );
310 queue->hooks = NULL;
311 queue->last_get_msg = current_time;
312 queue->keystate_lock = 0;
313 list_init( &queue->send_result );
314 list_init( &queue->callback_result );
315 list_init( &queue->pending_timers );
316 list_init( &queue->expired_timers );
317 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
319 thread->queue = queue;
321 if (new_input) release_object( new_input );
322 return queue;
325 /* free the message queue of a thread at thread exit */
326 void free_msg_queue( struct thread *thread )
328 remove_thread_hooks( thread );
329 if (!thread->queue) return;
330 release_object( thread->queue );
331 thread->queue = NULL;
334 /* synchronize thread input keystate with the desktop */
335 static void sync_input_keystate( struct thread_input *input )
337 int i;
338 if (!input->desktop || input->keystate_lock) return;
339 for (i = 0; i < sizeof(input->keystate); ++i)
341 if (input->desktop_keystate[i] == input->desktop->keystate[i]) continue;
342 input->keystate[i] = input->desktop_keystate[i] = input->desktop->keystate[i];
346 /* locks thread input keystate to prevent synchronization */
347 static void lock_input_keystate( struct thread_input *input )
349 input->keystate_lock++;
352 /* unlock the thread input keystate and synchronize it again */
353 static void unlock_input_keystate( struct thread_input *input )
355 input->keystate_lock--;
356 if (!input->keystate_lock) sync_input_keystate( input );
359 /* change the thread input data of a given thread */
360 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
362 struct msg_queue *queue = thread->queue;
364 if (!queue)
366 thread->queue = create_msg_queue( thread, new_input );
367 return thread->queue != NULL;
369 if (queue->input)
371 queue->input->cursor_count -= queue->cursor_count;
372 if (queue->keystate_lock) unlock_input_keystate( queue->input );
373 release_object( queue->input );
375 queue->input = (struct thread_input *)grab_object( new_input );
376 if (queue->keystate_lock) lock_input_keystate( queue->input );
377 new_input->cursor_count += queue->cursor_count;
378 return 1;
381 /* allocate a hardware message and its data */
382 static struct message *alloc_hardware_message( lparam_t info, struct hw_msg_source source,
383 unsigned int time, data_size_t extra_size )
385 struct hardware_msg_data *msg_data;
386 struct message *msg;
388 if (!(msg = mem_alloc( sizeof(*msg) ))) return NULL;
389 if (!(msg_data = mem_alloc( sizeof(*msg_data) + extra_size )))
391 free( msg );
392 return NULL;
394 memset( msg, 0, sizeof(*msg) );
395 msg->type = MSG_HARDWARE;
396 msg->time = time;
397 msg->data = msg_data;
398 msg->data_size = sizeof(*msg_data) + extra_size;
400 memset( msg_data, 0, sizeof(*msg_data) + extra_size );
401 msg_data->info = info;
402 msg_data->size = msg->data_size;
403 msg_data->source = source;
404 return msg;
407 static int is_cursor_clipped( struct desktop *desktop )
409 rectangle_t top_rect, clip_rect = desktop->cursor.clip;
410 get_top_window_rectangle( desktop, &top_rect );
411 return !is_rect_equal( &clip_rect, &top_rect );
414 static void queue_cursor_message( struct desktop *desktop, user_handle_t win, unsigned int message,
415 lparam_t wparam, lparam_t lparam )
417 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
418 struct thread_input *input;
419 struct message *msg;
421 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
423 msg->msg = message;
424 msg->wparam = wparam;
425 msg->lparam = lparam;
426 msg->x = desktop->cursor.x;
427 msg->y = desktop->cursor.y;
428 if (!(msg->win = win) && (input = desktop->foreground_input)) msg->win = input->active;
429 queue_hardware_message( desktop, msg, 1 );
432 static int update_desktop_cursor_window( struct desktop *desktop, user_handle_t win )
434 int updated = win != desktop->cursor.win;
435 user_handle_t handle = desktop->cursor.handle;
436 desktop->cursor.win = win;
437 if (updated)
439 /* when clipping send the message to the foreground window as well, as some driver have an artificial overlay window */
440 if (is_cursor_clipped( desktop )) queue_cursor_message( desktop, 0, WM_WINE_SETCURSOR, win, handle );
441 queue_cursor_message( desktop, win, WM_WINE_SETCURSOR, win, handle );
443 return updated;
446 static int update_desktop_cursor_pos( struct desktop *desktop, user_handle_t win, int x, int y )
448 struct thread_input *input;
449 int updated;
451 x = max( min( x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
452 y = max( min( y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
453 updated = (desktop->cursor.x != x || desktop->cursor.y != y);
454 desktop->cursor.x = x;
455 desktop->cursor.y = y;
456 desktop->cursor.last_change = get_tick_count();
458 if (!win && (input = desktop->foreground_input)) win = input->capture;
459 if (!win || !is_window_visible( win ) || is_window_transparent( win ))
460 win = shallow_window_from_point( desktop, x, y );
461 if (update_desktop_cursor_window( desktop, win )) updated = 1;
463 return updated;
466 static void update_desktop_cursor_handle( struct desktop *desktop, user_handle_t handle )
468 int updated = desktop->cursor.handle != handle;
469 user_handle_t win = desktop->cursor.win;
470 desktop->cursor.handle = handle;
471 if (updated)
473 /* when clipping send the message to the foreground window as well, as some driver have an artificial overlay window */
474 if (is_cursor_clipped( desktop )) queue_cursor_message( desktop, 0, WM_WINE_SETCURSOR, win, handle );
475 queue_cursor_message( desktop, win, WM_WINE_SETCURSOR, win, handle );
479 /* set the cursor position and queue the corresponding mouse message */
480 static void set_cursor_pos( struct desktop *desktop, int x, int y )
482 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
483 const struct rawinput_device *device;
484 struct message *msg;
486 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
488 update_desktop_cursor_pos( desktop, 0, x, y );
489 return;
492 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
494 msg->msg = WM_MOUSEMOVE;
495 msg->x = x;
496 msg->y = y;
497 queue_hardware_message( desktop, msg, 1 );
500 /* retrieve default position and time for synthesized messages */
501 static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
503 struct desktop *desktop = queue->input->desktop;
505 *x = desktop->cursor.x;
506 *y = desktop->cursor.y;
507 *time = get_tick_count();
510 /* set the cursor clip rectangle */
511 void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect, unsigned int flags, int reset )
513 rectangle_t top_rect;
514 int x, y;
516 get_top_window_rectangle( desktop, &top_rect );
517 if (rect)
519 rectangle_t new_rect = *rect;
520 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
521 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
522 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
523 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
524 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
525 desktop->cursor.clip = new_rect;
527 else desktop->cursor.clip = top_rect;
529 /* warp the mouse to be inside the clip rect */
530 x = max( min( desktop->cursor.x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
531 y = max( min( desktop->cursor.y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
532 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
534 /* request clip cursor rectangle reset to the desktop thread */
535 if (reset) post_desktop_message( desktop, WM_WINE_CLIPCURSOR, flags, FALSE );
537 /* notify foreground thread, of reset, or to apply new cursor clipping rect */
538 queue_cursor_message( desktop, 0, WM_WINE_CLIPCURSOR, flags, reset );
541 /* change the foreground input and reset the cursor clip rect */
542 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
544 if (desktop->foreground_input == input) return;
545 set_clip_rectangle( desktop, NULL, SET_CURSOR_NOCLIP, 1 );
546 desktop->foreground_input = input;
549 /* get the hook table for a given thread */
550 struct hook_table *get_queue_hooks( struct thread *thread )
552 if (!thread->queue) return NULL;
553 return thread->queue->hooks;
556 /* set the hook table for a given thread, allocating the queue if needed */
557 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
559 struct msg_queue *queue = thread->queue;
560 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
561 if (queue->hooks) release_object( queue->hooks );
562 queue->hooks = hooks;
565 /* check the queue status */
566 static inline int is_signaled( struct msg_queue *queue )
568 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
571 /* set some queue bits */
572 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
574 if (bits & (QS_KEY | QS_MOUSEBUTTON))
576 if (!queue->keystate_lock) lock_input_keystate( queue->input );
577 queue->keystate_lock = 1;
579 queue->wake_bits |= bits;
580 queue->changed_bits |= bits;
581 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
584 /* clear some queue bits */
585 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
587 queue->wake_bits &= ~bits;
588 queue->changed_bits &= ~bits;
589 if (!(queue->wake_bits & (QS_KEY | QS_MOUSEBUTTON)))
591 if (queue->keystate_lock) unlock_input_keystate( queue->input );
592 queue->keystate_lock = 0;
596 /* check if message is matched by the filter */
597 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
599 return (msg >= first && msg <= last);
602 /* check whether a message filter contains at least one potential hardware message */
603 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
605 /* hardware message ranges are (in numerical order):
606 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
607 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
608 * WM_MOUSEFIRST .. WM_MOUSELAST
610 if (last < WM_NCMOUSEFIRST) return 0;
611 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
612 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
613 if (first > WM_MOUSELAST) return 0;
614 return 1;
617 /* get the QS_* bit corresponding to a given hardware message */
618 static inline int get_hardware_msg_bit( unsigned int message )
620 if (message == WM_INPUT_DEVICE_CHANGE || message == WM_INPUT) return QS_RAWINPUT;
621 if (message == WM_MOUSEMOVE || message == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
622 if (message >= WM_KEYFIRST && message <= WM_KEYLAST) return QS_KEY;
623 if (message == WM_WINE_CLIPCURSOR) return QS_RAWINPUT;
624 if (message == WM_WINE_SETCURSOR) return QS_RAWINPUT;
625 return QS_MOUSEBUTTON;
628 /* get the current thread queue, creating it if needed */
629 static inline struct msg_queue *get_current_queue(void)
631 struct msg_queue *queue = current->queue;
632 if (!queue) queue = create_msg_queue( current, NULL );
633 return queue;
636 /* get a (pseudo-)unique id to tag hardware messages */
637 static inline unsigned int get_unique_id(void)
639 static unsigned int id;
640 if (!++id) id = 1; /* avoid an id of 0 */
641 return id;
644 /* try to merge a WM_MOUSEMOVE message with the last in the list; return 1 if successful */
645 static int merge_mousemove( struct thread_input *input, const struct message *msg )
647 struct message *prev;
648 struct list *ptr;
650 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
652 prev = LIST_ENTRY( ptr, struct message, entry );
653 if (prev->msg != WM_INPUT) break;
655 if (!ptr) return 0;
656 if (prev->result) return 0;
657 if (prev->win && msg->win && prev->win != msg->win) return 0;
658 if (prev->msg != msg->msg) return 0;
659 if (prev->type != msg->type) return 0;
660 /* now we can merge it */
661 prev->wparam = msg->wparam;
662 prev->lparam = msg->lparam;
663 prev->x = msg->x;
664 prev->y = msg->y;
665 prev->time = msg->time;
666 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
668 struct hardware_msg_data *prev_data = prev->data;
669 struct hardware_msg_data *msg_data = msg->data;
670 prev_data->info = msg_data->info;
672 list_remove( ptr );
673 list_add_tail( &input->msg_list, ptr );
674 return 1;
677 /* try to merge a unique message with the last in the list; return 1 if successful */
678 static int merge_unique_message( struct thread_input *input, unsigned int message, const struct message *msg )
680 struct message *prev;
682 LIST_FOR_EACH_ENTRY_REV( prev, &input->msg_list, struct message, entry )
683 if (prev->msg == message) break;
684 if (&prev->entry == &input->msg_list) return 0;
686 if (prev->result) return 0;
687 if (prev->win != msg->win) return 0;
688 if (prev->type != msg->type) return 0;
690 /* now we can merge it */
691 prev->wparam = msg->wparam;
692 prev->lparam = msg->lparam;
693 prev->x = msg->x;
694 prev->y = msg->y;
695 prev->time = msg->time;
696 list_remove( &prev->entry );
697 list_add_tail( &input->msg_list, &prev->entry );
699 return 1;
702 /* try to merge a message with the messages in the list; return 1 if successful */
703 static int merge_message( struct thread_input *input, const struct message *msg )
705 if (msg->msg == WM_MOUSEMOVE) return merge_mousemove( input, msg );
706 if (msg->msg == WM_WINE_CLIPCURSOR) return merge_unique_message( input, WM_WINE_CLIPCURSOR, msg );
707 if (msg->msg == WM_WINE_SETCURSOR) return merge_unique_message( input, WM_WINE_SETCURSOR, msg );
708 return 0;
711 /* free a result structure */
712 static void free_result( struct message_result *result )
714 if (result->timeout) remove_timeout_user( result->timeout );
715 free( result->data );
716 if (result->callback_msg) free_message( result->callback_msg );
717 if (result->hardware_msg) free_message( result->hardware_msg );
718 if (result->desktop) release_object( result->desktop );
719 free( result );
722 /* remove the result from the sender list it is on */
723 static inline void remove_result_from_sender( struct message_result *result )
725 assert( result->sender );
727 list_remove( &result->sender_entry );
728 result->sender = NULL;
729 if (!result->receiver) free_result( result );
732 /* store the message result in the appropriate structure */
733 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
735 res->result = result;
736 res->error = error;
737 res->replied = 1;
738 if (res->timeout)
740 remove_timeout_user( res->timeout );
741 res->timeout = NULL;
744 if (res->hardware_msg)
746 if (!error && result) /* rejected by the hook */
747 free_message( res->hardware_msg );
748 else
749 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
751 res->hardware_msg = NULL;
754 if (res->sender)
756 if (res->callback_msg)
758 /* queue the callback message in the sender queue */
759 struct callback_msg_data *data = res->callback_msg->data;
760 data->result = result;
761 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
762 set_queue_bits( res->sender, QS_SENDMESSAGE );
763 res->callback_msg = NULL;
764 remove_result_from_sender( res );
766 else
768 /* wake sender queue if waiting on this result */
769 if (list_head(&res->sender->send_result) == &res->sender_entry)
770 set_queue_bits( res->sender, QS_SMRESULT );
773 else if (!res->receiver) free_result( res );
776 /* free a message when deleting a queue or window */
777 static void free_message( struct message *msg )
779 struct message_result *result = msg->result;
780 if (result)
782 result->msg = NULL;
783 result->receiver = NULL;
784 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
786 free( msg->data );
787 free( msg );
790 /* remove (and free) a message from a message list */
791 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
792 enum message_kind kind )
794 list_remove( &msg->entry );
795 switch(kind)
797 case SEND_MESSAGE:
798 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
799 break;
800 case POST_MESSAGE:
801 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
802 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
803 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
804 clear_queue_bits( queue, QS_HOTKEY );
805 break;
807 free_message( msg );
810 /* message timed out without getting a reply */
811 static void result_timeout( void *private )
813 struct message_result *result = private;
815 assert( !result->replied );
817 result->timeout = NULL;
819 if (result->msg) /* not received yet */
821 struct message *msg = result->msg;
823 result->msg = NULL;
824 msg->result = NULL;
825 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
826 result->receiver = NULL;
828 store_message_result( result, 0, STATUS_TIMEOUT );
831 /* allocate and fill a message result structure */
832 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
833 struct msg_queue *recv_queue,
834 struct message *msg, timeout_t timeout )
836 struct message_result *result = mem_alloc( sizeof(*result) );
837 if (result)
839 result->msg = msg;
840 result->sender = send_queue;
841 result->receiver = recv_queue;
842 result->replied = 0;
843 result->data = NULL;
844 result->data_size = 0;
845 result->timeout = NULL;
846 result->hardware_msg = NULL;
847 result->desktop = NULL;
848 result->callback_msg = NULL;
850 if (msg->type == MSG_CALLBACK)
852 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
854 if (!callback_msg)
856 free( result );
857 return NULL;
859 callback_msg->type = MSG_CALLBACK_RESULT;
860 callback_msg->win = msg->win;
861 callback_msg->msg = msg->msg;
862 callback_msg->wparam = 0;
863 callback_msg->lparam = 0;
864 callback_msg->time = get_tick_count();
865 callback_msg->result = NULL;
866 /* steal the data from the original message */
867 callback_msg->data = msg->data;
868 callback_msg->data_size = msg->data_size;
869 msg->data = NULL;
870 msg->data_size = 0;
872 result->callback_msg = callback_msg;
873 list_add_head( &send_queue->callback_result, &result->sender_entry );
875 else if (send_queue)
877 list_add_head( &send_queue->send_result, &result->sender_entry );
878 clear_queue_bits( send_queue, QS_SMRESULT );
881 if (timeout != TIMEOUT_INFINITE)
882 result->timeout = add_timeout_user( timeout, result_timeout, result );
884 return result;
887 /* receive a message, removing it from the sent queue */
888 static void receive_message( struct msg_queue *queue, struct message *msg,
889 struct get_message_reply *reply )
891 struct message_result *result = msg->result;
893 reply->total = msg->data_size;
894 if (msg->data_size > get_reply_max_size())
896 set_error( STATUS_BUFFER_OVERFLOW );
897 return;
899 reply->type = msg->type;
900 reply->win = msg->win;
901 reply->msg = msg->msg;
902 reply->wparam = msg->wparam;
903 reply->lparam = msg->lparam;
904 reply->x = msg->x;
905 reply->y = msg->y;
906 reply->time = msg->time;
908 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
910 list_remove( &msg->entry );
911 /* put the result on the receiver result stack */
912 if (result)
914 result->msg = NULL;
915 result->recv_next = queue->recv_result;
916 queue->recv_result = result;
918 free( msg );
919 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
922 /* set the result of the current received message */
923 static void reply_message( struct msg_queue *queue, lparam_t result,
924 unsigned int error, int remove, const void *data, data_size_t len )
926 struct message_result *res = queue->recv_result;
928 if (remove)
930 queue->recv_result = res->recv_next;
931 res->receiver = NULL;
932 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
934 free_result( res );
935 return;
938 if (!res->replied)
940 if (len && (res->data = memdup( data, len ))) res->data_size = len;
941 store_message_result( res, result, error );
945 static int match_window( user_handle_t win, user_handle_t msg_win )
947 if (!win) return 1;
948 if (win == -1 || win == 1) return !msg_win;
949 if (msg_win == win) return 1;
950 return is_child_window( win, msg_win );
953 /* retrieve a posted message */
954 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
955 unsigned int first, unsigned int last, unsigned int flags,
956 struct get_message_reply *reply )
958 struct message *msg;
960 /* check against the filters */
961 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
963 if (!match_window( win, msg->win )) continue;
964 if (!check_msg_filter( msg->msg, first, last )) continue;
965 goto found; /* found one */
967 return 0;
969 /* return it to the app */
970 found:
971 reply->total = msg->data_size;
972 if (msg->data_size > get_reply_max_size())
974 set_error( STATUS_BUFFER_OVERFLOW );
975 return 1;
977 reply->type = msg->type;
978 reply->win = msg->win;
979 reply->msg = msg->msg;
980 reply->wparam = msg->wparam;
981 reply->lparam = msg->lparam;
982 reply->x = msg->x;
983 reply->y = msg->y;
984 reply->time = msg->time;
986 if (flags & PM_REMOVE)
988 if (msg->data)
990 set_reply_data_ptr( msg->data, msg->data_size );
991 msg->data = NULL;
992 msg->data_size = 0;
994 remove_queue_message( queue, msg, POST_MESSAGE );
996 else if (msg->data) set_reply_data( msg->data, msg->data_size );
998 return 1;
1001 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
1002 struct get_message_reply *reply )
1004 if (queue->quit_message)
1006 reply->total = 0;
1007 reply->type = MSG_POSTED;
1008 reply->win = 0;
1009 reply->msg = WM_QUIT;
1010 reply->wparam = queue->exit_code;
1011 reply->lparam = 0;
1013 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
1015 if (flags & PM_REMOVE)
1017 queue->quit_message = 0;
1018 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
1019 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1021 return 1;
1023 else
1024 return 0;
1027 /* empty a message list and free all the messages */
1028 static void empty_msg_list( struct list *list )
1030 struct list *ptr;
1032 while ((ptr = list_head( list )) != NULL)
1034 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1035 list_remove( &msg->entry );
1036 free_message( msg );
1040 /* cleanup all pending results when deleting a queue */
1041 static void cleanup_results( struct msg_queue *queue )
1043 struct list *entry;
1045 while ((entry = list_head( &queue->send_result )) != NULL)
1047 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
1050 while ((entry = list_head( &queue->callback_result )) != NULL)
1052 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
1055 while (queue->recv_result)
1056 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
1059 /* check if the thread owning the queue is hung (not checking for messages) */
1060 static int is_queue_hung( struct msg_queue *queue )
1062 struct wait_queue_entry *entry;
1064 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
1065 return 0; /* less than 5 seconds since last get message -> not hung */
1067 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
1069 if (get_wait_queue_thread(entry)->queue == queue)
1070 return 0; /* thread is waiting on queue -> not hung */
1072 return 1;
1075 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
1077 struct msg_queue *queue = (struct msg_queue *)obj;
1078 struct process *process = get_wait_queue_thread(entry)->process;
1080 /* a thread can only wait on its own queue */
1081 if (get_wait_queue_thread(entry)->queue != queue)
1083 set_error( STATUS_ACCESS_DENIED );
1084 return 0;
1086 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
1088 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
1089 set_fd_events( queue->fd, POLLIN );
1090 add_queue( obj, entry );
1091 return 1;
1094 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
1096 struct msg_queue *queue = (struct msg_queue *)obj;
1098 remove_queue( obj, entry );
1099 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
1100 set_fd_events( queue->fd, 0 );
1103 static void msg_queue_dump( struct object *obj, int verbose )
1105 struct msg_queue *queue = (struct msg_queue *)obj;
1106 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
1107 queue->wake_bits, queue->wake_mask );
1110 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
1112 struct msg_queue *queue = (struct msg_queue *)obj;
1113 int ret = 0;
1115 if (queue->fd)
1117 if ((ret = check_fd_events( queue->fd, POLLIN )))
1118 /* stop waiting on select() if we are signaled */
1119 set_fd_events( queue->fd, 0 );
1120 else if (!list_empty( &obj->wait_queue ))
1121 /* restart waiting on poll() if we are no longer signaled */
1122 set_fd_events( queue->fd, POLLIN );
1124 return ret || is_signaled( queue );
1127 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
1129 struct msg_queue *queue = (struct msg_queue *)obj;
1130 queue->wake_mask = 0;
1131 queue->changed_mask = 0;
1134 static void msg_queue_destroy( struct object *obj )
1136 struct msg_queue *queue = (struct msg_queue *)obj;
1137 struct list *ptr;
1138 struct hotkey *hotkey, *hotkey2;
1139 int i;
1141 cleanup_results( queue );
1142 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
1144 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
1146 if (hotkey->queue == queue)
1148 list_remove( &hotkey->entry );
1149 free( hotkey );
1153 while ((ptr = list_head( &queue->pending_timers )))
1155 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1156 list_remove( &timer->entry );
1157 free( timer );
1159 while ((ptr = list_head( &queue->expired_timers )))
1161 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1162 list_remove( &timer->entry );
1163 free( timer );
1165 if (queue->timeout) remove_timeout_user( queue->timeout );
1166 queue->input->cursor_count -= queue->cursor_count;
1167 if (queue->keystate_lock) unlock_input_keystate( queue->input );
1168 release_object( queue->input );
1169 if (queue->hooks) release_object( queue->hooks );
1170 if (queue->fd) release_object( queue->fd );
1173 static void msg_queue_poll_event( struct fd *fd, int event )
1175 struct msg_queue *queue = get_fd_user( fd );
1176 assert( queue->obj.ops == &msg_queue_ops );
1178 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1179 else set_fd_events( queue->fd, 0 );
1180 wake_up( &queue->obj, 0 );
1183 static void thread_input_dump( struct object *obj, int verbose )
1185 struct thread_input *input = (struct thread_input *)obj;
1186 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
1187 input->focus, input->capture, input->active );
1190 static void thread_input_destroy( struct object *obj )
1192 struct thread_input *input = (struct thread_input *)obj;
1193 struct desktop *desktop;
1195 empty_msg_list( &input->msg_list );
1196 if ((desktop = input->desktop))
1198 if (desktop->foreground_input == input) desktop->foreground_input = NULL;
1199 release_object( desktop );
1203 /* fix the thread input data when a window is destroyed */
1204 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1206 struct thread_input *input = queue->input;
1208 if (window == input->focus) input->focus = 0;
1209 if (window == input->capture) input->capture = 0;
1210 if (window == input->active) input->active = 0;
1211 if (window == input->menu_owner) input->menu_owner = 0;
1212 if (window == input->move_size) input->move_size = 0;
1213 if (window == input->caret) set_caret_window( input, 0 );
1216 /* check if the specified window can be set in the input data of a given queue */
1217 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1219 struct thread *thread;
1220 int ret = 0;
1222 if (!window) return 1; /* we can always clear the data */
1224 if ((thread = get_window_thread( window )))
1226 ret = (queue->input == thread->queue->input);
1227 if (!ret) set_error( STATUS_ACCESS_DENIED );
1228 release_object( thread );
1230 else set_error( STATUS_INVALID_HANDLE );
1232 return ret;
1235 /* make sure the specified thread has a queue */
1236 int init_thread_queue( struct thread *thread )
1238 if (thread->queue) return 1;
1239 return (create_msg_queue( thread, NULL ) != NULL);
1242 /* attach two thread input data structures */
1243 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1245 struct desktop *desktop;
1246 struct thread_input *input;
1247 int ret;
1249 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1250 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1251 input = (struct thread_input *)grab_object( thread_to->queue->input );
1252 if (input->desktop != desktop)
1254 set_error( STATUS_ACCESS_DENIED );
1255 release_object( input );
1256 release_object( desktop );
1257 return 0;
1259 release_object( desktop );
1261 if (thread_from->queue)
1263 if (!input->focus) input->focus = thread_from->queue->input->focus;
1264 if (!input->active) input->active = thread_from->queue->input->active;
1267 ret = assign_thread_input( thread_from, input );
1268 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1269 release_object( input );
1270 return ret;
1273 /* detach two thread input data structures */
1274 void detach_thread_input( struct thread *thread_from )
1276 struct thread *thread;
1277 struct thread_input *input, *old_input = thread_from->queue->input;
1279 if ((input = create_thread_input( thread_from )))
1281 if (old_input->focus && (thread = get_window_thread( old_input->focus )))
1283 if (thread == thread_from)
1285 input->focus = old_input->focus;
1286 old_input->focus = 0;
1288 release_object( thread );
1290 if (old_input->active && (thread = get_window_thread( old_input->active )))
1292 if (thread == thread_from)
1294 input->active = old_input->active;
1295 old_input->active = 0;
1297 release_object( thread );
1299 assign_thread_input( thread_from, input );
1300 release_object( input );
1305 /* set the next timer to expire */
1306 static void set_next_timer( struct msg_queue *queue )
1308 struct list *ptr;
1310 if (queue->timeout)
1312 remove_timeout_user( queue->timeout );
1313 queue->timeout = NULL;
1315 if ((ptr = list_head( &queue->pending_timers )))
1317 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1318 queue->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, queue );
1320 /* set/clear QS_TIMER bit */
1321 if (list_empty( &queue->expired_timers ))
1322 clear_queue_bits( queue, QS_TIMER );
1323 else
1324 set_queue_bits( queue, QS_TIMER );
1327 /* find a timer from its window and id */
1328 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1329 unsigned int msg, lparam_t id )
1331 struct list *ptr;
1333 /* we need to search both lists */
1335 LIST_FOR_EACH( ptr, &queue->pending_timers )
1337 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1338 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1340 LIST_FOR_EACH( ptr, &queue->expired_timers )
1342 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1343 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1345 return NULL;
1348 /* callback for the next timer expiration */
1349 static void timer_callback( void *private )
1351 struct msg_queue *queue = private;
1352 struct list *ptr;
1354 queue->timeout = NULL;
1355 /* move on to the next timer */
1356 ptr = list_head( &queue->pending_timers );
1357 list_remove( ptr );
1358 list_add_tail( &queue->expired_timers, ptr );
1359 set_next_timer( queue );
1362 /* link a timer at its rightful place in the queue list */
1363 static void link_timer( struct msg_queue *queue, struct timer *timer )
1365 struct list *ptr;
1367 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1369 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1370 if (t->when <= timer->when) break;
1372 list_add_before( ptr, &timer->entry );
1375 /* remove a timer from the queue timer list and free it */
1376 static void free_timer( struct msg_queue *queue, struct timer *timer )
1378 list_remove( &timer->entry );
1379 free( timer );
1380 set_next_timer( queue );
1383 /* restart an expired timer */
1384 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1386 list_remove( &timer->entry );
1387 while (-timer->when <= monotonic_time) timer->when -= (timeout_t)timer->rate * 10000;
1388 link_timer( queue, timer );
1389 set_next_timer( queue );
1392 /* find an expired timer matching the filtering parameters */
1393 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1394 unsigned int get_first, unsigned int get_last,
1395 int remove )
1397 struct list *ptr;
1399 LIST_FOR_EACH( ptr, &queue->expired_timers )
1401 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1402 if (win && timer->win != win) continue;
1403 if (check_msg_filter( timer->msg, get_first, get_last ))
1405 if (remove) restart_timer( queue, timer );
1406 return timer;
1409 return NULL;
1412 /* add a timer */
1413 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1415 struct timer *timer = mem_alloc( sizeof(*timer) );
1416 if (timer)
1418 timer->rate = max( rate, 1 );
1419 timer->when = -monotonic_time - (timeout_t)timer->rate * 10000;
1420 link_timer( queue, timer );
1421 /* check if we replaced the next timer */
1422 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1424 return timer;
1427 /* change the input key state for a given key */
1428 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1430 if (down)
1432 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1433 keystate[key] |= down;
1435 else keystate[key] &= ~0x80;
1438 /* update the input key state for a keyboard message */
1439 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1440 unsigned int msg, lparam_t wparam )
1442 unsigned char key;
1443 int down = 0;
1445 switch (msg)
1447 case WM_LBUTTONDOWN:
1448 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1449 /* fall through */
1450 case WM_LBUTTONUP:
1451 set_input_key_state( keystate, VK_LBUTTON, down );
1452 break;
1453 case WM_MBUTTONDOWN:
1454 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1455 /* fall through */
1456 case WM_MBUTTONUP:
1457 set_input_key_state( keystate, VK_MBUTTON, down );
1458 break;
1459 case WM_RBUTTONDOWN:
1460 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1461 /* fall through */
1462 case WM_RBUTTONUP:
1463 set_input_key_state( keystate, VK_RBUTTON, down );
1464 break;
1465 case WM_XBUTTONDOWN:
1466 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1467 /* fall through */
1468 case WM_XBUTTONUP:
1469 if (wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1470 else if (wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1471 break;
1472 case WM_KEYDOWN:
1473 case WM_SYSKEYDOWN:
1474 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1475 /* fall through */
1476 case WM_KEYUP:
1477 case WM_SYSKEYUP:
1478 key = (unsigned char)wparam;
1479 set_input_key_state( keystate, key, down );
1480 switch(key)
1482 case VK_LCONTROL:
1483 case VK_RCONTROL:
1484 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1485 set_input_key_state( keystate, VK_CONTROL, down );
1486 break;
1487 case VK_LMENU:
1488 case VK_RMENU:
1489 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1490 set_input_key_state( keystate, VK_MENU, down );
1491 break;
1492 case VK_LSHIFT:
1493 case VK_RSHIFT:
1494 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1495 set_input_key_state( keystate, VK_SHIFT, down );
1496 break;
1498 break;
1502 /* update the desktop key state according to a mouse message flags */
1503 static void update_desktop_mouse_state( struct desktop *desktop, unsigned int flags, lparam_t wparam )
1505 if (flags & MOUSEEVENTF_LEFTDOWN)
1506 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONDOWN, wparam );
1507 if (flags & MOUSEEVENTF_LEFTUP)
1508 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONUP, wparam );
1509 if (flags & MOUSEEVENTF_RIGHTDOWN)
1510 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONDOWN, wparam );
1511 if (flags & MOUSEEVENTF_RIGHTUP)
1512 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONUP, wparam );
1513 if (flags & MOUSEEVENTF_MIDDLEDOWN)
1514 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONDOWN, wparam );
1515 if (flags & MOUSEEVENTF_MIDDLEUP)
1516 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONUP, wparam );
1517 if (flags & MOUSEEVENTF_XDOWN)
1518 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONDOWN, wparam );
1519 if (flags & MOUSEEVENTF_XUP)
1520 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONUP, wparam );
1523 /* release the hardware message currently being processed by the given thread */
1524 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id )
1526 struct thread_input *input = queue->input;
1527 struct message *msg, *other;
1528 int clr_bit;
1530 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1532 if (msg->unique_id == hw_id) break;
1534 if (&msg->entry == &input->msg_list) return; /* not found */
1536 /* clear the queue bit for that message */
1537 clr_bit = get_hardware_msg_bit( msg->msg );
1538 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1540 if (other != msg && get_hardware_msg_bit( other->msg ) == clr_bit)
1542 clr_bit = 0;
1543 break;
1546 if (clr_bit) clear_queue_bits( queue, clr_bit );
1548 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1549 list_remove( &msg->entry );
1550 free_message( msg );
1553 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1555 struct hotkey *hotkey;
1556 unsigned int modifiers = 0;
1558 if (msg->msg != WM_KEYDOWN && msg->msg != WM_SYSKEYDOWN) return 0;
1560 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1561 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1562 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1563 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1565 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1567 if (hotkey->vkey != msg->wparam) continue;
1568 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1571 return 0;
1573 found:
1574 msg->type = MSG_POSTED;
1575 msg->win = hotkey->win;
1576 msg->msg = WM_HOTKEY;
1577 msg->wparam = hotkey->id;
1578 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1580 free( msg->data );
1581 msg->data = NULL;
1582 msg->data_size = 0;
1584 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1585 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1586 hotkey->queue->hotkey_count++;
1587 return 1;
1590 /* find the window that should receive a given hardware message */
1591 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1592 struct message *msg, unsigned int *msg_code,
1593 struct thread **thread )
1595 user_handle_t win = 0;
1597 *thread = NULL;
1598 *msg_code = msg->msg;
1599 switch (get_hardware_msg_bit( msg->msg ))
1601 case QS_RAWINPUT:
1602 if (!(win = msg->win) && input) win = input->focus;
1603 break;
1604 case QS_KEY:
1605 if (input && !(win = input->focus))
1607 win = input->active;
1608 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1610 break;
1611 case QS_MOUSEMOVE:
1612 case QS_MOUSEBUTTON:
1613 if (!input || !(win = input->capture))
1615 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1616 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1617 *thread = window_thread_from_point( win, msg->x, msg->y );
1619 break;
1622 if (!*thread)
1623 *thread = get_window_thread( win );
1624 return win;
1627 static struct rawinput_device *find_rawinput_device( struct process *process, unsigned short usage_page, unsigned short usage )
1629 struct rawinput_device *device, *end;
1631 for (device = process->rawinput_devices, end = device + process->rawinput_device_count; device != end; device++)
1633 if (device->usage_page != usage_page || device->usage != usage) continue;
1634 return device;
1637 return NULL;
1640 static void prepend_cursor_history( int x, int y, unsigned int time, lparam_t info )
1642 cursor_pos_t *pos = &cursor_history[--cursor_history_latest % ARRAY_SIZE(cursor_history)];
1644 pos->x = x;
1645 pos->y = y;
1646 pos->time = time;
1647 pos->info = info;
1650 /* queue a hardware message into a given thread input */
1651 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1653 user_handle_t win;
1654 struct thread *thread;
1655 struct thread_input *input;
1656 struct hardware_msg_data *msg_data = msg->data;
1657 unsigned int msg_code;
1659 update_input_key_state( desktop, desktop->keystate, msg->msg, msg->wparam );
1660 last_input_time = get_tick_count();
1661 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1663 switch (get_hardware_msg_bit( msg->msg ))
1665 case QS_KEY:
1666 if (queue_hotkey_message( desktop, msg )) return;
1667 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1668 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1669 msg->lparam &= ~(KF_EXTENDED << 16);
1670 break;
1671 case QS_MOUSEMOVE:
1672 prepend_cursor_history( msg->x, msg->y, msg->time, msg_data->info );
1673 if (update_desktop_cursor_pos( desktop, msg->win, msg->x, msg->y )) always_queue = 1;
1674 /* fallthrough */
1675 case QS_MOUSEBUTTON:
1676 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1677 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1678 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1679 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1680 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1681 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1682 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1683 break;
1685 msg->x = desktop->cursor.x;
1686 msg->y = desktop->cursor.y;
1688 if (msg->win && (thread = get_window_thread( msg->win )))
1690 input = thread->queue->input;
1691 release_object( thread );
1693 else input = desktop->foreground_input;
1695 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1696 if (!win || !thread)
1698 if (input) update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1699 free_message( msg );
1700 return;
1702 input = thread->queue->input;
1704 if (!always_queue || merge_message( input, msg )) free_message( msg );
1705 else
1707 msg->unique_id = 0; /* will be set once we return it to the app */
1708 list_add_tail( &input->msg_list, &msg->entry );
1709 set_queue_bits( thread->queue, get_hardware_msg_bit( msg->msg ) );
1711 release_object( thread );
1714 /* send the low-level hook message for a given hardware message */
1715 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1716 const hw_input_t *input, struct msg_queue *sender )
1718 struct thread *hook_thread;
1719 struct msg_queue *queue;
1720 struct message *msg;
1721 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1722 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1724 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1725 if (!(queue = hook_thread->queue)) return 0;
1726 if (is_queue_hung( queue )) return 0;
1728 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1730 msg->type = MSG_HOOK_LL;
1731 msg->win = 0;
1732 msg->msg = id;
1733 msg->wparam = hardware_msg->msg;
1734 msg->x = hardware_msg->x;
1735 msg->y = hardware_msg->y;
1736 msg->time = hardware_msg->time;
1737 msg->data_size = hardware_msg->data_size;
1738 msg->result = NULL;
1740 if (input->type == INPUT_KEYBOARD)
1742 unsigned short vkey = input->kbd.vkey;
1743 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1744 msg->lparam = (input->kbd.scan << 16) | vkey;
1746 else msg->lparam = input->mouse.data << 16;
1748 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1749 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1751 free_message( msg );
1752 return 0;
1754 msg->result->hardware_msg = hardware_msg;
1755 msg->result->desktop = (struct desktop *)grab_object( desktop );
1756 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1757 set_queue_bits( queue, QS_SENDMESSAGE );
1758 return 1;
1761 /* get the foreground thread for a desktop and a window receiving input */
1762 static struct thread *get_foreground_thread( struct desktop *desktop, user_handle_t window )
1764 /* if desktop has no foreground process, assume the receiving window is */
1765 if (desktop->foreground_input) return get_window_thread( desktop->foreground_input->focus );
1766 if (window) return get_window_thread( window );
1767 return NULL;
1770 struct rawinput_message
1772 struct thread *foreground;
1773 struct desktop *desktop;
1774 struct hw_msg_source source;
1775 unsigned int time;
1776 unsigned int message;
1777 struct hardware_msg_data data;
1778 const void *hid_report;
1781 /* check if process is supposed to receive a WM_INPUT message and eventually queue it */
1782 static int queue_rawinput_message( struct process* process, void *arg )
1784 const struct rawinput_message* raw_msg = arg;
1785 const struct rawinput_device *device = NULL;
1786 struct desktop *target_desktop = NULL, *desktop = NULL;
1787 struct thread *target_thread = NULL, *foreground = NULL;
1788 struct message *msg;
1789 data_size_t report_size;
1790 int wparam = RIM_INPUT;
1792 if (raw_msg->data.rawinput.type == RIM_TYPEMOUSE)
1793 device = process->rawinput_mouse;
1794 else if (raw_msg->data.rawinput.type == RIM_TYPEKEYBOARD)
1795 device = process->rawinput_kbd;
1796 else
1797 device = find_rawinput_device( process, raw_msg->data.rawinput.hid.usage_page, raw_msg->data.rawinput.hid.usage );
1798 if (!device) return 0;
1800 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && !(device->flags & RIDEV_DEVNOTIFY)) return 0;
1802 if (raw_msg->desktop) desktop = (struct desktop *)grab_object( raw_msg->desktop );
1803 else if (!(desktop = get_desktop_obj( process, process->desktop, 0 ))) goto done;
1805 if (raw_msg->foreground) foreground = (struct thread *)grab_object( raw_msg->foreground );
1806 else if (!(foreground = get_foreground_thread( desktop, 0 ))) goto done;
1808 if (process != foreground->process)
1810 if (raw_msg->message == WM_INPUT && !(device->flags & RIDEV_INPUTSINK)) goto done;
1811 if (!(target_thread = get_window_thread( device->target ))) goto done;
1812 if (!(target_desktop = get_thread_desktop( target_thread, 0 ))) goto done;
1813 if (target_desktop != desktop) goto done;
1814 wparam = RIM_INPUTSINK;
1817 if (raw_msg->data.rawinput.type != RIM_TYPEHID || !raw_msg->hid_report) report_size = 0;
1818 else report_size = raw_msg->data.size - sizeof(raw_msg->data);
1820 if (!(msg = alloc_hardware_message( raw_msg->data.info, raw_msg->source, raw_msg->time, report_size )))
1821 goto done;
1823 msg->win = device->target;
1824 msg->msg = raw_msg->message;
1825 msg->wparam = wparam;
1826 msg->lparam = 0;
1827 memcpy( msg->data, &raw_msg->data, sizeof(raw_msg->data) );
1828 if (report_size) memcpy( (struct hardware_msg_data *)msg->data + 1, raw_msg->hid_report, report_size );
1830 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && raw_msg->data.rawinput.type == RIM_TYPEHID)
1832 msg->wparam = raw_msg->data.rawinput.hid.param;
1833 msg->lparam = raw_msg->data.rawinput.hid.device;
1836 queue_hardware_message( desktop, msg, 1 );
1838 done:
1839 if (target_thread) release_object( target_thread );
1840 if (target_desktop) release_object( target_desktop );
1841 if (foreground) release_object( foreground );
1842 if (desktop) release_object( desktop );
1843 return 0;
1846 /* queue a hardware message for a mouse event */
1847 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1848 unsigned int origin, struct msg_queue *sender )
1850 const struct rawinput_device *device;
1851 struct hardware_msg_data *msg_data;
1852 struct rawinput_message raw_msg;
1853 struct message *msg;
1854 struct thread *foreground;
1855 unsigned int i, time, flags;
1856 struct hw_msg_source source = { IMDT_MOUSE, origin };
1857 int wait = 0, x, y;
1859 static const unsigned int messages[] =
1861 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1862 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1863 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1864 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1865 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1866 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1867 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1868 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1869 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1870 0, /* 0x0200 = unused */
1871 0, /* 0x0400 = unused */
1872 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1873 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1876 desktop->cursor.last_change = get_tick_count();
1877 flags = input->mouse.flags;
1878 time = input->mouse.time;
1879 if (!time) time = desktop->cursor.last_change;
1881 if (flags & MOUSEEVENTF_MOVE)
1883 if (flags & MOUSEEVENTF_ABSOLUTE)
1885 x = input->mouse.x;
1886 y = input->mouse.y;
1887 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1888 x == desktop->cursor.x && y == desktop->cursor.y)
1889 flags &= ~MOUSEEVENTF_MOVE;
1891 else
1893 x = desktop->cursor.x + input->mouse.x;
1894 y = desktop->cursor.y + input->mouse.y;
1897 else
1899 x = desktop->cursor.x;
1900 y = desktop->cursor.y;
1903 if ((foreground = get_foreground_thread( desktop, win )))
1905 memset( &raw_msg, 0, sizeof(raw_msg) );
1906 raw_msg.foreground = foreground;
1907 raw_msg.desktop = desktop;
1908 raw_msg.source = source;
1909 raw_msg.time = time;
1910 raw_msg.message = WM_INPUT;
1912 msg_data = &raw_msg.data;
1913 msg_data->info = input->mouse.info;
1914 msg_data->size = sizeof(*msg_data);
1915 msg_data->flags = flags;
1916 msg_data->rawinput.type = RIM_TYPEMOUSE;
1917 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1918 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1919 msg_data->rawinput.mouse.data = input->mouse.data;
1921 enum_processes( queue_rawinput_message, &raw_msg );
1922 release_object( foreground );
1925 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
1927 if (flags & MOUSEEVENTF_MOVE) update_desktop_cursor_pos( desktop, win, x, y );
1928 update_desktop_mouse_state( desktop, flags, input->mouse.data << 16 );
1929 return 0;
1932 for (i = 0; i < ARRAY_SIZE( messages ); i++)
1934 if (!messages[i]) continue;
1935 if (!(flags & (1 << i))) continue;
1936 flags &= ~(1 << i);
1938 if (!(msg = alloc_hardware_message( input->mouse.info, source, time, 0 ))) return 0;
1939 msg_data = msg->data;
1941 msg->win = get_user_full_handle( win );
1942 msg->msg = messages[i];
1943 msg->wparam = input->mouse.data << 16;
1944 msg->lparam = 0;
1945 msg->x = x;
1946 msg->y = y;
1947 if (origin == IMO_INJECTED) msg_data->flags = LLMHF_INJECTED;
1949 /* specify a sender only when sending the last message */
1950 if (!(flags & ((1 << ARRAY_SIZE( messages )) - 1)))
1952 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1953 queue_hardware_message( desktop, msg, 0 );
1955 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1956 queue_hardware_message( desktop, msg, 0 );
1958 return wait;
1961 /* queue a hardware message for a keyboard event */
1962 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1963 unsigned int origin, struct msg_queue *sender )
1965 struct hw_msg_source source = { IMDT_KEYBOARD, origin };
1966 const struct rawinput_device *device;
1967 struct hardware_msg_data *msg_data;
1968 struct rawinput_message raw_msg;
1969 struct message *msg;
1970 struct thread *foreground;
1971 unsigned char vkey = input->kbd.vkey;
1972 unsigned int message_code, time;
1973 int wait;
1975 if (!(time = input->kbd.time)) time = get_tick_count();
1977 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1979 switch (vkey)
1981 case VK_MENU:
1982 case VK_LMENU:
1983 case VK_RMENU:
1984 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1985 break;
1986 case VK_CONTROL:
1987 case VK_LCONTROL:
1988 case VK_RCONTROL:
1989 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1990 break;
1991 case VK_SHIFT:
1992 case VK_LSHIFT:
1993 case VK_RSHIFT:
1994 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1995 break;
1999 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
2000 switch (vkey)
2002 case VK_LMENU:
2003 case VK_RMENU:
2004 if (input->kbd.flags & KEYEVENTF_KEYUP)
2006 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
2007 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
2008 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
2009 message_code = WM_SYSKEYUP;
2010 desktop->keystate[VK_MENU] &= ~0x02;
2012 else
2014 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
2015 if (desktop->keystate[VK_CONTROL] & 0x80) break;
2016 message_code = WM_SYSKEYDOWN;
2017 desktop->keystate[VK_MENU] |= 0x02;
2019 break;
2021 case VK_LCONTROL:
2022 case VK_RCONTROL:
2023 /* send WM_SYSKEYUP on release if Alt still pressed */
2024 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
2025 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
2026 message_code = WM_SYSKEYUP;
2027 desktop->keystate[VK_MENU] &= ~0x02;
2028 break;
2030 default:
2031 /* send WM_SYSKEY for Alt-anykey and for F10 */
2032 if (desktop->keystate[VK_CONTROL] & 0x80) break;
2033 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
2034 /* fall through */
2035 case VK_F10:
2036 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
2037 desktop->keystate[VK_MENU] &= ~0x02;
2038 break;
2041 if ((foreground = get_foreground_thread( desktop, win )))
2043 memset( &raw_msg, 0, sizeof(raw_msg) );
2044 raw_msg.foreground = foreground;
2045 raw_msg.desktop = desktop;
2046 raw_msg.source = source;
2047 raw_msg.time = time;
2048 raw_msg.message = WM_INPUT;
2050 msg_data = &raw_msg.data;
2051 msg_data->info = input->kbd.info;
2052 msg_data->size = sizeof(*msg_data);
2053 msg_data->flags = input->kbd.flags;
2054 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
2055 msg_data->rawinput.kbd.message = message_code;
2056 msg_data->rawinput.kbd.vkey = vkey;
2057 msg_data->rawinput.kbd.scan = input->kbd.scan;
2059 enum_processes( queue_rawinput_message, &raw_msg );
2060 release_object( foreground );
2063 if ((device = current->process->rawinput_kbd) && (device->flags & RIDEV_NOLEGACY))
2065 update_input_key_state( desktop, desktop->keystate, message_code, vkey );
2066 return 0;
2069 if (!(msg = alloc_hardware_message( input->kbd.info, source, time, 0 ))) return 0;
2070 msg_data = msg->data;
2072 msg->win = get_user_full_handle( win );
2073 msg->msg = message_code;
2074 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
2075 if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
2077 if (input->kbd.flags & KEYEVENTF_UNICODE && !vkey)
2079 msg->wparam = VK_PACKET;
2081 else
2083 unsigned int flags = 0;
2084 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
2085 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
2086 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
2087 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
2089 msg->wparam = vkey;
2090 msg->lparam |= flags << 16;
2091 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
2094 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
2095 queue_hardware_message( desktop, msg, 1 );
2097 return wait;
2100 /* queue a hardware message for a custom type of event */
2101 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
2102 unsigned int origin, const hw_input_t *input )
2104 struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
2105 struct hardware_msg_data *msg_data;
2106 struct rawinput_message raw_msg;
2107 struct message *msg;
2108 data_size_t report_size = 0;
2110 switch (input->hw.msg)
2112 case WM_INPUT:
2113 case WM_INPUT_DEVICE_CHANGE:
2114 memset( &raw_msg, 0, sizeof(raw_msg) );
2115 raw_msg.source = source;
2116 raw_msg.time = get_tick_count();
2117 raw_msg.message = input->hw.msg;
2119 if (input->hw.rawinput.type == RIM_TYPEHID)
2121 raw_msg.hid_report = get_req_data();
2122 report_size = input->hw.rawinput.hid.length * input->hw.rawinput.hid.count;
2125 if (report_size != get_req_data_size())
2127 set_error( STATUS_INVALID_PARAMETER );
2128 return;
2131 msg_data = &raw_msg.data;
2132 msg_data->size = sizeof(*msg_data) + report_size;
2133 msg_data->rawinput = input->hw.rawinput;
2135 enum_processes( queue_rawinput_message, &raw_msg );
2136 return;
2139 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
2141 msg->win = get_user_full_handle( win );
2142 msg->msg = input->hw.msg;
2143 msg->wparam = 0;
2144 msg->lparam = input->hw.lparam;
2145 msg->x = desktop->cursor.x;
2146 msg->y = desktop->cursor.y;
2148 queue_hardware_message( desktop, msg, 1 );
2151 /* check message filter for a hardware message */
2152 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
2153 user_handle_t filter_win, unsigned int first, unsigned int last )
2155 switch (get_hardware_msg_bit( msg_code ))
2157 case QS_KEY:
2158 /* we can only test the window for a keyboard message since the
2159 * dest window for a mouse message depends on hittest */
2160 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
2161 return 0;
2162 /* the message code is final for a keyboard message, we can simply check it */
2163 return check_msg_filter( msg_code, first, last );
2165 case QS_MOUSEMOVE:
2166 case QS_MOUSEBUTTON:
2167 /* we need to check all possible values that the message can have in the end */
2168 if (check_msg_filter( msg_code, first, last )) return 1;
2169 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
2171 /* all other messages can become non-client messages */
2172 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
2174 /* clicks can become double-clicks or non-client double-clicks */
2175 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
2176 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
2178 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2179 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2181 return 0;
2183 default:
2184 return check_msg_filter( msg_code, first, last );
2189 /* find a hardware message for the given queue */
2190 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
2191 unsigned int first, unsigned int last, unsigned int flags,
2192 struct get_message_reply *reply )
2194 struct thread_input *input = thread->queue->input;
2195 struct thread *win_thread;
2196 struct list *ptr;
2197 user_handle_t win;
2198 int clear_bits, got_one = 0;
2199 unsigned int msg_code;
2201 ptr = list_head( &input->msg_list );
2202 if (hw_id)
2204 while (ptr)
2206 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2207 if (msg->unique_id == hw_id) break;
2208 ptr = list_next( &input->msg_list, ptr );
2210 if (!ptr) ptr = list_head( &input->msg_list );
2211 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
2214 if (ptr == list_head( &input->msg_list ))
2215 clear_bits = QS_INPUT;
2216 else
2217 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
2219 while (ptr)
2221 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2222 struct hardware_msg_data *data = msg->data;
2224 ptr = list_next( &input->msg_list, ptr );
2225 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
2226 if (!win || !win_thread)
2228 /* no window at all, remove it */
2229 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2230 list_remove( &msg->entry );
2231 free_message( msg );
2232 continue;
2234 if (win_thread != thread)
2236 if (win_thread->queue->input == input)
2238 /* wake the other thread */
2239 set_queue_bits( win_thread->queue, get_hardware_msg_bit( msg->msg ) );
2240 got_one = 1;
2242 else
2244 /* for another thread input, drop it */
2245 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2246 list_remove( &msg->entry );
2247 free_message( msg );
2249 release_object( win_thread );
2250 continue;
2252 release_object( win_thread );
2254 /* if we already got a message for another thread, or if it doesn't
2255 * match the filter we skip it */
2256 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
2258 clear_bits &= ~get_hardware_msg_bit( msg->msg );
2259 continue;
2262 reply->total = msg->data_size;
2263 if (msg->data_size > get_reply_max_size())
2265 set_error( STATUS_BUFFER_OVERFLOW );
2266 return 1;
2269 /* now we can return it */
2270 if (!msg->unique_id) msg->unique_id = get_unique_id();
2271 reply->type = MSG_HARDWARE;
2272 reply->win = win;
2273 reply->msg = msg_code;
2274 reply->wparam = msg->wparam;
2275 reply->lparam = msg->lparam;
2276 reply->x = msg->x;
2277 reply->y = msg->y;
2278 reply->time = msg->time;
2280 data->hw_id = msg->unique_id;
2281 set_reply_data( msg->data, msg->data_size );
2282 if (get_hardware_msg_bit( msg->msg ) == QS_RAWINPUT && (flags & PM_REMOVE))
2283 release_hardware_message( current->queue, data->hw_id );
2284 return 1;
2286 /* nothing found, clear the hardware queue bits */
2287 clear_queue_bits( thread->queue, clear_bits );
2288 return 0;
2291 /* increment (or decrement if 'incr' is negative) the queue paint count */
2292 void inc_queue_paint_count( struct thread *thread, int incr )
2294 struct msg_queue *queue = thread->queue;
2296 assert( queue );
2298 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2300 if (queue->paint_count)
2301 set_queue_bits( queue, QS_PAINT );
2302 else
2303 clear_queue_bits( queue, QS_PAINT );
2307 /* remove all messages and timers belonging to a certain window */
2308 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2310 struct msg_queue *queue = thread->queue;
2311 struct list *ptr;
2312 int i;
2314 if (!queue) return;
2316 /* remove timers */
2318 ptr = list_head( &queue->pending_timers );
2319 while (ptr)
2321 struct list *next = list_next( &queue->pending_timers, ptr );
2322 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2323 if (timer->win == win) free_timer( queue, timer );
2324 ptr = next;
2326 ptr = list_head( &queue->expired_timers );
2327 while (ptr)
2329 struct list *next = list_next( &queue->expired_timers, ptr );
2330 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2331 if (timer->win == win) free_timer( queue, timer );
2332 ptr = next;
2335 /* remove messages */
2336 for (i = 0; i < NB_MSG_KINDS; i++)
2338 struct list *ptr, *next;
2340 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2342 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2343 if (msg->win == win)
2345 if (msg->msg == WM_QUIT && !queue->quit_message)
2347 queue->quit_message = 1;
2348 queue->exit_code = msg->wparam;
2350 remove_queue_message( queue, msg, i );
2355 thread_input_cleanup_window( queue, win );
2358 /* post a message to a window */
2359 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2361 struct message *msg;
2362 struct thread *thread = get_window_thread( win );
2364 if (!thread) return;
2366 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2368 msg->type = MSG_POSTED;
2369 msg->win = get_user_full_handle( win );
2370 msg->msg = message;
2371 msg->wparam = wparam;
2372 msg->lparam = lparam;
2373 msg->result = NULL;
2374 msg->data = NULL;
2375 msg->data_size = 0;
2377 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2379 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2380 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2381 if (message == WM_HOTKEY)
2383 set_queue_bits( thread->queue, QS_HOTKEY );
2384 thread->queue->hotkey_count++;
2387 release_object( thread );
2390 /* send a notify message to a window */
2391 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2393 struct message *msg;
2394 struct thread *thread = get_window_thread( win );
2396 if (!thread) return;
2398 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2400 msg->type = MSG_NOTIFY;
2401 msg->win = get_user_full_handle( win );
2402 msg->msg = message;
2403 msg->wparam = wparam;
2404 msg->lparam = lparam;
2405 msg->result = NULL;
2406 msg->data = NULL;
2407 msg->data_size = 0;
2409 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2411 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2412 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2414 release_object( thread );
2417 /* post a win event */
2418 void post_win_event( struct thread *thread, unsigned int event,
2419 user_handle_t win, unsigned int object_id,
2420 unsigned int child_id, client_ptr_t hook_proc,
2421 const WCHAR *module, data_size_t module_size,
2422 user_handle_t hook)
2424 struct message *msg;
2426 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2428 struct winevent_msg_data *data;
2430 msg->type = MSG_WINEVENT;
2431 msg->win = get_user_full_handle( win );
2432 msg->msg = event;
2433 msg->wparam = object_id;
2434 msg->lparam = child_id;
2435 msg->time = get_tick_count();
2436 msg->result = NULL;
2438 if ((data = malloc( sizeof(*data) + module_size )))
2440 data->hook = hook;
2441 data->tid = get_thread_id( current );
2442 data->hook_proc = hook_proc;
2443 memcpy( data + 1, module, module_size );
2445 msg->data = data;
2446 msg->data_size = sizeof(*data) + module_size;
2448 if (debug_level > 1)
2449 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2450 get_thread_id(thread), event, win, object_id, child_id );
2451 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2452 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2454 else
2455 free( msg );
2459 /* free all hotkeys on a desktop, optionally filtering by window */
2460 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2462 struct hotkey *hotkey, *hotkey2;
2464 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2466 if (!window || hotkey->win == window)
2468 list_remove( &hotkey->entry );
2469 free( hotkey );
2475 /* check if the thread owning the window is hung */
2476 DECL_HANDLER(is_window_hung)
2478 struct thread *thread;
2480 thread = get_window_thread( req->win );
2481 if (thread)
2483 reply->is_hung = is_queue_hung( thread->queue );
2484 release_object( thread );
2486 else reply->is_hung = 0;
2490 /* get the message queue of the current thread */
2491 DECL_HANDLER(get_msg_queue)
2493 struct msg_queue *queue = get_current_queue();
2495 reply->handle = 0;
2496 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2500 /* set the file descriptor associated to the current thread queue */
2501 DECL_HANDLER(set_queue_fd)
2503 struct msg_queue *queue = get_current_queue();
2504 struct file *file;
2505 int unix_fd;
2507 if (queue->fd) /* fd can only be set once */
2509 set_error( STATUS_ACCESS_DENIED );
2510 return;
2512 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2514 if ((unix_fd = get_file_unix_fd( file )) != -1)
2516 if ((unix_fd = dup( unix_fd )) != -1)
2517 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2518 else
2519 file_set_error();
2521 release_object( file );
2525 /* set the current message queue wakeup mask */
2526 DECL_HANDLER(set_queue_mask)
2528 struct msg_queue *queue = get_current_queue();
2530 if (queue)
2532 queue->wake_mask = req->wake_mask;
2533 queue->changed_mask = req->changed_mask;
2534 reply->wake_bits = queue->wake_bits;
2535 reply->changed_bits = queue->changed_bits;
2536 if (is_signaled( queue ))
2538 /* if skip wait is set, do what would have been done in the subsequent wait */
2539 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2540 else wake_up( &queue->obj, 0 );
2546 /* get the current message queue status */
2547 DECL_HANDLER(get_queue_status)
2549 struct msg_queue *queue = current->queue;
2550 if (queue)
2552 reply->wake_bits = queue->wake_bits;
2553 reply->changed_bits = queue->changed_bits;
2554 queue->changed_bits &= ~req->clear_bits;
2556 else reply->wake_bits = reply->changed_bits = 0;
2560 /* send a message to a thread queue */
2561 DECL_HANDLER(send_message)
2563 struct message *msg;
2564 struct msg_queue *send_queue = get_current_queue();
2565 struct msg_queue *recv_queue = NULL;
2566 struct thread *thread = NULL;
2568 if (!(thread = get_thread_from_id( req->id ))) return;
2570 if (!(recv_queue = thread->queue))
2572 set_error( STATUS_INVALID_PARAMETER );
2573 release_object( thread );
2574 return;
2576 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2578 set_error( STATUS_TIMEOUT );
2579 release_object( thread );
2580 return;
2583 if ((msg = mem_alloc( sizeof(*msg) )))
2585 msg->type = req->type;
2586 msg->win = get_user_full_handle( req->win );
2587 msg->msg = req->msg;
2588 msg->wparam = req->wparam;
2589 msg->lparam = req->lparam;
2590 msg->result = NULL;
2591 msg->data = NULL;
2592 msg->data_size = get_req_data_size();
2594 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2596 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2598 free( msg );
2599 release_object( thread );
2600 return;
2603 switch(msg->type)
2605 case MSG_OTHER_PROCESS:
2606 case MSG_ASCII:
2607 case MSG_UNICODE:
2608 case MSG_CALLBACK:
2609 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2611 free_message( msg );
2612 break;
2614 /* fall through */
2615 case MSG_NOTIFY:
2616 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2617 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2618 break;
2619 case MSG_POSTED:
2620 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2621 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2622 if (msg->msg == WM_HOTKEY)
2624 set_queue_bits( recv_queue, QS_HOTKEY );
2625 recv_queue->hotkey_count++;
2627 break;
2628 case MSG_HARDWARE: /* should use send_hardware_message instead */
2629 case MSG_CALLBACK_RESULT: /* cannot send this one */
2630 case MSG_HOOK_LL: /* generated internally */
2631 default:
2632 set_error( STATUS_INVALID_PARAMETER );
2633 free( msg );
2634 break;
2637 release_object( thread );
2640 /* send a hardware message to a thread queue */
2641 DECL_HANDLER(send_hardware_message)
2643 struct thread *thread = NULL;
2644 struct desktop *desktop;
2645 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2646 struct msg_queue *sender = get_current_queue();
2648 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2650 if (req->win)
2652 if (!(thread = get_window_thread( req->win ))) return;
2653 if (desktop != thread->queue->input->desktop)
2655 /* don't allow queuing events to a different desktop */
2656 release_object( desktop );
2657 return;
2661 reply->prev_x = desktop->cursor.x;
2662 reply->prev_y = desktop->cursor.y;
2664 switch (req->input.type)
2666 case INPUT_MOUSE:
2667 reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2668 break;
2669 case INPUT_KEYBOARD:
2670 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2671 break;
2672 case INPUT_HARDWARE:
2673 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2674 break;
2675 default:
2676 set_error( STATUS_INVALID_PARAMETER );
2678 if (thread) release_object( thread );
2680 reply->new_x = desktop->cursor.x;
2681 reply->new_y = desktop->cursor.y;
2682 release_object( desktop );
2685 /* post a quit message to the current queue */
2686 DECL_HANDLER(post_quit_message)
2688 struct msg_queue *queue = get_current_queue();
2690 if (!queue)
2691 return;
2693 queue->quit_message = 1;
2694 queue->exit_code = req->exit_code;
2695 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2698 /* get a message from the current queue */
2699 DECL_HANDLER(get_message)
2701 struct timer *timer;
2702 struct list *ptr;
2703 struct msg_queue *queue = get_current_queue();
2704 user_handle_t get_win = get_user_full_handle( req->get_win );
2705 unsigned int filter = req->flags >> 16;
2707 reply->active_hooks = get_active_hooks();
2709 if (get_win && get_win != 1 && get_win != -1 && !get_user_object( get_win, USER_WINDOW ))
2711 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2712 return;
2715 if (!queue) return;
2716 queue->last_get_msg = current_time;
2717 if (!filter) filter = QS_ALLINPUT;
2719 /* first check for sent messages */
2720 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2722 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2723 receive_message( queue, msg, reply );
2724 return;
2727 /* clear changed bits so we can wait on them if we don't find a message */
2728 if (filter & QS_POSTMESSAGE)
2730 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2731 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2733 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2734 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2736 /* then check for posted messages */
2737 if ((filter & QS_POSTMESSAGE) &&
2738 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2739 return;
2741 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2742 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2743 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2744 return;
2746 /* only check for quit messages if not posted messages pending */
2747 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2748 return;
2750 /* then check for any raw hardware message */
2751 if ((filter & QS_INPUT) &&
2752 filter_contains_hw_range( req->get_first, req->get_last ) &&
2753 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2754 return;
2756 /* now check for WM_PAINT */
2757 if ((filter & QS_PAINT) &&
2758 queue->paint_count &&
2759 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2760 (reply->win = find_window_to_repaint( get_win, current )))
2762 reply->type = MSG_POSTED;
2763 reply->msg = WM_PAINT;
2764 reply->wparam = 0;
2765 reply->lparam = 0;
2766 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2767 return;
2770 /* now check for timer */
2771 if ((filter & QS_TIMER) &&
2772 (timer = find_expired_timer( queue, get_win, req->get_first,
2773 req->get_last, (req->flags & PM_REMOVE) )))
2775 reply->type = MSG_POSTED;
2776 reply->win = timer->win;
2777 reply->msg = timer->msg;
2778 reply->wparam = timer->id;
2779 reply->lparam = timer->lparam;
2780 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2781 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2782 set_event( current->process->idle_event );
2783 return;
2786 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2787 queue->wake_mask = req->wake_mask;
2788 queue->changed_mask = req->changed_mask;
2789 set_error( STATUS_PENDING ); /* FIXME */
2793 /* reply to a sent message */
2794 DECL_HANDLER(reply_message)
2796 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2797 else if (current->queue->recv_result)
2798 reply_message( current->queue, req->result, 0, req->remove,
2799 get_req_data(), get_req_data_size() );
2803 /* accept the current hardware message */
2804 DECL_HANDLER(accept_hardware_message)
2806 if (current->queue)
2807 release_hardware_message( current->queue, req->hw_id );
2808 else
2809 set_error( STATUS_ACCESS_DENIED );
2813 /* retrieve the reply for the last message sent */
2814 DECL_HANDLER(get_message_reply)
2816 struct message_result *result;
2817 struct list *entry;
2818 struct msg_queue *queue = current->queue;
2820 if (queue)
2822 set_error( STATUS_PENDING );
2823 reply->result = 0;
2825 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2827 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2828 if (result->replied || req->cancel)
2830 if (result->replied)
2832 reply->result = result->result;
2833 set_error( result->error );
2834 if (result->data)
2836 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2837 set_reply_data_ptr( result->data, data_len );
2838 result->data = NULL;
2839 result->data_size = 0;
2842 remove_result_from_sender( result );
2844 entry = list_head( &queue->send_result );
2845 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2846 else
2848 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2849 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2850 else clear_queue_bits( queue, QS_SMRESULT );
2854 else set_error( STATUS_ACCESS_DENIED );
2858 /* set a window timer */
2859 DECL_HANDLER(set_win_timer)
2861 struct timer *timer;
2862 struct msg_queue *queue;
2863 struct thread *thread = NULL;
2864 user_handle_t win = 0;
2865 lparam_t id = req->id;
2867 if (req->win)
2869 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2871 set_error( STATUS_INVALID_HANDLE );
2872 return;
2874 if (thread->process != current->process)
2876 release_object( thread );
2877 set_error( STATUS_ACCESS_DENIED );
2878 return;
2880 queue = thread->queue;
2881 /* remove it if it existed already */
2882 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2884 else
2886 queue = get_current_queue();
2887 /* look for a timer with this id */
2888 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2890 /* free and reuse id */
2891 free_timer( queue, timer );
2893 else
2895 lparam_t end_id = queue->next_timer_id;
2897 /* find a free id for it */
2898 while (1)
2900 id = queue->next_timer_id;
2901 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2903 if (!find_timer( queue, 0, req->msg, id )) break;
2904 if (queue->next_timer_id == end_id)
2906 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2907 return;
2913 if ((timer = set_timer( queue, req->rate )))
2915 timer->win = win;
2916 timer->msg = req->msg;
2917 timer->id = id;
2918 timer->lparam = req->lparam;
2919 reply->id = id;
2921 if (thread) release_object( thread );
2924 /* kill a window timer */
2925 DECL_HANDLER(kill_win_timer)
2927 struct timer *timer;
2928 struct thread *thread;
2929 user_handle_t win = 0;
2931 if (req->win)
2933 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2935 set_error( STATUS_INVALID_HANDLE );
2936 return;
2938 if (thread->process != current->process)
2940 release_object( thread );
2941 set_error( STATUS_ACCESS_DENIED );
2942 return;
2945 else thread = (struct thread *)grab_object( current );
2947 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2948 free_timer( thread->queue, timer );
2949 else
2950 set_error( STATUS_INVALID_PARAMETER );
2952 release_object( thread );
2955 DECL_HANDLER(register_hotkey)
2957 struct desktop *desktop;
2958 user_handle_t win_handle = req->window;
2959 struct hotkey *hotkey;
2960 struct hotkey *new_hotkey = NULL;
2961 struct thread *thread;
2962 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2964 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2966 if (win_handle)
2968 if (!(win_handle = get_valid_window_handle( win_handle )))
2970 release_object( desktop );
2971 return;
2974 thread = get_window_thread( win_handle );
2975 if (thread) release_object( thread );
2977 if (thread != current)
2979 release_object( desktop );
2980 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2981 return;
2985 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2987 if (req->vkey == hotkey->vkey &&
2988 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2990 release_object( desktop );
2991 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2992 return;
2994 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2995 new_hotkey = hotkey;
2998 if (new_hotkey)
3000 reply->replaced = 1;
3001 reply->flags = new_hotkey->flags;
3002 reply->vkey = new_hotkey->vkey;
3004 else
3006 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
3007 if (new_hotkey)
3009 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
3010 new_hotkey->queue = current->queue;
3011 new_hotkey->win = win_handle;
3012 new_hotkey->id = req->id;
3016 if (new_hotkey)
3018 new_hotkey->flags = req->flags;
3019 new_hotkey->vkey = req->vkey;
3022 release_object( desktop );
3025 DECL_HANDLER(unregister_hotkey)
3027 struct desktop *desktop;
3028 user_handle_t win_handle = req->window;
3029 struct hotkey *hotkey;
3030 struct thread *thread;
3032 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3034 if (win_handle)
3036 if (!(win_handle = get_valid_window_handle( win_handle )))
3038 release_object( desktop );
3039 return;
3042 thread = get_window_thread( win_handle );
3043 if (thread) release_object( thread );
3045 if (thread != current)
3047 release_object( desktop );
3048 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
3049 return;
3053 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
3055 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
3056 goto found;
3059 release_object( desktop );
3060 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
3061 return;
3063 found:
3064 reply->flags = hotkey->flags;
3065 reply->vkey = hotkey->vkey;
3066 list_remove( &hotkey->entry );
3067 free( hotkey );
3068 release_object( desktop );
3071 /* attach (or detach) thread inputs */
3072 DECL_HANDLER(attach_thread_input)
3074 struct thread *thread_from = get_thread_from_id( req->tid_from );
3075 struct thread *thread_to = get_thread_from_id( req->tid_to );
3077 if (!thread_from || !thread_to)
3079 if (thread_from) release_object( thread_from );
3080 if (thread_to) release_object( thread_to );
3081 return;
3083 if (thread_from != thread_to)
3085 if (req->attach)
3087 if ((thread_to->queue || thread_to == current) &&
3088 (thread_from->queue || thread_from == current))
3089 attach_thread_input( thread_from, thread_to );
3090 else
3091 set_error( STATUS_INVALID_PARAMETER );
3093 else
3095 if (thread_from->queue && thread_to->queue &&
3096 thread_from->queue->input == thread_to->queue->input)
3097 detach_thread_input( thread_from );
3098 else
3099 set_error( STATUS_ACCESS_DENIED );
3102 else set_error( STATUS_ACCESS_DENIED );
3103 release_object( thread_from );
3104 release_object( thread_to );
3108 /* get thread input data */
3109 DECL_HANDLER(get_thread_input)
3111 struct thread *thread = NULL;
3112 struct desktop *desktop;
3113 struct thread_input *input;
3115 if (req->tid)
3117 if (!(thread = get_thread_from_id( req->tid ))) return;
3118 if (!(desktop = get_thread_desktop( thread, 0 )))
3120 release_object( thread );
3121 return;
3123 input = thread->queue ? thread->queue->input : NULL;
3125 else
3127 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3128 input = desktop->foreground_input; /* get the foreground thread info */
3131 if (input)
3133 reply->focus = input->focus;
3134 reply->capture = input->capture;
3135 reply->active = input->active;
3136 reply->menu_owner = input->menu_owner;
3137 reply->move_size = input->move_size;
3138 reply->caret = input->caret;
3139 reply->cursor = input->cursor;
3140 reply->show_count = input->cursor_count;
3141 reply->rect = input->caret_rect;
3144 /* foreground window is active window of foreground thread */
3145 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
3146 if (thread) release_object( thread );
3147 release_object( desktop );
3151 /* retrieve queue keyboard state for current thread or global async state */
3152 DECL_HANDLER(get_key_state)
3154 struct desktop *desktop;
3155 data_size_t size = min( 256, get_reply_max_size() );
3157 if (req->async) /* get global async key state */
3159 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3160 if (req->key >= 0)
3162 reply->state = desktop->keystate[req->key & 0xff];
3163 desktop->keystate[req->key & 0xff] &= ~0x40;
3165 set_reply_data( desktop->keystate, size );
3166 release_object( desktop );
3168 else
3170 struct msg_queue *queue = get_current_queue();
3171 unsigned char *keystate = queue->input->keystate;
3172 if (req->key >= 0)
3174 sync_input_keystate( queue->input );
3175 reply->state = keystate[req->key & 0xff];
3177 set_reply_data( keystate, size );
3182 /* set queue keyboard state for current thread */
3183 DECL_HANDLER(set_key_state)
3185 struct desktop *desktop;
3186 struct msg_queue *queue = get_current_queue();
3187 data_size_t size = min( 256, get_req_data_size() );
3189 memcpy( queue->input->keystate, get_req_data(), size );
3190 memcpy( queue->input->desktop_keystate, queue->input->desktop->keystate, 256 );
3191 if (req->async && (desktop = get_thread_desktop( current, 0 )))
3193 memcpy( desktop->keystate, get_req_data(), size );
3194 release_object( desktop );
3199 /* set the system foreground window */
3200 DECL_HANDLER(set_foreground_window)
3202 struct thread *thread = NULL;
3203 struct desktop *desktop;
3204 struct msg_queue *queue = get_current_queue();
3206 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3207 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
3208 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
3209 reply->send_msg_new = FALSE;
3211 if (is_valid_foreground_window( req->handle ) &&
3212 (thread = get_window_thread( req->handle )) &&
3213 thread->queue->input->desktop == desktop)
3215 set_foreground_input( desktop, thread->queue->input );
3216 reply->send_msg_new = (desktop->foreground_input != queue->input);
3218 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
3220 if (thread) release_object( thread );
3221 release_object( desktop );
3225 /* set the current thread focus window */
3226 DECL_HANDLER(set_focus_window)
3228 struct msg_queue *queue = get_current_queue();
3230 reply->previous = 0;
3231 if (queue && check_queue_input_window( queue, req->handle ))
3233 reply->previous = queue->input->focus;
3234 queue->input->focus = get_user_full_handle( req->handle );
3239 /* set the current thread active window */
3240 DECL_HANDLER(set_active_window)
3242 struct msg_queue *queue = get_current_queue();
3244 reply->previous = 0;
3245 if (queue && check_queue_input_window( queue, req->handle ))
3247 if (!req->handle || make_window_active( req->handle ))
3249 reply->previous = queue->input->active;
3250 queue->input->active = get_user_full_handle( req->handle );
3252 else set_error( STATUS_INVALID_HANDLE );
3257 /* set the current thread capture window */
3258 DECL_HANDLER(set_capture_window)
3260 struct msg_queue *queue = get_current_queue();
3262 reply->previous = reply->full_handle = 0;
3263 if (queue && check_queue_input_window( queue, req->handle ))
3265 struct thread_input *input = queue->input;
3267 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
3268 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
3270 set_error(STATUS_ACCESS_DENIED);
3271 return;
3273 reply->previous = input->capture;
3274 input->capture = get_user_full_handle( req->handle );
3275 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
3276 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
3277 reply->full_handle = input->capture;
3282 /* Set the current thread caret window */
3283 DECL_HANDLER(set_caret_window)
3285 struct msg_queue *queue = get_current_queue();
3287 reply->previous = 0;
3288 if (queue && check_queue_input_window( queue, req->handle ))
3290 struct thread_input *input = queue->input;
3292 reply->previous = input->caret;
3293 reply->old_rect = input->caret_rect;
3294 reply->old_hide = input->caret_hide;
3295 reply->old_state = input->caret_state;
3297 set_caret_window( input, get_user_full_handle(req->handle) );
3298 input->caret_rect.right = input->caret_rect.left + req->width;
3299 input->caret_rect.bottom = input->caret_rect.top + req->height;
3304 /* Set the current thread caret information */
3305 DECL_HANDLER(set_caret_info)
3307 struct msg_queue *queue = get_current_queue();
3308 struct thread_input *input;
3310 if (!queue) return;
3311 input = queue->input;
3312 reply->full_handle = input->caret;
3313 reply->old_rect = input->caret_rect;
3314 reply->old_hide = input->caret_hide;
3315 reply->old_state = input->caret_state;
3317 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3319 set_error( STATUS_ACCESS_DENIED );
3320 return;
3322 if (req->flags & SET_CARET_POS)
3324 input->caret_rect.right += req->x - input->caret_rect.left;
3325 input->caret_rect.bottom += req->y - input->caret_rect.top;
3326 input->caret_rect.left = req->x;
3327 input->caret_rect.top = req->y;
3329 if (req->flags & SET_CARET_HIDE)
3331 input->caret_hide += req->hide;
3332 if (input->caret_hide < 0) input->caret_hide = 0;
3334 if (req->flags & SET_CARET_STATE)
3336 switch (req->state)
3338 case CARET_STATE_OFF: input->caret_state = 0; break;
3339 case CARET_STATE_ON: input->caret_state = 1; break;
3340 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3341 case CARET_STATE_ON_IF_MOVED:
3342 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3343 break;
3349 /* get the time of the last input event */
3350 DECL_HANDLER(get_last_input_time)
3352 reply->time = last_input_time;
3355 /* set/get the current cursor */
3356 DECL_HANDLER(set_cursor)
3358 struct msg_queue *queue = get_current_queue();
3359 struct thread_input *input;
3360 struct desktop *desktop;
3362 if (!queue) return;
3363 input = queue->input;
3364 desktop = input->desktop;
3366 reply->prev_handle = input->cursor;
3367 reply->prev_count = input->cursor_count;
3368 reply->prev_x = desktop->cursor.x;
3369 reply->prev_y = desktop->cursor.y;
3371 if (req->flags & SET_CURSOR_HANDLE)
3373 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3375 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3376 return;
3378 input->cursor = req->handle;
3380 if (req->flags & SET_CURSOR_COUNT)
3382 queue->cursor_count += req->show_count;
3383 input->cursor_count += req->show_count;
3385 if (req->flags & SET_CURSOR_POS) set_cursor_pos( desktop, req->x, req->y );
3386 if (req->flags & SET_CURSOR_CLIP) set_clip_rectangle( desktop, &req->clip, req->flags, 0 );
3387 if (req->flags & SET_CURSOR_NOCLIP) set_clip_rectangle( desktop, NULL, SET_CURSOR_NOCLIP, 0 );
3389 if (req->flags & (SET_CURSOR_HANDLE | SET_CURSOR_COUNT))
3391 if (input->cursor_count < 0) update_desktop_cursor_handle( desktop, 0 );
3392 else update_desktop_cursor_handle( desktop, input->cursor );
3395 reply->new_x = desktop->cursor.x;
3396 reply->new_y = desktop->cursor.y;
3397 reply->new_clip = desktop->cursor.clip;
3398 reply->last_change = desktop->cursor.last_change;
3401 /* Get the history of the 64 last cursor positions */
3402 DECL_HANDLER(get_cursor_history)
3404 cursor_pos_t *pos;
3405 unsigned int i, count = min( 64, get_reply_max_size() / sizeof(*pos) );
3407 if ((pos = set_reply_data_size( count * sizeof(*pos) )))
3408 for (i = 0; i < count; i++)
3409 pos[i] = cursor_history[(i + cursor_history_latest) % ARRAY_SIZE(cursor_history)];
3412 DECL_HANDLER(get_rawinput_buffer)
3414 struct thread_input *input = current->queue->input;
3415 data_size_t size = 0, next_size = 0, pos = 0;
3416 struct list *ptr;
3417 char *buf, *tmp;
3418 int count = 0, buf_size = 16 * sizeof(struct hardware_msg_data);
3420 if (!req->buffer_size) buf = NULL;
3421 else if (!(buf = mem_alloc( buf_size ))) return;
3423 ptr = list_head( &input->msg_list );
3424 while (ptr)
3426 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
3427 struct hardware_msg_data *data = msg->data;
3428 data_size_t extra_size = data->size - sizeof(*data);
3430 ptr = list_next( &input->msg_list, ptr );
3431 if (msg->msg != WM_INPUT) continue;
3433 next_size = req->rawinput_size + extra_size;
3434 if (size + next_size > req->buffer_size) break;
3435 if (pos + data->size > get_reply_max_size()) break;
3436 if (pos + data->size > buf_size)
3438 buf_size += buf_size / 2 + extra_size;
3439 if (!(tmp = realloc( buf, buf_size )))
3441 free( buf );
3442 set_error( STATUS_NO_MEMORY );
3443 return;
3445 buf = tmp;
3448 memcpy( buf + pos, data, data->size );
3449 list_remove( &msg->entry );
3450 free_message( msg );
3452 size += next_size;
3453 pos += sizeof(*data) + extra_size;
3454 count++;
3457 reply->next_size = next_size;
3458 reply->count = count;
3459 set_reply_data_ptr( buf, pos );
3462 DECL_HANDLER(update_rawinput_devices)
3464 const struct rawinput_device *tmp, *devices = get_req_data();
3465 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3466 size_t size = device_count * sizeof(*devices);
3467 struct process *process = current->process;
3469 if (!size)
3471 process->rawinput_device_count = 0;
3472 process->rawinput_mouse = NULL;
3473 process->rawinput_kbd = NULL;
3474 return;
3477 if (!(tmp = realloc( process->rawinput_devices, size )))
3479 set_error( STATUS_NO_MEMORY );
3480 return;
3482 process->rawinput_devices = (struct rawinput_device *)tmp;
3483 process->rawinput_device_count = device_count;
3484 memcpy( process->rawinput_devices, devices, size );
3486 process->rawinput_mouse = find_rawinput_device( process, 1, 2 );
3487 process->rawinput_kbd = find_rawinput_device( process, 1, 6 );