dmime: Translate some DMUS_CURVE_PMSG messages to MIDI.
[wine.git] / server / queue.c
blob0558bd111f9ada9ea4eb9142c70ed1025a5d4315
1 /*
2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <poll.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winternl.h"
37 #include "ntuser.h"
39 #include "handle.h"
40 #include "file.h"
41 #include "thread.h"
42 #include "process.h"
43 #include "request.h"
44 #include "user.h"
46 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
47 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
49 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
50 #define NB_MSG_KINDS (POST_MESSAGE+1)
53 struct message_result
55 struct list sender_entry; /* entry in sender list */
56 struct message *msg; /* message the result is for */
57 struct message_result *recv_next; /* next in receiver list */
58 struct msg_queue *sender; /* sender queue */
59 struct msg_queue *receiver; /* receiver queue */
60 int replied; /* has it been replied to? */
61 unsigned int error; /* error code to pass back to sender */
62 lparam_t result; /* reply result */
63 struct message *hardware_msg; /* hardware message if low-level hook result */
64 struct desktop *desktop; /* desktop for hardware message */
65 struct message *callback_msg; /* message to queue for callback */
66 void *data; /* message reply data */
67 unsigned int data_size; /* size of message reply data */
68 struct timeout_user *timeout; /* result timeout */
71 struct message
73 struct list entry; /* entry in message list */
74 enum message_type type; /* message type */
75 user_handle_t win; /* window handle */
76 unsigned int msg; /* message code */
77 lparam_t wparam; /* parameters */
78 lparam_t lparam; /* parameters */
79 int x; /* message position */
80 int y;
81 unsigned int time; /* message time */
82 void *data; /* message data for sent messages */
83 unsigned int data_size; /* size of message data */
84 unsigned int unique_id; /* unique id for nested hw message waits */
85 struct message_result *result; /* result in sender queue */
88 struct timer
90 struct list entry; /* entry in timer list */
91 abstime_t when; /* next expiration */
92 unsigned int rate; /* timer rate in ms */
93 user_handle_t win; /* window handle */
94 unsigned int msg; /* message to post */
95 lparam_t id; /* timer id */
96 lparam_t lparam; /* lparam for message */
99 struct thread_input
101 struct object obj; /* object header */
102 struct desktop *desktop; /* desktop that this thread input belongs to */
103 user_handle_t focus; /* focus window */
104 user_handle_t capture; /* capture window */
105 user_handle_t active; /* active window */
106 user_handle_t menu_owner; /* current menu owner window */
107 user_handle_t move_size; /* current moving/resizing window */
108 user_handle_t caret; /* caret window */
109 rectangle_t caret_rect; /* caret rectangle */
110 int caret_hide; /* caret hide count */
111 int caret_state; /* caret on/off state */
112 user_handle_t cursor; /* current cursor */
113 int cursor_count; /* cursor show count */
114 struct list msg_list; /* list of hardware messages */
115 unsigned char keystate[256]; /* state of each key */
116 unsigned char desktop_keystate[256]; /* desktop keystate when keystate was synced */
117 int keystate_lock; /* keystate is locked */
120 struct msg_queue
122 struct object obj; /* object header */
123 struct fd *fd; /* optional file descriptor to poll */
124 unsigned int wake_bits; /* wakeup bits */
125 unsigned int wake_mask; /* wakeup mask */
126 unsigned int changed_bits; /* changed wakeup bits */
127 unsigned int changed_mask; /* changed wakeup mask */
128 int paint_count; /* pending paint messages count */
129 int hotkey_count; /* pending hotkey messages count */
130 int quit_message; /* is there a pending quit message? */
131 int exit_code; /* exit code of pending quit message */
132 int cursor_count; /* per-queue cursor show count */
133 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
134 struct list send_result; /* stack of sent messages waiting for result */
135 struct list callback_result; /* list of callback messages waiting for result */
136 struct message_result *recv_result; /* stack of received messages waiting for result */
137 struct list pending_timers; /* list of pending timers */
138 struct list expired_timers; /* list of expired timers */
139 lparam_t next_timer_id; /* id for the next timer with a 0 window */
140 struct timeout_user *timeout; /* timeout for next timer to expire */
141 struct thread_input *input; /* thread input descriptor */
142 struct hook_table *hooks; /* hook table */
143 timeout_t last_get_msg; /* time of last get message call */
144 int keystate_lock; /* owns an input keystate lock */
147 struct hotkey
149 struct list entry; /* entry in desktop hotkey list */
150 struct msg_queue *queue; /* queue owning this hotkey */
151 user_handle_t win; /* window handle */
152 int id; /* hotkey id */
153 unsigned int vkey; /* virtual key code */
154 unsigned int flags; /* key modifiers */
157 static void msg_queue_dump( struct object *obj, int verbose );
158 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
159 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
160 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
161 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
162 static void msg_queue_destroy( struct object *obj );
163 static void msg_queue_poll_event( struct fd *fd, int event );
164 static void thread_input_dump( struct object *obj, int verbose );
165 static void thread_input_destroy( struct object *obj );
166 static void timer_callback( void *private );
168 static const struct object_ops msg_queue_ops =
170 sizeof(struct msg_queue), /* size */
171 &no_type, /* type */
172 msg_queue_dump, /* dump */
173 msg_queue_add_queue, /* add_queue */
174 msg_queue_remove_queue, /* remove_queue */
175 msg_queue_signaled, /* signaled */
176 msg_queue_satisfied, /* satisfied */
177 no_signal, /* signal */
178 no_get_fd, /* get_fd */
179 default_map_access, /* map_access */
180 default_get_sd, /* get_sd */
181 default_set_sd, /* set_sd */
182 no_get_full_name, /* get_full_name */
183 no_lookup_name, /* lookup_name */
184 no_link_name, /* link_name */
185 NULL, /* unlink_name */
186 no_open_file, /* open_file */
187 no_kernel_obj_list, /* get_kernel_obj_list */
188 no_close_handle, /* close_handle */
189 msg_queue_destroy /* destroy */
192 static const struct fd_ops msg_queue_fd_ops =
194 NULL, /* get_poll_events */
195 msg_queue_poll_event, /* poll_event */
196 NULL, /* flush */
197 NULL, /* get_fd_type */
198 NULL, /* ioctl */
199 NULL, /* queue_async */
200 NULL, /* reselect_async */
201 NULL /* cancel async */
205 static const struct object_ops thread_input_ops =
207 sizeof(struct thread_input), /* size */
208 &no_type, /* type */
209 thread_input_dump, /* dump */
210 no_add_queue, /* add_queue */
211 NULL, /* remove_queue */
212 NULL, /* signaled */
213 NULL, /* satisfied */
214 no_signal, /* signal */
215 no_get_fd, /* get_fd */
216 default_map_access, /* map_access */
217 default_get_sd, /* get_sd */
218 default_set_sd, /* set_sd */
219 no_get_full_name, /* get_full_name */
220 no_lookup_name, /* lookup_name */
221 no_link_name, /* link_name */
222 NULL, /* unlink_name */
223 no_open_file, /* open_file */
224 no_kernel_obj_list, /* get_kernel_obj_list */
225 no_close_handle, /* close_handle */
226 thread_input_destroy /* destroy */
229 /* pointer to input structure of foreground thread */
230 static unsigned int last_input_time;
232 static cursor_pos_t cursor_history[64];
233 static unsigned int cursor_history_latest;
235 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
236 static void free_message( struct message *msg );
238 /* set the caret window in a given thread input */
239 static void set_caret_window( struct thread_input *input, user_handle_t win )
241 if (!win || win != input->caret)
243 input->caret_rect.left = 0;
244 input->caret_rect.top = 0;
245 input->caret_rect.right = 0;
246 input->caret_rect.bottom = 0;
248 input->caret = win;
249 input->caret_hide = 1;
250 input->caret_state = 0;
253 /* create a thread input object */
254 static struct thread_input *create_thread_input( struct thread *thread )
256 struct thread_input *input;
258 if ((input = alloc_object( &thread_input_ops )))
260 input->focus = 0;
261 input->capture = 0;
262 input->active = 0;
263 input->menu_owner = 0;
264 input->move_size = 0;
265 input->cursor = 0;
266 input->cursor_count = 0;
267 list_init( &input->msg_list );
268 set_caret_window( input, 0 );
269 memset( input->keystate, 0, sizeof(input->keystate) );
270 input->keystate_lock = 0;
272 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
274 release_object( input );
275 return NULL;
277 memcpy( input->desktop_keystate, input->desktop->keystate, sizeof(input->desktop_keystate) );
279 return input;
282 /* create a message queue object */
283 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
285 struct thread_input *new_input = NULL;
286 struct msg_queue *queue;
287 int i;
289 if (!input)
291 if (!(new_input = create_thread_input( thread ))) return NULL;
292 input = new_input;
295 if ((queue = alloc_object( &msg_queue_ops )))
297 queue->fd = NULL;
298 queue->wake_bits = 0;
299 queue->wake_mask = 0;
300 queue->changed_bits = 0;
301 queue->changed_mask = 0;
302 queue->paint_count = 0;
303 queue->hotkey_count = 0;
304 queue->quit_message = 0;
305 queue->cursor_count = 0;
306 queue->recv_result = NULL;
307 queue->next_timer_id = 0x7fff;
308 queue->timeout = NULL;
309 queue->input = (struct thread_input *)grab_object( input );
310 queue->hooks = NULL;
311 queue->last_get_msg = current_time;
312 queue->keystate_lock = 0;
313 list_init( &queue->send_result );
314 list_init( &queue->callback_result );
315 list_init( &queue->pending_timers );
316 list_init( &queue->expired_timers );
317 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
319 thread->queue = queue;
321 if (new_input) release_object( new_input );
322 return queue;
325 /* free the message queue of a thread at thread exit */
326 void free_msg_queue( struct thread *thread )
328 remove_thread_hooks( thread );
329 if (!thread->queue) return;
330 release_object( thread->queue );
331 thread->queue = NULL;
334 /* synchronize thread input keystate with the desktop */
335 static void sync_input_keystate( struct thread_input *input )
337 int i;
338 if (!input->desktop || input->keystate_lock) return;
339 for (i = 0; i < sizeof(input->keystate); ++i)
341 if (input->desktop_keystate[i] == input->desktop->keystate[i]) continue;
342 input->keystate[i] = input->desktop_keystate[i] = input->desktop->keystate[i];
346 /* locks thread input keystate to prevent synchronization */
347 static void lock_input_keystate( struct thread_input *input )
349 input->keystate_lock++;
352 /* unlock the thread input keystate and synchronize it again */
353 static void unlock_input_keystate( struct thread_input *input )
355 input->keystate_lock--;
356 if (!input->keystate_lock) sync_input_keystate( input );
359 /* change the thread input data of a given thread */
360 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
362 struct msg_queue *queue = thread->queue;
364 if (!queue)
366 thread->queue = create_msg_queue( thread, new_input );
367 return thread->queue != NULL;
369 if (queue->input)
371 queue->input->cursor_count -= queue->cursor_count;
372 if (queue->keystate_lock) unlock_input_keystate( queue->input );
373 release_object( queue->input );
375 queue->input = (struct thread_input *)grab_object( new_input );
376 if (queue->keystate_lock) lock_input_keystate( queue->input );
377 new_input->cursor_count += queue->cursor_count;
378 return 1;
381 /* allocate a hardware message and its data */
382 static struct message *alloc_hardware_message( lparam_t info, struct hw_msg_source source,
383 unsigned int time, data_size_t extra_size )
385 struct hardware_msg_data *msg_data;
386 struct message *msg;
388 if (!(msg = mem_alloc( sizeof(*msg) ))) return NULL;
389 if (!(msg_data = mem_alloc( sizeof(*msg_data) + extra_size )))
391 free( msg );
392 return NULL;
394 memset( msg, 0, sizeof(*msg) );
395 msg->type = MSG_HARDWARE;
396 msg->time = time;
397 msg->data = msg_data;
398 msg->data_size = sizeof(*msg_data) + extra_size;
400 memset( msg_data, 0, sizeof(*msg_data) + extra_size );
401 msg_data->info = info;
402 msg_data->size = msg->data_size;
403 msg_data->source = source;
404 return msg;
407 static int is_cursor_clipped( struct desktop *desktop )
409 rectangle_t top_rect, clip_rect = desktop->cursor.clip;
410 get_top_window_rectangle( desktop, &top_rect );
411 return !is_rect_equal( &clip_rect, &top_rect );
414 static void queue_cursor_message( struct desktop *desktop, user_handle_t win, unsigned int message,
415 lparam_t wparam, lparam_t lparam )
417 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
418 struct thread_input *input;
419 struct message *msg;
421 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
423 msg->msg = message;
424 msg->wparam = wparam;
425 msg->lparam = lparam;
426 msg->x = desktop->cursor.x;
427 msg->y = desktop->cursor.y;
428 if (!(msg->win = win) && (input = desktop->foreground_input)) msg->win = input->active;
429 queue_hardware_message( desktop, msg, 1 );
432 static int update_desktop_cursor_window( struct desktop *desktop, user_handle_t win )
434 int updated = win != desktop->cursor.win;
435 user_handle_t handle = desktop->cursor.handle;
436 desktop->cursor.win = win;
437 if (updated)
439 struct thread *thread;
441 if ((thread = get_window_thread( win )))
443 struct thread_input *input = thread->queue->input;
444 if (input) handle = input->cursor_count < 0 ? 0 : input->cursor;
445 release_object( thread );
448 /* when clipping send the message to the foreground window as well, as some driver have an artificial overlay window */
449 if (is_cursor_clipped( desktop )) queue_cursor_message( desktop, 0, WM_WINE_SETCURSOR, win, handle );
450 queue_cursor_message( desktop, win, WM_WINE_SETCURSOR, win, handle );
452 return updated;
455 static int update_desktop_cursor_pos( struct desktop *desktop, user_handle_t win, int x, int y )
457 struct thread_input *input;
458 int updated;
460 x = max( min( x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
461 y = max( min( y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
462 updated = (desktop->cursor.x != x || desktop->cursor.y != y);
463 desktop->cursor.x = x;
464 desktop->cursor.y = y;
465 desktop->cursor.last_change = get_tick_count();
467 if (!win && (input = desktop->foreground_input)) win = input->capture;
468 if (!win || !is_window_visible( win ) || is_window_transparent( win ))
469 win = shallow_window_from_point( desktop, x, y );
470 if (update_desktop_cursor_window( desktop, win )) updated = 1;
472 return updated;
475 static void update_desktop_cursor_handle( struct desktop *desktop, user_handle_t handle )
477 int updated = desktop->cursor.handle != handle;
478 user_handle_t win = desktop->cursor.win;
479 desktop->cursor.handle = handle;
480 if (updated)
482 /* when clipping send the message to the foreground window as well, as some driver have an artificial overlay window */
483 if (is_cursor_clipped( desktop )) queue_cursor_message( desktop, 0, WM_WINE_SETCURSOR, win, handle );
484 queue_cursor_message( desktop, win, WM_WINE_SETCURSOR, win, handle );
488 /* set the cursor position and queue the corresponding mouse message */
489 static void set_cursor_pos( struct desktop *desktop, int x, int y )
491 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
492 const struct rawinput_device *device;
493 struct message *msg;
495 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
497 update_desktop_cursor_pos( desktop, 0, x, y );
498 return;
501 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
503 msg->msg = WM_MOUSEMOVE;
504 msg->x = x;
505 msg->y = y;
506 queue_hardware_message( desktop, msg, 1 );
509 /* retrieve default position and time for synthesized messages */
510 static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
512 struct desktop *desktop = queue->input->desktop;
514 *x = desktop->cursor.x;
515 *y = desktop->cursor.y;
516 *time = get_tick_count();
519 /* set the cursor clip rectangle */
520 void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect, unsigned int flags, int reset )
522 rectangle_t top_rect;
523 int x, y;
525 get_top_window_rectangle( desktop, &top_rect );
526 if (rect)
528 rectangle_t new_rect = *rect;
529 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
530 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
531 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
532 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
533 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
534 desktop->cursor.clip = new_rect;
536 else desktop->cursor.clip = top_rect;
538 /* warp the mouse to be inside the clip rect */
539 x = max( min( desktop->cursor.x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
540 y = max( min( desktop->cursor.y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
541 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
543 /* request clip cursor rectangle reset to the desktop thread */
544 if (reset) post_desktop_message( desktop, WM_WINE_CLIPCURSOR, flags, FALSE );
546 /* notify foreground thread, of reset, or to apply new cursor clipping rect */
547 queue_cursor_message( desktop, 0, WM_WINE_CLIPCURSOR, flags, reset );
550 /* change the foreground input and reset the cursor clip rect */
551 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
553 if (desktop->foreground_input == input) return;
554 set_clip_rectangle( desktop, NULL, SET_CURSOR_NOCLIP, 1 );
555 desktop->foreground_input = input;
558 /* get the hook table for a given thread */
559 struct hook_table *get_queue_hooks( struct thread *thread )
561 if (!thread->queue) return NULL;
562 return thread->queue->hooks;
565 /* set the hook table for a given thread, allocating the queue if needed */
566 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
568 struct msg_queue *queue = thread->queue;
569 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
570 if (queue->hooks) release_object( queue->hooks );
571 queue->hooks = hooks;
574 /* check the queue status */
575 static inline int is_signaled( struct msg_queue *queue )
577 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
580 /* set some queue bits */
581 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
583 if (bits & (QS_KEY | QS_MOUSEBUTTON))
585 if (!queue->keystate_lock) lock_input_keystate( queue->input );
586 queue->keystate_lock = 1;
588 queue->wake_bits |= bits;
589 queue->changed_bits |= bits;
590 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
593 /* clear some queue bits */
594 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
596 queue->wake_bits &= ~bits;
597 queue->changed_bits &= ~bits;
598 if (!(queue->wake_bits & (QS_KEY | QS_MOUSEBUTTON)))
600 if (queue->keystate_lock) unlock_input_keystate( queue->input );
601 queue->keystate_lock = 0;
605 /* check if message is matched by the filter */
606 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
608 return (msg >= first && msg <= last);
611 /* check whether a message filter contains at least one potential hardware message */
612 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
614 /* hardware message ranges are (in numerical order):
615 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
616 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
617 * WM_MOUSEFIRST .. WM_MOUSELAST
619 if (last < WM_NCMOUSEFIRST) return 0;
620 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
621 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
622 if (first > WM_MOUSELAST) return 0;
623 return 1;
626 /* get the QS_* bit corresponding to a given hardware message */
627 static inline int get_hardware_msg_bit( unsigned int message )
629 if (message == WM_INPUT_DEVICE_CHANGE || message == WM_INPUT) return QS_RAWINPUT;
630 if (message == WM_MOUSEMOVE || message == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
631 if (message >= WM_KEYFIRST && message <= WM_KEYLAST) return QS_KEY;
632 if (message == WM_WINE_CLIPCURSOR) return QS_RAWINPUT;
633 if (message == WM_WINE_SETCURSOR) return QS_RAWINPUT;
634 return QS_MOUSEBUTTON;
637 /* get the current thread queue, creating it if needed */
638 static inline struct msg_queue *get_current_queue(void)
640 struct msg_queue *queue = current->queue;
641 if (!queue) queue = create_msg_queue( current, NULL );
642 return queue;
645 /* get a (pseudo-)unique id to tag hardware messages */
646 static inline unsigned int get_unique_id(void)
648 static unsigned int id;
649 if (!++id) id = 1; /* avoid an id of 0 */
650 return id;
653 /* try to merge a WM_MOUSEMOVE message with the last in the list; return 1 if successful */
654 static int merge_mousemove( struct thread_input *input, const struct message *msg )
656 struct message *prev;
657 struct list *ptr;
659 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
661 prev = LIST_ENTRY( ptr, struct message, entry );
662 if (prev->msg != WM_INPUT) break;
664 if (!ptr) return 0;
665 if (prev->result) return 0;
666 if (prev->win && msg->win && prev->win != msg->win) return 0;
667 if (prev->msg != msg->msg) return 0;
668 if (prev->type != msg->type) return 0;
669 /* now we can merge it */
670 prev->wparam = msg->wparam;
671 prev->lparam = msg->lparam;
672 prev->x = msg->x;
673 prev->y = msg->y;
674 prev->time = msg->time;
675 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
677 struct hardware_msg_data *prev_data = prev->data;
678 struct hardware_msg_data *msg_data = msg->data;
679 prev_data->info = msg_data->info;
681 list_remove( ptr );
682 list_add_tail( &input->msg_list, ptr );
683 return 1;
686 /* try to merge a unique message with the last in the list; return 1 if successful */
687 static int merge_unique_message( struct thread_input *input, unsigned int message, const struct message *msg )
689 struct message *prev;
691 LIST_FOR_EACH_ENTRY_REV( prev, &input->msg_list, struct message, entry )
692 if (prev->msg == message) break;
693 if (&prev->entry == &input->msg_list) return 0;
695 if (prev->result) return 0;
696 if (prev->win != msg->win) return 0;
697 if (prev->type != msg->type) return 0;
699 /* now we can merge it */
700 prev->wparam = msg->wparam;
701 prev->lparam = msg->lparam;
702 prev->x = msg->x;
703 prev->y = msg->y;
704 prev->time = msg->time;
705 list_remove( &prev->entry );
706 list_add_tail( &input->msg_list, &prev->entry );
708 return 1;
711 /* try to merge a message with the messages in the list; return 1 if successful */
712 static int merge_message( struct thread_input *input, const struct message *msg )
714 if (msg->msg == WM_MOUSEMOVE) return merge_mousemove( input, msg );
715 if (msg->msg == WM_WINE_CLIPCURSOR) return merge_unique_message( input, WM_WINE_CLIPCURSOR, msg );
716 if (msg->msg == WM_WINE_SETCURSOR) return merge_unique_message( input, WM_WINE_SETCURSOR, msg );
717 return 0;
720 /* free a result structure */
721 static void free_result( struct message_result *result )
723 if (result->timeout) remove_timeout_user( result->timeout );
724 free( result->data );
725 if (result->callback_msg) free_message( result->callback_msg );
726 if (result->hardware_msg) free_message( result->hardware_msg );
727 if (result->desktop) release_object( result->desktop );
728 free( result );
731 /* remove the result from the sender list it is on */
732 static inline void remove_result_from_sender( struct message_result *result )
734 assert( result->sender );
736 list_remove( &result->sender_entry );
737 result->sender = NULL;
738 if (!result->receiver) free_result( result );
741 /* store the message result in the appropriate structure */
742 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
744 res->result = result;
745 res->error = error;
746 res->replied = 1;
747 if (res->timeout)
749 remove_timeout_user( res->timeout );
750 res->timeout = NULL;
753 if (res->hardware_msg)
755 if (!error && result) /* rejected by the hook */
756 free_message( res->hardware_msg );
757 else
758 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
760 res->hardware_msg = NULL;
763 if (res->sender)
765 if (res->callback_msg)
767 /* queue the callback message in the sender queue */
768 struct callback_msg_data *data = res->callback_msg->data;
769 data->result = result;
770 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
771 set_queue_bits( res->sender, QS_SENDMESSAGE );
772 res->callback_msg = NULL;
773 remove_result_from_sender( res );
775 else
777 /* wake sender queue if waiting on this result */
778 if (list_head(&res->sender->send_result) == &res->sender_entry)
779 set_queue_bits( res->sender, QS_SMRESULT );
782 else if (!res->receiver) free_result( res );
785 /* free a message when deleting a queue or window */
786 static void free_message( struct message *msg )
788 struct message_result *result = msg->result;
789 if (result)
791 result->msg = NULL;
792 result->receiver = NULL;
793 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
795 free( msg->data );
796 free( msg );
799 /* remove (and free) a message from a message list */
800 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
801 enum message_kind kind )
803 list_remove( &msg->entry );
804 switch(kind)
806 case SEND_MESSAGE:
807 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
808 break;
809 case POST_MESSAGE:
810 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
811 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
812 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
813 clear_queue_bits( queue, QS_HOTKEY );
814 break;
816 free_message( msg );
819 /* message timed out without getting a reply */
820 static void result_timeout( void *private )
822 struct message_result *result = private;
824 assert( !result->replied );
826 result->timeout = NULL;
828 if (result->msg) /* not received yet */
830 struct message *msg = result->msg;
832 result->msg = NULL;
833 msg->result = NULL;
834 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
835 result->receiver = NULL;
837 store_message_result( result, 0, STATUS_TIMEOUT );
840 /* allocate and fill a message result structure */
841 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
842 struct msg_queue *recv_queue,
843 struct message *msg, timeout_t timeout )
845 struct message_result *result = mem_alloc( sizeof(*result) );
846 if (result)
848 result->msg = msg;
849 result->sender = send_queue;
850 result->receiver = recv_queue;
851 result->replied = 0;
852 result->data = NULL;
853 result->data_size = 0;
854 result->timeout = NULL;
855 result->hardware_msg = NULL;
856 result->desktop = NULL;
857 result->callback_msg = NULL;
859 if (msg->type == MSG_CALLBACK)
861 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
863 if (!callback_msg)
865 free( result );
866 return NULL;
868 callback_msg->type = MSG_CALLBACK_RESULT;
869 callback_msg->win = msg->win;
870 callback_msg->msg = msg->msg;
871 callback_msg->wparam = 0;
872 callback_msg->lparam = 0;
873 callback_msg->time = get_tick_count();
874 callback_msg->result = NULL;
875 /* steal the data from the original message */
876 callback_msg->data = msg->data;
877 callback_msg->data_size = msg->data_size;
878 msg->data = NULL;
879 msg->data_size = 0;
881 result->callback_msg = callback_msg;
882 list_add_head( &send_queue->callback_result, &result->sender_entry );
884 else if (send_queue)
886 list_add_head( &send_queue->send_result, &result->sender_entry );
887 clear_queue_bits( send_queue, QS_SMRESULT );
890 if (timeout != TIMEOUT_INFINITE)
891 result->timeout = add_timeout_user( timeout, result_timeout, result );
893 return result;
896 /* receive a message, removing it from the sent queue */
897 static void receive_message( struct msg_queue *queue, struct message *msg,
898 struct get_message_reply *reply )
900 struct message_result *result = msg->result;
902 reply->total = msg->data_size;
903 if (msg->data_size > get_reply_max_size())
905 set_error( STATUS_BUFFER_OVERFLOW );
906 return;
908 reply->type = msg->type;
909 reply->win = msg->win;
910 reply->msg = msg->msg;
911 reply->wparam = msg->wparam;
912 reply->lparam = msg->lparam;
913 reply->x = msg->x;
914 reply->y = msg->y;
915 reply->time = msg->time;
917 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
919 list_remove( &msg->entry );
920 /* put the result on the receiver result stack */
921 if (result)
923 result->msg = NULL;
924 result->recv_next = queue->recv_result;
925 queue->recv_result = result;
927 free( msg );
928 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
931 /* set the result of the current received message */
932 static void reply_message( struct msg_queue *queue, lparam_t result,
933 unsigned int error, int remove, const void *data, data_size_t len )
935 struct message_result *res = queue->recv_result;
937 if (remove)
939 queue->recv_result = res->recv_next;
940 res->receiver = NULL;
941 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
943 free_result( res );
944 return;
947 if (!res->replied)
949 if (len && (res->data = memdup( data, len ))) res->data_size = len;
950 store_message_result( res, result, error );
954 static int match_window( user_handle_t win, user_handle_t msg_win )
956 if (!win) return 1;
957 if (win == -1 || win == 1) return !msg_win;
958 if (msg_win == win) return 1;
959 return is_child_window( win, msg_win );
962 /* retrieve a posted message */
963 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
964 unsigned int first, unsigned int last, unsigned int flags,
965 struct get_message_reply *reply )
967 struct message *msg;
969 /* check against the filters */
970 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
972 if (!match_window( win, msg->win )) continue;
973 if (!check_msg_filter( msg->msg, first, last )) continue;
974 goto found; /* found one */
976 return 0;
978 /* return it to the app */
979 found:
980 reply->total = msg->data_size;
981 if (msg->data_size > get_reply_max_size())
983 set_error( STATUS_BUFFER_OVERFLOW );
984 return 1;
986 reply->type = msg->type;
987 reply->win = msg->win;
988 reply->msg = msg->msg;
989 reply->wparam = msg->wparam;
990 reply->lparam = msg->lparam;
991 reply->x = msg->x;
992 reply->y = msg->y;
993 reply->time = msg->time;
995 if (flags & PM_REMOVE)
997 if (msg->data)
999 set_reply_data_ptr( msg->data, msg->data_size );
1000 msg->data = NULL;
1001 msg->data_size = 0;
1003 remove_queue_message( queue, msg, POST_MESSAGE );
1005 else if (msg->data) set_reply_data( msg->data, msg->data_size );
1007 return 1;
1010 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
1011 struct get_message_reply *reply )
1013 if (queue->quit_message)
1015 reply->total = 0;
1016 reply->type = MSG_POSTED;
1017 reply->win = 0;
1018 reply->msg = WM_QUIT;
1019 reply->wparam = queue->exit_code;
1020 reply->lparam = 0;
1022 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
1024 if (flags & PM_REMOVE)
1026 queue->quit_message = 0;
1027 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
1028 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1030 return 1;
1032 else
1033 return 0;
1036 /* empty a message list and free all the messages */
1037 static void empty_msg_list( struct list *list )
1039 struct list *ptr;
1041 while ((ptr = list_head( list )) != NULL)
1043 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1044 list_remove( &msg->entry );
1045 free_message( msg );
1049 /* cleanup all pending results when deleting a queue */
1050 static void cleanup_results( struct msg_queue *queue )
1052 struct list *entry;
1054 while ((entry = list_head( &queue->send_result )) != NULL)
1056 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
1059 while ((entry = list_head( &queue->callback_result )) != NULL)
1061 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
1064 while (queue->recv_result)
1065 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
1068 /* check if the thread owning the queue is hung (not checking for messages) */
1069 static int is_queue_hung( struct msg_queue *queue )
1071 struct wait_queue_entry *entry;
1073 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
1074 return 0; /* less than 5 seconds since last get message -> not hung */
1076 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
1078 if (get_wait_queue_thread(entry)->queue == queue)
1079 return 0; /* thread is waiting on queue -> not hung */
1081 return 1;
1084 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
1086 struct msg_queue *queue = (struct msg_queue *)obj;
1087 struct process *process = get_wait_queue_thread(entry)->process;
1089 /* a thread can only wait on its own queue */
1090 if (get_wait_queue_thread(entry)->queue != queue)
1092 set_error( STATUS_ACCESS_DENIED );
1093 return 0;
1095 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
1097 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
1098 set_fd_events( queue->fd, POLLIN );
1099 add_queue( obj, entry );
1100 return 1;
1103 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
1105 struct msg_queue *queue = (struct msg_queue *)obj;
1107 remove_queue( obj, entry );
1108 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
1109 set_fd_events( queue->fd, 0 );
1112 static void msg_queue_dump( struct object *obj, int verbose )
1114 struct msg_queue *queue = (struct msg_queue *)obj;
1115 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
1116 queue->wake_bits, queue->wake_mask );
1119 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
1121 struct msg_queue *queue = (struct msg_queue *)obj;
1122 int ret = 0;
1124 if (queue->fd)
1126 if ((ret = check_fd_events( queue->fd, POLLIN )))
1127 /* stop waiting on select() if we are signaled */
1128 set_fd_events( queue->fd, 0 );
1129 else if (!list_empty( &obj->wait_queue ))
1130 /* restart waiting on poll() if we are no longer signaled */
1131 set_fd_events( queue->fd, POLLIN );
1133 return ret || is_signaled( queue );
1136 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
1138 struct msg_queue *queue = (struct msg_queue *)obj;
1139 queue->wake_mask = 0;
1140 queue->changed_mask = 0;
1143 static void msg_queue_destroy( struct object *obj )
1145 struct msg_queue *queue = (struct msg_queue *)obj;
1146 struct list *ptr;
1147 struct hotkey *hotkey, *hotkey2;
1148 int i;
1150 cleanup_results( queue );
1151 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
1153 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
1155 if (hotkey->queue == queue)
1157 list_remove( &hotkey->entry );
1158 free( hotkey );
1162 while ((ptr = list_head( &queue->pending_timers )))
1164 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1165 list_remove( &timer->entry );
1166 free( timer );
1168 while ((ptr = list_head( &queue->expired_timers )))
1170 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1171 list_remove( &timer->entry );
1172 free( timer );
1174 if (queue->timeout) remove_timeout_user( queue->timeout );
1175 queue->input->cursor_count -= queue->cursor_count;
1176 if (queue->keystate_lock) unlock_input_keystate( queue->input );
1177 release_object( queue->input );
1178 if (queue->hooks) release_object( queue->hooks );
1179 if (queue->fd) release_object( queue->fd );
1182 static void msg_queue_poll_event( struct fd *fd, int event )
1184 struct msg_queue *queue = get_fd_user( fd );
1185 assert( queue->obj.ops == &msg_queue_ops );
1187 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1188 else set_fd_events( queue->fd, 0 );
1189 wake_up( &queue->obj, 0 );
1192 static void thread_input_dump( struct object *obj, int verbose )
1194 struct thread_input *input = (struct thread_input *)obj;
1195 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
1196 input->focus, input->capture, input->active );
1199 static void thread_input_destroy( struct object *obj )
1201 struct thread_input *input = (struct thread_input *)obj;
1202 struct desktop *desktop;
1204 empty_msg_list( &input->msg_list );
1205 if ((desktop = input->desktop))
1207 if (desktop->foreground_input == input) desktop->foreground_input = NULL;
1208 release_object( desktop );
1212 /* fix the thread input data when a window is destroyed */
1213 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1215 struct thread_input *input = queue->input;
1217 if (window == input->focus) input->focus = 0;
1218 if (window == input->capture) input->capture = 0;
1219 if (window == input->active) input->active = 0;
1220 if (window == input->menu_owner) input->menu_owner = 0;
1221 if (window == input->move_size) input->move_size = 0;
1222 if (window == input->caret) set_caret_window( input, 0 );
1225 /* check if the specified window can be set in the input data of a given queue */
1226 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1228 struct thread *thread;
1229 int ret = 0;
1231 if (!window) return 1; /* we can always clear the data */
1233 if ((thread = get_window_thread( window )))
1235 ret = (queue->input == thread->queue->input);
1236 if (!ret) set_error( STATUS_ACCESS_DENIED );
1237 release_object( thread );
1239 else set_error( STATUS_INVALID_HANDLE );
1241 return ret;
1244 /* make sure the specified thread has a queue */
1245 int init_thread_queue( struct thread *thread )
1247 if (thread->queue) return 1;
1248 return (create_msg_queue( thread, NULL ) != NULL);
1251 /* attach two thread input data structures */
1252 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1254 struct desktop *desktop;
1255 struct thread_input *input;
1256 int ret;
1258 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1259 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1260 input = (struct thread_input *)grab_object( thread_to->queue->input );
1261 if (input->desktop != desktop)
1263 set_error( STATUS_ACCESS_DENIED );
1264 release_object( input );
1265 release_object( desktop );
1266 return 0;
1268 release_object( desktop );
1270 if (thread_from->queue)
1272 if (!input->focus) input->focus = thread_from->queue->input->focus;
1273 if (!input->active) input->active = thread_from->queue->input->active;
1276 ret = assign_thread_input( thread_from, input );
1277 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1278 release_object( input );
1279 return ret;
1282 /* detach two thread input data structures */
1283 void detach_thread_input( struct thread *thread_from )
1285 struct thread *thread;
1286 struct thread_input *input, *old_input = thread_from->queue->input;
1288 if ((input = create_thread_input( thread_from )))
1290 if (old_input->focus && (thread = get_window_thread( old_input->focus )))
1292 if (thread == thread_from)
1294 input->focus = old_input->focus;
1295 old_input->focus = 0;
1297 release_object( thread );
1299 if (old_input->active && (thread = get_window_thread( old_input->active )))
1301 if (thread == thread_from)
1303 input->active = old_input->active;
1304 old_input->active = 0;
1306 release_object( thread );
1308 assign_thread_input( thread_from, input );
1309 release_object( input );
1314 /* set the next timer to expire */
1315 static void set_next_timer( struct msg_queue *queue )
1317 struct list *ptr;
1319 if (queue->timeout)
1321 remove_timeout_user( queue->timeout );
1322 queue->timeout = NULL;
1324 if ((ptr = list_head( &queue->pending_timers )))
1326 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1327 queue->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, queue );
1329 /* set/clear QS_TIMER bit */
1330 if (list_empty( &queue->expired_timers ))
1331 clear_queue_bits( queue, QS_TIMER );
1332 else
1333 set_queue_bits( queue, QS_TIMER );
1336 /* find a timer from its window and id */
1337 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1338 unsigned int msg, lparam_t id )
1340 struct list *ptr;
1342 /* we need to search both lists */
1344 LIST_FOR_EACH( ptr, &queue->pending_timers )
1346 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1347 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1349 LIST_FOR_EACH( ptr, &queue->expired_timers )
1351 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1352 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1354 return NULL;
1357 /* callback for the next timer expiration */
1358 static void timer_callback( void *private )
1360 struct msg_queue *queue = private;
1361 struct list *ptr;
1363 queue->timeout = NULL;
1364 /* move on to the next timer */
1365 ptr = list_head( &queue->pending_timers );
1366 list_remove( ptr );
1367 list_add_tail( &queue->expired_timers, ptr );
1368 set_next_timer( queue );
1371 /* link a timer at its rightful place in the queue list */
1372 static void link_timer( struct msg_queue *queue, struct timer *timer )
1374 struct list *ptr;
1376 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1378 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1379 if (t->when <= timer->when) break;
1381 list_add_before( ptr, &timer->entry );
1384 /* remove a timer from the queue timer list and free it */
1385 static void free_timer( struct msg_queue *queue, struct timer *timer )
1387 list_remove( &timer->entry );
1388 free( timer );
1389 set_next_timer( queue );
1392 /* restart an expired timer */
1393 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1395 list_remove( &timer->entry );
1396 while (-timer->when <= monotonic_time) timer->when -= (timeout_t)timer->rate * 10000;
1397 link_timer( queue, timer );
1398 set_next_timer( queue );
1401 /* find an expired timer matching the filtering parameters */
1402 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1403 unsigned int get_first, unsigned int get_last,
1404 int remove )
1406 struct list *ptr;
1408 LIST_FOR_EACH( ptr, &queue->expired_timers )
1410 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1411 if (win && timer->win != win) continue;
1412 if (check_msg_filter( timer->msg, get_first, get_last ))
1414 if (remove) restart_timer( queue, timer );
1415 return timer;
1418 return NULL;
1421 /* add a timer */
1422 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1424 struct timer *timer = mem_alloc( sizeof(*timer) );
1425 if (timer)
1427 timer->rate = max( rate, 1 );
1428 timer->when = -monotonic_time - (timeout_t)timer->rate * 10000;
1429 link_timer( queue, timer );
1430 /* check if we replaced the next timer */
1431 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1433 return timer;
1436 /* change the input key state for a given key */
1437 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1439 if (down)
1441 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1442 keystate[key] |= down;
1444 else keystate[key] &= ~0x80;
1447 /* update the input key state for a keyboard message */
1448 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1449 unsigned int msg, lparam_t wparam )
1451 unsigned char key;
1452 int down = 0;
1454 switch (msg)
1456 case WM_LBUTTONDOWN:
1457 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1458 /* fall through */
1459 case WM_LBUTTONUP:
1460 set_input_key_state( keystate, VK_LBUTTON, down );
1461 break;
1462 case WM_MBUTTONDOWN:
1463 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1464 /* fall through */
1465 case WM_MBUTTONUP:
1466 set_input_key_state( keystate, VK_MBUTTON, down );
1467 break;
1468 case WM_RBUTTONDOWN:
1469 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1470 /* fall through */
1471 case WM_RBUTTONUP:
1472 set_input_key_state( keystate, VK_RBUTTON, down );
1473 break;
1474 case WM_XBUTTONDOWN:
1475 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1476 /* fall through */
1477 case WM_XBUTTONUP:
1478 if (wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1479 else if (wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1480 break;
1481 case WM_KEYDOWN:
1482 case WM_SYSKEYDOWN:
1483 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1484 /* fall through */
1485 case WM_KEYUP:
1486 case WM_SYSKEYUP:
1487 key = (unsigned char)wparam;
1488 set_input_key_state( keystate, key, down );
1489 switch(key)
1491 case VK_LCONTROL:
1492 case VK_RCONTROL:
1493 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1494 set_input_key_state( keystate, VK_CONTROL, down );
1495 break;
1496 case VK_LMENU:
1497 case VK_RMENU:
1498 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1499 set_input_key_state( keystate, VK_MENU, down );
1500 break;
1501 case VK_LSHIFT:
1502 case VK_RSHIFT:
1503 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1504 set_input_key_state( keystate, VK_SHIFT, down );
1505 break;
1507 break;
1511 /* update the desktop key state according to a mouse message flags */
1512 static void update_desktop_mouse_state( struct desktop *desktop, unsigned int flags, lparam_t wparam )
1514 if (flags & MOUSEEVENTF_LEFTDOWN)
1515 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONDOWN, wparam );
1516 if (flags & MOUSEEVENTF_LEFTUP)
1517 update_input_key_state( desktop, desktop->keystate, WM_LBUTTONUP, wparam );
1518 if (flags & MOUSEEVENTF_RIGHTDOWN)
1519 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONDOWN, wparam );
1520 if (flags & MOUSEEVENTF_RIGHTUP)
1521 update_input_key_state( desktop, desktop->keystate, WM_RBUTTONUP, wparam );
1522 if (flags & MOUSEEVENTF_MIDDLEDOWN)
1523 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONDOWN, wparam );
1524 if (flags & MOUSEEVENTF_MIDDLEUP)
1525 update_input_key_state( desktop, desktop->keystate, WM_MBUTTONUP, wparam );
1526 if (flags & MOUSEEVENTF_XDOWN)
1527 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONDOWN, wparam );
1528 if (flags & MOUSEEVENTF_XUP)
1529 update_input_key_state( desktop, desktop->keystate, WM_XBUTTONUP, wparam );
1532 /* release the hardware message currently being processed by the given thread */
1533 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id )
1535 struct thread_input *input = queue->input;
1536 struct message *msg, *other;
1537 int clr_bit;
1539 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1541 if (msg->unique_id == hw_id) break;
1543 if (&msg->entry == &input->msg_list) return; /* not found */
1545 /* clear the queue bit for that message */
1546 clr_bit = get_hardware_msg_bit( msg->msg );
1547 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1549 if (other != msg && get_hardware_msg_bit( other->msg ) == clr_bit)
1551 clr_bit = 0;
1552 break;
1555 if (clr_bit) clear_queue_bits( queue, clr_bit );
1557 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1558 list_remove( &msg->entry );
1559 free_message( msg );
1562 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1564 struct hotkey *hotkey;
1565 unsigned int modifiers = 0;
1567 if (msg->msg != WM_KEYDOWN && msg->msg != WM_SYSKEYDOWN) return 0;
1569 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1570 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1571 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1572 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1574 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1576 if (hotkey->vkey != msg->wparam) continue;
1577 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1580 return 0;
1582 found:
1583 msg->type = MSG_POSTED;
1584 msg->win = hotkey->win;
1585 msg->msg = WM_HOTKEY;
1586 msg->wparam = hotkey->id;
1587 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1589 free( msg->data );
1590 msg->data = NULL;
1591 msg->data_size = 0;
1593 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1594 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1595 hotkey->queue->hotkey_count++;
1596 return 1;
1599 /* find the window that should receive a given hardware message */
1600 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1601 struct message *msg, unsigned int *msg_code,
1602 struct thread **thread )
1604 user_handle_t win = 0;
1606 *thread = NULL;
1607 *msg_code = msg->msg;
1608 switch (get_hardware_msg_bit( msg->msg ))
1610 case QS_RAWINPUT:
1611 if (!(win = msg->win) && input) win = input->focus;
1612 break;
1613 case QS_KEY:
1614 if (input && !(win = input->focus))
1616 win = input->active;
1617 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1619 break;
1620 case QS_MOUSEMOVE:
1621 case QS_MOUSEBUTTON:
1622 if (!input || !(win = input->capture))
1624 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1625 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1626 *thread = window_thread_from_point( win, msg->x, msg->y );
1628 break;
1631 if (!*thread)
1632 *thread = get_window_thread( win );
1633 return win;
1636 static struct rawinput_device *find_rawinput_device( struct process *process, unsigned short usage_page, unsigned short usage )
1638 struct rawinput_device *device, *end;
1640 for (device = process->rawinput_devices, end = device + process->rawinput_device_count; device != end; device++)
1642 if (device->usage_page != usage_page || device->usage != usage) continue;
1643 return device;
1646 return NULL;
1649 static void prepend_cursor_history( int x, int y, unsigned int time, lparam_t info )
1651 cursor_pos_t *pos = &cursor_history[--cursor_history_latest % ARRAY_SIZE(cursor_history)];
1653 pos->x = x;
1654 pos->y = y;
1655 pos->time = time;
1656 pos->info = info;
1659 /* queue a hardware message into a given thread input */
1660 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1662 user_handle_t win;
1663 struct thread *thread;
1664 struct thread_input *input;
1665 struct hardware_msg_data *msg_data = msg->data;
1666 unsigned int msg_code;
1668 update_input_key_state( desktop, desktop->keystate, msg->msg, msg->wparam );
1669 last_input_time = get_tick_count();
1670 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1672 switch (get_hardware_msg_bit( msg->msg ))
1674 case QS_KEY:
1675 if (queue_hotkey_message( desktop, msg )) return;
1676 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1677 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1678 msg->lparam &= ~(KF_EXTENDED << 16);
1679 break;
1680 case QS_MOUSEMOVE:
1681 prepend_cursor_history( msg->x, msg->y, msg->time, msg_data->info );
1682 if (update_desktop_cursor_pos( desktop, msg->win, msg->x, msg->y )) always_queue = 1;
1683 /* fallthrough */
1684 case QS_MOUSEBUTTON:
1685 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1686 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1687 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1688 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1689 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1690 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1691 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1692 break;
1694 msg->x = desktop->cursor.x;
1695 msg->y = desktop->cursor.y;
1697 if (msg->win && (thread = get_window_thread( msg->win )))
1699 input = thread->queue->input;
1700 release_object( thread );
1702 else input = desktop->foreground_input;
1704 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1705 if (!win || !thread)
1707 if (input) update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
1708 free_message( msg );
1709 return;
1711 input = thread->queue->input;
1713 if (!always_queue || merge_message( input, msg )) free_message( msg );
1714 else
1716 msg->unique_id = 0; /* will be set once we return it to the app */
1717 list_add_tail( &input->msg_list, &msg->entry );
1718 set_queue_bits( thread->queue, get_hardware_msg_bit( msg->msg ) );
1720 release_object( thread );
1723 /* send the low-level hook message for a given hardware message */
1724 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1725 const hw_input_t *input, struct msg_queue *sender )
1727 struct thread *hook_thread;
1728 struct msg_queue *queue;
1729 struct message *msg;
1730 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1731 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1733 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1734 if (!(queue = hook_thread->queue)) return 0;
1735 if (is_queue_hung( queue )) return 0;
1737 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1739 msg->type = MSG_HOOK_LL;
1740 msg->win = 0;
1741 msg->msg = id;
1742 msg->wparam = hardware_msg->msg;
1743 msg->x = hardware_msg->x;
1744 msg->y = hardware_msg->y;
1745 msg->time = hardware_msg->time;
1746 msg->data_size = hardware_msg->data_size;
1747 msg->result = NULL;
1749 if (input->type == INPUT_KEYBOARD)
1751 unsigned short vkey = input->kbd.vkey;
1752 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1753 msg->lparam = (input->kbd.scan << 16) | vkey;
1755 else msg->lparam = input->mouse.data << 16;
1757 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1758 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1760 free_message( msg );
1761 return 0;
1763 msg->result->hardware_msg = hardware_msg;
1764 msg->result->desktop = (struct desktop *)grab_object( desktop );
1765 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1766 set_queue_bits( queue, QS_SENDMESSAGE );
1767 return 1;
1770 /* get the foreground thread for a desktop and a window receiving input */
1771 static struct thread *get_foreground_thread( struct desktop *desktop, user_handle_t window )
1773 /* if desktop has no foreground process, assume the receiving window is */
1774 if (desktop->foreground_input) return get_window_thread( desktop->foreground_input->focus );
1775 if (window) return get_window_thread( window );
1776 return NULL;
1779 struct rawinput_message
1781 struct thread *foreground;
1782 struct desktop *desktop;
1783 struct hw_msg_source source;
1784 unsigned int time;
1785 unsigned int message;
1786 struct hardware_msg_data data;
1787 const void *hid_report;
1790 /* check if process is supposed to receive a WM_INPUT message and eventually queue it */
1791 static int queue_rawinput_message( struct process* process, void *arg )
1793 const struct rawinput_message* raw_msg = arg;
1794 const struct rawinput_device *device = NULL;
1795 struct desktop *target_desktop = NULL, *desktop = NULL;
1796 struct thread *target_thread = NULL, *foreground = NULL;
1797 struct message *msg;
1798 data_size_t report_size;
1799 int wparam = RIM_INPUT;
1801 if (raw_msg->data.rawinput.type == RIM_TYPEMOUSE)
1802 device = process->rawinput_mouse;
1803 else if (raw_msg->data.rawinput.type == RIM_TYPEKEYBOARD)
1804 device = process->rawinput_kbd;
1805 else
1806 device = find_rawinput_device( process, raw_msg->data.rawinput.hid.usage_page, raw_msg->data.rawinput.hid.usage );
1807 if (!device) return 0;
1809 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && !(device->flags & RIDEV_DEVNOTIFY)) return 0;
1811 if (raw_msg->desktop) desktop = (struct desktop *)grab_object( raw_msg->desktop );
1812 else if (!(desktop = get_desktop_obj( process, process->desktop, 0 ))) goto done;
1814 if (raw_msg->foreground) foreground = (struct thread *)grab_object( raw_msg->foreground );
1815 else if (!(foreground = get_foreground_thread( desktop, 0 ))) goto done;
1817 if (process != foreground->process)
1819 if (raw_msg->message == WM_INPUT && !(device->flags & RIDEV_INPUTSINK)) goto done;
1820 if (!(target_thread = get_window_thread( device->target ))) goto done;
1821 if (!(target_desktop = get_thread_desktop( target_thread, 0 ))) goto done;
1822 if (target_desktop != desktop) goto done;
1823 wparam = RIM_INPUTSINK;
1826 if (raw_msg->data.rawinput.type != RIM_TYPEHID || !raw_msg->hid_report) report_size = 0;
1827 else report_size = raw_msg->data.size - sizeof(raw_msg->data);
1829 if (!(msg = alloc_hardware_message( raw_msg->data.info, raw_msg->source, raw_msg->time, report_size )))
1830 goto done;
1832 msg->win = device->target;
1833 msg->msg = raw_msg->message;
1834 msg->wparam = wparam;
1835 msg->lparam = 0;
1836 memcpy( msg->data, &raw_msg->data, sizeof(raw_msg->data) );
1837 if (report_size) memcpy( (struct hardware_msg_data *)msg->data + 1, raw_msg->hid_report, report_size );
1839 if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && raw_msg->data.rawinput.type == RIM_TYPEHID)
1841 msg->wparam = raw_msg->data.rawinput.hid.param;
1842 msg->lparam = raw_msg->data.rawinput.hid.device;
1845 queue_hardware_message( desktop, msg, 1 );
1847 done:
1848 if (target_thread) release_object( target_thread );
1849 if (target_desktop) release_object( target_desktop );
1850 if (foreground) release_object( foreground );
1851 if (desktop) release_object( desktop );
1852 return 0;
1855 /* queue a hardware message for a mouse event */
1856 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1857 unsigned int origin, struct msg_queue *sender )
1859 const struct rawinput_device *device;
1860 struct hardware_msg_data *msg_data;
1861 struct rawinput_message raw_msg;
1862 struct message *msg;
1863 struct thread *foreground;
1864 unsigned int i, time, flags;
1865 struct hw_msg_source source = { IMDT_MOUSE, origin };
1866 int wait = 0, x, y;
1868 static const unsigned int messages[] =
1870 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1871 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1872 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1873 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1874 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1875 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1876 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1877 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1878 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1879 0, /* 0x0200 = unused */
1880 0, /* 0x0400 = unused */
1881 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1882 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1885 desktop->cursor.last_change = get_tick_count();
1886 flags = input->mouse.flags;
1887 time = input->mouse.time;
1888 if (!time) time = desktop->cursor.last_change;
1890 if (flags & MOUSEEVENTF_MOVE)
1892 if (flags & MOUSEEVENTF_ABSOLUTE)
1894 x = input->mouse.x;
1895 y = input->mouse.y;
1896 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1897 x == desktop->cursor.x && y == desktop->cursor.y)
1898 flags &= ~MOUSEEVENTF_MOVE;
1900 else
1902 x = desktop->cursor.x + input->mouse.x;
1903 y = desktop->cursor.y + input->mouse.y;
1906 else
1908 x = desktop->cursor.x;
1909 y = desktop->cursor.y;
1912 if ((foreground = get_foreground_thread( desktop, win )))
1914 memset( &raw_msg, 0, sizeof(raw_msg) );
1915 raw_msg.foreground = foreground;
1916 raw_msg.desktop = desktop;
1917 raw_msg.source = source;
1918 raw_msg.time = time;
1919 raw_msg.message = WM_INPUT;
1921 msg_data = &raw_msg.data;
1922 msg_data->info = input->mouse.info;
1923 msg_data->size = sizeof(*msg_data);
1924 msg_data->flags = flags;
1925 msg_data->rawinput.type = RIM_TYPEMOUSE;
1926 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1927 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1928 msg_data->rawinput.mouse.data = input->mouse.data;
1930 enum_processes( queue_rawinput_message, &raw_msg );
1931 release_object( foreground );
1934 if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
1936 if (flags & MOUSEEVENTF_MOVE) update_desktop_cursor_pos( desktop, win, x, y );
1937 update_desktop_mouse_state( desktop, flags, input->mouse.data << 16 );
1938 return 0;
1941 for (i = 0; i < ARRAY_SIZE( messages ); i++)
1943 if (!messages[i]) continue;
1944 if (!(flags & (1 << i))) continue;
1945 flags &= ~(1 << i);
1947 if (!(msg = alloc_hardware_message( input->mouse.info, source, time, 0 ))) return 0;
1948 msg_data = msg->data;
1950 msg->win = get_user_full_handle( win );
1951 msg->msg = messages[i];
1952 msg->wparam = input->mouse.data << 16;
1953 msg->lparam = 0;
1954 msg->x = x;
1955 msg->y = y;
1956 if (origin == IMO_INJECTED) msg_data->flags = LLMHF_INJECTED;
1958 /* specify a sender only when sending the last message */
1959 if (!(flags & ((1 << ARRAY_SIZE( messages )) - 1)))
1961 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1962 queue_hardware_message( desktop, msg, 0 );
1964 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1965 queue_hardware_message( desktop, msg, 0 );
1967 return wait;
1970 /* queue a hardware message for a keyboard event */
1971 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1972 unsigned int origin, struct msg_queue *sender )
1974 struct hw_msg_source source = { IMDT_KEYBOARD, origin };
1975 const struct rawinput_device *device;
1976 struct hardware_msg_data *msg_data;
1977 struct rawinput_message raw_msg;
1978 struct message *msg;
1979 struct thread *foreground;
1980 unsigned char vkey = input->kbd.vkey;
1981 unsigned int message_code, time;
1982 int wait;
1984 if (!(time = input->kbd.time)) time = get_tick_count();
1986 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1988 switch (vkey)
1990 case VK_MENU:
1991 case VK_LMENU:
1992 case VK_RMENU:
1993 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1994 break;
1995 case VK_CONTROL:
1996 case VK_LCONTROL:
1997 case VK_RCONTROL:
1998 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1999 break;
2000 case VK_SHIFT:
2001 case VK_LSHIFT:
2002 case VK_RSHIFT:
2003 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
2004 break;
2008 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
2009 switch (vkey)
2011 case VK_LMENU:
2012 case VK_RMENU:
2013 if (input->kbd.flags & KEYEVENTF_KEYUP)
2015 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
2016 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
2017 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
2018 message_code = WM_SYSKEYUP;
2019 desktop->keystate[VK_MENU] &= ~0x02;
2021 else
2023 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
2024 if (desktop->keystate[VK_CONTROL] & 0x80) break;
2025 message_code = WM_SYSKEYDOWN;
2026 desktop->keystate[VK_MENU] |= 0x02;
2028 break;
2030 case VK_LCONTROL:
2031 case VK_RCONTROL:
2032 /* send WM_SYSKEYUP on release if Alt still pressed */
2033 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
2034 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
2035 message_code = WM_SYSKEYUP;
2036 desktop->keystate[VK_MENU] &= ~0x02;
2037 break;
2039 default:
2040 /* send WM_SYSKEY for Alt-anykey and for F10 */
2041 if (desktop->keystate[VK_CONTROL] & 0x80) break;
2042 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
2043 /* fall through */
2044 case VK_F10:
2045 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
2046 desktop->keystate[VK_MENU] &= ~0x02;
2047 break;
2050 if ((foreground = get_foreground_thread( desktop, win )))
2052 memset( &raw_msg, 0, sizeof(raw_msg) );
2053 raw_msg.foreground = foreground;
2054 raw_msg.desktop = desktop;
2055 raw_msg.source = source;
2056 raw_msg.time = time;
2057 raw_msg.message = WM_INPUT;
2059 msg_data = &raw_msg.data;
2060 msg_data->info = input->kbd.info;
2061 msg_data->size = sizeof(*msg_data);
2062 msg_data->flags = input->kbd.flags;
2063 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
2064 msg_data->rawinput.kbd.message = message_code;
2065 msg_data->rawinput.kbd.vkey = vkey;
2066 msg_data->rawinput.kbd.scan = input->kbd.scan;
2068 enum_processes( queue_rawinput_message, &raw_msg );
2069 release_object( foreground );
2072 if ((device = current->process->rawinput_kbd) && (device->flags & RIDEV_NOLEGACY))
2074 update_input_key_state( desktop, desktop->keystate, message_code, vkey );
2075 return 0;
2078 if (!(msg = alloc_hardware_message( input->kbd.info, source, time, 0 ))) return 0;
2079 msg_data = msg->data;
2081 msg->win = get_user_full_handle( win );
2082 msg->msg = message_code;
2083 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
2084 if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
2086 if (input->kbd.flags & KEYEVENTF_UNICODE && !vkey)
2088 msg->wparam = VK_PACKET;
2090 else
2092 unsigned int flags = 0;
2093 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
2094 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
2095 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
2096 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
2098 msg->wparam = vkey;
2099 msg->lparam |= flags << 16;
2100 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
2103 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
2104 queue_hardware_message( desktop, msg, 1 );
2106 return wait;
2109 /* queue a hardware message for a custom type of event */
2110 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
2111 unsigned int origin, const hw_input_t *input )
2113 struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
2114 struct hardware_msg_data *msg_data;
2115 struct rawinput_message raw_msg;
2116 struct message *msg;
2117 data_size_t report_size = 0;
2119 switch (input->hw.msg)
2121 case WM_INPUT:
2122 case WM_INPUT_DEVICE_CHANGE:
2123 memset( &raw_msg, 0, sizeof(raw_msg) );
2124 raw_msg.source = source;
2125 raw_msg.time = get_tick_count();
2126 raw_msg.message = input->hw.msg;
2128 if (input->hw.rawinput.type == RIM_TYPEHID)
2130 raw_msg.hid_report = get_req_data();
2131 report_size = input->hw.rawinput.hid.length * input->hw.rawinput.hid.count;
2134 if (report_size != get_req_data_size())
2136 set_error( STATUS_INVALID_PARAMETER );
2137 return;
2140 msg_data = &raw_msg.data;
2141 msg_data->size = sizeof(*msg_data) + report_size;
2142 msg_data->rawinput = input->hw.rawinput;
2144 enum_processes( queue_rawinput_message, &raw_msg );
2145 return;
2148 if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
2150 msg->win = get_user_full_handle( win );
2151 msg->msg = input->hw.msg;
2152 msg->wparam = 0;
2153 msg->lparam = input->hw.lparam;
2154 msg->x = desktop->cursor.x;
2155 msg->y = desktop->cursor.y;
2157 queue_hardware_message( desktop, msg, 1 );
2160 /* check message filter for a hardware message */
2161 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
2162 user_handle_t filter_win, unsigned int first, unsigned int last )
2164 switch (get_hardware_msg_bit( msg_code ))
2166 case QS_KEY:
2167 /* we can only test the window for a keyboard message since the
2168 * dest window for a mouse message depends on hittest */
2169 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
2170 return 0;
2171 /* the message code is final for a keyboard message, we can simply check it */
2172 return check_msg_filter( msg_code, first, last );
2174 case QS_MOUSEMOVE:
2175 case QS_MOUSEBUTTON:
2176 /* we need to check all possible values that the message can have in the end */
2177 if (check_msg_filter( msg_code, first, last )) return 1;
2178 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
2180 /* all other messages can become non-client messages */
2181 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
2183 /* clicks can become double-clicks or non-client double-clicks */
2184 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
2185 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
2187 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2188 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
2190 return 0;
2192 default:
2193 return check_msg_filter( msg_code, first, last );
2198 /* find a hardware message for the given queue */
2199 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
2200 unsigned int first, unsigned int last, unsigned int flags,
2201 struct get_message_reply *reply )
2203 struct thread_input *input = thread->queue->input;
2204 struct thread *win_thread;
2205 struct list *ptr;
2206 user_handle_t win;
2207 int clear_bits, got_one = 0;
2208 unsigned int msg_code;
2210 ptr = list_head( &input->msg_list );
2211 if (hw_id)
2213 while (ptr)
2215 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2216 if (msg->unique_id == hw_id) break;
2217 ptr = list_next( &input->msg_list, ptr );
2219 if (!ptr) ptr = list_head( &input->msg_list );
2220 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
2223 if (ptr == list_head( &input->msg_list ))
2224 clear_bits = QS_INPUT;
2225 else
2226 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
2228 while (ptr)
2230 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2231 struct hardware_msg_data *data = msg->data;
2233 ptr = list_next( &input->msg_list, ptr );
2234 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
2235 if (!win || !win_thread)
2237 /* no window at all, remove it */
2238 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2239 list_remove( &msg->entry );
2240 free_message( msg );
2241 continue;
2243 if (win_thread != thread)
2245 if (win_thread->queue->input == input)
2247 /* wake the other thread */
2248 set_queue_bits( win_thread->queue, get_hardware_msg_bit( msg->msg ) );
2249 got_one = 1;
2251 else
2253 /* for another thread input, drop it */
2254 update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
2255 list_remove( &msg->entry );
2256 free_message( msg );
2258 release_object( win_thread );
2259 continue;
2261 release_object( win_thread );
2263 /* if we already got a message for another thread, or if it doesn't
2264 * match the filter we skip it */
2265 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
2267 clear_bits &= ~get_hardware_msg_bit( msg->msg );
2268 continue;
2271 reply->total = msg->data_size;
2272 if (msg->data_size > get_reply_max_size())
2274 set_error( STATUS_BUFFER_OVERFLOW );
2275 return 1;
2278 /* now we can return it */
2279 if (!msg->unique_id) msg->unique_id = get_unique_id();
2280 reply->type = MSG_HARDWARE;
2281 reply->win = win;
2282 reply->msg = msg_code;
2283 reply->wparam = msg->wparam;
2284 reply->lparam = msg->lparam;
2285 reply->x = msg->x;
2286 reply->y = msg->y;
2287 reply->time = msg->time;
2289 data->hw_id = msg->unique_id;
2290 set_reply_data( msg->data, msg->data_size );
2291 if (get_hardware_msg_bit( msg->msg ) == QS_RAWINPUT && (flags & PM_REMOVE))
2292 release_hardware_message( current->queue, data->hw_id );
2293 return 1;
2295 /* nothing found, clear the hardware queue bits */
2296 clear_queue_bits( thread->queue, clear_bits );
2297 return 0;
2300 /* increment (or decrement if 'incr' is negative) the queue paint count */
2301 void inc_queue_paint_count( struct thread *thread, int incr )
2303 struct msg_queue *queue = thread->queue;
2305 assert( queue );
2307 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2309 if (queue->paint_count)
2310 set_queue_bits( queue, QS_PAINT );
2311 else
2312 clear_queue_bits( queue, QS_PAINT );
2316 /* remove all messages and timers belonging to a certain window */
2317 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2319 struct msg_queue *queue = thread->queue;
2320 struct list *ptr;
2321 int i;
2323 if (!queue) return;
2325 /* remove timers */
2327 ptr = list_head( &queue->pending_timers );
2328 while (ptr)
2330 struct list *next = list_next( &queue->pending_timers, ptr );
2331 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2332 if (timer->win == win) free_timer( queue, timer );
2333 ptr = next;
2335 ptr = list_head( &queue->expired_timers );
2336 while (ptr)
2338 struct list *next = list_next( &queue->expired_timers, ptr );
2339 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2340 if (timer->win == win) free_timer( queue, timer );
2341 ptr = next;
2344 /* remove messages */
2345 for (i = 0; i < NB_MSG_KINDS; i++)
2347 struct list *ptr, *next;
2349 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2351 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2352 if (msg->win == win)
2354 if (msg->msg == WM_QUIT && !queue->quit_message)
2356 queue->quit_message = 1;
2357 queue->exit_code = msg->wparam;
2359 remove_queue_message( queue, msg, i );
2364 thread_input_cleanup_window( queue, win );
2367 /* post a message to a window */
2368 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2370 struct message *msg;
2371 struct thread *thread = get_window_thread( win );
2373 if (!thread) return;
2375 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2377 msg->type = MSG_POSTED;
2378 msg->win = get_user_full_handle( win );
2379 msg->msg = message;
2380 msg->wparam = wparam;
2381 msg->lparam = lparam;
2382 msg->result = NULL;
2383 msg->data = NULL;
2384 msg->data_size = 0;
2386 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2388 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2389 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2390 if (message == WM_HOTKEY)
2392 set_queue_bits( thread->queue, QS_HOTKEY );
2393 thread->queue->hotkey_count++;
2396 release_object( thread );
2399 /* send a notify message to a window */
2400 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2402 struct message *msg;
2403 struct thread *thread = get_window_thread( win );
2405 if (!thread) return;
2407 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2409 msg->type = MSG_NOTIFY;
2410 msg->win = get_user_full_handle( win );
2411 msg->msg = message;
2412 msg->wparam = wparam;
2413 msg->lparam = lparam;
2414 msg->result = NULL;
2415 msg->data = NULL;
2416 msg->data_size = 0;
2418 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2420 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2421 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2423 release_object( thread );
2426 /* post a win event */
2427 void post_win_event( struct thread *thread, unsigned int event,
2428 user_handle_t win, unsigned int object_id,
2429 unsigned int child_id, client_ptr_t hook_proc,
2430 const WCHAR *module, data_size_t module_size,
2431 user_handle_t hook)
2433 struct message *msg;
2435 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2437 struct winevent_msg_data *data;
2439 msg->type = MSG_WINEVENT;
2440 msg->win = get_user_full_handle( win );
2441 msg->msg = event;
2442 msg->wparam = object_id;
2443 msg->lparam = child_id;
2444 msg->time = get_tick_count();
2445 msg->result = NULL;
2447 if ((data = malloc( sizeof(*data) + module_size )))
2449 data->hook = hook;
2450 data->tid = get_thread_id( current );
2451 data->hook_proc = hook_proc;
2452 memcpy( data + 1, module, module_size );
2454 msg->data = data;
2455 msg->data_size = sizeof(*data) + module_size;
2457 if (debug_level > 1)
2458 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2459 get_thread_id(thread), event, win, object_id, child_id );
2460 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2461 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2463 else
2464 free( msg );
2468 /* free all hotkeys on a desktop, optionally filtering by window */
2469 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2471 struct hotkey *hotkey, *hotkey2;
2473 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2475 if (!window || hotkey->win == window)
2477 list_remove( &hotkey->entry );
2478 free( hotkey );
2484 /* check if the thread owning the window is hung */
2485 DECL_HANDLER(is_window_hung)
2487 struct thread *thread;
2489 thread = get_window_thread( req->win );
2490 if (thread)
2492 reply->is_hung = is_queue_hung( thread->queue );
2493 release_object( thread );
2495 else reply->is_hung = 0;
2499 /* get the message queue of the current thread */
2500 DECL_HANDLER(get_msg_queue)
2502 struct msg_queue *queue = get_current_queue();
2504 reply->handle = 0;
2505 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2509 /* set the file descriptor associated to the current thread queue */
2510 DECL_HANDLER(set_queue_fd)
2512 struct msg_queue *queue = get_current_queue();
2513 struct file *file;
2514 int unix_fd;
2516 if (queue->fd) /* fd can only be set once */
2518 set_error( STATUS_ACCESS_DENIED );
2519 return;
2521 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2523 if ((unix_fd = get_file_unix_fd( file )) != -1)
2525 if ((unix_fd = dup( unix_fd )) != -1)
2526 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2527 else
2528 file_set_error();
2530 release_object( file );
2534 /* set the current message queue wakeup mask */
2535 DECL_HANDLER(set_queue_mask)
2537 struct msg_queue *queue = get_current_queue();
2539 if (queue)
2541 queue->wake_mask = req->wake_mask;
2542 queue->changed_mask = req->changed_mask;
2543 reply->wake_bits = queue->wake_bits;
2544 reply->changed_bits = queue->changed_bits;
2545 if (is_signaled( queue ))
2547 /* if skip wait is set, do what would have been done in the subsequent wait */
2548 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2549 else wake_up( &queue->obj, 0 );
2555 /* get the current message queue status */
2556 DECL_HANDLER(get_queue_status)
2558 struct msg_queue *queue = current->queue;
2559 if (queue)
2561 reply->wake_bits = queue->wake_bits;
2562 reply->changed_bits = queue->changed_bits;
2563 queue->changed_bits &= ~req->clear_bits;
2565 else reply->wake_bits = reply->changed_bits = 0;
2569 /* send a message to a thread queue */
2570 DECL_HANDLER(send_message)
2572 struct message *msg;
2573 struct msg_queue *send_queue = get_current_queue();
2574 struct msg_queue *recv_queue = NULL;
2575 struct thread *thread = NULL;
2577 if (!(thread = get_thread_from_id( req->id ))) return;
2579 if (!(recv_queue = thread->queue))
2581 set_error( STATUS_INVALID_PARAMETER );
2582 release_object( thread );
2583 return;
2585 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2587 set_error( STATUS_TIMEOUT );
2588 release_object( thread );
2589 return;
2592 if ((msg = mem_alloc( sizeof(*msg) )))
2594 msg->type = req->type;
2595 msg->win = get_user_full_handle( req->win );
2596 msg->msg = req->msg;
2597 msg->wparam = req->wparam;
2598 msg->lparam = req->lparam;
2599 msg->result = NULL;
2600 msg->data = NULL;
2601 msg->data_size = get_req_data_size();
2603 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2605 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2607 free( msg );
2608 release_object( thread );
2609 return;
2612 switch(msg->type)
2614 case MSG_OTHER_PROCESS:
2615 case MSG_ASCII:
2616 case MSG_UNICODE:
2617 case MSG_CALLBACK:
2618 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2620 free_message( msg );
2621 break;
2623 /* fall through */
2624 case MSG_NOTIFY:
2625 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2626 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2627 break;
2628 case MSG_POSTED:
2629 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2630 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2631 if (msg->msg == WM_HOTKEY)
2633 set_queue_bits( recv_queue, QS_HOTKEY );
2634 recv_queue->hotkey_count++;
2636 break;
2637 case MSG_HARDWARE: /* should use send_hardware_message instead */
2638 case MSG_CALLBACK_RESULT: /* cannot send this one */
2639 case MSG_HOOK_LL: /* generated internally */
2640 default:
2641 set_error( STATUS_INVALID_PARAMETER );
2642 free( msg );
2643 break;
2646 release_object( thread );
2649 /* send a hardware message to a thread queue */
2650 DECL_HANDLER(send_hardware_message)
2652 struct thread *thread = NULL;
2653 struct desktop *desktop;
2654 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2655 struct msg_queue *sender = get_current_queue();
2657 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2659 if (req->win)
2661 if (!(thread = get_window_thread( req->win ))) return;
2662 if (desktop != thread->queue->input->desktop)
2664 /* don't allow queuing events to a different desktop */
2665 release_object( desktop );
2666 return;
2670 reply->prev_x = desktop->cursor.x;
2671 reply->prev_y = desktop->cursor.y;
2673 switch (req->input.type)
2675 case INPUT_MOUSE:
2676 reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2677 break;
2678 case INPUT_KEYBOARD:
2679 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2680 break;
2681 case INPUT_HARDWARE:
2682 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2683 break;
2684 default:
2685 set_error( STATUS_INVALID_PARAMETER );
2687 if (thread) release_object( thread );
2689 reply->new_x = desktop->cursor.x;
2690 reply->new_y = desktop->cursor.y;
2691 release_object( desktop );
2694 /* post a quit message to the current queue */
2695 DECL_HANDLER(post_quit_message)
2697 struct msg_queue *queue = get_current_queue();
2699 if (!queue)
2700 return;
2702 queue->quit_message = 1;
2703 queue->exit_code = req->exit_code;
2704 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2707 /* get a message from the current queue */
2708 DECL_HANDLER(get_message)
2710 struct timer *timer;
2711 struct list *ptr;
2712 struct msg_queue *queue = get_current_queue();
2713 user_handle_t get_win = get_user_full_handle( req->get_win );
2714 unsigned int filter = req->flags >> 16;
2716 reply->active_hooks = get_active_hooks();
2718 if (get_win && get_win != 1 && get_win != -1 && !get_user_object( get_win, USER_WINDOW ))
2720 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2721 return;
2724 if (!queue) return;
2725 queue->last_get_msg = current_time;
2726 if (!filter) filter = QS_ALLINPUT;
2728 /* first check for sent messages */
2729 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2731 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2732 receive_message( queue, msg, reply );
2733 return;
2736 /* clear changed bits so we can wait on them if we don't find a message */
2737 if (filter & QS_POSTMESSAGE)
2739 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2740 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2742 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2743 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2745 /* then check for posted messages */
2746 if ((filter & QS_POSTMESSAGE) &&
2747 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2748 return;
2750 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2751 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2752 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2753 return;
2755 /* only check for quit messages if not posted messages pending */
2756 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2757 return;
2759 /* then check for any raw hardware message */
2760 if ((filter & QS_INPUT) &&
2761 filter_contains_hw_range( req->get_first, req->get_last ) &&
2762 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2763 return;
2765 /* now check for WM_PAINT */
2766 if ((filter & QS_PAINT) &&
2767 queue->paint_count &&
2768 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2769 (reply->win = find_window_to_repaint( get_win, current )))
2771 reply->type = MSG_POSTED;
2772 reply->msg = WM_PAINT;
2773 reply->wparam = 0;
2774 reply->lparam = 0;
2775 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2776 return;
2779 /* now check for timer */
2780 if ((filter & QS_TIMER) &&
2781 (timer = find_expired_timer( queue, get_win, req->get_first,
2782 req->get_last, (req->flags & PM_REMOVE) )))
2784 reply->type = MSG_POSTED;
2785 reply->win = timer->win;
2786 reply->msg = timer->msg;
2787 reply->wparam = timer->id;
2788 reply->lparam = timer->lparam;
2789 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2790 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2791 set_event( current->process->idle_event );
2792 return;
2795 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2796 queue->wake_mask = req->wake_mask;
2797 queue->changed_mask = req->changed_mask;
2798 set_error( STATUS_PENDING ); /* FIXME */
2802 /* reply to a sent message */
2803 DECL_HANDLER(reply_message)
2805 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2806 else if (current->queue->recv_result)
2807 reply_message( current->queue, req->result, 0, req->remove,
2808 get_req_data(), get_req_data_size() );
2812 /* accept the current hardware message */
2813 DECL_HANDLER(accept_hardware_message)
2815 if (current->queue)
2816 release_hardware_message( current->queue, req->hw_id );
2817 else
2818 set_error( STATUS_ACCESS_DENIED );
2822 /* retrieve the reply for the last message sent */
2823 DECL_HANDLER(get_message_reply)
2825 struct message_result *result;
2826 struct list *entry;
2827 struct msg_queue *queue = current->queue;
2829 if (queue)
2831 set_error( STATUS_PENDING );
2832 reply->result = 0;
2834 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2836 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2837 if (result->replied || req->cancel)
2839 if (result->replied)
2841 reply->result = result->result;
2842 set_error( result->error );
2843 if (result->data)
2845 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2846 set_reply_data_ptr( result->data, data_len );
2847 result->data = NULL;
2848 result->data_size = 0;
2851 remove_result_from_sender( result );
2853 entry = list_head( &queue->send_result );
2854 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2855 else
2857 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2858 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2859 else clear_queue_bits( queue, QS_SMRESULT );
2863 else set_error( STATUS_ACCESS_DENIED );
2867 /* set a window timer */
2868 DECL_HANDLER(set_win_timer)
2870 struct timer *timer;
2871 struct msg_queue *queue;
2872 struct thread *thread = NULL;
2873 user_handle_t win = 0;
2874 lparam_t id = req->id;
2876 if (req->win)
2878 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2880 set_error( STATUS_INVALID_HANDLE );
2881 return;
2883 if (thread->process != current->process)
2885 release_object( thread );
2886 set_error( STATUS_ACCESS_DENIED );
2887 return;
2889 queue = thread->queue;
2890 /* remove it if it existed already */
2891 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2893 else
2895 queue = get_current_queue();
2896 /* look for a timer with this id */
2897 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2899 /* free and reuse id */
2900 free_timer( queue, timer );
2902 else
2904 lparam_t end_id = queue->next_timer_id;
2906 /* find a free id for it */
2907 while (1)
2909 id = queue->next_timer_id;
2910 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2912 if (!find_timer( queue, 0, req->msg, id )) break;
2913 if (queue->next_timer_id == end_id)
2915 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2916 return;
2922 if ((timer = set_timer( queue, req->rate )))
2924 timer->win = win;
2925 timer->msg = req->msg;
2926 timer->id = id;
2927 timer->lparam = req->lparam;
2928 reply->id = id;
2930 if (thread) release_object( thread );
2933 /* kill a window timer */
2934 DECL_HANDLER(kill_win_timer)
2936 struct timer *timer;
2937 struct thread *thread;
2938 user_handle_t win = 0;
2940 if (req->win)
2942 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2944 set_error( STATUS_INVALID_HANDLE );
2945 return;
2947 if (thread->process != current->process)
2949 release_object( thread );
2950 set_error( STATUS_ACCESS_DENIED );
2951 return;
2954 else thread = (struct thread *)grab_object( current );
2956 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2957 free_timer( thread->queue, timer );
2958 else
2959 set_error( STATUS_INVALID_PARAMETER );
2961 release_object( thread );
2964 DECL_HANDLER(register_hotkey)
2966 struct desktop *desktop;
2967 user_handle_t win_handle = req->window;
2968 struct hotkey *hotkey;
2969 struct hotkey *new_hotkey = NULL;
2970 struct thread *thread;
2971 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2973 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2975 if (win_handle)
2977 if (!(win_handle = get_valid_window_handle( win_handle )))
2979 release_object( desktop );
2980 return;
2983 thread = get_window_thread( win_handle );
2984 if (thread) release_object( thread );
2986 if (thread != current)
2988 release_object( desktop );
2989 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2990 return;
2994 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2996 if (req->vkey == hotkey->vkey &&
2997 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2999 release_object( desktop );
3000 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
3001 return;
3003 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
3004 new_hotkey = hotkey;
3007 if (new_hotkey)
3009 reply->replaced = 1;
3010 reply->flags = new_hotkey->flags;
3011 reply->vkey = new_hotkey->vkey;
3013 else
3015 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
3016 if (new_hotkey)
3018 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
3019 new_hotkey->queue = current->queue;
3020 new_hotkey->win = win_handle;
3021 new_hotkey->id = req->id;
3025 if (new_hotkey)
3027 new_hotkey->flags = req->flags;
3028 new_hotkey->vkey = req->vkey;
3031 release_object( desktop );
3034 DECL_HANDLER(unregister_hotkey)
3036 struct desktop *desktop;
3037 user_handle_t win_handle = req->window;
3038 struct hotkey *hotkey;
3039 struct thread *thread;
3041 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3043 if (win_handle)
3045 if (!(win_handle = get_valid_window_handle( win_handle )))
3047 release_object( desktop );
3048 return;
3051 thread = get_window_thread( win_handle );
3052 if (thread) release_object( thread );
3054 if (thread != current)
3056 release_object( desktop );
3057 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
3058 return;
3062 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
3064 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
3065 goto found;
3068 release_object( desktop );
3069 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
3070 return;
3072 found:
3073 reply->flags = hotkey->flags;
3074 reply->vkey = hotkey->vkey;
3075 list_remove( &hotkey->entry );
3076 free( hotkey );
3077 release_object( desktop );
3080 /* attach (or detach) thread inputs */
3081 DECL_HANDLER(attach_thread_input)
3083 struct thread *thread_from = get_thread_from_id( req->tid_from );
3084 struct thread *thread_to = get_thread_from_id( req->tid_to );
3086 if (!thread_from || !thread_to)
3088 if (thread_from) release_object( thread_from );
3089 if (thread_to) release_object( thread_to );
3090 return;
3092 if (thread_from != thread_to)
3094 if (req->attach)
3096 if ((thread_to->queue || thread_to == current) &&
3097 (thread_from->queue || thread_from == current))
3098 attach_thread_input( thread_from, thread_to );
3099 else
3100 set_error( STATUS_INVALID_PARAMETER );
3102 else
3104 if (thread_from->queue && thread_to->queue &&
3105 thread_from->queue->input == thread_to->queue->input)
3106 detach_thread_input( thread_from );
3107 else
3108 set_error( STATUS_ACCESS_DENIED );
3111 else set_error( STATUS_ACCESS_DENIED );
3112 release_object( thread_from );
3113 release_object( thread_to );
3117 /* get thread input data */
3118 DECL_HANDLER(get_thread_input)
3120 struct thread *thread = NULL;
3121 struct desktop *desktop;
3122 struct thread_input *input;
3124 if (req->tid)
3126 if (!(thread = get_thread_from_id( req->tid ))) return;
3127 if (!(desktop = get_thread_desktop( thread, 0 )))
3129 release_object( thread );
3130 return;
3132 input = thread->queue ? thread->queue->input : NULL;
3134 else
3136 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3137 input = desktop->foreground_input; /* get the foreground thread info */
3140 if (input)
3142 reply->focus = input->focus;
3143 reply->capture = input->capture;
3144 reply->active = input->active;
3145 reply->menu_owner = input->menu_owner;
3146 reply->move_size = input->move_size;
3147 reply->caret = input->caret;
3148 reply->cursor = input->cursor;
3149 reply->show_count = input->cursor_count;
3150 reply->rect = input->caret_rect;
3153 /* foreground window is active window of foreground thread */
3154 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
3155 if (thread) release_object( thread );
3156 release_object( desktop );
3160 /* retrieve queue keyboard state for current thread or global async state */
3161 DECL_HANDLER(get_key_state)
3163 struct desktop *desktop;
3164 data_size_t size = min( 256, get_reply_max_size() );
3166 if (req->async) /* get global async key state */
3168 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3169 if (req->key >= 0)
3171 reply->state = desktop->keystate[req->key & 0xff];
3172 desktop->keystate[req->key & 0xff] &= ~0x40;
3174 set_reply_data( desktop->keystate, size );
3175 release_object( desktop );
3177 else
3179 struct msg_queue *queue = get_current_queue();
3180 unsigned char *keystate = queue->input->keystate;
3181 if (req->key >= 0)
3183 sync_input_keystate( queue->input );
3184 reply->state = keystate[req->key & 0xff];
3186 set_reply_data( keystate, size );
3191 /* set queue keyboard state for current thread */
3192 DECL_HANDLER(set_key_state)
3194 struct desktop *desktop;
3195 struct msg_queue *queue = get_current_queue();
3196 data_size_t size = min( 256, get_req_data_size() );
3198 memcpy( queue->input->keystate, get_req_data(), size );
3199 memcpy( queue->input->desktop_keystate, queue->input->desktop->keystate, 256 );
3200 if (req->async && (desktop = get_thread_desktop( current, 0 )))
3202 memcpy( desktop->keystate, get_req_data(), size );
3203 release_object( desktop );
3208 /* set the system foreground window */
3209 DECL_HANDLER(set_foreground_window)
3211 struct thread *thread = NULL;
3212 struct desktop *desktop;
3213 struct msg_queue *queue = get_current_queue();
3215 if (!(desktop = get_thread_desktop( current, 0 ))) return;
3216 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
3217 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
3218 reply->send_msg_new = FALSE;
3220 if (is_valid_foreground_window( req->handle ) &&
3221 (thread = get_window_thread( req->handle )) &&
3222 thread->queue->input->desktop == desktop)
3224 set_foreground_input( desktop, thread->queue->input );
3225 reply->send_msg_new = (desktop->foreground_input != queue->input);
3227 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
3229 if (thread) release_object( thread );
3230 release_object( desktop );
3234 /* set the current thread focus window */
3235 DECL_HANDLER(set_focus_window)
3237 struct msg_queue *queue = get_current_queue();
3239 reply->previous = 0;
3240 if (queue && check_queue_input_window( queue, req->handle ))
3242 reply->previous = queue->input->focus;
3243 queue->input->focus = get_user_full_handle( req->handle );
3248 /* set the current thread active window */
3249 DECL_HANDLER(set_active_window)
3251 struct msg_queue *queue = get_current_queue();
3253 reply->previous = 0;
3254 if (queue && check_queue_input_window( queue, req->handle ))
3256 if (!req->handle || make_window_active( req->handle ))
3258 reply->previous = queue->input->active;
3259 queue->input->active = get_user_full_handle( req->handle );
3261 else set_error( STATUS_INVALID_HANDLE );
3266 /* set the current thread capture window */
3267 DECL_HANDLER(set_capture_window)
3269 struct msg_queue *queue = get_current_queue();
3271 reply->previous = reply->full_handle = 0;
3272 if (queue && check_queue_input_window( queue, req->handle ))
3274 struct thread_input *input = queue->input;
3276 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
3277 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
3279 set_error(STATUS_ACCESS_DENIED);
3280 return;
3282 reply->previous = input->capture;
3283 input->capture = get_user_full_handle( req->handle );
3284 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
3285 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
3286 reply->full_handle = input->capture;
3291 /* Set the current thread caret window */
3292 DECL_HANDLER(set_caret_window)
3294 struct msg_queue *queue = get_current_queue();
3296 reply->previous = 0;
3297 if (queue && check_queue_input_window( queue, req->handle ))
3299 struct thread_input *input = queue->input;
3301 reply->previous = input->caret;
3302 reply->old_rect = input->caret_rect;
3303 reply->old_hide = input->caret_hide;
3304 reply->old_state = input->caret_state;
3306 set_caret_window( input, get_user_full_handle(req->handle) );
3307 input->caret_rect.right = input->caret_rect.left + req->width;
3308 input->caret_rect.bottom = input->caret_rect.top + req->height;
3313 /* Set the current thread caret information */
3314 DECL_HANDLER(set_caret_info)
3316 struct msg_queue *queue = get_current_queue();
3317 struct thread_input *input;
3319 if (!queue) return;
3320 input = queue->input;
3321 reply->full_handle = input->caret;
3322 reply->old_rect = input->caret_rect;
3323 reply->old_hide = input->caret_hide;
3324 reply->old_state = input->caret_state;
3326 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3328 set_error( STATUS_ACCESS_DENIED );
3329 return;
3331 if (req->flags & SET_CARET_POS)
3333 input->caret_rect.right += req->x - input->caret_rect.left;
3334 input->caret_rect.bottom += req->y - input->caret_rect.top;
3335 input->caret_rect.left = req->x;
3336 input->caret_rect.top = req->y;
3338 if (req->flags & SET_CARET_HIDE)
3340 input->caret_hide += req->hide;
3341 if (input->caret_hide < 0) input->caret_hide = 0;
3343 if (req->flags & SET_CARET_STATE)
3345 switch (req->state)
3347 case CARET_STATE_OFF: input->caret_state = 0; break;
3348 case CARET_STATE_ON: input->caret_state = 1; break;
3349 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3350 case CARET_STATE_ON_IF_MOVED:
3351 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3352 break;
3358 /* get the time of the last input event */
3359 DECL_HANDLER(get_last_input_time)
3361 reply->time = last_input_time;
3364 /* set/get the current cursor */
3365 DECL_HANDLER(set_cursor)
3367 struct msg_queue *queue = get_current_queue();
3368 struct thread_input *input;
3369 struct desktop *desktop;
3371 if (!queue) return;
3372 input = queue->input;
3373 desktop = input->desktop;
3375 reply->prev_handle = input->cursor;
3376 reply->prev_count = input->cursor_count;
3377 reply->prev_x = desktop->cursor.x;
3378 reply->prev_y = desktop->cursor.y;
3380 if (req->flags & SET_CURSOR_HANDLE)
3382 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3384 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3385 return;
3387 input->cursor = req->handle;
3389 if (req->flags & SET_CURSOR_COUNT)
3391 queue->cursor_count += req->show_count;
3392 input->cursor_count += req->show_count;
3394 if (req->flags & SET_CURSOR_POS) set_cursor_pos( desktop, req->x, req->y );
3395 if (req->flags & SET_CURSOR_CLIP) set_clip_rectangle( desktop, &req->clip, req->flags, 0 );
3396 if (req->flags & SET_CURSOR_NOCLIP) set_clip_rectangle( desktop, NULL, SET_CURSOR_NOCLIP, 0 );
3398 if (req->flags & (SET_CURSOR_HANDLE | SET_CURSOR_COUNT))
3400 if (input->cursor_count < 0) update_desktop_cursor_handle( desktop, 0 );
3401 else update_desktop_cursor_handle( desktop, input->cursor );
3404 reply->new_x = desktop->cursor.x;
3405 reply->new_y = desktop->cursor.y;
3406 reply->new_clip = desktop->cursor.clip;
3407 reply->last_change = desktop->cursor.last_change;
3410 /* Get the history of the 64 last cursor positions */
3411 DECL_HANDLER(get_cursor_history)
3413 cursor_pos_t *pos;
3414 unsigned int i, count = min( 64, get_reply_max_size() / sizeof(*pos) );
3416 if ((pos = set_reply_data_size( count * sizeof(*pos) )))
3417 for (i = 0; i < count; i++)
3418 pos[i] = cursor_history[(i + cursor_history_latest) % ARRAY_SIZE(cursor_history)];
3421 DECL_HANDLER(get_rawinput_buffer)
3423 struct thread_input *input = current->queue->input;
3424 data_size_t size = 0, next_size = 0, pos = 0;
3425 struct list *ptr;
3426 char *buf, *tmp;
3427 int count = 0, buf_size = 16 * sizeof(struct hardware_msg_data);
3429 if (!req->buffer_size) buf = NULL;
3430 else if (!(buf = mem_alloc( buf_size ))) return;
3432 ptr = list_head( &input->msg_list );
3433 while (ptr)
3435 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
3436 struct hardware_msg_data *data = msg->data;
3437 data_size_t extra_size = data->size - sizeof(*data);
3439 ptr = list_next( &input->msg_list, ptr );
3440 if (msg->msg != WM_INPUT) continue;
3442 next_size = req->rawinput_size + extra_size;
3443 if (size + next_size > req->buffer_size) break;
3444 if (pos + data->size > get_reply_max_size()) break;
3445 if (pos + data->size > buf_size)
3447 buf_size += buf_size / 2 + extra_size;
3448 if (!(tmp = realloc( buf, buf_size )))
3450 free( buf );
3451 set_error( STATUS_NO_MEMORY );
3452 return;
3454 buf = tmp;
3457 memcpy( buf + pos, data, data->size );
3458 list_remove( &msg->entry );
3459 free_message( msg );
3461 size += next_size;
3462 pos += sizeof(*data) + extra_size;
3463 count++;
3466 reply->next_size = next_size;
3467 reply->count = count;
3468 set_reply_data_ptr( buf, pos );
3471 DECL_HANDLER(update_rawinput_devices)
3473 const struct rawinput_device *tmp, *devices = get_req_data();
3474 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3475 size_t size = device_count * sizeof(*devices);
3476 struct process *process = current->process;
3478 if (!size)
3480 process->rawinput_device_count = 0;
3481 process->rawinput_mouse = NULL;
3482 process->rawinput_kbd = NULL;
3483 return;
3486 if (!(tmp = realloc( process->rawinput_devices, size )))
3488 set_error( STATUS_NO_MEMORY );
3489 return;
3491 process->rawinput_devices = (struct rawinput_device *)tmp;
3492 process->rawinput_device_count = device_count;
3493 memcpy( process->rawinput_devices, devices, size );
3495 process->rawinput_mouse = find_rawinput_device( process, 1, 2 );
3496 process->rawinput_kbd = find_rawinput_device( process, 1, 6 );