oleaut32: Implement OleLoadPictureFile.
[wine.git] / server / queue.c
blobcd913ae03e53a370ce6a3b9924c3cd1b1014bce0
1 /*
2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <poll.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winternl.h"
37 #include "ntuser.h"
39 #include "handle.h"
40 #include "file.h"
41 #include "thread.h"
42 #include "process.h"
43 #include "request.h"
44 #include "user.h"
46 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
47 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
49 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
50 #define NB_MSG_KINDS (POST_MESSAGE+1)
53 struct message_result
55 struct list sender_entry; /* entry in sender list */
56 struct message *msg; /* message the result is for */
57 struct message_result *recv_next; /* next in receiver list */
58 struct msg_queue *sender; /* sender queue */
59 struct msg_queue *receiver; /* receiver queue */
60 int replied; /* has it been replied to? */
61 unsigned int error; /* error code to pass back to sender */
62 lparam_t result; /* reply result */
63 struct message *hardware_msg; /* hardware message if low-level hook result */
64 struct desktop *desktop; /* desktop for hardware message */
65 struct message *callback_msg; /* message to queue for callback */
66 void *data; /* message reply data */
67 unsigned int data_size; /* size of message reply data */
68 struct timeout_user *timeout; /* result timeout */
71 struct message
73 struct list entry; /* entry in message list */
74 enum message_type type; /* message type */
75 user_handle_t win; /* window handle */
76 unsigned int msg; /* message code */
77 lparam_t wparam; /* parameters */
78 lparam_t lparam; /* parameters */
79 int x; /* message position */
80 int y;
81 unsigned int time; /* message time */
82 void *data; /* message data for sent messages */
83 unsigned int data_size; /* size of message data */
84 unsigned int unique_id; /* unique id for nested hw message waits */
85 struct message_result *result; /* result in sender queue */
88 struct timer
90 struct list entry; /* entry in timer list */
91 abstime_t when; /* next expiration */
92 unsigned int rate; /* timer rate in ms */
93 user_handle_t win; /* window handle */
94 unsigned int msg; /* message to post */
95 lparam_t id; /* timer id */
96 lparam_t lparam; /* lparam for message */
99 struct thread_input
101 struct object obj; /* object header */
102 struct desktop *desktop; /* desktop that this thread input belongs to */
103 user_handle_t focus; /* focus window */
104 user_handle_t capture; /* capture window */
105 user_handle_t active; /* active window */
106 user_handle_t menu_owner; /* current menu owner window */
107 user_handle_t move_size; /* current moving/resizing window */
108 user_handle_t caret; /* caret window */
109 rectangle_t caret_rect; /* caret rectangle */
110 int caret_hide; /* caret hide count */
111 int caret_state; /* caret on/off state */
112 user_handle_t cursor; /* current cursor */
113 int cursor_count; /* cursor show count */
114 struct list msg_list; /* list of hardware messages */
115 unsigned char keystate[256]; /* state of each key */
116 unsigned char desktop_keystate[256]; /* desktop keystate when keystate was synced */
117 int keystate_lock; /* keystate is locked */
120 struct msg_queue
122 struct object obj; /* object header */
123 struct fd *fd; /* optional file descriptor to poll */
124 unsigned int wake_bits; /* wakeup bits */
125 unsigned int wake_mask; /* wakeup mask */
126 unsigned int changed_bits; /* changed wakeup bits */
127 unsigned int changed_mask; /* changed wakeup mask */
128 int paint_count; /* pending paint messages count */
129 int hotkey_count; /* pending hotkey messages count */
130 int quit_message; /* is there a pending quit message? */
131 int exit_code; /* exit code of pending quit message */
132 int cursor_count; /* per-queue cursor show count */
133 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
134 struct list send_result; /* stack of sent messages waiting for result */
135 struct list callback_result; /* list of callback messages waiting for result */
136 struct message_result *recv_result; /* stack of received messages waiting for result */
137 struct list pending_timers; /* list of pending timers */
138 struct list expired_timers; /* list of expired timers */
139 lparam_t next_timer_id; /* id for the next timer with a 0 window */
140 struct timeout_user *timeout; /* timeout for next timer to expire */
141 struct thread_input *input; /* thread input descriptor */
142 struct hook_table *hooks; /* hook table */
143 timeout_t last_get_msg; /* time of last get message call */
144 int keystate_lock; /* owns an input keystate lock */
147 struct hotkey
149 struct list entry; /* entry in desktop hotkey list */
150 struct msg_queue *queue; /* queue owning this hotkey */
151 user_handle_t win; /* window handle */
152 int id; /* hotkey id */
153 unsigned int vkey; /* virtual key code */
154 unsigned int flags; /* key modifiers */
157 static void msg_queue_dump( struct object *obj, int verbose );
158 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
159 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
160 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
161 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
162 static void msg_queue_destroy( struct object *obj );
163 static void msg_queue_poll_event( struct fd *fd, int event );
164 static void thread_input_dump( struct object *obj, int verbose );
165 static void thread_input_destroy( struct object *obj );
166 static void timer_callback( void *private );
168 static const struct object_ops msg_queue_ops =
170 sizeof(struct msg_queue), /* size */
171 &no_type, /* type */
172 msg_queue_dump, /* dump */
173 msg_queue_add_queue, /* add_queue */
174 msg_queue_remove_queue, /* remove_queue */
175 msg_queue_signaled, /* signaled */
176 msg_queue_satisfied, /* satisfied */
177 no_signal, /* signal */
178 no_get_fd, /* get_fd */
179 default_map_access, /* map_access */
180 default_get_sd, /* get_sd */
181 default_set_sd, /* set_sd */
182 no_get_full_name, /* get_full_name */
183 no_lookup_name, /* lookup_name */
184 no_link_name, /* link_name */
185 NULL, /* unlink_name */
186 no_open_file, /* open_file */
187 no_kernel_obj_list, /* get_kernel_obj_list */
188 no_close_handle, /* close_handle */
189 msg_queue_destroy /* destroy */
192 static const struct fd_ops msg_queue_fd_ops =
194 NULL, /* get_poll_events */
195 msg_queue_poll_event, /* poll_event */
196 NULL, /* flush */
197 NULL, /* get_fd_type */
198 NULL, /* ioctl */
199 NULL, /* queue_async */
200 NULL, /* reselect_async */
201 NULL /* cancel async */
205 static const struct object_ops thread_input_ops =
207 sizeof(struct thread_input), /* size */
208 &no_type, /* type */
209 thread_input_dump, /* dump */
210 no_add_queue, /* add_queue */
211 NULL, /* remove_queue */
212 NULL, /* signaled */
213 NULL, /* satisfied */
214 no_signal, /* signal */
215 no_get_fd, /* get_fd */
216 default_map_access, /* map_access */
217 default_get_sd, /* get_sd */
218 default_set_sd, /* set_sd */
219 no_get_full_name, /* get_full_name */
220 no_lookup_name, /* lookup_name */
221 no_link_name, /* link_name */
222 NULL, /* unlink_name */
223 no_open_file, /* open_file */
224 no_kernel_obj_list, /* get_kernel_obj_list */
225 no_close_handle, /* close_handle */
226 thread_input_destroy /* destroy */
229 /* pointer to input structure of foreground thread */
230 static unsigned int last_input_time;
232 static cursor_pos_t cursor_history[64];
233 static unsigned int cursor_history_latest;
235 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
236 static void free_message( struct message *msg );
238 /* set the caret window in a given thread input */
239 static void set_caret_window( struct thread_input *input, user_handle_t win )
241 if (!win || win != input->caret)
243 input->caret_rect.left = 0;
244 input->caret_rect.top = 0;
245 input->caret_rect.right = 0;
246 input->caret_rect.bottom = 0;
248 input->caret = win;
249 input->caret_hide = 1;
250 input->caret_state = 0;
253 /* create a thread input object */
254 static struct thread_input *create_thread_input( struct thread *thread )
256 struct thread_input *input;
258 if ((input = alloc_object( &thread_input_ops )))
260 input->focus = 0;
261 input->capture = 0;
262 input->active = 0;
263 input->menu_owner = 0;
264 input->move_size = 0;
265 input->cursor = 0;
266 input->cursor_count = 0;
267 list_init( &input->msg_list );
268 set_caret_window( input, 0 );
269 memset( input->keystate, 0, sizeof(input->keystate) );
270 input->keystate_lock = 0;
272 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
274 release_object( input );
275 return NULL;
277 memcpy( input->desktop_keystate, input->desktop->keystate, sizeof(input->desktop_keystate) );
279 return input;
282 /* create a message queue object */
283 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
285 struct thread_input *new_input = NULL;
286 struct msg_queue *queue;
287 int i;
289 if (!input)
291 if (!(new_input = create_thread_input( thread ))) return NULL;
292 input = new_input;
295 if ((queue = alloc_object( &msg_queue_ops )))
297 queue->fd = NULL;
298 queue->wake_bits = 0;
299 queue->wake_mask = 0;
300 queue->changed_bits = 0;
301 queue->changed_mask = 0;
302 queue->paint_count = 0;
303 queue->hotkey_count = 0;
304 queue->quit_message = 0;
305 queue->cursor_count = 0;
306 queue->recv_result = NULL;
307 queue->next_timer_id = 0x7fff;
308 queue->timeout = NULL;
309 queue->input = (struct thread_input *)grab_object( input );
310 queue->hooks = NULL;
311 queue->last_get_msg = current_time;
312 queue->keystate_lock = 0;
313 list_init( &queue->send_result );
314 list_init( &queue->callback_result );
315 list_init( &queue->pending_timers );
316 list_init( &queue->expired_timers );
317 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
319 thread->queue = queue;
321 if (new_input) release_object( new_input );
322 return queue;
325 /* free the message queue of a thread at thread exit */
326 void free_msg_queue( struct thread *thread )
328 remove_thread_hooks( thread );
329 if (!thread->queue) return;
330 release_object( thread->queue );
331 thread->queue = NULL;
334 /* synchronize thread input keystate with the desktop */
335 static void sync_input_keystate( struct thread_input *input )
337 int i;
338 if (!input->desktop || input->keystate_lock) return;
339 for (i = 0; i < sizeof(input->keystate); ++i)
341 if (input->desktop_keystate[i] == input->desktop->keystate[i]) continue;
342 input->keystate[i] = input->desktop_keystate[i] = input->desktop->keystate[i];
346 /* locks thread input keystate to prevent synchronization */
347 static void lock_input_keystate( struct thread_input *input )
349 input->keystate_lock++;
352 /* unlock the thread input keystate and synchronize it again */
353 static void unlock_input_keystate( struct thread_input *input )
355 input->keystate_lock--;
356 if (!input->keystate_lock) sync_input_keystate( input );
359 /* change the thread input data of a given thread */
360 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
362 struct msg_queue *queue = thread->queue;
364 if (!queue)
366 thread->queue = create_msg_queue( thread, new_input );
367 return thread->queue != NULL;
369 if (queue->input)
371 queue->input->cursor_count -= queue->cursor_count;
372 if (queue->keystate_lock) unlock_input_keystate( queue->input );
373 release_object( queue->input );
375 queue->input = (struct thread_input *)grab_object( new_input );
376 if (queue->keystate_lock) lock_input_keystate( queue->input );
377 new_input->cursor_count += queue->cursor_count;
378 return 1;
381 /* allocate a hardware message and its data */
382 static struct message *alloc_hardware_message( lparam_t info, struct hw_msg_source source,
383 unsigned int time, data_size_t extra_size )
385 struct hardware_msg_data *msg_data;
386 struct message *msg;
388 if (!(msg = mem_alloc( sizeof(*msg) ))) return NULL;
389 if (!(msg_data = mem_alloc( sizeof(*msg_data) + extra_size )))
391 free( msg );
392 return NULL;
394 memset( msg, 0, sizeof(*msg) );
395 msg->type = MSG_HARDWARE;
396 msg->time = time;
397 msg->data = msg_data;
398 msg->data_size = sizeof(*msg_data) + extra_size;
400 memset( msg_data, 0, sizeof(*msg_data) + extra_size );
401 msg_data->info = info;
402 msg_data->size = msg->data_size;
403 msg_data->source = source;
404 return msg;
407 static int is_cursor_clipped( struct desktop *desktop )
409 rectangle_t top_rect, clip_rect = desktop->cursor.clip;
410 get_top_window_rectangle( desktop, &top_rect );
411 return !is_rect_equal( &clip_rect, &top_rect );
414 static void queue_cursor_message( struct desktop *desktop, user_handle_t win, unsigned int message,
415 lparam_t wparam, lparam_t lparam )
417 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
418 struct thread_input *input;
419 struct message *msg;
421 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
423 msg->msg = message;
424 msg->wparam = wparam;
425 msg->lparam = lparam;
426 msg->x = desktop->cursor.x;
427 msg->y = desktop->cursor.y;
428 if (!(msg->win = win) && (input = desktop->foreground_input)) msg->win = input->active;
429 queue_hardware_message( desktop, msg, 1 );
432 static struct thread_input *get_desktop_cursor_thread_input( struct desktop *desktop )
434 struct thread_input *input = NULL;
435 struct thread *thread;
437 if ((thread = get_window_thread( desktop->cursor.win )))
439 if (thread->queue) input = thread->queue->input;
440 release_object( thread );
443 return input;
446 static int update_desktop_cursor_window( struct desktop *desktop, user_handle_t win )
448 int updated = win != desktop->cursor.win;
449 struct thread_input *input;
450 desktop->cursor.win = win;
452 if (updated && (input = get_desktop_cursor_thread_input( desktop )))
454 user_handle_t handle = input->cursor_count < 0 ? 0 : input->cursor;
455 /* when clipping send the message to the foreground window as well, as some driver have an artificial overlay window */
456 if (is_cursor_clipped( desktop )) queue_cursor_message( desktop, 0, WM_WINE_SETCURSOR, win, handle );
457 queue_cursor_message( desktop, win, WM_WINE_SETCURSOR, win, handle );
460 return updated;
463 static int update_desktop_cursor_pos( struct desktop *desktop, user_handle_t win, int x, int y )
465 int updated;
467 x = max( min( x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
468 y = max( min( y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
469 updated = (desktop->cursor.x != x || desktop->cursor.y != y);
470 desktop->cursor.x = x;
471 desktop->cursor.y = y;
472 desktop->cursor.last_change = get_tick_count();
474 if (!win || !is_window_visible( win ) || is_window_transparent( win ))
475 win = shallow_window_from_point( desktop, x, y );
476 if (update_desktop_cursor_window( desktop, win )) updated = 1;
478 return updated;
481 static void update_desktop_cursor_handle( struct desktop *desktop, struct thread_input *input )
483 if (input == get_desktop_cursor_thread_input( desktop ))
485 user_handle_t handle = input->cursor_count < 0 ? 0 : input->cursor, win = desktop->cursor.win;
486 /* when clipping send the message to the foreground window as well, as some driver have an artificial overlay window */
487 if (is_cursor_clipped( desktop )) queue_cursor_message( desktop, 0, WM_WINE_SETCURSOR, win, handle );
488 queue_cursor_message( desktop, win, WM_WINE_SETCURSOR, win, handle );
492 /* set the cursor position and queue the corresponding mouse message */
493 static void set_cursor_pos( struct desktop *desktop, int x, int y )
495 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
496 const struct rawinput_device *device;
497 struct message *msg;
499 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
501 update_desktop_cursor_pos( desktop, 0, x, y );
502 return;
505 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
507 msg->msg = WM_MOUSEMOVE;
508 msg->x = x;
509 msg->y = y;
510 queue_hardware_message( desktop, msg, 1 );
513 /* retrieve default position and time for synthesized messages */
514 static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
516 struct desktop *desktop = queue->input->desktop;
518 *x = desktop->cursor.x;
519 *y = desktop->cursor.y;
520 *time = get_tick_count();
523 /* set the cursor clip rectangle */
524 void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect, unsigned int flags, int reset )
526 rectangle_t top_rect;
527 int x, y;
529 get_top_window_rectangle( desktop, &top_rect );
530 if (rect)
532 rectangle_t new_rect = *rect;
533 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
534 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
535 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
536 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
537 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
538 desktop->cursor.clip = new_rect;
540 else desktop->cursor.clip = top_rect;
542 /* warp the mouse to be inside the clip rect */
543 x = max( min( desktop->cursor.x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
544 y = max( min( desktop->cursor.y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
545 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
547 /* request clip cursor rectangle reset to the desktop thread */
548 if (reset) post_desktop_message( desktop, WM_WINE_CLIPCURSOR, flags, FALSE );
550 /* notify foreground thread, of reset, or to apply new cursor clipping rect */
551 queue_cursor_message( desktop, 0, WM_WINE_CLIPCURSOR, flags, reset );
554 /* change the foreground input and reset the cursor clip rect */
555 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
557 if (desktop->foreground_input == input) return;
558 set_clip_rectangle( desktop, NULL, SET_CURSOR_NOCLIP, 1 );
559 desktop->foreground_input = input;
562 /* get the hook table for a given thread */
563 struct hook_table *get_queue_hooks( struct thread *thread )
565 if (!thread->queue) return NULL;
566 return thread->queue->hooks;
569 /* set the hook table for a given thread, allocating the queue if needed */
570 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
572 struct msg_queue *queue = thread->queue;
573 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
574 if (queue->hooks) release_object( queue->hooks );
575 queue->hooks = hooks;
578 /* check the queue status */
579 static inline int is_signaled( struct msg_queue *queue )
581 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
584 /* set some queue bits */
585 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
587 if (bits & (QS_KEY | QS_MOUSEBUTTON))
589 if (!queue->keystate_lock) lock_input_keystate( queue->input );
590 queue->keystate_lock = 1;
592 queue->wake_bits |= bits;
593 queue->changed_bits |= bits;
594 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
597 /* clear some queue bits */
598 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
600 queue->wake_bits &= ~bits;
601 queue->changed_bits &= ~bits;
602 if (!(queue->wake_bits & (QS_KEY | QS_MOUSEBUTTON)))
604 if (queue->keystate_lock) unlock_input_keystate( queue->input );
605 queue->keystate_lock = 0;
609 /* check if message is matched by the filter */
610 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
612 return (msg >= first && msg <= last);
615 /* check whether a message filter contains at least one potential hardware message */
616 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
618 /* hardware message ranges are (in numerical order):
619 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
620 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
621 * WM_MOUSEFIRST .. WM_MOUSELAST
623 if (last < WM_NCMOUSEFIRST) return 0;
624 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
625 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
626 if (first > WM_MOUSELAST) return 0;
627 return 1;
630 /* get the QS_* bit corresponding to a given hardware message */
631 static inline int get_hardware_msg_bit( unsigned int message )
633 if (message == WM_INPUT_DEVICE_CHANGE || message == WM_INPUT) return QS_RAWINPUT;
634 if (message == WM_MOUSEMOVE || message == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
635 if (message >= WM_KEYFIRST && message <= WM_KEYLAST) return QS_KEY;
636 if (message == WM_WINE_CLIPCURSOR) return QS_RAWINPUT;
637 if (message == WM_WINE_SETCURSOR) return QS_RAWINPUT;
638 return QS_MOUSEBUTTON;
641 /* get the current thread queue, creating it if needed */
642 static inline struct msg_queue *get_current_queue(void)
644 struct msg_queue *queue = current->queue;
645 if (!queue) queue = create_msg_queue( current, NULL );
646 return queue;
649 /* get a (pseudo-)unique id to tag hardware messages */
650 static inline unsigned int get_unique_id(void)
652 static unsigned int id;
653 if (!++id) id = 1; /* avoid an id of 0 */
654 return id;
657 /* try to merge a WM_MOUSEMOVE message with the last in the list; return 1 if successful */
658 static int merge_mousemove( struct thread_input *input, const struct message *msg )
660 struct message *prev;
661 struct list *ptr;
663 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
665 prev = LIST_ENTRY( ptr, struct message, entry );
666 if (prev->msg != WM_INPUT) break;
668 if (!ptr) return 0;
669 if (prev->result) return 0;
670 if (prev->win && msg->win && prev->win != msg->win) return 0;
671 if (prev->msg != msg->msg) return 0;
672 if (prev->type != msg->type) return 0;
673 /* now we can merge it */
674 prev->wparam = msg->wparam;
675 prev->lparam = msg->lparam;
676 prev->x = msg->x;
677 prev->y = msg->y;
678 prev->time = msg->time;
679 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
681 struct hardware_msg_data *prev_data = prev->data;
682 struct hardware_msg_data *msg_data = msg->data;
683 prev_data->info = msg_data->info;
685 list_remove( ptr );
686 list_add_tail( &input->msg_list, ptr );
687 return 1;
690 /* try to merge a unique message with the last in the list; return 1 if successful */
691 static int merge_unique_message( struct thread_input *input, unsigned int message, const struct message *msg )
693 struct message *prev;
695 LIST_FOR_EACH_ENTRY_REV( prev, &input->msg_list, struct message, entry )
696 if (prev->msg == message) break;
697 if (&prev->entry == &input->msg_list) return 0;
699 if (prev->result) return 0;
700 if (prev->win != msg->win) return 0;
701 if (prev->type != msg->type) return 0;
703 /* now we can merge it */
704 prev->wparam = msg->wparam;
705 prev->lparam = msg->lparam;
706 prev->x = msg->x;
707 prev->y = msg->y;
708 prev->time = msg->time;
709 list_remove( &prev->entry );
710 list_add_tail( &input->msg_list, &prev->entry );
712 return 1;
715 /* try to merge a message with the messages in the list; return 1 if successful */
716 static int merge_message( struct thread_input *input, const struct message *msg )
718 if (msg->msg == WM_MOUSEMOVE) return merge_mousemove( input, msg );
719 if (msg->msg == WM_WINE_CLIPCURSOR) return merge_unique_message( input, WM_WINE_CLIPCURSOR, msg );
720 if (msg->msg == WM_WINE_SETCURSOR) return merge_unique_message( input, WM_WINE_SETCURSOR, msg );
721 return 0;
724 /* free a result structure */
725 static void free_result( struct message_result *result )
727 if (result->timeout) remove_timeout_user( result->timeout );
728 free( result->data );
729 if (result->callback_msg) free_message( result->callback_msg );
730 if (result->hardware_msg) free_message( result->hardware_msg );
731 if (result->desktop) release_object( result->desktop );
732 free( result );
735 /* remove the result from the sender list it is on */
736 static inline void remove_result_from_sender( struct message_result *result )
738 assert( result->sender );
740 list_remove( &result->sender_entry );
741 result->sender = NULL;
742 if (!result->receiver) free_result( result );
745 /* store the message result in the appropriate structure */
746 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
748 res->result = result;
749 res->error = error;
750 res->replied = 1;
751 if (res->timeout)
753 remove_timeout_user( res->timeout );
754 res->timeout = NULL;
757 if (res->hardware_msg)
759 if (!error && result) /* rejected by the hook */
760 free_message( res->hardware_msg );
761 else
762 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
764 res->hardware_msg = NULL;
767 if (res->sender)
769 if (res->callback_msg)
771 /* queue the callback message in the sender queue */
772 struct callback_msg_data *data = res->callback_msg->data;
773 data->result = result;
774 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
775 set_queue_bits( res->sender, QS_SENDMESSAGE );
776 res->callback_msg = NULL;
777 remove_result_from_sender( res );
779 else
781 /* wake sender queue if waiting on this result */
782 if (list_head(&res->sender->send_result) == &res->sender_entry)
783 set_queue_bits( res->sender, QS_SMRESULT );
786 else if (!res->receiver) free_result( res );
789 /* free a message when deleting a queue or window */
790 static void free_message( struct message *msg )
792 struct message_result *result = msg->result;
793 if (result)
795 result->msg = NULL;
796 result->receiver = NULL;
797 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
799 free( msg->data );
800 free( msg );
803 /* remove (and free) a message from a message list */
804 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
805 enum message_kind kind )
807 list_remove( &msg->entry );
808 switch(kind)
810 case SEND_MESSAGE:
811 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
812 break;
813 case POST_MESSAGE:
814 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
815 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
816 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
817 clear_queue_bits( queue, QS_HOTKEY );
818 break;
820 free_message( msg );
823 /* message timed out without getting a reply */
824 static void result_timeout( void *private )
826 struct message_result *result = private;
828 assert( !result->replied );
830 result->timeout = NULL;
832 if (result->msg) /* not received yet */
834 struct message *msg = result->msg;
836 result->msg = NULL;
837 msg->result = NULL;
838 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
839 result->receiver = NULL;
841 store_message_result( result, 0, STATUS_TIMEOUT );
844 /* allocate and fill a message result structure */
845 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
846 struct msg_queue *recv_queue,
847 struct message *msg, timeout_t timeout )
849 struct message_result *result = mem_alloc( sizeof(*result) );
850 if (result)
852 result->msg = msg;
853 result->sender = send_queue;
854 result->receiver = recv_queue;
855 result->replied = 0;
856 result->data = NULL;
857 result->data_size = 0;
858 result->timeout = NULL;
859 result->hardware_msg = NULL;
860 result->desktop = NULL;
861 result->callback_msg = NULL;
863 if (msg->type == MSG_CALLBACK)
865 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
867 if (!callback_msg)
869 free( result );
870 return NULL;
872 callback_msg->type = MSG_CALLBACK_RESULT;
873 callback_msg->win = msg->win;
874 callback_msg->msg = msg->msg;
875 callback_msg->wparam = 0;
876 callback_msg->lparam = 0;
877 callback_msg->time = get_tick_count();
878 callback_msg->result = NULL;
879 /* steal the data from the original message */
880 callback_msg->data = msg->data;
881 callback_msg->data_size = msg->data_size;
882 msg->data = NULL;
883 msg->data_size = 0;
885 result->callback_msg = callback_msg;
886 list_add_head( &send_queue->callback_result, &result->sender_entry );
888 else if (send_queue)
890 list_add_head( &send_queue->send_result, &result->sender_entry );
891 clear_queue_bits( send_queue, QS_SMRESULT );
894 if (timeout != TIMEOUT_INFINITE)
895 result->timeout = add_timeout_user( timeout, result_timeout, result );
897 return result;
900 /* receive a message, removing it from the sent queue */
901 static void receive_message( struct msg_queue *queue, struct message *msg,
902 struct get_message_reply *reply )
904 struct message_result *result = msg->result;
906 reply->total = msg->data_size;
907 if (msg->data_size > get_reply_max_size())
909 set_error( STATUS_BUFFER_OVERFLOW );
910 return;
912 reply->type = msg->type;
913 reply->win = msg->win;
914 reply->msg = msg->msg;
915 reply->wparam = msg->wparam;
916 reply->lparam = msg->lparam;
917 reply->x = msg->x;
918 reply->y = msg->y;
919 reply->time = msg->time;
921 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
923 list_remove( &msg->entry );
924 /* put the result on the receiver result stack */
925 if (result)
927 result->msg = NULL;
928 result->recv_next = queue->recv_result;
929 queue->recv_result = result;
931 free( msg );
932 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
935 /* set the result of the current received message */
936 static void reply_message( struct msg_queue *queue, lparam_t result,
937 unsigned int error, int remove, const void *data, data_size_t len )
939 struct message_result *res = queue->recv_result;
941 if (remove)
943 queue->recv_result = res->recv_next;
944 res->receiver = NULL;
945 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
947 free_result( res );
948 return;
951 if (!res->replied)
953 if (len && (res->data = memdup( data, len ))) res->data_size = len;
954 store_message_result( res, result, error );
958 static int match_window( user_handle_t win, user_handle_t msg_win )
960 if (!win) return 1;
961 if (win == -1 || win == 1) return !msg_win;
962 if (msg_win == win) return 1;
963 return is_child_window( win, msg_win );
966 /* retrieve a posted message */
967 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
968 unsigned int first, unsigned int last, unsigned int flags,
969 struct get_message_reply *reply )
971 struct message *msg;
973 /* check against the filters */
974 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
976 if (!match_window( win, msg->win )) continue;
977 if (!check_msg_filter( msg->msg, first, last )) continue;
978 goto found; /* found one */
980 return 0;
982 /* return it to the app */
983 found:
984 reply->total = msg->data_size;
985 if (msg->data_size > get_reply_max_size())
987 set_error( STATUS_BUFFER_OVERFLOW );
988 return 1;
990 reply->type = msg->type;
991 reply->win = msg->win;
992 reply->msg = msg->msg;
993 reply->wparam = msg->wparam;
994 reply->lparam = msg->lparam;
995 reply->x = msg->x;
996 reply->y = msg->y;
997 reply->time = msg->time;
999 if (flags & PM_REMOVE)
1001 if (msg->data)
1003 set_reply_data_ptr( msg->data, msg->data_size );
1004 msg->data = NULL;
1005 msg->data_size = 0;
1007 remove_queue_message( queue, msg, POST_MESSAGE );
1009 else if (msg->data) set_reply_data( msg->data, msg->data_size );
1011 return 1;
1014 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
1015 struct get_message_reply *reply )
1017 if (queue->quit_message)
1019 reply->total = 0;
1020 reply->type = MSG_POSTED;
1021 reply->win = 0;
1022 reply->msg = WM_QUIT;
1023 reply->wparam = queue->exit_code;
1024 reply->lparam = 0;
1026 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
1028 if (flags & PM_REMOVE)
1030 queue->quit_message = 0;
1031 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
1032 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1034 return 1;
1036 else
1037 return 0;
1040 /* empty a message list and free all the messages */
1041 static void empty_msg_list( struct list *list )
1043 struct list *ptr;
1045 while ((ptr = list_head( list )) != NULL)
1047 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1048 list_remove( &msg->entry );
1049 free_message( msg );
1053 /* cleanup all pending results when deleting a queue */
1054 static void cleanup_results( struct msg_queue *queue )
1056 struct list *entry;
1058 while ((entry = list_head( &queue->send_result )) != NULL)
1060 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
1063 while ((entry = list_head( &queue->callback_result )) != NULL)
1065 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
1068 while (queue->recv_result)
1069 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
1072 /* check if the thread owning the queue is hung (not checking for messages) */
1073 static int is_queue_hung( struct msg_queue *queue )
1075 struct wait_queue_entry *entry;
1077 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
1078 return 0; /* less than 5 seconds since last get message -> not hung */
1080 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
1082 if (get_wait_queue_thread(entry)->queue == queue)
1083 return 0; /* thread is waiting on queue -> not hung */
1085 return 1;
1088 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
1090 struct msg_queue *queue = (struct msg_queue *)obj;
1091 struct process *process = get_wait_queue_thread(entry)->process;
1093 /* a thread can only wait on its own queue */
1094 if (get_wait_queue_thread(entry)->queue != queue)
1096 set_error( STATUS_ACCESS_DENIED );
1097 return 0;
1099 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
1101 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
1102 set_fd_events( queue->fd, POLLIN );
1103 add_queue( obj, entry );
1104 return 1;
1107 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
1109 struct msg_queue *queue = (struct msg_queue *)obj;
1111 remove_queue( obj, entry );
1112 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
1113 set_fd_events( queue->fd, 0 );
1116 static void msg_queue_dump( struct object *obj, int verbose )
1118 struct msg_queue *queue = (struct msg_queue *)obj;
1119 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
1120 queue->wake_bits, queue->wake_mask );
1123 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
1125 struct msg_queue *queue = (struct msg_queue *)obj;
1126 int ret = 0;
1128 if (queue->fd)
1130 if ((ret = check_fd_events( queue->fd, POLLIN )))
1131 /* stop waiting on select() if we are signaled */
1132 set_fd_events( queue->fd, 0 );
1133 else if (!list_empty( &obj->wait_queue ))
1134 /* restart waiting on poll() if we are no longer signaled */
1135 set_fd_events( queue->fd, POLLIN );
1137 return ret || is_signaled( queue );
1140 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
1142 struct msg_queue *queue = (struct msg_queue *)obj;
1143 queue->wake_mask = 0;
1144 queue->changed_mask = 0;
1147 static void msg_queue_destroy( struct object *obj )
1149 struct msg_queue *queue = (struct msg_queue *)obj;
1150 struct list *ptr;
1151 struct hotkey *hotkey, *hotkey2;
1152 int i;
1154 cleanup_results( queue );
1155 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
1157 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
1159 if (hotkey->queue == queue)
1161 list_remove( &hotkey->entry );
1162 free( hotkey );
1166 while ((ptr = list_head( &queue->pending_timers )))
1168 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1169 list_remove( &timer->entry );
1170 free( timer );
1172 while ((ptr = list_head( &queue->expired_timers )))
1174 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1175 list_remove( &timer->entry );
1176 free( timer );
1178 if (queue->timeout) remove_timeout_user( queue->timeout );
1179 queue->input->cursor_count -= queue->cursor_count;
1180 if (queue->keystate_lock) unlock_input_keystate( queue->input );
1181 release_object( queue->input );
1182 if (queue->hooks) release_object( queue->hooks );
1183 if (queue->fd) release_object( queue->fd );
1186 static void msg_queue_poll_event( struct fd *fd, int event )
1188 struct msg_queue *queue = get_fd_user( fd );
1189 assert( queue->obj.ops == &msg_queue_ops );
1191 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1192 else set_fd_events( queue->fd, 0 );
1193 wake_up( &queue->obj, 0 );
1196 static void thread_input_dump( struct object *obj, int verbose )
1198 struct thread_input *input = (struct thread_input *)obj;
1199 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
1200 input->focus, input->capture, input->active );
1203 static void thread_input_destroy( struct object *obj )
1205 struct thread_input *input = (struct thread_input *)obj;
1206 struct desktop *desktop;
1208 empty_msg_list( &input->msg_list );
1209 if ((desktop = input->desktop))
1211 if (desktop->foreground_input == input) desktop->foreground_input = NULL;
1212 release_object( desktop );
1216 /* fix the thread input data when a window is destroyed */
1217 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1219 struct thread_input *input = queue->input;
1221 if (window == input->focus) input->focus = 0;
1222 if (window == input->capture) input->capture = 0;
1223 if (window == input->active) input->active = 0;
1224 if (window == input->menu_owner) input->menu_owner = 0;
1225 if (window == input->move_size) input->move_size = 0;
1226 if (window == input->caret) set_caret_window( input, 0 );
1229 /* check if the specified window can be set in the input data of a given queue */
1230 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1232 struct thread *thread;
1233 int ret = 0;
1235 if (!window) return 1; /* we can always clear the data */
1237 if ((thread = get_window_thread( window )))
1239 ret = (queue->input == thread->queue->input);
1240 if (!ret) set_error( STATUS_ACCESS_DENIED );
1241 release_object( thread );
1243 else set_error( STATUS_INVALID_HANDLE );
1245 return ret;
1248 /* make sure the specified thread has a queue */
1249 int init_thread_queue( struct thread *thread )
1251 if (thread->queue) return 1;
1252 return (create_msg_queue( thread, NULL ) != NULL);
1255 /* attach two thread input data structures */
1256 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1258 struct desktop *desktop;
1259 struct thread_input *input;
1260 int ret;
1262 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1263 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1264 input = (struct thread_input *)grab_object( thread_to->queue->input );
1265 if (input->desktop != desktop)
1267 set_error( STATUS_ACCESS_DENIED );
1268 release_object( input );
1269 release_object( desktop );
1270 return 0;
1272 release_object( desktop );
1274 if (thread_from->queue)
1276 if (!input->focus) input->focus = thread_from->queue->input->focus;
1277 if (!input->active) input->active = thread_from->queue->input->active;
1280 ret = assign_thread_input( thread_from, input );
1281 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1282 release_object( input );
1283 return ret;
1286 /* detach two thread input data structures */
1287 void detach_thread_input( struct thread *thread_from )
1289 struct thread *thread;
1290 struct thread_input *input, *old_input = thread_from->queue->input;
1292 if ((input = create_thread_input( thread_from )))
1294 if (old_input->focus && (thread = get_window_thread( old_input->focus )))
1296 if (thread == thread_from)
1298 input->focus = old_input->focus;
1299 old_input->focus = 0;
1301 release_object( thread );
1303 if (old_input->active && (thread = get_window_thread( old_input->active )))
1305 if (thread == thread_from)
1307 input->active = old_input->active;
1308 old_input->active = 0;
1310 release_object( thread );
1312 assign_thread_input( thread_from, input );
1313 release_object( input );
1318 /* set the next timer to expire */
1319 static void set_next_timer( struct msg_queue *queue )
1321 struct list *ptr;
1323 if (queue->timeout)
1325 remove_timeout_user( queue->timeout );
1326 queue->timeout = NULL;
1328 if ((ptr = list_head( &queue->pending_timers )))
1330 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1331 queue->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, queue );
1333 /* set/clear QS_TIMER bit */
1334 if (list_empty( &queue->expired_timers ))
1335 clear_queue_bits( queue, QS_TIMER );
1336 else
1337 set_queue_bits( queue, QS_TIMER );
1340 /* find a timer from its window and id */
1341 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1342 unsigned int msg, lparam_t id )
1344 struct list *ptr;
1346 /* we need to search both lists */
1348 LIST_FOR_EACH( ptr, &queue->pending_timers )
1350 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1351 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1353 LIST_FOR_EACH( ptr, &queue->expired_timers )
1355 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1356 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1358 return NULL;
1361 /* callback for the next timer expiration */
1362 static void timer_callback( void *private )
1364 struct msg_queue *queue = private;
1365 struct list *ptr;
1367 queue->timeout = NULL;
1368 /* move on to the next timer */
1369 ptr = list_head( &queue->pending_timers );
1370 list_remove( ptr );
1371 list_add_tail( &queue->expired_timers, ptr );
1372 set_next_timer( queue );
1375 /* link a timer at its rightful place in the queue list */
1376 static void link_timer( struct msg_queue *queue, struct timer *timer )
1378 struct list *ptr;
1380 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1382 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1383 if (t->when <= timer->when) break;
1385 list_add_before( ptr, &timer->entry );
1388 /* remove a timer from the queue timer list and free it */
1389 static void free_timer( struct msg_queue *queue, struct timer *timer )
1391 list_remove( &timer->entry );
1392 free( timer );
1393 set_next_timer( queue );
1396 /* restart an expired timer */
1397 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1399 list_remove( &timer->entry );
1400 while (-timer->when <= monotonic_time) timer->when -= (timeout_t)timer->rate * 10000;
1401 link_timer( queue, timer );
1402 set_next_timer( queue );
1405 /* find an expired timer matching the filtering parameters */
1406 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1407 unsigned int get_first, unsigned int get_last,
1408 int remove )
1410 struct list *ptr;
1412 LIST_FOR_EACH( ptr, &queue->expired_timers )
1414 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1415 if (win && timer->win != win) continue;
1416 if (check_msg_filter( timer->msg, get_first, get_last ))
1418 if (remove) restart_timer( queue, timer );
1419 return timer;
1422 return NULL;
1425 /* add a timer */
1426 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1428 struct timer *timer = mem_alloc( sizeof(*timer) );
1429 if (timer)
1431 timer->rate = max( rate, 1 );
1432 timer->when = -monotonic_time - (timeout_t)timer->rate * 10000;
1433 link_timer( queue, timer );
1434 /* check if we replaced the next timer */
1435 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1437 return timer;
1440 /* change the input key state for a given key */
1441 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1443 if (down)
1445 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1446 keystate[key] |= down;
1448 else keystate[key] &= ~0x80;
1451 /* update the input key state for a keyboard message */
1452 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1453 unsigned int msg, lparam_t wparam )
1455 unsigned char key;
1456 int down = 0;
1458 switch (msg)
1460 case WM_LBUTTONDOWN:
1461 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1462 /* fall through */
1463 case WM_LBUTTONUP:
1464 set_input_key_state( keystate, VK_LBUTTON, down );
1465 break;
1466 case WM_MBUTTONDOWN:
1467 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1468 /* fall through */
1469 case WM_MBUTTONUP:
1470 set_input_key_state( keystate, VK_MBUTTON, down );
1471 break;
1472 case WM_RBUTTONDOWN:
1473 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1474 /* fall through */
1475 case WM_RBUTTONUP:
1476 set_input_key_state( keystate, VK_RBUTTON, down );
1477 break;
1478 case WM_XBUTTONDOWN:
1479 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1480 /* fall through */
1481 case WM_XBUTTONUP:
1482 if (wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1483 else if (wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1484 break;
1485 case WM_KEYDOWN:
1486 case WM_SYSKEYDOWN:
1487 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1488 /* fall through */
1489 case WM_KEYUP:
1490 case WM_SYSKEYUP:
1491 key = (unsigned char)wparam;
1492 set_input_key_state( keystate, key, down );
1493 switch(key)
1495 case VK_LCONTROL:
1496 case VK_RCONTROL:
1497 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1498 set_input_key_state( keystate, VK_CONTROL, down );
1499 break;
1500 case VK_LMENU:
1501 case VK_RMENU:
1502 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1503 set_input_key_state( keystate, VK_MENU, down );
1504 break;
1505 case VK_LSHIFT:
1506 case VK_RSHIFT:
1507 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1508 set_input_key_state( keystate, VK_SHIFT, down );
1509 break;
1511 break;
1515 /* update the desktop key state according to a mouse message flags */
1516 static void update_desktop_mouse_state( struct desktop *desktop, unsigned int flags, lparam_t wparam )
1518 if (flags & MOUSEEVENTF_LEFTDOWN)
1519 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONDOWN, wparam );
1520 if (flags & MOUSEEVENTF_LEFTUP)
1521 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONUP, wparam );
1522 if (flags & MOUSEEVENTF_RIGHTDOWN)
1523 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONDOWN, wparam );
1524 if (flags & MOUSEEVENTF_RIGHTUP)
1525 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONUP, wparam );
1526 if (flags & MOUSEEVENTF_MIDDLEDOWN)
1527 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONDOWN, wparam );
1528 if (flags & MOUSEEVENTF_MIDDLEUP)
1529 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONUP, wparam );
1530 if (flags & MOUSEEVENTF_XDOWN)
1531 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONDOWN, wparam );
1532 if (flags & MOUSEEVENTF_XUP)
1533 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONUP, wparam );
1536 /* release the hardware message currently being processed by the given thread */
1537 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id )
1539 struct thread_input *input = queue->input;
1540 struct message *msg, *other;
1541 int clr_bit;
1543 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1545 if (msg->unique_id == hw_id) break;
1547 if (&msg->entry == &input->msg_list) return; /* not found */
1549 /* clear the queue bit for that message */
1550 clr_bit = get_hardware_msg_bit( msg->msg );
1551 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1553 if (other != msg && get_hardware_msg_bit( other->msg ) == clr_bit)
1555 clr_bit = 0;
1556 break;
1559 if (clr_bit) clear_queue_bits( queue, clr_bit );
1561 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1562 list_remove( &msg->entry );
1563 free_message( msg );
1566 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1568 struct hotkey *hotkey;
1569 unsigned int modifiers = 0;
1571 if (msg->msg != WM_KEYDOWN && msg->msg != WM_SYSKEYDOWN) return 0;
1573 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1574 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1575 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1576 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1578 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1580 if (hotkey->vkey != msg->wparam) continue;
1581 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1584 return 0;
1586 found:
1587 msg->type = MSG_POSTED;
1588 msg->win = hotkey->win;
1589 msg->msg = WM_HOTKEY;
1590 msg->wparam = hotkey->id;
1591 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1593 free( msg->data );
1594 msg->data = NULL;
1595 msg->data_size = 0;
1597 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1598 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1599 hotkey->queue->hotkey_count++;
1600 return 1;
1603 /* find the window that should receive a given hardware message */
1604 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1605 struct message *msg, unsigned int *msg_code,
1606 struct thread **thread )
1608 user_handle_t win = 0;
1610 *thread = NULL;
1611 *msg_code = msg->msg;
1612 switch (get_hardware_msg_bit( msg->msg ))
1614 case QS_RAWINPUT:
1615 if (!(win = msg->win) && input) win = input->focus;
1616 break;
1617 case QS_KEY:
1618 if (input && !(win = input->focus))
1620 win = input->active;
1621 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1623 break;
1624 case QS_MOUSEMOVE:
1625 case QS_MOUSEBUTTON:
1626 if (!input || !(win = input->capture))
1628 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1629 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1630 *thread = window_thread_from_point( win, msg->x, msg->y );
1632 break;
1635 if (!*thread)
1636 *thread = get_window_thread( win );
1637 return win;
1640 static struct rawinput_device *find_rawinput_device( struct process *process, unsigned short usage_page, unsigned short usage )
1642 struct rawinput_device *device, *end;
1644 for (device = process->rawinput_devices, end = device + process->rawinput_device_count; device != end; device++)
1646 if (device->usage_page != usage_page || device->usage != usage) continue;
1647 return device;
1650 return NULL;
1653 static void prepend_cursor_history( int x, int y, unsigned int time, lparam_t info )
1655 cursor_pos_t *pos = &cursor_history[--cursor_history_latest % ARRAY_SIZE(cursor_history)];
1657 pos->x = x;
1658 pos->y = y;
1659 pos->time = time;
1660 pos->info = info;
1663 /* queue a hardware message into a given thread input */
1664 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1666 user_handle_t win;
1667 struct thread *thread;
1668 struct thread_input *input;
1669 struct hardware_msg_data *msg_data = msg->data;
1670 unsigned int msg_code;
1672 update_input_key_state( desktop, desktop->keystate, msg->msg, msg->wparam );
1673 last_input_time = get_tick_count();
1674 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1676 switch (get_hardware_msg_bit( msg->msg ))
1678 case QS_KEY:
1679 if (queue_hotkey_message( desktop, msg )) return;
1680 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1681 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1682 msg->lparam &= ~(KF_EXTENDED << 16);
1683 break;
1684 case QS_MOUSEMOVE:
1685 prepend_cursor_history( msg->x, msg->y, msg->time, msg_data->info );
1686 /* fallthrough */
1687 case QS_MOUSEBUTTON:
1688 if (update_desktop_cursor_pos( desktop, msg->win, msg->x, msg->y )) always_queue = 1;
1689 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1690 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1691 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1692 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1693 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1694 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1695 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1696 break;
1698 msg->x = desktop->cursor.x;
1699 msg->y = desktop->cursor.y;
1701 if (msg->win && (thread = get_window_thread( msg->win )))
1703 input = thread->queue->input;
1704 release_object( thread );
1706 else input = desktop->foreground_input;
1708 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1709 if (!win || !thread)
1711 if (input) update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1712 free_message( msg );
1713 return;
1715 input = thread->queue->input;
1717 if (win != msg->win) always_queue = 1;
1718 if (!always_queue || merge_message( input, msg )) free_message( msg );
1719 else
1721 msg->unique_id = 0; /* will be set once we return it to the app */
1722 list_add_tail( &input->msg_list, &msg->entry );
1723 set_queue_bits( thread->queue, get_hardware_msg_bit( msg->msg ) );
1725 release_object( thread );
1728 /* send the low-level hook message for a given hardware message */
1729 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1730 const hw_input_t *input, struct msg_queue *sender )
1732 struct thread *hook_thread;
1733 struct msg_queue *queue;
1734 struct message *msg;
1735 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1736 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1738 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1739 if (!(queue = hook_thread->queue)) return 0;
1740 if (is_queue_hung( queue )) return 0;
1742 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1744 msg->type = MSG_HOOK_LL;
1745 msg->win = 0;
1746 msg->msg = id;
1747 msg->wparam = hardware_msg->msg;
1748 msg->x = hardware_msg->x;
1749 msg->y = hardware_msg->y;
1750 msg->time = hardware_msg->time;
1751 msg->data_size = hardware_msg->data_size;
1752 msg->result = NULL;
1754 if (input->type == INPUT_KEYBOARD)
1756 unsigned short vkey = input->kbd.vkey;
1757 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1758 msg->lparam = (input->kbd.scan << 16) | vkey;
1760 else msg->lparam = input->mouse.data << 16;
1762 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1763 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1765 free_message( msg );
1766 return 0;
1768 msg->result->hardware_msg = hardware_msg;
1769 msg->result->desktop = (struct desktop *)grab_object( desktop );
1770 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1771 set_queue_bits( queue, QS_SENDMESSAGE );
1772 return 1;
1775 /* get the foreground thread for a desktop and a window receiving input */
1776 static struct thread *get_foreground_thread( struct desktop *desktop, user_handle_t window )
1778 /* if desktop has no foreground process, assume the receiving window is */
1779 if (desktop->foreground_input) return get_window_thread( desktop->foreground_input->focus );
1780 if (window) return get_window_thread( window );
1781 return NULL;
1784 struct rawinput_message
1786 struct thread *foreground;
1787 struct desktop *desktop;
1788 struct hw_msg_source source;
1789 unsigned int time;
1790 unsigned int message;
1791 struct hardware_msg_data data;
1792 const void *hid_report;
1795 /* check if process is supposed to receive a WM_INPUT message and eventually queue it */
1796 static int queue_rawinput_message( struct process* process, void *arg )
1798 const struct rawinput_message* raw_msg = arg;
1799 const struct rawinput_device *device = NULL;
1800 struct desktop *target_desktop = NULL, *desktop = NULL;
1801 struct thread *target_thread = NULL, *foreground = NULL;
1802 struct message *msg;
1803 data_size_t report_size;
1804 int wparam = RIM_INPUT;
1806 if (raw_msg->data.rawinput.type == RIM_TYPEMOUSE)
1807 device = process->rawinput_mouse;
1808 else if (raw_msg->data.rawinput.type == RIM_TYPEKEYBOARD)
1809 device = process->rawinput_kbd;
1810 else
1811 device = find_rawinput_device( process, raw_msg->data.rawinput.hid.usage_page, raw_msg->data.rawinput.hid.usage );
1812 if (!device) return 0;
1814 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && !(device->flags & RIDEV_DEVNOTIFY)) return 0;
1816 if (raw_msg->desktop) desktop = (struct desktop *)grab_object( raw_msg->desktop );
1817 else if (!(desktop = get_desktop_obj( process, process->desktop, 0 ))) goto done;
1819 if (raw_msg->foreground) foreground = (struct thread *)grab_object( raw_msg->foreground );
1820 else if (!(foreground = get_foreground_thread( desktop, 0 ))) goto done;
1822 if (process != foreground->process)
1824 if (raw_msg->message == WM_INPUT && !(device->flags & RIDEV_INPUTSINK)) goto done;
1825 if (!(target_thread = get_window_thread( device->target ))) goto done;
1826 if (!(target_desktop = get_thread_desktop( target_thread, 0 ))) goto done;
1827 if (target_desktop != desktop) goto done;
1828 wparam = RIM_INPUTSINK;
1831 if (raw_msg->data.rawinput.type != RIM_TYPEHID || !raw_msg->hid_report) report_size = 0;
1832 else report_size = raw_msg->data.size - sizeof(raw_msg->data);
1834 if (!(msg = alloc_hardware_message( raw_msg->data.info, raw_msg->source, raw_msg->time, report_size )))
1835 goto done;
1837 msg->win = device->target;
1838 msg->msg = raw_msg->message;
1839 msg->wparam = wparam;
1840 msg->lparam = 0;
1841 memcpy( msg->data, &raw_msg->data, sizeof(raw_msg->data) );
1842 if (report_size) memcpy( (struct hardware_msg_data *)msg->data + 1, raw_msg->hid_report, report_size );
1844 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && raw_msg->data.rawinput.type == RIM_TYPEHID)
1846 msg->wparam = raw_msg->data.rawinput.hid.param;
1847 msg->lparam = raw_msg->data.rawinput.hid.device;
1850 queue_hardware_message( desktop, msg, 1 );
1852 done:
1853 if (target_thread) release_object( target_thread );
1854 if (target_desktop) release_object( target_desktop );
1855 if (foreground) release_object( foreground );
1856 if (desktop) release_object( desktop );
1857 return 0;
1860 /* queue a hardware message for a mouse event */
1861 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1862 unsigned int origin, struct msg_queue *sender )
1864 const struct rawinput_device *device;
1865 struct hardware_msg_data *msg_data;
1866 struct rawinput_message raw_msg;
1867 struct message *msg;
1868 struct thread *foreground;
1869 unsigned int i, time, flags;
1870 struct hw_msg_source source = { IMDT_MOUSE, origin };
1871 int wait = 0, x, y;
1873 static const unsigned int messages[] =
1875 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1876 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1877 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1878 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1879 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1880 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1881 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1882 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1883 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1884 0, /* 0x0200 = unused */
1885 0, /* 0x0400 = unused */
1886 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1887 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1890 desktop->cursor.last_change = get_tick_count();
1891 flags = input->mouse.flags;
1892 time = input->mouse.time;
1893 if (!time) time = desktop->cursor.last_change;
1895 if (flags & MOUSEEVENTF_MOVE)
1897 if (flags & MOUSEEVENTF_ABSOLUTE)
1899 x = input->mouse.x;
1900 y = input->mouse.y;
1901 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1902 x == desktop->cursor.x && y == desktop->cursor.y)
1903 flags &= ~MOUSEEVENTF_MOVE;
1905 else
1907 x = desktop->cursor.x + input->mouse.x;
1908 y = desktop->cursor.y + input->mouse.y;
1911 else
1913 x = desktop->cursor.x;
1914 y = desktop->cursor.y;
1917 if ((foreground = get_foreground_thread( desktop, win )))
1919 memset( &raw_msg, 0, sizeof(raw_msg) );
1920 raw_msg.foreground = foreground;
1921 raw_msg.desktop = desktop;
1922 raw_msg.source = source;
1923 raw_msg.time = time;
1924 raw_msg.message = WM_INPUT;
1926 msg_data = &raw_msg.data;
1927 msg_data->info = input->mouse.info;
1928 msg_data->size = sizeof(*msg_data);
1929 msg_data->flags = flags;
1930 msg_data->rawinput.type = RIM_TYPEMOUSE;
1931 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1932 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1933 msg_data->rawinput.mouse.data = input->mouse.data;
1935 enum_processes( queue_rawinput_message, &raw_msg );
1936 release_object( foreground );
1939 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
1941 if (flags & MOUSEEVENTF_MOVE) update_desktop_cursor_pos( desktop, win, x, y );
1942 update_desktop_mouse_state( desktop, flags, input->mouse.data << 16 );
1943 return 0;
1946 for (i = 0; i < ARRAY_SIZE( messages ); i++)
1948 if (!messages[i]) continue;
1949 if (!(flags & (1 << i))) continue;
1950 flags &= ~(1 << i);
1952 if (!(msg = alloc_hardware_message( input->mouse.info, source, time, 0 ))) return 0;
1953 msg_data = msg->data;
1955 msg->win = get_user_full_handle( win );
1956 msg->msg = messages[i];
1957 msg->wparam = input->mouse.data << 16;
1958 msg->lparam = 0;
1959 msg->x = x;
1960 msg->y = y;
1961 if (origin == IMO_INJECTED) msg_data->flags = LLMHF_INJECTED;
1963 /* specify a sender only when sending the last message */
1964 if (!(flags & ((1 << ARRAY_SIZE( messages )) - 1)))
1966 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1967 queue_hardware_message( desktop, msg, 0 );
1969 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1970 queue_hardware_message( desktop, msg, 0 );
1972 return wait;
1975 /* queue a hardware message for a keyboard event */
1976 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1977 unsigned int origin, struct msg_queue *sender )
1979 struct hw_msg_source source = { IMDT_KEYBOARD, origin };
1980 const struct rawinput_device *device;
1981 struct hardware_msg_data *msg_data;
1982 struct rawinput_message raw_msg;
1983 struct message *msg;
1984 struct thread *foreground;
1985 unsigned char vkey = input->kbd.vkey;
1986 unsigned int message_code, time;
1987 int wait;
1989 if (!(time = input->kbd.time)) time = get_tick_count();
1991 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1993 switch (vkey)
1995 case VK_MENU:
1996 case VK_LMENU:
1997 case VK_RMENU:
1998 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1999 break;
2000 case VK_CONTROL:
2001 case VK_LCONTROL:
2002 case VK_RCONTROL:
2003 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
2004 break;
2005 case VK_SHIFT:
2006 case VK_LSHIFT:
2007 case VK_RSHIFT:
2008 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
2009 break;
2013 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
2014 switch (vkey)
2016 case VK_LMENU:
2017 case VK_RMENU:
2018 if (input->kbd.flags & KEYEVENTF_KEYUP)
2020 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
2021 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
2022 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
2023 message_code = WM_SYSKEYUP;
2024 desktop->keystate[VK_MENU] &= ~0x02;
2026 else
2028 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
2029 if (desktop->keystate[VK_CONTROL] & 0x80) break;
2030 message_code = WM_SYSKEYDOWN;
2031 desktop->keystate[VK_MENU] |= 0x02;
2033 break;
2035 case VK_LCONTROL:
2036 case VK_RCONTROL:
2037 /* send WM_SYSKEYUP on release if Alt still pressed */
2038 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
2039 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
2040 message_code = WM_SYSKEYUP;
2041 desktop->keystate[VK_MENU] &= ~0x02;
2042 break;
2044 default:
2045 /* send WM_SYSKEY for Alt-anykey and for F10 */
2046 if (desktop->keystate[VK_CONTROL] & 0x80) break;
2047 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
2048 /* fall through */
2049 case VK_F10:
2050 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
2051 desktop->keystate[VK_MENU] &= ~0x02;
2052 break;
2055 if ((foreground = get_foreground_thread( desktop, win )))
2057 memset( &raw_msg, 0, sizeof(raw_msg) );
2058 raw_msg.foreground = foreground;
2059 raw_msg.desktop = desktop;
2060 raw_msg.source = source;
2061 raw_msg.time = time;
2062 raw_msg.message = WM_INPUT;
2064 msg_data = &raw_msg.data;
2065 msg_data->info = input->kbd.info;
2066 msg_data->size = sizeof(*msg_data);
2067 msg_data->flags = input->kbd.flags;
2068 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
2069 msg_data->rawinput.kbd.message = message_code;
2070 msg_data->rawinput.kbd.vkey = vkey;
2071 msg_data->rawinput.kbd.scan = input->kbd.scan;
2073 enum_processes( queue_rawinput_message, &raw_msg );
2074 release_object( foreground );
2077 if ((device = current->process->rawinput_kbd) && (device->flags & RIDEV_NOLEGACY))
2079 update_input_key_state( desktop, desktop->keystate, message_code, vkey );
2080 return 0;
2083 if (!(msg = alloc_hardware_message( input->kbd.info, source, time, 0 ))) return 0;
2084 msg_data = msg->data;
2086 msg->win = get_user_full_handle( win );
2087 msg->msg = message_code;
2088 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
2089 if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
2091 if (input->kbd.flags & KEYEVENTF_UNICODE && !vkey)
2093 msg->wparam = VK_PACKET;
2095 else
2097 unsigned int flags = 0;
2098 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
2099 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
2100 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
2101 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
2103 msg->wparam = vkey;
2104 msg->lparam |= flags << 16;
2105 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
2108 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
2109 queue_hardware_message( desktop, msg, 1 );
2111 return wait;
2114 /* queue a hardware message for a custom type of event */
2115 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
2116 unsigned int origin, const hw_input_t *input )
2118 struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
2119 struct hardware_msg_data *msg_data;
2120 struct rawinput_message raw_msg;
2121 struct message *msg;
2122 data_size_t report_size = 0;
2124 switch (input->hw.msg)
2126 case WM_INPUT:
2127 case WM_INPUT_DEVICE_CHANGE:
2128 memset( &raw_msg, 0, sizeof(raw_msg) );
2129 raw_msg.source = source;
2130 raw_msg.time = get_tick_count();
2131 raw_msg.message = input->hw.msg;
2133 if (input->hw.rawinput.type == RIM_TYPEHID)
2135 raw_msg.hid_report = get_req_data();
2136 report_size = input->hw.rawinput.hid.length * input->hw.rawinput.hid.count;
2139 if (report_size != get_req_data_size())
2141 set_error( STATUS_INVALID_PARAMETER );
2142 return;
2145 msg_data = &raw_msg.data;
2146 msg_data->size = sizeof(*msg_data) + report_size;
2147 msg_data->rawinput = input->hw.rawinput;
2149 enum_processes( queue_rawinput_message, &raw_msg );
2150 return;
2153 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
2155 msg->win = get_user_full_handle( win );
2156 msg->msg = input->hw.msg;
2157 msg->wparam = 0;
2158 msg->lparam = input->hw.lparam;
2159 msg->x = desktop->cursor.x;
2160 msg->y = desktop->cursor.y;
2162 queue_hardware_message( desktop, msg, 1 );
2165 /* check message filter for a hardware message */
2166 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
2167 user_handle_t filter_win, unsigned int first, unsigned int last )
2169 switch (get_hardware_msg_bit( msg_code ))
2171 case QS_KEY:
2172 /* we can only test the window for a keyboard message since the
2173 * dest window for a mouse message depends on hittest */
2174 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
2175 return 0;
2176 /* the message code is final for a keyboard message, we can simply check it */
2177 return check_msg_filter( msg_code, first, last );
2179 case QS_MOUSEMOVE:
2180 case QS_MOUSEBUTTON:
2181 /* we need to check all possible values that the message can have in the end */
2182 if (check_msg_filter( msg_code, first, last )) return 1;
2183 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
2185 /* all other messages can become non-client messages */
2186 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
2188 /* clicks can become double-clicks or non-client double-clicks */
2189 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
2190 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
2192 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2193 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2195 return 0;
2197 default:
2198 return check_msg_filter( msg_code, first, last );
2202 /* is this message an internal driver notification message */
2203 static inline BOOL is_internal_hardware_message( unsigned int message )
2205 return (message >= WM_WINE_FIRST_DRIVER_MSG && message <= WM_WINE_LAST_DRIVER_MSG);
2208 /* find a hardware message for the given queue */
2209 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
2210 unsigned int first, unsigned int last, unsigned int flags,
2211 struct get_message_reply *reply )
2213 struct thread_input *input = thread->queue->input;
2214 struct thread *win_thread;
2215 struct list *ptr;
2216 user_handle_t win;
2217 int clear_bits, got_one = 0;
2218 unsigned int msg_code;
2220 ptr = list_head( &input->msg_list );
2221 if (hw_id)
2223 while (ptr)
2225 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2226 if (msg->unique_id == hw_id) break;
2227 ptr = list_next( &input->msg_list, ptr );
2229 if (!ptr) ptr = list_head( &input->msg_list );
2230 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
2233 if (ptr == list_head( &input->msg_list ))
2234 clear_bits = QS_INPUT;
2235 else
2236 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
2238 while (ptr)
2240 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2241 struct hardware_msg_data *data = msg->data;
2243 ptr = list_next( &input->msg_list, ptr );
2244 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
2245 if (!win || !win_thread)
2247 /* no window at all, remove it */
2248 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2249 list_remove( &msg->entry );
2250 free_message( msg );
2251 continue;
2253 if (win_thread != thread)
2255 if (win_thread->queue->input == input)
2257 /* wake the other thread */
2258 set_queue_bits( win_thread->queue, get_hardware_msg_bit( msg->msg ) );
2259 got_one = 1;
2261 else
2263 /* for another thread input, drop it */
2264 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2265 list_remove( &msg->entry );
2266 free_message( msg );
2268 release_object( win_thread );
2269 continue;
2271 release_object( win_thread );
2273 /* if we already got a message for another thread, or if it doesn't
2274 * match the filter we skip it */
2275 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
2277 clear_bits &= ~get_hardware_msg_bit( msg->msg );
2278 continue;
2281 reply->total = msg->data_size;
2282 if (msg->data_size > get_reply_max_size())
2284 set_error( STATUS_BUFFER_OVERFLOW );
2285 return 1;
2288 /* now we can return it */
2289 if (!msg->unique_id) msg->unique_id = get_unique_id();
2290 reply->type = MSG_HARDWARE;
2291 reply->win = win;
2292 reply->msg = msg_code;
2293 reply->wparam = msg->wparam;
2294 reply->lparam = msg->lparam;
2295 reply->x = msg->x;
2296 reply->y = msg->y;
2297 reply->time = msg->time;
2299 data->hw_id = msg->unique_id;
2300 set_reply_data( msg->data, msg->data_size );
2301 if ((get_hardware_msg_bit( msg->msg ) == QS_RAWINPUT && (flags & PM_REMOVE)) ||
2302 is_internal_hardware_message( msg->msg ))
2303 release_hardware_message( current->queue, data->hw_id );
2304 return 1;
2306 /* nothing found, clear the hardware queue bits */
2307 clear_queue_bits( thread->queue, clear_bits );
2308 return 0;
2311 /* increment (or decrement if 'incr' is negative) the queue paint count */
2312 void inc_queue_paint_count( struct thread *thread, int incr )
2314 struct msg_queue *queue = thread->queue;
2316 assert( queue );
2318 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2320 if (queue->paint_count)
2321 set_queue_bits( queue, QS_PAINT );
2322 else
2323 clear_queue_bits( queue, QS_PAINT );
2327 /* remove all messages and timers belonging to a certain window */
2328 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2330 struct msg_queue *queue = thread->queue;
2331 struct list *ptr;
2332 int i;
2334 if (!queue) return;
2336 /* remove timers */
2338 ptr = list_head( &queue->pending_timers );
2339 while (ptr)
2341 struct list *next = list_next( &queue->pending_timers, ptr );
2342 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2343 if (timer->win == win) free_timer( queue, timer );
2344 ptr = next;
2346 ptr = list_head( &queue->expired_timers );
2347 while (ptr)
2349 struct list *next = list_next( &queue->expired_timers, ptr );
2350 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2351 if (timer->win == win) free_timer( queue, timer );
2352 ptr = next;
2355 /* remove messages */
2356 for (i = 0; i < NB_MSG_KINDS; i++)
2358 struct list *ptr, *next;
2360 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2362 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2363 if (msg->win == win)
2365 if (msg->msg == WM_QUIT && !queue->quit_message)
2367 queue->quit_message = 1;
2368 queue->exit_code = msg->wparam;
2370 remove_queue_message( queue, msg, i );
2375 thread_input_cleanup_window( queue, win );
2378 /* post a message to a window */
2379 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2381 struct message *msg;
2382 struct thread *thread = get_window_thread( win );
2384 if (!thread) return;
2386 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2388 msg->type = MSG_POSTED;
2389 msg->win = get_user_full_handle( win );
2390 msg->msg = message;
2391 msg->wparam = wparam;
2392 msg->lparam = lparam;
2393 msg->result = NULL;
2394 msg->data = NULL;
2395 msg->data_size = 0;
2397 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2399 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2400 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2401 if (message == WM_HOTKEY)
2403 set_queue_bits( thread->queue, QS_HOTKEY );
2404 thread->queue->hotkey_count++;
2407 release_object( thread );
2410 /* send a notify message to a window */
2411 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2413 struct message *msg;
2414 struct thread *thread = get_window_thread( win );
2416 if (!thread) return;
2418 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2420 msg->type = MSG_NOTIFY;
2421 msg->win = get_user_full_handle( win );
2422 msg->msg = message;
2423 msg->wparam = wparam;
2424 msg->lparam = lparam;
2425 msg->result = NULL;
2426 msg->data = NULL;
2427 msg->data_size = 0;
2429 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2431 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2432 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2434 release_object( thread );
2437 /* post a win event */
2438 void post_win_event( struct thread *thread, unsigned int event,
2439 user_handle_t win, unsigned int object_id,
2440 unsigned int child_id, client_ptr_t hook_proc,
2441 const WCHAR *module, data_size_t module_size,
2442 user_handle_t hook)
2444 struct message *msg;
2446 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2448 struct winevent_msg_data *data;
2450 msg->type = MSG_WINEVENT;
2451 msg->win = get_user_full_handle( win );
2452 msg->msg = event;
2453 msg->wparam = object_id;
2454 msg->lparam = child_id;
2455 msg->time = get_tick_count();
2456 msg->result = NULL;
2458 if ((data = malloc( sizeof(*data) + module_size )))
2460 data->hook = hook;
2461 data->tid = get_thread_id( current );
2462 data->hook_proc = hook_proc;
2463 memcpy( data + 1, module, module_size );
2465 msg->data = data;
2466 msg->data_size = sizeof(*data) + module_size;
2468 if (debug_level > 1)
2469 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2470 get_thread_id(thread), event, win, object_id, child_id );
2471 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2472 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2474 else
2475 free( msg );
2479 /* free all hotkeys on a desktop, optionally filtering by window */
2480 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2482 struct hotkey *hotkey, *hotkey2;
2484 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2486 if (!window || hotkey->win == window)
2488 list_remove( &hotkey->entry );
2489 free( hotkey );
2495 /* check if the thread owning the window is hung */
2496 DECL_HANDLER(is_window_hung)
2498 struct thread *thread;
2500 thread = get_window_thread( req->win );
2501 if (thread)
2503 reply->is_hung = is_queue_hung( thread->queue );
2504 release_object( thread );
2506 else reply->is_hung = 0;
2510 /* get the message queue of the current thread */
2511 DECL_HANDLER(get_msg_queue)
2513 struct msg_queue *queue = get_current_queue();
2515 reply->handle = 0;
2516 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2520 /* set the file descriptor associated to the current thread queue */
2521 DECL_HANDLER(set_queue_fd)
2523 struct msg_queue *queue = get_current_queue();
2524 struct file *file;
2525 int unix_fd;
2527 if (queue->fd) /* fd can only be set once */
2529 set_error( STATUS_ACCESS_DENIED );
2530 return;
2532 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2534 if ((unix_fd = get_file_unix_fd( file )) != -1)
2536 if ((unix_fd = dup( unix_fd )) != -1)
2537 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2538 else
2539 file_set_error();
2541 release_object( file );
2545 /* set the current message queue wakeup mask */
2546 DECL_HANDLER(set_queue_mask)
2548 struct msg_queue *queue = get_current_queue();
2550 if (queue)
2552 queue->wake_mask = req->wake_mask;
2553 queue->changed_mask = req->changed_mask;
2554 reply->wake_bits = queue->wake_bits;
2555 reply->changed_bits = queue->changed_bits;
2556 if (is_signaled( queue ))
2558 /* if skip wait is set, do what would have been done in the subsequent wait */
2559 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2560 else wake_up( &queue->obj, 0 );
2566 /* get the current message queue status */
2567 DECL_HANDLER(get_queue_status)
2569 struct msg_queue *queue = current->queue;
2570 if (queue)
2572 reply->wake_bits = queue->wake_bits;
2573 reply->changed_bits = queue->changed_bits;
2574 queue->changed_bits &= ~req->clear_bits;
2576 else reply->wake_bits = reply->changed_bits = 0;
2580 /* send a message to a thread queue */
2581 DECL_HANDLER(send_message)
2583 struct message *msg;
2584 struct msg_queue *send_queue = get_current_queue();
2585 struct msg_queue *recv_queue = NULL;
2586 struct thread *thread = NULL;
2588 if (!(thread = get_thread_from_id( req->id ))) return;
2590 if (!(recv_queue = thread->queue))
2592 set_error( STATUS_INVALID_PARAMETER );
2593 release_object( thread );
2594 return;
2596 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2598 set_error( STATUS_TIMEOUT );
2599 release_object( thread );
2600 return;
2603 if ((msg = mem_alloc( sizeof(*msg) )))
2605 msg->type = req->type;
2606 msg->win = get_user_full_handle( req->win );
2607 msg->msg = req->msg;
2608 msg->wparam = req->wparam;
2609 msg->lparam = req->lparam;
2610 msg->result = NULL;
2611 msg->data = NULL;
2612 msg->data_size = get_req_data_size();
2614 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2616 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2618 free( msg );
2619 release_object( thread );
2620 return;
2623 switch(msg->type)
2625 case MSG_OTHER_PROCESS:
2626 case MSG_ASCII:
2627 case MSG_UNICODE:
2628 case MSG_CALLBACK:
2629 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2631 free_message( msg );
2632 break;
2634 /* fall through */
2635 case MSG_NOTIFY:
2636 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2637 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2638 break;
2639 case MSG_POSTED:
2640 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2641 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2642 if (msg->msg == WM_HOTKEY)
2644 set_queue_bits( recv_queue, QS_HOTKEY );
2645 recv_queue->hotkey_count++;
2647 break;
2648 case MSG_HARDWARE: /* should use send_hardware_message instead */
2649 case MSG_CALLBACK_RESULT: /* cannot send this one */
2650 case MSG_HOOK_LL: /* generated internally */
2651 default:
2652 set_error( STATUS_INVALID_PARAMETER );
2653 free( msg );
2654 break;
2657 release_object( thread );
2660 /* send a hardware message to a thread queue */
2661 DECL_HANDLER(send_hardware_message)
2663 struct thread *thread = NULL;
2664 struct desktop *desktop;
2665 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2666 struct msg_queue *sender = get_current_queue();
2668 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2670 if (req->win)
2672 if (!(thread = get_window_thread( req->win ))) return;
2673 if (desktop != thread->queue->input->desktop)
2675 /* don't allow queuing events to a different desktop */
2676 release_object( desktop );
2677 return;
2681 reply->prev_x = desktop->cursor.x;
2682 reply->prev_y = desktop->cursor.y;
2684 switch (req->input.type)
2686 case INPUT_MOUSE:
2687 reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2688 break;
2689 case INPUT_KEYBOARD:
2690 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2691 break;
2692 case INPUT_HARDWARE:
2693 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2694 break;
2695 default:
2696 set_error( STATUS_INVALID_PARAMETER );
2698 if (thread) release_object( thread );
2700 reply->new_x = desktop->cursor.x;
2701 reply->new_y = desktop->cursor.y;
2702 release_object( desktop );
2705 /* post a quit message to the current queue */
2706 DECL_HANDLER(post_quit_message)
2708 struct msg_queue *queue = get_current_queue();
2710 if (!queue)
2711 return;
2713 queue->quit_message = 1;
2714 queue->exit_code = req->exit_code;
2715 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2718 /* get a message from the current queue */
2719 DECL_HANDLER(get_message)
2721 struct timer *timer;
2722 struct list *ptr;
2723 struct msg_queue *queue = get_current_queue();
2724 user_handle_t get_win = get_user_full_handle( req->get_win );
2725 unsigned int filter = req->flags >> 16;
2727 reply->active_hooks = get_active_hooks();
2729 if (get_win && get_win != 1 && get_win != -1 && !get_user_object( get_win, USER_WINDOW ))
2731 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2732 return;
2735 if (!queue) return;
2736 queue->last_get_msg = current_time;
2737 if (!filter) filter = QS_ALLINPUT;
2739 /* first check for sent messages */
2740 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2742 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2743 receive_message( queue, msg, reply );
2744 return;
2747 /* clear changed bits so we can wait on them if we don't find a message */
2748 if (filter & QS_POSTMESSAGE)
2750 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2751 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2753 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2754 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2756 /* then check for posted messages */
2757 if ((filter & QS_POSTMESSAGE) &&
2758 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2759 return;
2761 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2762 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2763 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2764 return;
2766 /* only check for quit messages if not posted messages pending */
2767 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2768 return;
2770 /* then check for any raw hardware message */
2771 if ((filter & QS_INPUT) &&
2772 filter_contains_hw_range( req->get_first, req->get_last ) &&
2773 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2774 return;
2776 /* check for any internal driver message */
2777 if (get_hardware_message( current, req->hw_id, get_win, WM_WINE_FIRST_DRIVER_MSG,
2778 WM_WINE_LAST_DRIVER_MSG, req->flags, reply ))
2779 return;
2781 /* now check for WM_PAINT */
2782 if ((filter & QS_PAINT) &&
2783 queue->paint_count &&
2784 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2785 (reply->win = find_window_to_repaint( get_win, current )))
2787 reply->type = MSG_POSTED;
2788 reply->msg = WM_PAINT;
2789 reply->wparam = 0;
2790 reply->lparam = 0;
2791 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2792 return;
2795 /* now check for timer */
2796 if ((filter & QS_TIMER) &&
2797 (timer = find_expired_timer( queue, get_win, req->get_first,
2798 req->get_last, (req->flags & PM_REMOVE) )))
2800 reply->type = MSG_POSTED;
2801 reply->win = timer->win;
2802 reply->msg = timer->msg;
2803 reply->wparam = timer->id;
2804 reply->lparam = timer->lparam;
2805 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2806 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2807 set_event( current->process->idle_event );
2808 return;
2811 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2812 queue->wake_mask = req->wake_mask;
2813 queue->changed_mask = req->changed_mask;
2814 set_error( STATUS_PENDING ); /* FIXME */
2818 /* reply to a sent message */
2819 DECL_HANDLER(reply_message)
2821 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2822 else if (current->queue->recv_result)
2823 reply_message( current->queue, req->result, 0, req->remove,
2824 get_req_data(), get_req_data_size() );
2828 /* accept the current hardware message */
2829 DECL_HANDLER(accept_hardware_message)
2831 if (current->queue)
2832 release_hardware_message( current->queue, req->hw_id );
2833 else
2834 set_error( STATUS_ACCESS_DENIED );
2838 /* retrieve the reply for the last message sent */
2839 DECL_HANDLER(get_message_reply)
2841 struct message_result *result;
2842 struct list *entry;
2843 struct msg_queue *queue = current->queue;
2845 if (queue)
2847 set_error( STATUS_PENDING );
2848 reply->result = 0;
2850 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2852 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2853 if (result->replied || req->cancel)
2855 if (result->replied)
2857 reply->result = result->result;
2858 set_error( result->error );
2859 if (result->data)
2861 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2862 set_reply_data_ptr( result->data, data_len );
2863 result->data = NULL;
2864 result->data_size = 0;
2867 remove_result_from_sender( result );
2869 entry = list_head( &queue->send_result );
2870 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2871 else
2873 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2874 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2875 else clear_queue_bits( queue, QS_SMRESULT );
2879 else set_error( STATUS_ACCESS_DENIED );
2883 /* set a window timer */
2884 DECL_HANDLER(set_win_timer)
2886 struct timer *timer;
2887 struct msg_queue *queue;
2888 struct thread *thread = NULL;
2889 user_handle_t win = 0;
2890 lparam_t id = req->id;
2892 if (req->win)
2894 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2896 set_error( STATUS_INVALID_HANDLE );
2897 return;
2899 if (thread->process != current->process)
2901 release_object( thread );
2902 set_error( STATUS_ACCESS_DENIED );
2903 return;
2905 queue = thread->queue;
2906 /* remove it if it existed already */
2907 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2909 else
2911 queue = get_current_queue();
2912 /* look for a timer with this id */
2913 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2915 /* free and reuse id */
2916 free_timer( queue, timer );
2918 else
2920 lparam_t end_id = queue->next_timer_id;
2922 /* find a free id for it */
2923 while (1)
2925 id = queue->next_timer_id;
2926 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2928 if (!find_timer( queue, 0, req->msg, id )) break;
2929 if (queue->next_timer_id == end_id)
2931 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2932 return;
2938 if ((timer = set_timer( queue, req->rate )))
2940 timer->win = win;
2941 timer->msg = req->msg;
2942 timer->id = id;
2943 timer->lparam = req->lparam;
2944 reply->id = id;
2946 if (thread) release_object( thread );
2949 /* kill a window timer */
2950 DECL_HANDLER(kill_win_timer)
2952 struct timer *timer;
2953 struct thread *thread;
2954 user_handle_t win = 0;
2956 if (req->win)
2958 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2960 set_error( STATUS_INVALID_HANDLE );
2961 return;
2963 if (thread->process != current->process)
2965 release_object( thread );
2966 set_error( STATUS_ACCESS_DENIED );
2967 return;
2970 else thread = (struct thread *)grab_object( current );
2972 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2973 free_timer( thread->queue, timer );
2974 else
2975 set_error( STATUS_INVALID_PARAMETER );
2977 release_object( thread );
2980 DECL_HANDLER(register_hotkey)
2982 struct desktop *desktop;
2983 user_handle_t win_handle = req->window;
2984 struct hotkey *hotkey;
2985 struct hotkey *new_hotkey = NULL;
2986 struct thread *thread;
2987 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2989 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2991 if (win_handle)
2993 if (!(win_handle = get_valid_window_handle( win_handle )))
2995 release_object( desktop );
2996 return;
2999 thread = get_window_thread( win_handle );
3000 if (thread) release_object( thread );
3002 if (thread != current)
3004 release_object( desktop );
3005 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
3006 return;
3010 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
3012 if (req->vkey == hotkey->vkey &&
3013 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
3015 release_object( desktop );
3016 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
3017 return;
3019 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
3020 new_hotkey = hotkey;
3023 if (new_hotkey)
3025 reply->replaced = 1;
3026 reply->flags = new_hotkey->flags;
3027 reply->vkey = new_hotkey->vkey;
3029 else
3031 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
3032 if (new_hotkey)
3034 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
3035 new_hotkey->queue = current->queue;
3036 new_hotkey->win = win_handle;
3037 new_hotkey->id = req->id;
3041 if (new_hotkey)
3043 new_hotkey->flags = req->flags;
3044 new_hotkey->vkey = req->vkey;
3047 release_object( desktop );
3050 DECL_HANDLER(unregister_hotkey)
3052 struct desktop *desktop;
3053 user_handle_t win_handle = req->window;
3054 struct hotkey *hotkey;
3055 struct thread *thread;
3057 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3059 if (win_handle)
3061 if (!(win_handle = get_valid_window_handle( win_handle )))
3063 release_object( desktop );
3064 return;
3067 thread = get_window_thread( win_handle );
3068 if (thread) release_object( thread );
3070 if (thread != current)
3072 release_object( desktop );
3073 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
3074 return;
3078 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
3080 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
3081 goto found;
3084 release_object( desktop );
3085 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
3086 return;
3088 found:
3089 reply->flags = hotkey->flags;
3090 reply->vkey = hotkey->vkey;
3091 list_remove( &hotkey->entry );
3092 free( hotkey );
3093 release_object( desktop );
3096 /* attach (or detach) thread inputs */
3097 DECL_HANDLER(attach_thread_input)
3099 struct thread *thread_from = get_thread_from_id( req->tid_from );
3100 struct thread *thread_to = get_thread_from_id( req->tid_to );
3102 if (!thread_from || !thread_to)
3104 if (thread_from) release_object( thread_from );
3105 if (thread_to) release_object( thread_to );
3106 return;
3108 if (thread_from != thread_to)
3110 if (req->attach)
3112 if ((thread_to->queue || thread_to == current) &&
3113 (thread_from->queue || thread_from == current))
3114 attach_thread_input( thread_from, thread_to );
3115 else
3116 set_error( STATUS_INVALID_PARAMETER );
3118 else
3120 if (thread_from->queue && thread_to->queue &&
3121 thread_from->queue->input == thread_to->queue->input)
3122 detach_thread_input( thread_from );
3123 else
3124 set_error( STATUS_ACCESS_DENIED );
3127 else set_error( STATUS_ACCESS_DENIED );
3128 release_object( thread_from );
3129 release_object( thread_to );
3133 /* get thread input data */
3134 DECL_HANDLER(get_thread_input)
3136 struct thread *thread = NULL;
3137 struct desktop *desktop;
3138 struct thread_input *input;
3140 if (req->tid)
3142 if (!(thread = get_thread_from_id( req->tid ))) return;
3143 if (!(desktop = get_thread_desktop( thread, 0 )))
3145 release_object( thread );
3146 return;
3148 input = thread->queue ? thread->queue->input : NULL;
3150 else
3152 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3153 input = desktop->foreground_input; /* get the foreground thread info */
3156 if (input)
3158 reply->focus = input->focus;
3159 reply->capture = input->capture;
3160 reply->active = input->active;
3161 reply->menu_owner = input->menu_owner;
3162 reply->move_size = input->move_size;
3163 reply->caret = input->caret;
3164 reply->cursor = input->cursor;
3165 reply->show_count = input->cursor_count;
3166 reply->rect = input->caret_rect;
3169 /* foreground window is active window of foreground thread */
3170 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
3171 if (thread) release_object( thread );
3172 release_object( desktop );
3176 /* retrieve queue keyboard state for current thread or global async state */
3177 DECL_HANDLER(get_key_state)
3179 struct desktop *desktop;
3180 data_size_t size = min( 256, get_reply_max_size() );
3182 if (req->async) /* get global async key state */
3184 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3185 if (req->key >= 0)
3187 reply->state = desktop->keystate[req->key & 0xff];
3188 desktop->keystate[req->key & 0xff] &= ~0x40;
3190 set_reply_data( desktop->keystate, size );
3191 release_object( desktop );
3193 else
3195 struct msg_queue *queue = get_current_queue();
3196 unsigned char *keystate = queue->input->keystate;
3197 if (req->key >= 0)
3199 sync_input_keystate( queue->input );
3200 reply->state = keystate[req->key & 0xff];
3202 set_reply_data( keystate, size );
3207 /* set queue keyboard state for current thread */
3208 DECL_HANDLER(set_key_state)
3210 struct desktop *desktop;
3211 struct msg_queue *queue = get_current_queue();
3212 data_size_t size = min( 256, get_req_data_size() );
3214 memcpy( queue->input->keystate, get_req_data(), size );
3215 memcpy( queue->input->desktop_keystate, queue->input->desktop->keystate, 256 );
3216 if (req->async && (desktop = get_thread_desktop( current, 0 )))
3218 memcpy( desktop->keystate, get_req_data(), size );
3219 release_object( desktop );
3224 /* set the system foreground window */
3225 DECL_HANDLER(set_foreground_window)
3227 struct thread *thread = NULL;
3228 struct desktop *desktop;
3229 struct msg_queue *queue = get_current_queue();
3231 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3232 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
3233 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
3234 reply->send_msg_new = FALSE;
3236 if (is_valid_foreground_window( req->handle ) &&
3237 (thread = get_window_thread( req->handle )) &&
3238 thread->queue->input->desktop == desktop)
3240 set_foreground_input( desktop, thread->queue->input );
3241 reply->send_msg_new = (desktop->foreground_input != queue->input);
3243 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
3245 if (thread) release_object( thread );
3246 release_object( desktop );
3250 /* set the current thread focus window */
3251 DECL_HANDLER(set_focus_window)
3253 struct msg_queue *queue = get_current_queue();
3255 reply->previous = 0;
3256 if (queue && check_queue_input_window( queue, req->handle ))
3258 reply->previous = queue->input->focus;
3259 queue->input->focus = get_user_full_handle( req->handle );
3264 /* set the current thread active window */
3265 DECL_HANDLER(set_active_window)
3267 struct msg_queue *queue = get_current_queue();
3269 reply->previous = 0;
3270 if (queue && check_queue_input_window( queue, req->handle ))
3272 if (!req->handle || make_window_active( req->handle ))
3274 reply->previous = queue->input->active;
3275 queue->input->active = get_user_full_handle( req->handle );
3277 else set_error( STATUS_INVALID_HANDLE );
3282 /* set the current thread capture window */
3283 DECL_HANDLER(set_capture_window)
3285 struct msg_queue *queue = get_current_queue();
3287 reply->previous = reply->full_handle = 0;
3288 if (queue && check_queue_input_window( queue, req->handle ))
3290 struct thread_input *input = queue->input;
3292 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
3293 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
3295 set_error(STATUS_ACCESS_DENIED);
3296 return;
3298 reply->previous = input->capture;
3299 input->capture = get_user_full_handle( req->handle );
3300 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
3301 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
3302 reply->full_handle = input->capture;
3307 /* Set the current thread caret window */
3308 DECL_HANDLER(set_caret_window)
3310 struct msg_queue *queue = get_current_queue();
3312 reply->previous = 0;
3313 if (queue && check_queue_input_window( queue, req->handle ))
3315 struct thread_input *input = queue->input;
3317 reply->previous = input->caret;
3318 reply->old_rect = input->caret_rect;
3319 reply->old_hide = input->caret_hide;
3320 reply->old_state = input->caret_state;
3322 set_caret_window( input, get_user_full_handle(req->handle) );
3323 input->caret_rect.right = input->caret_rect.left + req->width;
3324 input->caret_rect.bottom = input->caret_rect.top + req->height;
3329 /* Set the current thread caret information */
3330 DECL_HANDLER(set_caret_info)
3332 struct msg_queue *queue = get_current_queue();
3333 struct thread_input *input;
3335 if (!queue) return;
3336 input = queue->input;
3337 reply->full_handle = input->caret;
3338 reply->old_rect = input->caret_rect;
3339 reply->old_hide = input->caret_hide;
3340 reply->old_state = input->caret_state;
3342 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3344 set_error( STATUS_ACCESS_DENIED );
3345 return;
3347 if (req->flags & SET_CARET_POS)
3349 input->caret_rect.right += req->x - input->caret_rect.left;
3350 input->caret_rect.bottom += req->y - input->caret_rect.top;
3351 input->caret_rect.left = req->x;
3352 input->caret_rect.top = req->y;
3354 if (req->flags & SET_CARET_HIDE)
3356 input->caret_hide += req->hide;
3357 if (input->caret_hide < 0) input->caret_hide = 0;
3359 if (req->flags & SET_CARET_STATE)
3361 switch (req->state)
3363 case CARET_STATE_OFF: input->caret_state = 0; break;
3364 case CARET_STATE_ON: input->caret_state = 1; break;
3365 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3366 case CARET_STATE_ON_IF_MOVED:
3367 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3368 break;
3374 /* get the time of the last input event */
3375 DECL_HANDLER(get_last_input_time)
3377 reply->time = last_input_time;
3380 /* set/get the current cursor */
3381 DECL_HANDLER(set_cursor)
3383 struct msg_queue *queue = get_current_queue();
3384 struct thread_input *input;
3385 struct desktop *desktop;
3387 if (!queue) return;
3388 input = queue->input;
3389 desktop = input->desktop;
3391 reply->prev_handle = input->cursor;
3392 reply->prev_count = input->cursor_count;
3393 reply->prev_x = desktop->cursor.x;
3394 reply->prev_y = desktop->cursor.y;
3396 if (req->flags & SET_CURSOR_HANDLE)
3398 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3400 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3401 return;
3403 input->cursor = req->handle;
3405 if (req->flags & SET_CURSOR_COUNT)
3407 queue->cursor_count += req->show_count;
3408 input->cursor_count += req->show_count;
3410 if (req->flags & SET_CURSOR_POS) set_cursor_pos( desktop, req->x, req->y );
3411 if (req->flags & SET_CURSOR_CLIP) set_clip_rectangle( desktop, &req->clip, req->flags, 0 );
3412 if (req->flags & SET_CURSOR_NOCLIP) set_clip_rectangle( desktop, NULL, SET_CURSOR_NOCLIP, 0 );
3414 if (req->flags & (SET_CURSOR_HANDLE | SET_CURSOR_COUNT))
3415 update_desktop_cursor_handle( desktop, input );
3417 reply->new_x = desktop->cursor.x;
3418 reply->new_y = desktop->cursor.y;
3419 reply->new_clip = desktop->cursor.clip;
3420 reply->last_change = desktop->cursor.last_change;
3423 /* Get the history of the 64 last cursor positions */
3424 DECL_HANDLER(get_cursor_history)
3426 cursor_pos_t *pos;
3427 unsigned int i, count = min( 64, get_reply_max_size() / sizeof(*pos) );
3429 if ((pos = set_reply_data_size( count * sizeof(*pos) )))
3430 for (i = 0; i < count; i++)
3431 pos[i] = cursor_history[(i + cursor_history_latest) % ARRAY_SIZE(cursor_history)];
3434 DECL_HANDLER(get_rawinput_buffer)
3436 struct thread_input *input = current->queue->input;
3437 data_size_t size = 0, next_size = 0, pos = 0;
3438 struct list *ptr;
3439 char *buf, *tmp;
3440 int count = 0, buf_size = 16 * sizeof(struct hardware_msg_data);
3442 if (!req->buffer_size) buf = NULL;
3443 else if (!(buf = mem_alloc( buf_size ))) return;
3445 ptr = list_head( &input->msg_list );
3446 while (ptr)
3448 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
3449 struct hardware_msg_data *data = msg->data;
3450 data_size_t extra_size = data->size - sizeof(*data);
3452 ptr = list_next( &input->msg_list, ptr );
3453 if (msg->msg != WM_INPUT) continue;
3455 next_size = req->rawinput_size + extra_size;
3456 if (size + next_size > req->buffer_size) break;
3457 if (pos + data->size > get_reply_max_size()) break;
3458 if (pos + data->size > buf_size)
3460 buf_size += buf_size / 2 + extra_size;
3461 if (!(tmp = realloc( buf, buf_size )))
3463 free( buf );
3464 set_error( STATUS_NO_MEMORY );
3465 return;
3467 buf = tmp;
3470 memcpy( buf + pos, data, data->size );
3471 list_remove( &msg->entry );
3472 free_message( msg );
3474 size += next_size;
3475 pos += sizeof(*data) + extra_size;
3476 count++;
3479 reply->next_size = next_size;
3480 reply->count = count;
3481 set_reply_data_ptr( buf, pos );
3484 DECL_HANDLER(update_rawinput_devices)
3486 const struct rawinput_device *tmp, *devices = get_req_data();
3487 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3488 size_t size = device_count * sizeof(*devices);
3489 struct process *process = current->process;
3491 if (!size)
3493 process->rawinput_device_count = 0;
3494 process->rawinput_mouse = NULL;
3495 process->rawinput_kbd = NULL;
3496 return;
3499 if (!(tmp = realloc( process->rawinput_devices, size )))
3501 set_error( STATUS_NO_MEMORY );
3502 return;
3504 process->rawinput_devices = (struct rawinput_device *)tmp;
3505 process->rawinput_device_count = device_count;
3506 memcpy( process->rawinput_devices, devices, size );
3508 process->rawinput_mouse = find_rawinput_device( process, 1, 2 );
3509 process->rawinput_kbd = find_rawinput_device( process, 1, 6 );