rsaenh: Validate pbData in CPSetKeyParam().
[wine.git] / server / queue.c
blobed099b3b989740268ba26899afc02524871f77d1
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"
38 #include "hidusage.h"
40 #include "handle.h"
41 #include "file.h"
42 #include "thread.h"
43 #include "process.h"
44 #include "request.h"
45 #include "user.h"
47 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
48 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
50 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
51 #define NB_MSG_KINDS (POST_MESSAGE+1)
53 /* list of processes registered for rawinput in the input desktop */
54 static struct list rawinput_processes = LIST_INIT(rawinput_processes);
56 struct message_result
58 struct list sender_entry; /* entry in sender list */
59 struct message *msg; /* message the result is for */
60 struct message_result *recv_next; /* next in receiver list */
61 struct msg_queue *sender; /* sender queue */
62 struct msg_queue *receiver; /* receiver queue */
63 int replied; /* has it been replied to? */
64 unsigned int error; /* error code to pass back to sender */
65 lparam_t result; /* reply result */
66 struct message *hardware_msg; /* hardware message if low-level hook result */
67 struct desktop *desktop; /* desktop for hardware message */
68 struct message *callback_msg; /* message to queue for callback */
69 void *data; /* message reply data */
70 unsigned int data_size; /* size of message reply data */
71 struct timeout_user *timeout; /* result timeout */
74 struct message
76 struct list entry; /* entry in message list */
77 enum message_type type; /* message type */
78 user_handle_t win; /* window handle */
79 unsigned int msg; /* message code */
80 lparam_t wparam; /* parameters */
81 lparam_t lparam; /* parameters */
82 int x; /* message position */
83 int y;
84 unsigned int time; /* message time */
85 void *data; /* message data for sent messages */
86 unsigned int data_size; /* size of message data */
87 unsigned int unique_id; /* unique id for nested hw message waits */
88 struct message_result *result; /* result in sender queue */
91 struct timer
93 struct list entry; /* entry in timer list */
94 abstime_t when; /* next expiration */
95 unsigned int rate; /* timer rate in ms */
96 user_handle_t win; /* window handle */
97 unsigned int msg; /* message to post */
98 lparam_t id; /* timer id */
99 lparam_t lparam; /* lparam for message */
102 struct thread_input
104 struct object obj; /* object header */
105 struct desktop *desktop; /* desktop that this thread input belongs to */
106 user_handle_t focus; /* focus window */
107 user_handle_t capture; /* capture window */
108 user_handle_t active; /* active window */
109 user_handle_t menu_owner; /* current menu owner window */
110 user_handle_t move_size; /* current moving/resizing window */
111 user_handle_t caret; /* caret window */
112 rectangle_t caret_rect; /* caret rectangle */
113 int caret_hide; /* caret hide count */
114 int caret_state; /* caret on/off state */
115 user_handle_t cursor; /* current cursor */
116 int cursor_count; /* cursor show count */
117 struct list msg_list; /* list of hardware messages */
118 unsigned char keystate[256]; /* state of each key */
119 unsigned char desktop_keystate[256]; /* desktop keystate when keystate was synced */
120 int keystate_lock; /* keystate is locked */
123 struct msg_queue
125 struct object obj; /* object header */
126 struct fd *fd; /* optional file descriptor to poll */
127 unsigned int wake_bits; /* wakeup bits */
128 unsigned int wake_mask; /* wakeup mask */
129 unsigned int changed_bits; /* changed wakeup bits */
130 unsigned int changed_mask; /* changed wakeup mask */
131 int paint_count; /* pending paint messages count */
132 int hotkey_count; /* pending hotkey messages count */
133 int quit_message; /* is there a pending quit message? */
134 int exit_code; /* exit code of pending quit message */
135 int cursor_count; /* per-queue cursor show count */
136 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
137 struct list send_result; /* stack of sent messages waiting for result */
138 struct list callback_result; /* list of callback messages waiting for result */
139 struct message_result *recv_result; /* stack of received messages waiting for result */
140 struct list pending_timers; /* list of pending timers */
141 struct list expired_timers; /* list of expired timers */
142 lparam_t next_timer_id; /* id for the next timer with a 0 window */
143 struct timeout_user *timeout; /* timeout for next timer to expire */
144 struct thread_input *input; /* thread input descriptor */
145 struct hook_table *hooks; /* hook table */
146 timeout_t last_get_msg; /* time of last get message call */
147 int keystate_lock; /* owns an input keystate lock */
150 struct hotkey
152 struct list entry; /* entry in desktop hotkey list */
153 struct msg_queue *queue; /* queue owning this hotkey */
154 user_handle_t win; /* window handle */
155 int id; /* hotkey id */
156 unsigned int vkey; /* virtual key code */
157 unsigned int flags; /* key modifiers */
160 static void msg_queue_dump( struct object *obj, int verbose );
161 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
162 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
163 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
164 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
165 static void msg_queue_destroy( struct object *obj );
166 static void msg_queue_poll_event( struct fd *fd, int event );
167 static void thread_input_dump( struct object *obj, int verbose );
168 static void thread_input_destroy( struct object *obj );
169 static void timer_callback( void *private );
171 static const struct object_ops msg_queue_ops =
173 sizeof(struct msg_queue), /* size */
174 &no_type, /* type */
175 msg_queue_dump, /* dump */
176 msg_queue_add_queue, /* add_queue */
177 msg_queue_remove_queue, /* remove_queue */
178 msg_queue_signaled, /* signaled */
179 msg_queue_satisfied, /* satisfied */
180 no_signal, /* signal */
181 no_get_fd, /* get_fd */
182 default_map_access, /* map_access */
183 default_get_sd, /* get_sd */
184 default_set_sd, /* set_sd */
185 no_get_full_name, /* get_full_name */
186 no_lookup_name, /* lookup_name */
187 no_link_name, /* link_name */
188 NULL, /* unlink_name */
189 no_open_file, /* open_file */
190 no_kernel_obj_list, /* get_kernel_obj_list */
191 no_close_handle, /* close_handle */
192 msg_queue_destroy /* destroy */
195 static const struct fd_ops msg_queue_fd_ops =
197 NULL, /* get_poll_events */
198 msg_queue_poll_event, /* poll_event */
199 NULL, /* flush */
200 NULL, /* get_fd_type */
201 NULL, /* ioctl */
202 NULL, /* queue_async */
203 NULL, /* reselect_async */
204 NULL /* cancel async */
208 static const struct object_ops thread_input_ops =
210 sizeof(struct thread_input), /* size */
211 &no_type, /* type */
212 thread_input_dump, /* dump */
213 no_add_queue, /* add_queue */
214 NULL, /* remove_queue */
215 NULL, /* signaled */
216 NULL, /* satisfied */
217 no_signal, /* signal */
218 no_get_fd, /* get_fd */
219 default_map_access, /* map_access */
220 default_get_sd, /* get_sd */
221 default_set_sd, /* set_sd */
222 no_get_full_name, /* get_full_name */
223 no_lookup_name, /* lookup_name */
224 no_link_name, /* link_name */
225 NULL, /* unlink_name */
226 no_open_file, /* open_file */
227 no_kernel_obj_list, /* get_kernel_obj_list */
228 no_close_handle, /* close_handle */
229 thread_input_destroy /* destroy */
232 /* pointer to input structure of foreground thread */
233 static unsigned int last_input_time;
235 static cursor_pos_t cursor_history[64];
236 static unsigned int cursor_history_latest;
238 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
239 static void free_message( struct message *msg );
241 /* set the caret window in a given thread input */
242 static void set_caret_window( struct thread_input *input, user_handle_t win )
244 if (!win || win != input->caret)
246 input->caret_rect.left = 0;
247 input->caret_rect.top = 0;
248 input->caret_rect.right = 0;
249 input->caret_rect.bottom = 0;
251 input->caret = win;
252 input->caret_hide = 1;
253 input->caret_state = 0;
256 /* create a thread input object */
257 static struct thread_input *create_thread_input( struct thread *thread )
259 struct thread_input *input;
261 if ((input = alloc_object( &thread_input_ops )))
263 input->focus = 0;
264 input->capture = 0;
265 input->active = 0;
266 input->menu_owner = 0;
267 input->move_size = 0;
268 input->cursor = 0;
269 input->cursor_count = 0;
270 list_init( &input->msg_list );
271 set_caret_window( input, 0 );
272 memset( input->keystate, 0, sizeof(input->keystate) );
273 input->keystate_lock = 0;
275 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
277 release_object( input );
278 return NULL;
280 memcpy( input->desktop_keystate, input->desktop->keystate, sizeof(input->desktop_keystate) );
282 return input;
285 /* create a message queue object */
286 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
288 struct thread_input *new_input = NULL;
289 struct msg_queue *queue;
290 int i;
292 if (!input)
294 if (!(new_input = create_thread_input( thread ))) return NULL;
295 input = new_input;
298 if ((queue = alloc_object( &msg_queue_ops )))
300 queue->fd = NULL;
301 queue->wake_bits = 0;
302 queue->wake_mask = 0;
303 queue->changed_bits = 0;
304 queue->changed_mask = 0;
305 queue->paint_count = 0;
306 queue->hotkey_count = 0;
307 queue->quit_message = 0;
308 queue->cursor_count = 0;
309 queue->recv_result = NULL;
310 queue->next_timer_id = 0x7fff;
311 queue->timeout = NULL;
312 queue->input = (struct thread_input *)grab_object( input );
313 queue->hooks = NULL;
314 queue->last_get_msg = current_time;
315 queue->keystate_lock = 0;
316 list_init( &queue->send_result );
317 list_init( &queue->callback_result );
318 list_init( &queue->pending_timers );
319 list_init( &queue->expired_timers );
320 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
322 thread->queue = queue;
324 if (new_input) release_object( new_input );
325 return queue;
328 /* free the message queue of a thread at thread exit */
329 void free_msg_queue( struct thread *thread )
331 remove_thread_hooks( thread );
332 if (!thread->queue) return;
333 release_object( thread->queue );
334 thread->queue = NULL;
337 /* synchronize thread input keystate with the desktop */
338 static void sync_input_keystate( struct thread_input *input )
340 int i;
341 if (!input->desktop || input->keystate_lock) return;
342 for (i = 0; i < sizeof(input->keystate); ++i)
344 if (input->desktop_keystate[i] == input->desktop->keystate[i]) continue;
345 input->keystate[i] = input->desktop_keystate[i] = input->desktop->keystate[i];
349 /* locks thread input keystate to prevent synchronization */
350 static void lock_input_keystate( struct thread_input *input )
352 input->keystate_lock++;
355 /* unlock the thread input keystate and synchronize it again */
356 static void unlock_input_keystate( struct thread_input *input )
358 input->keystate_lock--;
359 if (!input->keystate_lock) sync_input_keystate( input );
362 /* change the thread input data of a given thread */
363 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
365 struct msg_queue *queue = thread->queue;
367 if (!queue)
369 thread->queue = create_msg_queue( thread, new_input );
370 return thread->queue != NULL;
372 if (queue->input)
374 queue->input->cursor_count -= queue->cursor_count;
375 if (queue->keystate_lock) unlock_input_keystate( queue->input );
376 release_object( queue->input );
378 queue->input = (struct thread_input *)grab_object( new_input );
379 if (queue->keystate_lock) lock_input_keystate( queue->input );
380 new_input->cursor_count += queue->cursor_count;
381 return 1;
384 /* allocate a hardware message and its data */
385 static struct message *alloc_hardware_message( lparam_t info, struct hw_msg_source source,
386 unsigned int time, data_size_t extra_size )
388 struct hardware_msg_data *msg_data;
389 struct message *msg;
391 if (!(msg = mem_alloc( sizeof(*msg) ))) return NULL;
392 if (!(msg_data = mem_alloc( sizeof(*msg_data) + extra_size )))
394 free( msg );
395 return NULL;
397 memset( msg, 0, sizeof(*msg) );
398 msg->type = MSG_HARDWARE;
399 msg->time = time;
400 msg->data = msg_data;
401 msg->data_size = sizeof(*msg_data) + extra_size;
403 memset( msg_data, 0, sizeof(*msg_data) + extra_size );
404 msg_data->info = info;
405 msg_data->size = msg->data_size;
406 msg_data->source = source;
407 return msg;
410 static int is_cursor_clipped( struct desktop *desktop )
412 rectangle_t top_rect, clip_rect = desktop->cursor.clip;
413 get_top_window_rectangle( desktop, &top_rect );
414 return !is_rect_equal( &clip_rect, &top_rect );
417 static void queue_cursor_message( struct desktop *desktop, user_handle_t win, unsigned int message,
418 lparam_t wparam, lparam_t lparam )
420 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
421 struct thread_input *input;
422 struct message *msg;
424 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
426 msg->msg = message;
427 msg->wparam = wparam;
428 msg->lparam = lparam;
429 msg->x = desktop->cursor.x;
430 msg->y = desktop->cursor.y;
431 if (!(msg->win = win) && (input = desktop->foreground_input)) msg->win = input->active;
432 queue_hardware_message( desktop, msg, 1 );
435 static struct thread_input *get_desktop_cursor_thread_input( struct desktop *desktop )
437 struct thread_input *input = NULL;
438 struct thread *thread;
440 if ((thread = get_window_thread( desktop->cursor.win )))
442 if (thread->queue) input = thread->queue->input;
443 release_object( thread );
446 return input;
449 static int update_desktop_cursor_window( struct desktop *desktop, user_handle_t win )
451 int updated = win != desktop->cursor.win;
452 struct thread_input *input;
453 desktop->cursor.win = win;
455 if (updated && (input = get_desktop_cursor_thread_input( desktop )))
457 user_handle_t handle = input->cursor_count < 0 ? 0 : input->cursor;
458 /* when clipping send the message to the foreground window as well, as some driver have an artificial overlay window */
459 if (is_cursor_clipped( desktop )) queue_cursor_message( desktop, 0, WM_WINE_SETCURSOR, win, handle );
460 queue_cursor_message( desktop, win, WM_WINE_SETCURSOR, win, handle );
463 return updated;
466 static int update_desktop_cursor_pos( struct desktop *desktop, user_handle_t win, int x, int y )
468 int updated;
470 x = max( min( x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
471 y = max( min( y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
472 updated = (desktop->cursor.x != x || desktop->cursor.y != y);
473 desktop->cursor.x = x;
474 desktop->cursor.y = y;
475 desktop->cursor.last_change = get_tick_count();
477 if (!win || !is_window_visible( win ) || is_window_transparent( win ))
478 win = shallow_window_from_point( desktop, x, y );
479 if (update_desktop_cursor_window( desktop, win )) updated = 1;
481 return updated;
484 static void update_desktop_cursor_handle( struct desktop *desktop, struct thread_input *input, user_handle_t handle )
486 if (input == get_desktop_cursor_thread_input( desktop ))
488 user_handle_t win = desktop->cursor.win;
489 /* when clipping send the message to the foreground window as well, as some driver have an artificial overlay window */
490 if (is_cursor_clipped( desktop )) queue_cursor_message( desktop, 0, WM_WINE_SETCURSOR, win, handle );
491 queue_cursor_message( desktop, win, WM_WINE_SETCURSOR, win, handle );
495 /* set the cursor position and queue the corresponding mouse message */
496 static void set_cursor_pos( struct desktop *desktop, int x, int y )
498 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
499 const struct rawinput_device *device;
500 struct message *msg;
502 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
504 update_desktop_cursor_pos( desktop, 0, x, y );
505 return;
508 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
510 msg->msg = WM_MOUSEMOVE;
511 msg->x = x;
512 msg->y = y;
513 queue_hardware_message( desktop, msg, 1 );
516 /* retrieve default position and time for synthesized messages */
517 static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
519 struct desktop *desktop = queue->input->desktop;
521 *x = desktop->cursor.x;
522 *y = desktop->cursor.y;
523 *time = get_tick_count();
526 /* set the cursor clip rectangle */
527 void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect, unsigned int flags, int reset )
529 rectangle_t top_rect;
530 unsigned int old_flags;
531 int x, y;
533 get_top_window_rectangle( desktop, &top_rect );
534 if (rect)
536 rectangle_t new_rect = *rect;
537 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
538 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
539 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
540 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
541 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
542 desktop->cursor.clip = new_rect;
544 else desktop->cursor.clip = top_rect;
546 old_flags = desktop->cursor.clip_flags;
547 desktop->cursor.clip_flags = flags;
549 /* warp the mouse to be inside the clip rect */
550 x = max( min( desktop->cursor.x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
551 y = max( min( desktop->cursor.y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
552 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
554 /* request clip cursor rectangle reset to the desktop thread */
555 if (reset) post_desktop_message( desktop, WM_WINE_CLIPCURSOR, flags, FALSE );
557 /* notify foreground thread of reset, clipped, or released cursor rect */
558 if (reset || flags != SET_CURSOR_NOCLIP || old_flags != SET_CURSOR_NOCLIP)
559 queue_cursor_message( desktop, 0, WM_WINE_CLIPCURSOR, flags, reset );
562 /* change the foreground input and reset the cursor clip rect */
563 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
565 if (desktop->foreground_input == input) return;
566 set_clip_rectangle( desktop, NULL, SET_CURSOR_NOCLIP, 1 );
567 desktop->foreground_input = input;
570 /* get the hook table for a given thread */
571 struct hook_table *get_queue_hooks( struct thread *thread )
573 if (!thread->queue) return NULL;
574 return thread->queue->hooks;
577 /* set the hook table for a given thread, allocating the queue if needed */
578 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
580 struct msg_queue *queue = thread->queue;
581 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
582 if (queue->hooks) release_object( queue->hooks );
583 queue->hooks = hooks;
586 /* check the queue status */
587 static inline int is_signaled( struct msg_queue *queue )
589 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
592 /* set some queue bits */
593 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
595 if (bits & (QS_KEY | QS_MOUSEBUTTON))
597 if (!queue->keystate_lock) lock_input_keystate( queue->input );
598 queue->keystate_lock = 1;
600 queue->wake_bits |= bits;
601 queue->changed_bits |= bits;
602 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
605 /* clear some queue bits */
606 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
608 queue->wake_bits &= ~bits;
609 queue->changed_bits &= ~bits;
610 if (!(queue->wake_bits & (QS_KEY | QS_MOUSEBUTTON)))
612 if (queue->keystate_lock) unlock_input_keystate( queue->input );
613 queue->keystate_lock = 0;
617 /* check if message is matched by the filter */
618 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
620 return (msg >= first && msg <= last);
623 /* check whether a message filter contains at least one potential hardware message */
624 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
626 /* hardware message ranges are (in numerical order):
627 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
628 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
629 * WM_MOUSEFIRST .. WM_MOUSELAST
631 if (last < WM_NCMOUSEFIRST) return 0;
632 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
633 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
634 if (first > WM_MOUSELAST) return 0;
635 return 1;
638 /* get the QS_* bit corresponding to a given hardware message */
639 static inline int get_hardware_msg_bit( unsigned int message )
641 if (message >= WM_POINTERUPDATE && message <= WM_POINTERLEAVE) return QS_POINTER;
642 if (message == WM_INPUT_DEVICE_CHANGE || message == WM_INPUT) return QS_RAWINPUT;
643 if (message == WM_MOUSEMOVE || message == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
644 if (message >= WM_KEYFIRST && message <= WM_KEYLAST) return QS_KEY;
645 if (message == WM_WINE_CLIPCURSOR) return QS_RAWINPUT;
646 if (message == WM_WINE_SETCURSOR) return QS_RAWINPUT;
647 return QS_MOUSEBUTTON;
650 /* get the current thread queue, creating it if needed */
651 static inline struct msg_queue *get_current_queue(void)
653 struct msg_queue *queue = current->queue;
654 if (!queue) queue = create_msg_queue( current, NULL );
655 return queue;
658 /* get a (pseudo-)unique id to tag hardware messages */
659 static inline unsigned int get_unique_id(void)
661 static unsigned int id;
662 if (!++id) id = 1; /* avoid an id of 0 */
663 return id;
666 /* try to merge a WM_MOUSEMOVE message with the last in the list; return 1 if successful */
667 static int merge_mousemove( struct thread_input *input, const struct message *msg )
669 struct message *prev;
670 struct list *ptr;
672 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
674 prev = LIST_ENTRY( ptr, struct message, entry );
675 if (prev->msg != WM_INPUT) break;
677 if (!ptr) return 0;
678 if (prev->result) return 0;
679 if (prev->win && msg->win && prev->win != msg->win) return 0;
680 if (prev->msg != msg->msg) return 0;
681 if (prev->type != msg->type) return 0;
682 /* now we can merge it */
683 prev->wparam = msg->wparam;
684 prev->lparam = msg->lparam;
685 prev->x = msg->x;
686 prev->y = msg->y;
687 prev->time = msg->time;
688 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
690 struct hardware_msg_data *prev_data = prev->data;
691 struct hardware_msg_data *msg_data = msg->data;
692 prev_data->info = msg_data->info;
694 list_remove( ptr );
695 list_add_tail( &input->msg_list, ptr );
696 return 1;
699 /* try to merge a unique message with the last in the list; return 1 if successful */
700 static int merge_unique_message( struct thread_input *input, unsigned int message, const struct message *msg )
702 struct message *prev;
704 LIST_FOR_EACH_ENTRY_REV( prev, &input->msg_list, struct message, entry )
705 if (prev->msg == message) break;
706 if (&prev->entry == &input->msg_list) return 0;
708 if (prev->result) return 0;
709 if (prev->win != msg->win) return 0;
710 if (prev->type != msg->type) return 0;
712 /* now we can merge it */
713 prev->wparam = msg->wparam;
714 prev->lparam = msg->lparam;
715 prev->x = msg->x;
716 prev->y = msg->y;
717 prev->time = msg->time;
718 list_remove( &prev->entry );
719 list_add_tail( &input->msg_list, &prev->entry );
721 return 1;
724 /* try to merge a message with the messages in the list; return 1 if successful */
725 static int merge_message( struct thread_input *input, const struct message *msg )
727 if (msg->msg == WM_MOUSEMOVE) return merge_mousemove( input, msg );
728 if (msg->msg == WM_WINE_CLIPCURSOR) return merge_unique_message( input, WM_WINE_CLIPCURSOR, msg );
729 if (msg->msg == WM_WINE_SETCURSOR) return merge_unique_message( input, WM_WINE_SETCURSOR, msg );
730 return 0;
733 /* free a result structure */
734 static void free_result( struct message_result *result )
736 if (result->timeout) remove_timeout_user( result->timeout );
737 free( result->data );
738 if (result->callback_msg) free_message( result->callback_msg );
739 if (result->hardware_msg) free_message( result->hardware_msg );
740 if (result->desktop) release_object( result->desktop );
741 free( result );
744 /* remove the result from the sender list it is on */
745 static inline void remove_result_from_sender( struct message_result *result )
747 assert( result->sender );
749 list_remove( &result->sender_entry );
750 result->sender = NULL;
751 if (!result->receiver) free_result( result );
754 /* store the message result in the appropriate structure */
755 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
757 res->result = result;
758 res->error = error;
759 res->replied = 1;
760 if (res->timeout)
762 remove_timeout_user( res->timeout );
763 res->timeout = NULL;
766 if (res->hardware_msg)
768 if (!error && result) /* rejected by the hook */
769 free_message( res->hardware_msg );
770 else
771 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
773 res->hardware_msg = NULL;
776 if (res->sender)
778 if (res->callback_msg)
780 /* queue the callback message in the sender queue */
781 struct callback_msg_data *data = res->callback_msg->data;
782 data->result = result;
783 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
784 set_queue_bits( res->sender, QS_SENDMESSAGE );
785 res->callback_msg = NULL;
786 remove_result_from_sender( res );
788 else
790 /* wake sender queue if waiting on this result */
791 if (list_head(&res->sender->send_result) == &res->sender_entry)
792 set_queue_bits( res->sender, QS_SMRESULT );
795 else if (!res->receiver) free_result( res );
798 /* free a message when deleting a queue or window */
799 static void free_message( struct message *msg )
801 struct message_result *result = msg->result;
802 if (result)
804 result->msg = NULL;
805 result->receiver = NULL;
806 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
808 free( msg->data );
809 free( msg );
812 /* remove (and free) a message from a message list */
813 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
814 enum message_kind kind )
816 list_remove( &msg->entry );
817 switch(kind)
819 case SEND_MESSAGE:
820 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
821 break;
822 case POST_MESSAGE:
823 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
824 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
825 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
826 clear_queue_bits( queue, QS_HOTKEY );
827 break;
829 free_message( msg );
832 /* message timed out without getting a reply */
833 static void result_timeout( void *private )
835 struct message_result *result = private;
837 assert( !result->replied );
839 result->timeout = NULL;
841 if (result->msg) /* not received yet */
843 struct message *msg = result->msg;
845 result->msg = NULL;
846 msg->result = NULL;
847 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
848 result->receiver = NULL;
850 store_message_result( result, 0, STATUS_TIMEOUT );
853 /* allocate and fill a message result structure */
854 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
855 struct msg_queue *recv_queue,
856 struct message *msg, timeout_t timeout )
858 struct message_result *result = mem_alloc( sizeof(*result) );
859 if (result)
861 result->msg = msg;
862 result->sender = send_queue;
863 result->receiver = recv_queue;
864 result->replied = 0;
865 result->data = NULL;
866 result->data_size = 0;
867 result->timeout = NULL;
868 result->hardware_msg = NULL;
869 result->desktop = NULL;
870 result->callback_msg = NULL;
872 if (msg->type == MSG_CALLBACK)
874 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
876 if (!callback_msg)
878 free( result );
879 return NULL;
881 callback_msg->type = MSG_CALLBACK_RESULT;
882 callback_msg->win = msg->win;
883 callback_msg->msg = msg->msg;
884 callback_msg->wparam = 0;
885 callback_msg->lparam = 0;
886 callback_msg->time = get_tick_count();
887 callback_msg->result = NULL;
888 /* steal the data from the original message */
889 callback_msg->data = msg->data;
890 callback_msg->data_size = msg->data_size;
891 msg->data = NULL;
892 msg->data_size = 0;
894 result->callback_msg = callback_msg;
895 list_add_head( &send_queue->callback_result, &result->sender_entry );
897 else if (send_queue)
899 list_add_head( &send_queue->send_result, &result->sender_entry );
900 clear_queue_bits( send_queue, QS_SMRESULT );
903 if (timeout != TIMEOUT_INFINITE)
904 result->timeout = add_timeout_user( timeout, result_timeout, result );
906 return result;
909 /* receive a message, removing it from the sent queue */
910 static void receive_message( struct msg_queue *queue, struct message *msg,
911 struct get_message_reply *reply )
913 struct message_result *result = msg->result;
915 reply->total = msg->data_size;
916 if (msg->data_size > get_reply_max_size())
918 set_error( STATUS_BUFFER_OVERFLOW );
919 return;
921 reply->type = msg->type;
922 reply->win = msg->win;
923 reply->msg = msg->msg;
924 reply->wparam = msg->wparam;
925 reply->lparam = msg->lparam;
926 reply->x = msg->x;
927 reply->y = msg->y;
928 reply->time = msg->time;
930 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
932 list_remove( &msg->entry );
933 /* put the result on the receiver result stack */
934 if (result)
936 result->msg = NULL;
937 result->recv_next = queue->recv_result;
938 queue->recv_result = result;
940 free( msg );
941 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
944 /* set the result of the current received message */
945 static void reply_message( struct msg_queue *queue, lparam_t result,
946 unsigned int error, int remove, const void *data, data_size_t len )
948 struct message_result *res = queue->recv_result;
950 if (remove)
952 queue->recv_result = res->recv_next;
953 res->receiver = NULL;
954 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
956 free_result( res );
957 return;
960 if (!res->replied)
962 if (len && (res->data = memdup( data, len ))) res->data_size = len;
963 store_message_result( res, result, error );
967 static int match_window( user_handle_t win, user_handle_t msg_win )
969 if (!win) return 1;
970 if (win == -1 || win == 1) return !msg_win;
971 if (msg_win == win) return 1;
972 return is_child_window( win, msg_win );
975 /* retrieve a posted message */
976 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
977 unsigned int first, unsigned int last, unsigned int flags,
978 struct get_message_reply *reply )
980 struct message *msg;
982 /* check against the filters */
983 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
985 if (!match_window( win, msg->win )) continue;
986 if (!check_msg_filter( msg->msg, first, last )) continue;
987 goto found; /* found one */
989 return 0;
991 /* return it to the app */
992 found:
993 reply->total = msg->data_size;
994 if (msg->data_size > get_reply_max_size())
996 set_error( STATUS_BUFFER_OVERFLOW );
997 return 1;
999 reply->type = msg->type;
1000 reply->win = msg->win;
1001 reply->msg = msg->msg;
1002 reply->wparam = msg->wparam;
1003 reply->lparam = msg->lparam;
1004 reply->x = msg->x;
1005 reply->y = msg->y;
1006 reply->time = msg->time;
1008 if (flags & PM_REMOVE)
1010 if (msg->data)
1012 set_reply_data_ptr( msg->data, msg->data_size );
1013 msg->data = NULL;
1014 msg->data_size = 0;
1016 remove_queue_message( queue, msg, POST_MESSAGE );
1018 else if (msg->data) set_reply_data( msg->data, msg->data_size );
1020 return 1;
1023 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
1024 struct get_message_reply *reply )
1026 if (queue->quit_message)
1028 reply->total = 0;
1029 reply->type = MSG_POSTED;
1030 reply->win = 0;
1031 reply->msg = WM_QUIT;
1032 reply->wparam = queue->exit_code;
1033 reply->lparam = 0;
1035 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
1037 if (flags & PM_REMOVE)
1039 queue->quit_message = 0;
1040 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
1041 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1043 return 1;
1045 else
1046 return 0;
1049 /* empty a message list and free all the messages */
1050 static void empty_msg_list( struct list *list )
1052 struct list *ptr;
1054 while ((ptr = list_head( list )) != NULL)
1056 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1057 list_remove( &msg->entry );
1058 free_message( msg );
1062 /* cleanup all pending results when deleting a queue */
1063 static void cleanup_results( struct msg_queue *queue )
1065 struct list *entry;
1067 while ((entry = list_head( &queue->send_result )) != NULL)
1069 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
1072 while ((entry = list_head( &queue->callback_result )) != NULL)
1074 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
1077 while (queue->recv_result)
1078 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
1081 /* check if the thread owning the queue is hung (not checking for messages) */
1082 static int is_queue_hung( struct msg_queue *queue )
1084 struct wait_queue_entry *entry;
1086 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
1087 return 0; /* less than 5 seconds since last get message -> not hung */
1089 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
1091 if (get_wait_queue_thread(entry)->queue == queue)
1092 return 0; /* thread is waiting on queue -> not hung */
1094 return 1;
1097 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
1099 struct msg_queue *queue = (struct msg_queue *)obj;
1100 struct process *process = get_wait_queue_thread(entry)->process;
1102 /* a thread can only wait on its own queue */
1103 if (get_wait_queue_thread(entry)->queue != queue)
1105 set_error( STATUS_ACCESS_DENIED );
1106 return 0;
1108 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
1110 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
1111 set_fd_events( queue->fd, POLLIN );
1112 add_queue( obj, entry );
1113 return 1;
1116 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
1118 struct msg_queue *queue = (struct msg_queue *)obj;
1120 remove_queue( obj, entry );
1121 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
1122 set_fd_events( queue->fd, 0 );
1125 static void msg_queue_dump( struct object *obj, int verbose )
1127 struct msg_queue *queue = (struct msg_queue *)obj;
1128 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
1129 queue->wake_bits, queue->wake_mask );
1132 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
1134 struct msg_queue *queue = (struct msg_queue *)obj;
1135 int ret = 0;
1137 if (queue->fd)
1139 if ((ret = check_fd_events( queue->fd, POLLIN )))
1140 /* stop waiting on select() if we are signaled */
1141 set_fd_events( queue->fd, 0 );
1142 else if (!list_empty( &obj->wait_queue ))
1143 /* restart waiting on poll() if we are no longer signaled */
1144 set_fd_events( queue->fd, POLLIN );
1146 return ret || is_signaled( queue );
1149 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
1151 struct msg_queue *queue = (struct msg_queue *)obj;
1152 queue->wake_mask = 0;
1153 queue->changed_mask = 0;
1156 static void msg_queue_destroy( struct object *obj )
1158 struct msg_queue *queue = (struct msg_queue *)obj;
1159 struct list *ptr;
1160 struct hotkey *hotkey, *hotkey2;
1161 int i;
1163 cleanup_results( queue );
1164 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
1166 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
1168 if (hotkey->queue == queue)
1170 list_remove( &hotkey->entry );
1171 free( hotkey );
1175 while ((ptr = list_head( &queue->pending_timers )))
1177 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1178 list_remove( &timer->entry );
1179 free( timer );
1181 while ((ptr = list_head( &queue->expired_timers )))
1183 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1184 list_remove( &timer->entry );
1185 free( timer );
1187 if (queue->timeout) remove_timeout_user( queue->timeout );
1188 queue->input->cursor_count -= queue->cursor_count;
1189 if (queue->keystate_lock) unlock_input_keystate( queue->input );
1190 release_object( queue->input );
1191 if (queue->hooks) release_object( queue->hooks );
1192 if (queue->fd) release_object( queue->fd );
1195 static void msg_queue_poll_event( struct fd *fd, int event )
1197 struct msg_queue *queue = get_fd_user( fd );
1198 assert( queue->obj.ops == &msg_queue_ops );
1200 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1201 else set_fd_events( queue->fd, 0 );
1202 wake_up( &queue->obj, 0 );
1205 static void thread_input_dump( struct object *obj, int verbose )
1207 struct thread_input *input = (struct thread_input *)obj;
1208 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
1209 input->focus, input->capture, input->active );
1212 static void thread_input_destroy( struct object *obj )
1214 struct thread_input *input = (struct thread_input *)obj;
1215 struct desktop *desktop;
1217 empty_msg_list( &input->msg_list );
1218 if ((desktop = input->desktop))
1220 if (desktop->foreground_input == input) desktop->foreground_input = NULL;
1221 release_object( desktop );
1225 /* fix the thread input data when a window is destroyed */
1226 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1228 struct thread_input *input = queue->input;
1230 if (window == input->focus) input->focus = 0;
1231 if (window == input->capture) input->capture = 0;
1232 if (window == input->active) input->active = 0;
1233 if (window == input->menu_owner) input->menu_owner = 0;
1234 if (window == input->move_size) input->move_size = 0;
1235 if (window == input->caret) set_caret_window( input, 0 );
1238 /* check if the specified window can be set in the input data of a given queue */
1239 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1241 struct thread *thread;
1242 int ret = 0;
1244 if (!window) return 1; /* we can always clear the data */
1246 if ((thread = get_window_thread( window )))
1248 ret = (queue->input == thread->queue->input);
1249 if (!ret) set_error( STATUS_ACCESS_DENIED );
1250 release_object( thread );
1252 else set_error( STATUS_INVALID_HANDLE );
1254 return ret;
1257 /* make sure the specified thread has a queue */
1258 int init_thread_queue( struct thread *thread )
1260 if (thread->queue) return 1;
1261 return (create_msg_queue( thread, NULL ) != NULL);
1264 /* attach two thread input data structures */
1265 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1267 struct desktop *desktop;
1268 struct thread_input *input;
1269 int ret;
1271 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1272 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1273 input = (struct thread_input *)grab_object( thread_to->queue->input );
1274 if (input->desktop != desktop)
1276 set_error( STATUS_ACCESS_DENIED );
1277 release_object( input );
1278 release_object( desktop );
1279 return 0;
1281 release_object( desktop );
1283 if (thread_from->queue)
1285 if (!input->focus) input->focus = thread_from->queue->input->focus;
1286 if (!input->active) input->active = thread_from->queue->input->active;
1289 ret = assign_thread_input( thread_from, input );
1290 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1291 release_object( input );
1292 return ret;
1295 /* detach two thread input data structures */
1296 void detach_thread_input( struct thread *thread_from )
1298 struct thread *thread;
1299 struct thread_input *input, *old_input = thread_from->queue->input;
1301 if ((input = create_thread_input( thread_from )))
1303 if (old_input->focus && (thread = get_window_thread( old_input->focus )))
1305 if (thread == thread_from)
1307 input->focus = old_input->focus;
1308 old_input->focus = 0;
1310 release_object( thread );
1312 if (old_input->active && (thread = get_window_thread( old_input->active )))
1314 if (thread == thread_from)
1316 input->active = old_input->active;
1317 old_input->active = 0;
1319 release_object( thread );
1321 assign_thread_input( thread_from, input );
1322 release_object( input );
1327 /* set the next timer to expire */
1328 static void set_next_timer( struct msg_queue *queue )
1330 struct list *ptr;
1332 if (queue->timeout)
1334 remove_timeout_user( queue->timeout );
1335 queue->timeout = NULL;
1337 if ((ptr = list_head( &queue->pending_timers )))
1339 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1340 queue->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, queue );
1342 /* set/clear QS_TIMER bit */
1343 if (list_empty( &queue->expired_timers ))
1344 clear_queue_bits( queue, QS_TIMER );
1345 else
1346 set_queue_bits( queue, QS_TIMER );
1349 /* find a timer from its window and id */
1350 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1351 unsigned int msg, lparam_t id )
1353 struct list *ptr;
1355 /* we need to search both lists */
1357 LIST_FOR_EACH( ptr, &queue->pending_timers )
1359 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1360 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1362 LIST_FOR_EACH( ptr, &queue->expired_timers )
1364 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1365 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1367 return NULL;
1370 /* callback for the next timer expiration */
1371 static void timer_callback( void *private )
1373 struct msg_queue *queue = private;
1374 struct list *ptr;
1376 queue->timeout = NULL;
1377 /* move on to the next timer */
1378 ptr = list_head( &queue->pending_timers );
1379 list_remove( ptr );
1380 list_add_tail( &queue->expired_timers, ptr );
1381 set_next_timer( queue );
1384 /* link a timer at its rightful place in the queue list */
1385 static void link_timer( struct msg_queue *queue, struct timer *timer )
1387 struct list *ptr;
1389 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1391 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1392 if (t->when <= timer->when) break;
1394 list_add_before( ptr, &timer->entry );
1397 /* remove a timer from the queue timer list and free it */
1398 static void free_timer( struct msg_queue *queue, struct timer *timer )
1400 list_remove( &timer->entry );
1401 free( timer );
1402 set_next_timer( queue );
1405 /* restart an expired timer */
1406 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1408 list_remove( &timer->entry );
1409 while (-timer->when <= monotonic_time) timer->when -= (timeout_t)timer->rate * 10000;
1410 link_timer( queue, timer );
1411 set_next_timer( queue );
1414 /* find an expired timer matching the filtering parameters */
1415 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1416 unsigned int get_first, unsigned int get_last,
1417 int remove )
1419 struct list *ptr;
1421 LIST_FOR_EACH( ptr, &queue->expired_timers )
1423 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1424 if (win && timer->win != win) continue;
1425 if (check_msg_filter( timer->msg, get_first, get_last ))
1427 if (remove) restart_timer( queue, timer );
1428 return timer;
1431 return NULL;
1434 /* add a timer */
1435 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1437 struct timer *timer = mem_alloc( sizeof(*timer) );
1438 if (timer)
1440 timer->rate = max( rate, 1 );
1441 timer->when = -monotonic_time - (timeout_t)timer->rate * 10000;
1442 link_timer( queue, timer );
1443 /* check if we replaced the next timer */
1444 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1446 return timer;
1449 /* change the input key state for a given key */
1450 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1452 if (down)
1454 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1455 keystate[key] |= down;
1457 else keystate[key] &= ~0x80;
1460 /* update the input key state for a keyboard message */
1461 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1462 unsigned int msg, lparam_t wparam )
1464 unsigned char key;
1465 int down = 0;
1467 switch (msg)
1469 case WM_LBUTTONDOWN:
1470 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1471 /* fall through */
1472 case WM_LBUTTONUP:
1473 set_input_key_state( keystate, VK_LBUTTON, down );
1474 break;
1475 case WM_MBUTTONDOWN:
1476 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1477 /* fall through */
1478 case WM_MBUTTONUP:
1479 set_input_key_state( keystate, VK_MBUTTON, down );
1480 break;
1481 case WM_RBUTTONDOWN:
1482 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1483 /* fall through */
1484 case WM_RBUTTONUP:
1485 set_input_key_state( keystate, VK_RBUTTON, down );
1486 break;
1487 case WM_XBUTTONDOWN:
1488 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1489 /* fall through */
1490 case WM_XBUTTONUP:
1491 if (wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1492 else if (wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1493 break;
1494 case WM_KEYDOWN:
1495 case WM_SYSKEYDOWN:
1496 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1497 /* fall through */
1498 case WM_KEYUP:
1499 case WM_SYSKEYUP:
1500 key = (unsigned char)wparam;
1501 set_input_key_state( keystate, key, down );
1502 switch(key)
1504 case VK_LCONTROL:
1505 case VK_RCONTROL:
1506 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1507 set_input_key_state( keystate, VK_CONTROL, down );
1508 break;
1509 case VK_LMENU:
1510 case VK_RMENU:
1511 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1512 set_input_key_state( keystate, VK_MENU, down );
1513 break;
1514 case VK_LSHIFT:
1515 case VK_RSHIFT:
1516 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1517 set_input_key_state( keystate, VK_SHIFT, down );
1518 break;
1520 break;
1524 /* update the desktop key state according to a mouse message flags */
1525 static void update_desktop_mouse_state( struct desktop *desktop, unsigned int flags, lparam_t wparam )
1527 if (flags & MOUSEEVENTF_LEFTDOWN)
1528 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONDOWN, wparam );
1529 if (flags & MOUSEEVENTF_LEFTUP)
1530 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONUP, wparam );
1531 if (flags & MOUSEEVENTF_RIGHTDOWN)
1532 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONDOWN, wparam );
1533 if (flags & MOUSEEVENTF_RIGHTUP)
1534 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONUP, wparam );
1535 if (flags & MOUSEEVENTF_MIDDLEDOWN)
1536 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONDOWN, wparam );
1537 if (flags & MOUSEEVENTF_MIDDLEUP)
1538 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONUP, wparam );
1539 if (flags & MOUSEEVENTF_XDOWN)
1540 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONDOWN, wparam );
1541 if (flags & MOUSEEVENTF_XUP)
1542 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONUP, wparam );
1545 /* release the hardware message currently being processed by the given thread */
1546 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id )
1548 struct thread_input *input = queue->input;
1549 struct message *msg, *other;
1550 int clr_bit;
1552 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1554 if (msg->unique_id == hw_id) break;
1556 if (&msg->entry == &input->msg_list) return; /* not found */
1558 /* clear the queue bit for that message */
1559 clr_bit = get_hardware_msg_bit( msg->msg );
1560 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1562 if (other != msg && get_hardware_msg_bit( other->msg ) == clr_bit)
1564 clr_bit = 0;
1565 break;
1568 if (clr_bit) clear_queue_bits( queue, clr_bit );
1570 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1571 list_remove( &msg->entry );
1572 free_message( msg );
1575 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1577 struct hotkey *hotkey;
1578 unsigned int modifiers = 0;
1580 if (msg->msg != WM_KEYDOWN && msg->msg != WM_SYSKEYDOWN) return 0;
1582 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1583 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1584 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1585 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1587 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1589 if (hotkey->vkey != msg->wparam) continue;
1590 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1593 return 0;
1595 found:
1596 msg->type = MSG_POSTED;
1597 msg->win = hotkey->win;
1598 msg->msg = WM_HOTKEY;
1599 msg->wparam = hotkey->id;
1600 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1602 free( msg->data );
1603 msg->data = NULL;
1604 msg->data_size = 0;
1606 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1607 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1608 hotkey->queue->hotkey_count++;
1609 return 1;
1612 /* find the window that should receive a given hardware message */
1613 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1614 struct message *msg, unsigned int *msg_code,
1615 struct thread **thread )
1617 user_handle_t win = 0;
1619 *thread = NULL;
1620 *msg_code = msg->msg;
1621 switch (get_hardware_msg_bit( msg->msg ))
1623 case QS_POINTER:
1624 case QS_RAWINPUT:
1625 if (!(win = msg->win) && input) win = input->focus;
1626 break;
1627 case QS_KEY:
1628 if (input && !(win = input->focus))
1630 win = input->active;
1631 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1633 break;
1634 case QS_MOUSEMOVE:
1635 case QS_MOUSEBUTTON:
1636 if (!input || !(win = input->capture))
1638 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1639 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1640 *thread = window_thread_from_point( win, msg->x, msg->y );
1642 break;
1645 if (!*thread)
1646 *thread = get_window_thread( win );
1647 return win;
1650 static struct rawinput_device *find_rawinput_device( struct process *process, unsigned int usage )
1652 struct rawinput_device *device, *end;
1654 for (device = process->rawinput_devices, end = device + process->rawinput_device_count; device != end; device++)
1656 if (device->usage != usage) continue;
1657 return device;
1660 return NULL;
1663 static void prepend_cursor_history( int x, int y, unsigned int time, lparam_t info )
1665 cursor_pos_t *pos = &cursor_history[--cursor_history_latest % ARRAY_SIZE(cursor_history)];
1667 pos->x = x;
1668 pos->y = y;
1669 pos->time = time;
1670 pos->info = info;
1673 /* queue a hardware message into a given thread input */
1674 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1676 user_handle_t win;
1677 struct thread *thread;
1678 struct thread_input *input;
1679 struct hardware_msg_data *msg_data = msg->data;
1680 unsigned int msg_code;
1682 update_input_key_state( desktop, desktop->keystate, msg->msg, msg->wparam );
1683 last_input_time = get_tick_count();
1684 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1686 switch (get_hardware_msg_bit( msg->msg ))
1688 case QS_KEY:
1689 if (queue_hotkey_message( desktop, msg )) return;
1690 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1691 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1692 msg->lparam &= ~(KF_EXTENDED << 16);
1693 break;
1694 case QS_MOUSEMOVE:
1695 prepend_cursor_history( msg->x, msg->y, msg->time, msg_data->info );
1696 /* fallthrough */
1697 case QS_MOUSEBUTTON:
1698 if (update_desktop_cursor_pos( desktop, msg->win, msg->x, msg->y )) always_queue = 1;
1699 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1700 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1701 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1702 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1703 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1704 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1705 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1706 break;
1708 msg->x = desktop->cursor.x;
1709 msg->y = desktop->cursor.y;
1711 if (msg->win && (thread = get_window_thread( msg->win )))
1713 input = thread->queue->input;
1714 release_object( thread );
1716 else input = desktop->foreground_input;
1718 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1719 if (!win || !thread)
1721 if (input) update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1722 free_message( msg );
1723 return;
1725 input = thread->queue->input;
1727 if (win != msg->win) always_queue = 1;
1728 if (!always_queue || merge_message( input, msg )) free_message( msg );
1729 else
1731 msg->unique_id = 0; /* will be set once we return it to the app */
1732 list_add_tail( &input->msg_list, &msg->entry );
1733 set_queue_bits( thread->queue, get_hardware_msg_bit( msg->msg ) );
1735 release_object( thread );
1738 /* send the low-level hook message for a given hardware message */
1739 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1740 const hw_input_t *input, struct msg_queue *sender )
1742 struct thread *hook_thread;
1743 struct msg_queue *queue;
1744 struct message *msg;
1745 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1746 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1748 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1749 if (!(queue = hook_thread->queue)) return 0;
1750 if (is_queue_hung( queue )) return 0;
1752 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1754 msg->type = MSG_HOOK_LL;
1755 msg->win = 0;
1756 msg->msg = id;
1757 msg->wparam = hardware_msg->msg;
1758 msg->x = hardware_msg->x;
1759 msg->y = hardware_msg->y;
1760 msg->time = hardware_msg->time;
1761 msg->data_size = hardware_msg->data_size;
1762 msg->result = NULL;
1764 if (input->type == INPUT_KEYBOARD)
1766 unsigned short vkey = input->kbd.vkey;
1767 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1768 msg->lparam = (input->kbd.scan << 16) | vkey;
1770 else msg->lparam = input->mouse.data << 16;
1772 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1773 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1775 free_message( msg );
1776 return 0;
1778 msg->result->hardware_msg = hardware_msg;
1779 msg->result->desktop = (struct desktop *)grab_object( desktop );
1780 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1781 set_queue_bits( queue, QS_SENDMESSAGE );
1782 return 1;
1785 /* get the foreground thread for a desktop and a window receiving input */
1786 static struct thread *get_foreground_thread( struct desktop *desktop, user_handle_t window )
1788 /* if desktop has no foreground process, assume the receiving window is */
1789 if (desktop->foreground_input) return get_window_thread( desktop->foreground_input->focus );
1790 if (window) return get_window_thread( window );
1791 return NULL;
1794 /* user32 reserves 1 & 2 for winemouse and winekeyboard,
1795 * keep this in sync with user_private.h */
1796 #define WINE_MOUSE_HANDLE 1
1797 #define WINE_KEYBOARD_HANDLE 2
1799 static void rawmouse_init( struct rawinput *header, RAWMOUSE *rawmouse, int x, int y, unsigned int flags,
1800 unsigned int buttons, lparam_t info )
1802 static const unsigned int button_flags[] =
1804 0, /* MOUSEEVENTF_MOVE */
1805 RI_MOUSE_LEFT_BUTTON_DOWN, /* MOUSEEVENTF_LEFTDOWN */
1806 RI_MOUSE_LEFT_BUTTON_UP, /* MOUSEEVENTF_LEFTUP */
1807 RI_MOUSE_RIGHT_BUTTON_DOWN, /* MOUSEEVENTF_RIGHTDOWN */
1808 RI_MOUSE_RIGHT_BUTTON_UP, /* MOUSEEVENTF_RIGHTUP */
1809 RI_MOUSE_MIDDLE_BUTTON_DOWN, /* MOUSEEVENTF_MIDDLEDOWN */
1810 RI_MOUSE_MIDDLE_BUTTON_UP, /* MOUSEEVENTF_MIDDLEUP */
1812 unsigned int i;
1814 header->type = RIM_TYPEMOUSE;
1815 header->device = WINE_MOUSE_HANDLE;
1816 header->wparam = 0;
1817 header->usage = MAKELONG(HID_USAGE_GENERIC_MOUSE, HID_USAGE_PAGE_GENERIC);
1819 rawmouse->usFlags = MOUSE_MOVE_RELATIVE;
1820 rawmouse->usButtonFlags = 0;
1821 rawmouse->usButtonData = 0;
1822 for (i = 1; i < ARRAY_SIZE(button_flags); ++i)
1824 if (flags & (1 << i)) rawmouse->usButtonFlags |= button_flags[i];
1826 if (flags & MOUSEEVENTF_WHEEL)
1828 rawmouse->usButtonFlags |= RI_MOUSE_WHEEL;
1829 rawmouse->usButtonData = buttons;
1831 if (flags & MOUSEEVENTF_HWHEEL)
1833 rawmouse->usButtonFlags |= RI_MOUSE_HORIZONTAL_WHEEL;
1834 rawmouse->usButtonData = buttons;
1836 if (flags & MOUSEEVENTF_XDOWN)
1838 if (buttons == XBUTTON1) rawmouse->usButtonFlags |= RI_MOUSE_BUTTON_4_DOWN;
1839 if (buttons == XBUTTON2) rawmouse->usButtonFlags |= RI_MOUSE_BUTTON_5_DOWN;
1841 if (flags & MOUSEEVENTF_XUP)
1843 if (buttons == XBUTTON1) rawmouse->usButtonFlags |= RI_MOUSE_BUTTON_4_UP;
1844 if (buttons == XBUTTON2) rawmouse->usButtonFlags |= RI_MOUSE_BUTTON_5_UP;
1847 rawmouse->ulRawButtons = 0;
1848 rawmouse->lLastX = x;
1849 rawmouse->lLastY = y;
1850 rawmouse->ulExtraInformation = info;
1853 static void rawkeyboard_init( struct rawinput *rawinput, RAWKEYBOARD *keyboard, unsigned short scan, unsigned short vkey,
1854 unsigned int flags, unsigned int message, lparam_t info )
1856 rawinput->type = RIM_TYPEKEYBOARD;
1857 rawinput->device = WINE_KEYBOARD_HANDLE;
1858 rawinput->wparam = 0;
1859 rawinput->usage = MAKELONG(HID_USAGE_GENERIC_KEYBOARD, HID_USAGE_PAGE_GENERIC);
1861 keyboard->MakeCode = scan;
1862 keyboard->Flags = (flags & KEYEVENTF_KEYUP) ? RI_KEY_BREAK : RI_KEY_MAKE;
1863 if (flags & KEYEVENTF_EXTENDEDKEY) keyboard->Flags |= RI_KEY_E0;
1864 keyboard->Reserved = 0;
1866 switch (vkey)
1868 case VK_LSHIFT:
1869 case VK_RSHIFT:
1870 keyboard->VKey = VK_SHIFT;
1871 keyboard->Flags &= ~RI_KEY_E0;
1872 break;
1874 case VK_LCONTROL:
1875 case VK_RCONTROL:
1876 keyboard->VKey = VK_CONTROL;
1877 break;
1878 case VK_LMENU:
1879 case VK_RMENU:
1880 keyboard->VKey = VK_MENU;
1881 break;
1883 default:
1884 keyboard->VKey = vkey;
1885 break;
1888 keyboard->Message = message;
1889 keyboard->ExtraInformation = info;
1892 static void rawhid_init( struct rawinput *rawinput, RAWHID *hid, const hw_input_t *input )
1894 rawinput->type = RIM_TYPEHID;
1895 rawinput->device = input->hw.hid.device;
1896 rawinput->wparam = input->hw.wparam;
1897 rawinput->usage = input->hw.hid.usage;
1899 hid->dwCount = input->hw.hid.count;
1900 hid->dwSizeHid = input->hw.hid.length;
1903 struct rawinput_message
1905 struct thread *foreground;
1906 struct hw_msg_source source;
1907 unsigned int time;
1908 unsigned int message;
1909 unsigned int flags;
1910 struct rawinput rawinput;
1911 union
1913 RAWKEYBOARD keyboard;
1914 RAWMOUSE mouse;
1915 RAWHID hid;
1916 } data;
1917 const void *hid_report;
1920 /* check if process is supposed to receive a WM_INPUT message and eventually queue it */
1921 static void queue_rawinput_message( struct desktop *desktop, struct process *process, void *args )
1923 const struct rawinput_message *raw_msg = args;
1924 const struct rawinput_device *device;
1925 struct hardware_msg_data *msg_data;
1926 struct message *msg;
1927 data_size_t report_size = 0, data_size = 0;
1928 int wparam = RIM_INPUT;
1929 lparam_t info = 0;
1931 if (raw_msg->rawinput.type == RIM_TYPEMOUSE)
1933 device = process->rawinput_mouse;
1934 data_size = sizeof(raw_msg->data.mouse);
1935 info = raw_msg->data.mouse.ulExtraInformation;
1937 else if (raw_msg->rawinput.type == RIM_TYPEKEYBOARD)
1939 device = process->rawinput_kbd;
1940 data_size = sizeof(raw_msg->data.keyboard);
1941 info = raw_msg->data.keyboard.ExtraInformation;
1943 else
1945 device = find_rawinput_device( process, raw_msg->rawinput.usage );
1946 data_size = offsetof(RAWHID, bRawData[0]);
1947 report_size = raw_msg->data.hid.dwCount * raw_msg->data.hid.dwSizeHid;
1949 if (!device) return;
1951 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && !(device->flags & RIDEV_DEVNOTIFY)) return;
1952 if (process != raw_msg->foreground->process)
1954 if (raw_msg->message == WM_INPUT && !(device->flags & RIDEV_INPUTSINK)) return;
1955 wparam = RIM_INPUTSINK;
1958 if (!(msg = alloc_hardware_message( info, raw_msg->source, raw_msg->time, data_size + report_size ))) return;
1959 msg->win = device->target;
1960 msg->msg = raw_msg->message;
1961 msg->wparam = wparam;
1962 msg->lparam = 0;
1964 msg_data = msg->data;
1965 msg_data->flags = raw_msg->flags;
1966 msg_data->rawinput = raw_msg->rawinput;
1967 memcpy( msg_data + 1, &raw_msg->data, data_size );
1968 if (report_size) memcpy( (char *)(msg_data + 1) + data_size, raw_msg->hid_report, report_size );
1970 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && raw_msg->rawinput.type == RIM_TYPEHID)
1972 msg->wparam = raw_msg->rawinput.wparam;
1973 msg->lparam = raw_msg->rawinput.device;
1976 queue_hardware_message( desktop, msg, 1 );
1979 static void dispatch_rawinput_message( struct desktop *desktop, struct rawinput_message *raw_msg )
1981 struct process *process;
1983 LIST_FOR_EACH_ENTRY( process, &rawinput_processes, struct process, rawinput_entry )
1984 queue_rawinput_message( desktop, process, raw_msg );
1987 /* queue a hardware message for a mouse event */
1988 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1989 unsigned int origin, struct msg_queue *sender )
1991 const struct rawinput_device *device;
1992 struct hardware_msg_data *msg_data;
1993 struct rawinput_message raw_msg;
1994 struct message *msg;
1995 struct thread *foreground;
1996 unsigned int i, time, flags;
1997 struct hw_msg_source source = { IMDT_MOUSE, origin };
1998 int wait = 0, x, y;
2000 static const unsigned int messages[] =
2002 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
2003 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
2004 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
2005 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
2006 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
2007 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
2008 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
2009 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
2010 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
2011 0, /* 0x0200 = unused */
2012 0, /* 0x0400 = unused */
2013 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
2014 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
2017 desktop->cursor.last_change = get_tick_count();
2018 flags = input->mouse.flags;
2019 time = input->mouse.time;
2020 if (!time) time = desktop->cursor.last_change;
2022 if (flags & MOUSEEVENTF_MOVE)
2024 if (flags & MOUSEEVENTF_ABSOLUTE)
2026 x = input->mouse.x;
2027 y = input->mouse.y;
2028 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
2029 x == desktop->cursor.x && y == desktop->cursor.y)
2030 flags &= ~MOUSEEVENTF_MOVE;
2032 else
2034 x = desktop->cursor.x + input->mouse.x;
2035 y = desktop->cursor.y + input->mouse.y;
2038 else
2040 x = desktop->cursor.x;
2041 y = desktop->cursor.y;
2044 if ((foreground = get_foreground_thread( desktop, win )))
2046 memset( &raw_msg, 0, sizeof(raw_msg) );
2047 raw_msg.foreground = foreground;
2048 raw_msg.source = source;
2049 raw_msg.time = time;
2050 raw_msg.message = WM_INPUT;
2051 raw_msg.flags = flags;
2052 rawmouse_init( &raw_msg.rawinput, &raw_msg.data.mouse, x - desktop->cursor.x, y - desktop->cursor.y,
2053 raw_msg.flags, input->mouse.data, input->mouse.info );
2055 dispatch_rawinput_message( desktop, &raw_msg );
2056 release_object( foreground );
2059 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
2061 if (flags & MOUSEEVENTF_MOVE) update_desktop_cursor_pos( desktop, win, x, y );
2062 update_desktop_mouse_state( desktop, flags, input->mouse.data << 16 );
2063 return 0;
2066 for (i = 0; i < ARRAY_SIZE( messages ); i++)
2068 if (!messages[i]) continue;
2069 if (!(flags & (1 << i))) continue;
2070 flags &= ~(1 << i);
2072 if (!(msg = alloc_hardware_message( input->mouse.info, source, time, 0 ))) return 0;
2073 msg_data = msg->data;
2075 msg->win = get_user_full_handle( win );
2076 msg->msg = messages[i];
2077 msg->wparam = input->mouse.data << 16;
2078 msg->lparam = 0;
2079 msg->x = x;
2080 msg->y = y;
2081 if (origin == IMO_INJECTED) msg_data->flags = LLMHF_INJECTED;
2083 /* specify a sender only when sending the last message */
2084 if (!(flags & ((1 << ARRAY_SIZE( messages )) - 1)))
2086 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
2087 queue_hardware_message( desktop, msg, 0 );
2089 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
2090 queue_hardware_message( desktop, msg, 0 );
2092 return wait;
2095 /* queue a hardware message for a keyboard event */
2096 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
2097 unsigned int origin, struct msg_queue *sender )
2099 struct hw_msg_source source = { IMDT_KEYBOARD, origin };
2100 const struct rawinput_device *device;
2101 struct hardware_msg_data *msg_data;
2102 struct message *msg;
2103 struct thread *foreground;
2104 unsigned char vkey = input->kbd.vkey;
2105 unsigned int message_code, time;
2106 int wait;
2108 if (!(time = input->kbd.time)) time = get_tick_count();
2110 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
2112 switch (vkey)
2114 case VK_MENU:
2115 case VK_LMENU:
2116 case VK_RMENU:
2117 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
2118 break;
2119 case VK_CONTROL:
2120 case VK_LCONTROL:
2121 case VK_RCONTROL:
2122 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
2123 break;
2124 case VK_SHIFT:
2125 case VK_LSHIFT:
2126 case VK_RSHIFT:
2127 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
2128 break;
2132 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
2133 switch (vkey)
2135 case VK_LMENU:
2136 case VK_RMENU:
2137 if (input->kbd.flags & KEYEVENTF_KEYUP)
2139 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
2140 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
2141 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
2142 message_code = WM_SYSKEYUP;
2143 desktop->keystate[VK_MENU] &= ~0x02;
2145 else
2147 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
2148 if (desktop->keystate[VK_CONTROL] & 0x80) break;
2149 message_code = WM_SYSKEYDOWN;
2150 desktop->keystate[VK_MENU] |= 0x02;
2152 break;
2154 case VK_LCONTROL:
2155 case VK_RCONTROL:
2156 /* send WM_SYSKEYUP on release if Alt still pressed */
2157 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
2158 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
2159 message_code = WM_SYSKEYUP;
2160 desktop->keystate[VK_MENU] &= ~0x02;
2161 break;
2163 default:
2164 /* send WM_SYSKEY for Alt-anykey and for F10 */
2165 if (desktop->keystate[VK_CONTROL] & 0x80) break;
2166 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
2167 /* fall through */
2168 case VK_F10:
2169 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
2170 desktop->keystate[VK_MENU] &= ~0x02;
2171 break;
2174 if ((foreground = get_foreground_thread( desktop, win )))
2176 struct rawinput_message raw_msg = {0};
2177 raw_msg.foreground = foreground;
2178 raw_msg.source = source;
2179 raw_msg.time = time;
2180 raw_msg.message = WM_INPUT;
2181 raw_msg.flags = input->kbd.flags;
2182 rawkeyboard_init( &raw_msg.rawinput, &raw_msg.data.keyboard, input->kbd.scan, vkey,
2183 raw_msg.flags, message_code, input->kbd.info );
2185 dispatch_rawinput_message( desktop, &raw_msg );
2186 release_object( foreground );
2189 if ((device = current->process->rawinput_kbd) && (device->flags & RIDEV_NOLEGACY))
2191 update_input_key_state( desktop, desktop->keystate, message_code, vkey );
2192 return 0;
2195 if (!(msg = alloc_hardware_message( input->kbd.info, source, time, 0 ))) return 0;
2196 msg_data = msg->data;
2198 msg->win = get_user_full_handle( win );
2199 msg->msg = message_code;
2200 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
2201 if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
2203 if (input->kbd.flags & KEYEVENTF_UNICODE && !vkey)
2205 msg->wparam = VK_PACKET;
2207 else
2209 unsigned int flags = 0;
2210 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
2211 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
2212 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
2213 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
2215 msg->wparam = vkey;
2216 msg->lparam |= flags << 16;
2217 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
2220 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
2221 queue_hardware_message( desktop, msg, 1 );
2223 return wait;
2226 struct pointer
2228 struct list entry;
2229 struct timeout_user *timeout;
2230 struct desktop *desktop;
2231 user_handle_t win;
2232 int primary;
2233 hw_input_t input;
2236 static void queue_pointer_message( struct pointer *pointer, int repeated );
2238 static void pointer_message_timeout( void *private )
2240 struct pointer *pointer = private;
2241 queue_pointer_message( pointer, 1 );
2244 static void queue_pointer_message( struct pointer *pointer, int repeated )
2246 static const unsigned int messages[][2] =
2248 {WM_POINTERUPDATE, 0},
2249 {WM_POINTERENTER, WM_POINTERDOWN},
2250 {WM_POINTERUP, WM_POINTERLEAVE},
2252 struct hw_msg_source source = { IMDT_UNAVAILABLE, IMDT_TOUCH };
2253 struct desktop *desktop = pointer->desktop;
2254 const hw_input_t *input = &pointer->input;
2255 unsigned int i, wparam = input->hw.wparam;
2256 timeout_t time = get_tick_count();
2257 user_handle_t win = pointer->win;
2258 rectangle_t top_rect;
2259 struct message *msg;
2260 int x, y;
2262 get_top_window_rectangle( desktop, &top_rect );
2263 x = LOWORD(input->hw.lparam) * (top_rect.right - top_rect.left) / 65535;
2264 y = HIWORD(input->hw.lparam) * (top_rect.bottom - top_rect.top) / 65535;
2266 if (pointer->primary) wparam |= POINTER_MESSAGE_FLAG_PRIMARY << 16;
2268 for (i = 0; i < 2 && messages[input->hw.msg - WM_POINTERUPDATE][i]; i++)
2270 if (!(msg = alloc_hardware_message( 0, source, time, 0 ))) return;
2272 msg->win = get_user_full_handle( win );
2273 msg->msg = messages[input->hw.msg - WM_POINTERUPDATE][i];
2274 msg->wparam = wparam;
2275 msg->lparam = MAKELONG(x, y);
2276 msg->x = desktop->cursor.x;
2277 msg->y = desktop->cursor.y;
2279 queue_hardware_message( desktop, msg, 1 );
2282 if (!repeated && pointer->primary && (msg = alloc_hardware_message( 0xff515700, source, time, 0 )))
2284 unsigned int message = WM_MOUSEMOVE;
2285 if (input->hw.msg == WM_POINTERDOWN) message = WM_LBUTTONDOWN;
2286 else if (input->hw.msg == WM_POINTERUP) message = WM_LBUTTONUP;
2288 msg->win = get_user_full_handle( win );
2289 msg->msg = message;
2290 msg->wparam = 0;
2291 msg->lparam = 0;
2292 msg->x = x;
2293 msg->y = y;
2295 if (!send_hook_ll_message( desktop, msg, input, NULL ))
2296 queue_hardware_message( desktop, msg, 0 );
2299 if (input->hw.msg != WM_POINTERUP)
2301 pointer->input.hw.msg = WM_POINTERUPDATE;
2302 pointer->input.hw.wparam &= ~(POINTER_MESSAGE_FLAG_NEW << 16);
2303 pointer->timeout = add_timeout_user( -160000, pointer_message_timeout, pointer );
2305 else
2307 list_remove( &pointer->entry );
2308 free( pointer );
2312 static struct pointer *find_pointer_from_id( struct desktop *desktop, unsigned int id )
2314 struct pointer *pointer;
2316 LIST_FOR_EACH_ENTRY( pointer, &desktop->pointers, struct pointer, entry )
2317 if (LOWORD(pointer->input.hw.wparam) == id) return pointer;
2319 pointer = mem_alloc( sizeof(struct pointer) );
2320 pointer->timeout = NULL;
2321 pointer->desktop = desktop;
2322 pointer->primary = list_empty( &desktop->pointers );
2323 list_add_tail( &desktop->pointers, &pointer->entry );
2325 return pointer;
2328 /* queue a hardware message for a custom type of event */
2329 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
2330 unsigned int origin, const hw_input_t *input )
2332 struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
2333 struct thread *foreground;
2334 struct pointer *pointer;
2335 struct message *msg;
2337 switch (input->hw.msg)
2339 case WM_INPUT:
2340 case WM_INPUT_DEVICE_CHANGE:
2341 if (input->hw.hid.length * input->hw.hid.count != get_req_data_size())
2342 set_error( STATUS_INVALID_PARAMETER );
2343 else if ((foreground = get_foreground_thread( desktop, win )))
2345 struct rawinput_message raw_msg = {0};
2346 raw_msg.foreground = foreground;
2347 raw_msg.source = source;
2348 raw_msg.time = get_tick_count();
2349 raw_msg.message = input->hw.msg;
2350 raw_msg.hid_report = get_req_data();
2351 rawhid_init( &raw_msg.rawinput, &raw_msg.data.hid, input );
2353 dispatch_rawinput_message( desktop, &raw_msg );
2354 release_object( foreground );
2356 return;
2359 if (input->hw.msg == WM_POINTERDOWN || input->hw.msg == WM_POINTERUP || input->hw.msg == WM_POINTERUPDATE)
2361 pointer = find_pointer_from_id( desktop, LOWORD(input->hw.wparam) );
2362 if (pointer->timeout) remove_timeout_user( pointer->timeout );
2363 pointer->input = *input;
2364 pointer->win = win;
2366 queue_pointer_message( pointer, 0 );
2367 return;
2370 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
2372 msg->win = get_user_full_handle( win );
2373 msg->msg = input->hw.msg;
2374 msg->wparam = input->hw.wparam;
2375 msg->lparam = input->hw.lparam;
2376 msg->x = desktop->cursor.x;
2377 msg->y = desktop->cursor.y;
2379 queue_hardware_message( desktop, msg, 1 );
2382 /* check message filter for a hardware message */
2383 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
2384 user_handle_t filter_win, unsigned int first, unsigned int last )
2386 switch (get_hardware_msg_bit( msg_code ))
2388 case QS_KEY:
2389 /* we can only test the window for a keyboard message since the
2390 * dest window for a mouse message depends on hittest */
2391 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
2392 return 0;
2393 /* the message code is final for a keyboard message, we can simply check it */
2394 return check_msg_filter( msg_code, first, last );
2396 case QS_MOUSEMOVE:
2397 case QS_MOUSEBUTTON:
2398 /* we need to check all possible values that the message can have in the end */
2399 if (check_msg_filter( msg_code, first, last )) return 1;
2400 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
2402 /* all other messages can become non-client messages */
2403 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
2405 /* clicks can become double-clicks or non-client double-clicks */
2406 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
2407 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
2409 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2410 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2412 return 0;
2414 default:
2415 return check_msg_filter( msg_code, first, last );
2419 /* is this message an internal driver notification message */
2420 static inline BOOL is_internal_hardware_message( unsigned int message )
2422 return (message >= WM_WINE_FIRST_DRIVER_MSG && message <= WM_WINE_LAST_DRIVER_MSG);
2425 /* find a hardware message for the given queue */
2426 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
2427 unsigned int first, unsigned int last, unsigned int flags,
2428 struct get_message_reply *reply )
2430 struct thread_input *input = thread->queue->input;
2431 struct thread *win_thread;
2432 struct list *ptr;
2433 user_handle_t win;
2434 int clear_bits, got_one = 0;
2435 unsigned int msg_code;
2437 ptr = list_head( &input->msg_list );
2438 if (hw_id)
2440 while (ptr)
2442 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2443 if (msg->unique_id == hw_id) break;
2444 ptr = list_next( &input->msg_list, ptr );
2446 if (!ptr) ptr = list_head( &input->msg_list );
2447 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
2450 if (ptr == list_head( &input->msg_list ))
2451 clear_bits = QS_INPUT;
2452 else
2453 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
2455 while (ptr)
2457 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2458 struct hardware_msg_data *data = msg->data;
2460 ptr = list_next( &input->msg_list, ptr );
2461 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
2462 if (!win || !win_thread)
2464 /* no window at all, remove it */
2465 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2466 list_remove( &msg->entry );
2467 free_message( msg );
2468 continue;
2470 if (win_thread != thread)
2472 if (win_thread->queue->input == input)
2474 /* wake the other thread */
2475 set_queue_bits( win_thread->queue, get_hardware_msg_bit( msg->msg ) );
2476 got_one = 1;
2478 else
2480 /* for another thread input, drop it */
2481 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2482 list_remove( &msg->entry );
2483 free_message( msg );
2485 release_object( win_thread );
2486 continue;
2488 release_object( win_thread );
2490 /* if we already got a message for another thread, or if it doesn't
2491 * match the filter we skip it */
2492 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
2494 clear_bits &= ~get_hardware_msg_bit( msg->msg );
2495 continue;
2498 reply->total = msg->data_size;
2499 if (msg->data_size > get_reply_max_size())
2501 set_error( STATUS_BUFFER_OVERFLOW );
2502 return 1;
2505 /* now we can return it */
2506 if (!msg->unique_id) msg->unique_id = get_unique_id();
2507 reply->type = MSG_HARDWARE;
2508 reply->win = win;
2509 reply->msg = msg_code;
2510 reply->wparam = msg->wparam;
2511 reply->lparam = msg->lparam;
2512 reply->x = msg->x;
2513 reply->y = msg->y;
2514 reply->time = msg->time;
2516 data->hw_id = msg->unique_id;
2517 set_reply_data( msg->data, msg->data_size );
2518 if ((get_hardware_msg_bit( msg->msg ) & (QS_RAWINPUT | QS_POINTER) && (flags & PM_REMOVE)) ||
2519 is_internal_hardware_message( msg->msg ))
2520 release_hardware_message( current->queue, data->hw_id );
2521 return 1;
2523 /* nothing found, clear the hardware queue bits */
2524 clear_queue_bits( thread->queue, clear_bits );
2525 return 0;
2528 /* increment (or decrement if 'incr' is negative) the queue paint count */
2529 void inc_queue_paint_count( struct thread *thread, int incr )
2531 struct msg_queue *queue = thread->queue;
2533 assert( queue );
2535 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2537 if (queue->paint_count)
2538 set_queue_bits( queue, QS_PAINT );
2539 else
2540 clear_queue_bits( queue, QS_PAINT );
2544 /* remove all messages and timers belonging to a certain window */
2545 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2547 struct msg_queue *queue = thread->queue;
2548 struct list *ptr;
2549 int i;
2551 if (!queue) return;
2553 /* remove timers */
2555 ptr = list_head( &queue->pending_timers );
2556 while (ptr)
2558 struct list *next = list_next( &queue->pending_timers, ptr );
2559 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2560 if (timer->win == win) free_timer( queue, timer );
2561 ptr = next;
2563 ptr = list_head( &queue->expired_timers );
2564 while (ptr)
2566 struct list *next = list_next( &queue->expired_timers, ptr );
2567 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2568 if (timer->win == win) free_timer( queue, timer );
2569 ptr = next;
2572 /* remove messages */
2573 for (i = 0; i < NB_MSG_KINDS; i++)
2575 struct list *ptr, *next;
2577 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2579 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2580 if (msg->win == win)
2582 if (msg->msg == WM_QUIT && !queue->quit_message)
2584 queue->quit_message = 1;
2585 queue->exit_code = msg->wparam;
2587 remove_queue_message( queue, msg, i );
2592 thread_input_cleanup_window( queue, win );
2595 /* post a message to a window */
2596 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2598 struct message *msg;
2599 struct thread *thread = get_window_thread( win );
2601 if (!thread) return;
2603 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2605 msg->type = MSG_POSTED;
2606 msg->win = get_user_full_handle( win );
2607 msg->msg = message;
2608 msg->wparam = wparam;
2609 msg->lparam = lparam;
2610 msg->result = NULL;
2611 msg->data = NULL;
2612 msg->data_size = 0;
2614 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2616 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2617 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2618 if (message == WM_HOTKEY)
2620 set_queue_bits( thread->queue, QS_HOTKEY );
2621 thread->queue->hotkey_count++;
2624 release_object( thread );
2627 /* send a notify message to a window */
2628 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2630 struct message *msg;
2631 struct thread *thread = get_window_thread( win );
2633 if (!thread) return;
2635 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2637 msg->type = MSG_NOTIFY;
2638 msg->win = get_user_full_handle( win );
2639 msg->msg = message;
2640 msg->wparam = wparam;
2641 msg->lparam = lparam;
2642 msg->result = NULL;
2643 msg->data = NULL;
2644 msg->data_size = 0;
2646 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2648 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2649 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2651 release_object( thread );
2654 /* post a win event */
2655 void post_win_event( struct thread *thread, unsigned int event,
2656 user_handle_t win, unsigned int object_id,
2657 unsigned int child_id, client_ptr_t hook_proc,
2658 const WCHAR *module, data_size_t module_size,
2659 user_handle_t hook)
2661 struct message *msg;
2663 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2665 struct winevent_msg_data *data;
2667 msg->type = MSG_WINEVENT;
2668 msg->win = get_user_full_handle( win );
2669 msg->msg = event;
2670 msg->wparam = object_id;
2671 msg->lparam = child_id;
2672 msg->time = get_tick_count();
2673 msg->result = NULL;
2675 if ((data = malloc( sizeof(*data) + module_size )))
2677 data->hook = hook;
2678 data->tid = get_thread_id( current );
2679 data->hook_proc = hook_proc;
2680 memcpy( data + 1, module, module_size );
2682 msg->data = data;
2683 msg->data_size = sizeof(*data) + module_size;
2685 if (debug_level > 1)
2686 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2687 get_thread_id(thread), event, win, object_id, child_id );
2688 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2689 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2691 else
2692 free( msg );
2696 void free_pointers( struct desktop *desktop )
2698 struct pointer *pointer, *next;
2700 LIST_FOR_EACH_ENTRY_SAFE( pointer, next, &desktop->pointers, struct pointer, entry )
2702 list_remove( &pointer->entry );
2703 if (pointer->timeout) remove_timeout_user( pointer->timeout );
2704 free( pointer );
2708 /* free all hotkeys on a desktop, optionally filtering by window */
2709 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2711 struct hotkey *hotkey, *hotkey2;
2713 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2715 if (!window || hotkey->win == window)
2717 list_remove( &hotkey->entry );
2718 free( hotkey );
2723 /* retrieve the desktop which should receive some hardware input event */
2724 static struct desktop *get_hardware_input_desktop( user_handle_t win )
2726 struct winstation *winstation;
2727 struct desktop *desktop;
2728 struct thread *thread;
2730 if (!win || !(thread = get_window_thread( win )))
2732 if (!(winstation = get_visible_winstation())) return NULL;
2733 return get_input_desktop( winstation );
2735 else
2737 /* if window is specified, use its desktop to make it the input desktop */
2738 desktop = (struct desktop *)grab_object( thread->queue->input->desktop );
2739 release_object( thread );
2742 return desktop;
2745 /* enable or disable rawinput for a given process */
2746 void set_rawinput_process( struct process *process, int enable )
2748 list_remove( &process->rawinput_entry ); /* remove it first, it might already be in the list */
2749 if (!process->rawinput_device_count || !enable) list_init( &process->rawinput_entry );
2750 else list_add_tail( &rawinput_processes, &process->rawinput_entry );
2754 /* check if the thread owning the window is hung */
2755 DECL_HANDLER(is_window_hung)
2757 struct thread *thread;
2759 thread = get_window_thread( req->win );
2760 if (thread)
2762 reply->is_hung = is_queue_hung( thread->queue );
2763 release_object( thread );
2765 else reply->is_hung = 0;
2769 /* get the message queue of the current thread */
2770 DECL_HANDLER(get_msg_queue)
2772 struct msg_queue *queue = get_current_queue();
2774 reply->handle = 0;
2775 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2779 /* set the file descriptor associated to the current thread queue */
2780 DECL_HANDLER(set_queue_fd)
2782 struct msg_queue *queue = get_current_queue();
2783 struct file *file;
2784 int unix_fd;
2786 if (queue->fd) /* fd can only be set once */
2788 set_error( STATUS_ACCESS_DENIED );
2789 return;
2791 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2793 if ((unix_fd = get_file_unix_fd( file )) != -1)
2795 if ((unix_fd = dup( unix_fd )) != -1)
2796 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2797 else
2798 file_set_error();
2800 release_object( file );
2804 /* set the current message queue wakeup mask */
2805 DECL_HANDLER(set_queue_mask)
2807 struct msg_queue *queue = get_current_queue();
2809 if (queue)
2811 queue->wake_mask = req->wake_mask;
2812 queue->changed_mask = req->changed_mask;
2813 reply->wake_bits = queue->wake_bits;
2814 reply->changed_bits = queue->changed_bits;
2815 if (is_signaled( queue ))
2817 /* if skip wait is set, do what would have been done in the subsequent wait */
2818 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2819 else wake_up( &queue->obj, 0 );
2825 /* get the current message queue status */
2826 DECL_HANDLER(get_queue_status)
2828 struct msg_queue *queue = current->queue;
2829 if (queue)
2831 reply->wake_bits = queue->wake_bits;
2832 reply->changed_bits = queue->changed_bits;
2833 queue->changed_bits &= ~req->clear_bits;
2835 else reply->wake_bits = reply->changed_bits = 0;
2839 /* send a message to a thread queue */
2840 DECL_HANDLER(send_message)
2842 struct message *msg;
2843 struct msg_queue *send_queue = get_current_queue();
2844 struct msg_queue *recv_queue = NULL;
2845 struct thread *thread = NULL;
2847 if (!(thread = get_thread_from_id( req->id ))) return;
2849 if (!(recv_queue = thread->queue))
2851 set_error( STATUS_INVALID_PARAMETER );
2852 release_object( thread );
2853 return;
2855 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2857 set_error( STATUS_TIMEOUT );
2858 release_object( thread );
2859 return;
2862 if ((msg = mem_alloc( sizeof(*msg) )))
2864 msg->type = req->type;
2865 msg->win = get_user_full_handle( req->win );
2866 msg->msg = req->msg;
2867 msg->wparam = req->wparam;
2868 msg->lparam = req->lparam;
2869 msg->result = NULL;
2870 msg->data = NULL;
2871 msg->data_size = get_req_data_size();
2873 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2875 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2877 free( msg );
2878 release_object( thread );
2879 return;
2882 switch(msg->type)
2884 case MSG_OTHER_PROCESS:
2885 case MSG_ASCII:
2886 case MSG_UNICODE:
2887 case MSG_CALLBACK:
2888 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2890 free_message( msg );
2891 break;
2893 /* fall through */
2894 case MSG_NOTIFY:
2895 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2896 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2897 break;
2898 case MSG_POSTED:
2899 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2900 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2901 if (msg->msg == WM_HOTKEY)
2903 set_queue_bits( recv_queue, QS_HOTKEY );
2904 recv_queue->hotkey_count++;
2906 break;
2907 case MSG_HARDWARE: /* should use send_hardware_message instead */
2908 case MSG_CALLBACK_RESULT: /* cannot send this one */
2909 case MSG_HOOK_LL: /* generated internally */
2910 default:
2911 set_error( STATUS_INVALID_PARAMETER );
2912 free( msg );
2913 break;
2916 release_object( thread );
2919 /* send a hardware message to a thread queue */
2920 DECL_HANDLER(send_hardware_message)
2922 struct desktop *desktop;
2923 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2924 struct msg_queue *sender = req->flags & SEND_HWMSG_INJECTED ? get_current_queue() : NULL;
2925 int wait = 0;
2927 if (!(desktop = get_hardware_input_desktop( req->win ))) return;
2928 if ((origin == IMO_INJECTED && desktop != current->queue->input->desktop) ||
2929 !set_input_desktop( desktop->winstation, desktop ))
2931 release_object( desktop );
2932 set_error( STATUS_ACCESS_DENIED );
2933 return;
2936 reply->prev_x = desktop->cursor.x;
2937 reply->prev_y = desktop->cursor.y;
2939 switch (req->input.type)
2941 case INPUT_MOUSE:
2942 wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2943 break;
2944 case INPUT_KEYBOARD:
2945 wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2946 break;
2947 case INPUT_HARDWARE:
2948 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2949 break;
2950 default:
2951 set_error( STATUS_INVALID_PARAMETER );
2954 reply->wait = sender ? wait : 0;
2955 reply->new_x = desktop->cursor.x;
2956 reply->new_y = desktop->cursor.y;
2957 release_object( desktop );
2960 /* post a quit message to the current queue */
2961 DECL_HANDLER(post_quit_message)
2963 struct msg_queue *queue = get_current_queue();
2965 if (!queue)
2966 return;
2968 queue->quit_message = 1;
2969 queue->exit_code = req->exit_code;
2970 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2973 /* get a message from the current queue */
2974 DECL_HANDLER(get_message)
2976 struct timer *timer;
2977 struct list *ptr;
2978 struct msg_queue *queue = get_current_queue();
2979 user_handle_t get_win = get_user_full_handle( req->get_win );
2980 unsigned int filter = req->flags >> 16;
2982 reply->active_hooks = get_active_hooks();
2984 if (get_win && get_win != 1 && get_win != -1 && !get_user_object( get_win, USER_WINDOW ))
2986 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2987 return;
2990 if (!queue) return;
2992 /* check for any hardware internal message */
2993 if (get_hardware_message( current, req->hw_id, get_win, WM_WINE_FIRST_DRIVER_MSG,
2994 WM_WINE_LAST_DRIVER_MSG, req->flags, reply ))
2995 return;
2997 if (req->internal) /* check for internal messages only, leave queue flags unchanged */
2999 set_error( STATUS_PENDING );
3000 return;
3003 queue->last_get_msg = current_time;
3004 if (!filter) filter = QS_ALLINPUT;
3006 /* first check for sent messages */
3007 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
3009 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
3010 receive_message( queue, msg, reply );
3011 return;
3014 /* clear changed bits so we can wait on them if we don't find a message */
3015 if (filter & QS_POSTMESSAGE)
3017 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
3018 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
3020 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
3021 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
3023 /* then check for posted messages */
3024 if ((filter & QS_POSTMESSAGE) &&
3025 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
3026 return;
3028 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
3029 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
3030 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
3031 return;
3033 /* only check for quit messages if not posted messages pending */
3034 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
3035 return;
3037 /* then check for any raw hardware message */
3038 if ((filter & QS_INPUT) &&
3039 filter_contains_hw_range( req->get_first, req->get_last ) &&
3040 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
3041 return;
3043 /* now check for WM_PAINT */
3044 if ((filter & QS_PAINT) &&
3045 queue->paint_count &&
3046 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
3047 (reply->win = find_window_to_repaint( get_win, current )))
3049 reply->type = MSG_POSTED;
3050 reply->msg = WM_PAINT;
3051 reply->wparam = 0;
3052 reply->lparam = 0;
3053 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
3054 return;
3057 /* now check for timer */
3058 if ((filter & QS_TIMER) &&
3059 (timer = find_expired_timer( queue, get_win, req->get_first,
3060 req->get_last, (req->flags & PM_REMOVE) )))
3062 reply->type = MSG_POSTED;
3063 reply->win = timer->win;
3064 reply->msg = timer->msg;
3065 reply->wparam = timer->id;
3066 reply->lparam = timer->lparam;
3067 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
3068 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
3069 set_event( current->process->idle_event );
3070 return;
3073 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
3074 queue->wake_mask = req->wake_mask;
3075 queue->changed_mask = req->changed_mask;
3076 set_error( STATUS_PENDING ); /* FIXME */
3080 /* reply to a sent message */
3081 DECL_HANDLER(reply_message)
3083 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
3084 else if (current->queue->recv_result)
3085 reply_message( current->queue, req->result, 0, req->remove,
3086 get_req_data(), get_req_data_size() );
3090 /* accept the current hardware message */
3091 DECL_HANDLER(accept_hardware_message)
3093 if (current->queue)
3094 release_hardware_message( current->queue, req->hw_id );
3095 else
3096 set_error( STATUS_ACCESS_DENIED );
3100 /* retrieve the reply for the last message sent */
3101 DECL_HANDLER(get_message_reply)
3103 struct message_result *result;
3104 struct list *entry;
3105 struct msg_queue *queue = current->queue;
3107 if (queue)
3109 set_error( STATUS_PENDING );
3110 reply->result = 0;
3112 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
3114 result = LIST_ENTRY( entry, struct message_result, sender_entry );
3115 if (result->replied || req->cancel)
3117 if (result->replied)
3119 reply->result = result->result;
3120 set_error( result->error );
3121 if (result->data)
3123 data_size_t data_len = min( result->data_size, get_reply_max_size() );
3124 set_reply_data_ptr( result->data, data_len );
3125 result->data = NULL;
3126 result->data_size = 0;
3129 remove_result_from_sender( result );
3131 entry = list_head( &queue->send_result );
3132 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
3133 else
3135 result = LIST_ENTRY( entry, struct message_result, sender_entry );
3136 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
3137 else clear_queue_bits( queue, QS_SMRESULT );
3141 else set_error( STATUS_ACCESS_DENIED );
3145 /* set a window timer */
3146 DECL_HANDLER(set_win_timer)
3148 struct timer *timer;
3149 struct msg_queue *queue;
3150 struct thread *thread = NULL;
3151 user_handle_t win = 0;
3152 lparam_t id = req->id;
3154 if (req->win)
3156 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
3158 set_error( STATUS_INVALID_HANDLE );
3159 return;
3161 if (thread->process != current->process)
3163 release_object( thread );
3164 set_error( STATUS_ACCESS_DENIED );
3165 return;
3167 queue = thread->queue;
3168 /* remove it if it existed already */
3169 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
3171 else
3173 queue = get_current_queue();
3174 /* look for a timer with this id */
3175 if (id && (timer = find_timer( queue, 0, req->msg, id )))
3177 /* free and reuse id */
3178 free_timer( queue, timer );
3180 else
3182 lparam_t end_id = queue->next_timer_id;
3184 /* find a free id for it */
3185 while (1)
3187 id = queue->next_timer_id;
3188 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
3190 if (!find_timer( queue, 0, req->msg, id )) break;
3191 if (queue->next_timer_id == end_id)
3193 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
3194 return;
3200 if ((timer = set_timer( queue, req->rate )))
3202 timer->win = win;
3203 timer->msg = req->msg;
3204 timer->id = id;
3205 timer->lparam = req->lparam;
3206 reply->id = id;
3208 if (thread) release_object( thread );
3211 /* kill a window timer */
3212 DECL_HANDLER(kill_win_timer)
3214 struct timer *timer;
3215 struct thread *thread;
3216 user_handle_t win = 0;
3218 if (req->win)
3220 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
3222 set_error( STATUS_INVALID_HANDLE );
3223 return;
3225 if (thread->process != current->process)
3227 release_object( thread );
3228 set_error( STATUS_ACCESS_DENIED );
3229 return;
3232 else thread = (struct thread *)grab_object( current );
3234 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
3235 free_timer( thread->queue, timer );
3236 else
3237 set_error( STATUS_INVALID_PARAMETER );
3239 release_object( thread );
3242 DECL_HANDLER(register_hotkey)
3244 struct desktop *desktop;
3245 user_handle_t win_handle = req->window;
3246 struct hotkey *hotkey;
3247 struct hotkey *new_hotkey = NULL;
3248 struct thread *thread;
3249 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
3251 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3253 if (win_handle)
3255 if (!(win_handle = get_valid_window_handle( win_handle )))
3257 release_object( desktop );
3258 return;
3261 thread = get_window_thread( win_handle );
3262 if (thread) release_object( thread );
3264 if (thread != current)
3266 release_object( desktop );
3267 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
3268 return;
3272 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
3274 if (req->vkey == hotkey->vkey &&
3275 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
3277 release_object( desktop );
3278 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
3279 return;
3281 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
3282 new_hotkey = hotkey;
3285 if (new_hotkey)
3287 reply->replaced = 1;
3288 reply->flags = new_hotkey->flags;
3289 reply->vkey = new_hotkey->vkey;
3291 else
3293 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
3294 if (new_hotkey)
3296 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
3297 new_hotkey->queue = current->queue;
3298 new_hotkey->win = win_handle;
3299 new_hotkey->id = req->id;
3303 if (new_hotkey)
3305 new_hotkey->flags = req->flags;
3306 new_hotkey->vkey = req->vkey;
3309 release_object( desktop );
3312 DECL_HANDLER(unregister_hotkey)
3314 struct desktop *desktop;
3315 user_handle_t win_handle = req->window;
3316 struct hotkey *hotkey;
3317 struct thread *thread;
3319 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3321 if (win_handle)
3323 if (!(win_handle = get_valid_window_handle( win_handle )))
3325 release_object( desktop );
3326 return;
3329 thread = get_window_thread( win_handle );
3330 if (thread) release_object( thread );
3332 if (thread != current)
3334 release_object( desktop );
3335 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
3336 return;
3340 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
3342 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
3343 goto found;
3346 release_object( desktop );
3347 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
3348 return;
3350 found:
3351 reply->flags = hotkey->flags;
3352 reply->vkey = hotkey->vkey;
3353 list_remove( &hotkey->entry );
3354 free( hotkey );
3355 release_object( desktop );
3358 /* attach (or detach) thread inputs */
3359 DECL_HANDLER(attach_thread_input)
3361 struct thread *thread_from = get_thread_from_id( req->tid_from );
3362 struct thread *thread_to = get_thread_from_id( req->tid_to );
3364 if (!thread_from || !thread_to)
3366 if (thread_from) release_object( thread_from );
3367 if (thread_to) release_object( thread_to );
3368 return;
3370 if (thread_from != thread_to)
3372 if (req->attach)
3374 if ((thread_to->queue || thread_to == current) &&
3375 (thread_from->queue || thread_from == current))
3376 attach_thread_input( thread_from, thread_to );
3377 else
3378 set_error( STATUS_INVALID_PARAMETER );
3380 else
3382 if (thread_from->queue && thread_to->queue &&
3383 thread_from->queue->input == thread_to->queue->input)
3384 detach_thread_input( thread_from );
3385 else
3386 set_error( STATUS_ACCESS_DENIED );
3389 else set_error( STATUS_ACCESS_DENIED );
3390 release_object( thread_from );
3391 release_object( thread_to );
3395 /* get thread input data */
3396 DECL_HANDLER(get_thread_input)
3398 struct thread *thread = NULL;
3399 struct desktop *desktop;
3400 struct thread_input *input;
3402 if (req->tid)
3404 if (!(thread = get_thread_from_id( req->tid ))) return;
3405 if (!(desktop = get_thread_desktop( thread, 0 )))
3407 release_object( thread );
3408 return;
3410 input = thread->queue ? thread->queue->input : NULL;
3412 else
3414 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3415 input = desktop->foreground_input; /* get the foreground thread info */
3418 if (input)
3420 reply->focus = input->focus;
3421 reply->capture = input->capture;
3422 reply->active = input->active;
3423 reply->menu_owner = input->menu_owner;
3424 reply->move_size = input->move_size;
3425 reply->caret = input->caret;
3426 reply->cursor = input->cursor;
3427 reply->show_count = input->cursor_count;
3428 reply->rect = input->caret_rect;
3431 /* foreground window is active window of foreground thread */
3432 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
3433 if (thread) release_object( thread );
3434 release_object( desktop );
3438 /* retrieve queue keyboard state for current thread or global async state */
3439 DECL_HANDLER(get_key_state)
3441 struct desktop *desktop;
3442 data_size_t size = min( 256, get_reply_max_size() );
3444 if (req->async) /* get global async key state */
3446 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3447 if (req->key >= 0)
3449 reply->state = desktop->keystate[req->key & 0xff];
3450 desktop->keystate[req->key & 0xff] &= ~0x40;
3452 set_reply_data( desktop->keystate, size );
3453 release_object( desktop );
3455 else
3457 struct msg_queue *queue = get_current_queue();
3458 unsigned char *keystate = queue->input->keystate;
3459 if (req->key >= 0)
3461 sync_input_keystate( queue->input );
3462 reply->state = keystate[req->key & 0xff];
3464 set_reply_data( keystate, size );
3469 /* set queue keyboard state for current thread */
3470 DECL_HANDLER(set_key_state)
3472 struct desktop *desktop;
3473 struct msg_queue *queue = get_current_queue();
3474 data_size_t size = min( 256, get_req_data_size() );
3476 memcpy( queue->input->keystate, get_req_data(), size );
3477 memcpy( queue->input->desktop_keystate, queue->input->desktop->keystate, 256 );
3478 if (req->async && (desktop = get_thread_desktop( current, 0 )))
3480 memcpy( desktop->keystate, get_req_data(), size );
3481 release_object( desktop );
3486 /* set the system foreground window */
3487 DECL_HANDLER(set_foreground_window)
3489 struct thread *thread = NULL;
3490 struct desktop *desktop;
3491 struct msg_queue *queue = get_current_queue();
3493 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3494 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
3495 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
3496 reply->send_msg_new = FALSE;
3498 if (is_valid_foreground_window( req->handle ) &&
3499 (thread = get_window_thread( req->handle )) &&
3500 thread->queue->input->desktop == desktop)
3502 set_foreground_input( desktop, thread->queue->input );
3503 reply->send_msg_new = (desktop->foreground_input != queue->input);
3505 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
3507 if (thread) release_object( thread );
3508 release_object( desktop );
3512 /* set the current thread focus window */
3513 DECL_HANDLER(set_focus_window)
3515 struct msg_queue *queue = get_current_queue();
3517 reply->previous = 0;
3518 if (queue && check_queue_input_window( queue, req->handle ))
3520 reply->previous = queue->input->focus;
3521 queue->input->focus = get_user_full_handle( req->handle );
3526 /* set the current thread active window */
3527 DECL_HANDLER(set_active_window)
3529 struct msg_queue *queue = get_current_queue();
3531 reply->previous = 0;
3532 if (queue && check_queue_input_window( queue, req->handle ))
3534 if (!req->handle || make_window_active( req->handle ))
3536 reply->previous = queue->input->active;
3537 queue->input->active = get_user_full_handle( req->handle );
3539 else set_error( STATUS_INVALID_HANDLE );
3544 /* set the current thread capture window */
3545 DECL_HANDLER(set_capture_window)
3547 struct msg_queue *queue = get_current_queue();
3549 reply->previous = reply->full_handle = 0;
3550 if (queue && check_queue_input_window( queue, req->handle ))
3552 struct thread_input *input = queue->input;
3554 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
3555 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
3557 set_error(STATUS_ACCESS_DENIED);
3558 return;
3560 reply->previous = input->capture;
3561 input->capture = get_user_full_handle( req->handle );
3562 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
3563 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
3564 reply->full_handle = input->capture;
3569 /* Set the current thread caret window */
3570 DECL_HANDLER(set_caret_window)
3572 struct msg_queue *queue = get_current_queue();
3574 reply->previous = 0;
3575 if (queue && check_queue_input_window( queue, req->handle ))
3577 struct thread_input *input = queue->input;
3579 reply->previous = input->caret;
3580 reply->old_rect = input->caret_rect;
3581 reply->old_hide = input->caret_hide;
3582 reply->old_state = input->caret_state;
3584 set_caret_window( input, get_user_full_handle(req->handle) );
3585 input->caret_rect.right = input->caret_rect.left + req->width;
3586 input->caret_rect.bottom = input->caret_rect.top + req->height;
3591 /* Set the current thread caret information */
3592 DECL_HANDLER(set_caret_info)
3594 struct msg_queue *queue = get_current_queue();
3595 struct thread_input *input;
3597 if (!queue) return;
3598 input = queue->input;
3599 reply->full_handle = input->caret;
3600 reply->old_rect = input->caret_rect;
3601 reply->old_hide = input->caret_hide;
3602 reply->old_state = input->caret_state;
3604 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3606 set_error( STATUS_ACCESS_DENIED );
3607 return;
3609 if (req->flags & SET_CARET_POS)
3611 input->caret_rect.right += req->x - input->caret_rect.left;
3612 input->caret_rect.bottom += req->y - input->caret_rect.top;
3613 input->caret_rect.left = req->x;
3614 input->caret_rect.top = req->y;
3616 if (req->flags & SET_CARET_HIDE)
3618 input->caret_hide += req->hide;
3619 if (input->caret_hide < 0) input->caret_hide = 0;
3621 if (req->flags & SET_CARET_STATE)
3623 switch (req->state)
3625 case CARET_STATE_OFF: input->caret_state = 0; break;
3626 case CARET_STATE_ON: input->caret_state = 1; break;
3627 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3628 case CARET_STATE_ON_IF_MOVED:
3629 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3630 break;
3636 /* get the time of the last input event */
3637 DECL_HANDLER(get_last_input_time)
3639 reply->time = last_input_time;
3642 /* set/get the current cursor */
3643 DECL_HANDLER(set_cursor)
3645 struct msg_queue *queue = get_current_queue();
3646 user_handle_t prev_cursor, new_cursor;
3647 struct thread_input *input;
3648 struct desktop *desktop;
3650 if (!queue) return;
3651 input = queue->input;
3652 desktop = input->desktop;
3653 prev_cursor = input->cursor_count < 0 ? 0 : input->cursor;
3655 reply->prev_handle = input->cursor;
3656 reply->prev_count = input->cursor_count;
3657 reply->prev_x = desktop->cursor.x;
3658 reply->prev_y = desktop->cursor.y;
3660 if (req->flags & SET_CURSOR_HANDLE)
3662 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3664 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3665 return;
3667 input->cursor = req->handle;
3669 if (req->flags & SET_CURSOR_COUNT)
3671 queue->cursor_count += req->show_count;
3672 input->cursor_count += req->show_count;
3674 if (req->flags & SET_CURSOR_POS) set_cursor_pos( desktop, req->x, req->y );
3675 if (req->flags & SET_CURSOR_CLIP) set_clip_rectangle( desktop, &req->clip, req->flags, 0 );
3676 if (req->flags & SET_CURSOR_NOCLIP) set_clip_rectangle( desktop, NULL, SET_CURSOR_NOCLIP, 0 );
3678 new_cursor = input->cursor_count < 0 ? 0 : input->cursor;
3679 if (prev_cursor != new_cursor) update_desktop_cursor_handle( desktop, input, new_cursor );
3681 reply->new_x = desktop->cursor.x;
3682 reply->new_y = desktop->cursor.y;
3683 reply->new_clip = desktop->cursor.clip;
3684 reply->last_change = desktop->cursor.last_change;
3687 /* Get the history of the 64 last cursor positions */
3688 DECL_HANDLER(get_cursor_history)
3690 cursor_pos_t *pos;
3691 unsigned int i, count = min( 64, get_reply_max_size() / sizeof(*pos) );
3693 if ((pos = set_reply_data_size( count * sizeof(*pos) )))
3694 for (i = 0; i < count; i++)
3695 pos[i] = cursor_history[(i + cursor_history_latest) % ARRAY_SIZE(cursor_history)];
3698 DECL_HANDLER(get_rawinput_buffer)
3700 const size_t align = is_machine_64bit( current->process->machine ) ? 7 : 3;
3701 data_size_t buffer_size = get_reply_max_size() & ~align;
3702 struct thread_input *input = current->queue->input;
3703 struct message *msg, *next_msg;
3704 int count = 0;
3705 char *buffer;
3707 if (req->header_size != sizeof(RAWINPUTHEADER))
3709 set_error( STATUS_INVALID_PARAMETER );
3710 return;
3713 if (!req->read_data)
3715 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
3717 if (msg->msg == WM_INPUT)
3719 struct hardware_msg_data *msg_data = msg->data;
3720 data_size_t size = msg_data->size - sizeof(*msg_data);
3721 reply->next_size = sizeof(RAWINPUTHEADER) + size;
3722 break;
3727 else if ((buffer = mem_alloc( buffer_size )))
3729 size_t total_size = 0, next_size = 0;
3731 reply->next_size = get_reply_max_size();
3733 LIST_FOR_EACH_ENTRY_SAFE( msg, next_msg, &input->msg_list, struct message, entry )
3735 if (msg->msg == WM_INPUT)
3737 RAWINPUT *rawinput = (RAWINPUT *)(buffer + total_size);
3738 struct hardware_msg_data *msg_data = msg->data;
3739 data_size_t data_size = msg_data->size - sizeof(*msg_data);
3741 if (total_size + sizeof(RAWINPUTHEADER) + data_size > buffer_size)
3743 next_size = sizeof(RAWINPUTHEADER) + data_size;
3744 break;
3747 rawinput->header.dwSize = sizeof(RAWINPUTHEADER) + data_size;
3748 rawinput->header.dwType = msg_data->rawinput.type;
3749 rawinput->header.hDevice = UlongToHandle(msg_data->rawinput.device);
3750 rawinput->header.wParam = msg_data->rawinput.wparam;
3751 memcpy( &rawinput->header + 1, msg_data + 1, data_size );
3753 total_size += (rawinput->header.dwSize + align) & ~align;
3754 list_remove( &msg->entry );
3755 free_message( msg );
3756 count++;
3760 if (!next_size)
3762 if (count) next_size = sizeof(RAWINPUT);
3763 else reply->next_size = 0;
3766 if (next_size && get_reply_max_size() <= next_size)
3768 set_error( STATUS_BUFFER_TOO_SMALL );
3769 reply->next_size = next_size;
3772 reply->count = count;
3773 set_reply_data_ptr( buffer, total_size );
3777 DECL_HANDLER(update_rawinput_devices)
3779 const struct rawinput_device *tmp, *devices = get_req_data();
3780 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3781 size_t size = device_count * sizeof(*devices);
3782 struct process *process = current->process;
3783 struct winstation *winstation;
3784 struct desktop *desktop;
3786 if (!size)
3788 process->rawinput_device_count = 0;
3789 process->rawinput_mouse = NULL;
3790 process->rawinput_kbd = NULL;
3791 set_rawinput_process( process, 0 );
3792 return;
3795 if (!(tmp = realloc( process->rawinput_devices, size )))
3797 set_error( STATUS_NO_MEMORY );
3798 return;
3800 process->rawinput_devices = (struct rawinput_device *)tmp;
3801 process->rawinput_device_count = device_count;
3802 memcpy( process->rawinput_devices, devices, size );
3804 process->rawinput_mouse = find_rawinput_device( process, MAKELONG(HID_USAGE_GENERIC_MOUSE, HID_USAGE_PAGE_GENERIC) );
3805 process->rawinput_kbd = find_rawinput_device( process, MAKELONG(HID_USAGE_GENERIC_KEYBOARD, HID_USAGE_PAGE_GENERIC) );
3807 if ((winstation = get_visible_winstation()) && (desktop = get_input_desktop( winstation )))
3809 struct thread *thread;
3811 /* one of the process thread might be connected to the input desktop, update the full list */
3812 LIST_FOR_EACH_ENTRY( thread, &desktop->threads, struct thread, desktop_entry )
3813 set_rawinput_process( thread->process, 1 );
3815 release_object( desktop );