blob: c5841c939be87fb165bff0d078edbbe566a412ef [file] [log] [blame]
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001/*
2 * Server-side message queues
3 *
4 * Copyright (C) 2000 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
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.
10 *
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.
15 *
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliardc5e433a2000-05-30 19:48:18 +000019 */
20
Alexandre Julliard5769d1d2002-04-26 19:05:15 +000021#include "config.h"
22#include "wine/port.h"
23
Alexandre Julliardc5e433a2000-05-30 19:48:18 +000024#include <assert.h>
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000025#include <stdarg.h>
Alexandre Julliardc5e433a2000-05-30 19:48:18 +000026#include <stdio.h>
27#include <stdlib.h>
28
Ge van Geldorp1a1583a2005-11-28 17:32:54 +010029#include "ntstatus.h"
30#define WIN32_NO_STATUS
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000031#include "windef.h"
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000032#include "winbase.h"
33#include "wingdi.h"
34#include "winuser.h"
Ge van Geldorp1a1583a2005-11-28 17:32:54 +010035#include "winternl.h"
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000036
Alexandre Julliardc5e433a2000-05-30 19:48:18 +000037#include "handle.h"
Alexandre Julliarde66207e2003-02-19 00:33:32 +000038#include "file.h"
Alexandre Julliardc5e433a2000-05-30 19:48:18 +000039#include "thread.h"
40#include "process.h"
41#include "request.h"
Alexandre Julliard1a66d222001-08-28 18:44:52 +000042#include "user.h"
Alexandre Julliardc5e433a2000-05-30 19:48:18 +000043
Alexandre Julliardcf2f1422005-03-14 17:17:09 +000044#define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
Alexandre Julliardcb7aa872005-03-24 19:16:54 +000045#define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
Alexandre Julliardcf2f1422005-03-14 17:17:09 +000046
Alexandre Julliard242e3952003-01-08 00:27:58 +000047enum message_kind { SEND_MESSAGE, POST_MESSAGE };
48#define NB_MSG_KINDS (POST_MESSAGE+1)
Alexandre Julliardd253c582001-08-07 19:19:08 +000049
50
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000051struct message_result
52{
Alexandre Julliard039e1312003-07-26 20:36:43 +000053 struct list sender_entry; /* entry in sender list */
Alexandre Julliard127127f2005-09-13 14:46:46 +000054 struct message *msg; /* message the result is for */
Alexandre Julliard039e1312003-07-26 20:36:43 +000055 struct message_result *recv_next; /* next in receiver list */
56 struct msg_queue *sender; /* sender queue */
57 struct msg_queue *receiver; /* receiver queue */
58 int replied; /* has it been replied to? */
Alexandre Julliard039e1312003-07-26 20:36:43 +000059 unsigned int error; /* error code to pass back to sender */
Alexandre Julliard31282b32008-12-10 16:01:50 +010060 lparam_t result; /* reply result */
Alexandre Julliard039e1312003-07-26 20:36:43 +000061 struct message *callback_msg; /* message to queue for callback */
62 void *data; /* message reply data */
63 unsigned int data_size; /* size of message reply data */
64 struct timeout_user *timeout; /* result timeout */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000065};
66
67struct message
68{
Alexandre Julliard8e3b0722005-02-25 21:05:11 +000069 struct list entry; /* entry in message list */
Alexandre Julliardd253c582001-08-07 19:19:08 +000070 enum message_type type; /* message type */
Alexandre Julliard1a66d222001-08-28 18:44:52 +000071 user_handle_t win; /* window handle */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000072 unsigned int msg; /* message code */
Alexandre Julliard31282b32008-12-10 16:01:50 +010073 lparam_t wparam; /* parameters */
74 lparam_t lparam; /* parameters */
Alexandre Julliard838d65a2001-06-19 19:16:41 +000075 unsigned int time; /* message time */
Alexandre Julliardd253c582001-08-07 19:19:08 +000076 void *data; /* message data for sent messages */
77 unsigned int data_size; /* size of message data */
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +000078 unsigned int unique_id; /* unique id for nested hw message waits */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000079 struct message_result *result; /* result in sender queue */
80};
81
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000082struct timer
83{
Alexandre Julliardff986a52004-11-29 18:08:18 +000084 struct list entry; /* entry in timer list */
Alexandre Julliardaaf477f2007-04-17 20:08:59 +020085 timeout_t when; /* next expiration */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000086 unsigned int rate; /* timer rate in ms */
Alexandre Julliard1a66d222001-08-28 18:44:52 +000087 user_handle_t win; /* window handle */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000088 unsigned int msg; /* message to post */
Alexandre Julliard31282b32008-12-10 16:01:50 +010089 lparam_t id; /* timer id */
90 lparam_t lparam; /* lparam for message */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000091};
92
Alexandre Julliardab5063b2002-10-11 18:50:15 +000093struct thread_input
94{
95 struct object obj; /* object header */
Alexandre Julliard5ad90c02005-07-11 13:30:23 +000096 struct desktop *desktop; /* desktop that this thread input belongs to */
Alexandre Julliardab5063b2002-10-11 18:50:15 +000097 user_handle_t focus; /* focus window */
98 user_handle_t capture; /* capture window */
99 user_handle_t active; /* active window */
100 user_handle_t menu_owner; /* current menu owner window */
101 user_handle_t move_size; /* current moving/resizing window */
102 user_handle_t caret; /* caret window */
Alexandre Julliard11e35232002-10-17 01:24:33 +0000103 rectangle_t caret_rect; /* caret rectangle */
104 int caret_hide; /* caret hide count */
105 int caret_state; /* caret on/off state */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000106 struct list msg_list; /* list of hardware messages */
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000107 unsigned char keystate[256]; /* state of each key */
108};
109
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000110struct msg_queue
111{
Alexandre Julliard039e1312003-07-26 20:36:43 +0000112 struct object obj; /* object header */
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200113 struct fd *fd; /* optional file descriptor to poll */
Alexandre Julliard039e1312003-07-26 20:36:43 +0000114 unsigned int wake_bits; /* wakeup bits */
115 unsigned int wake_mask; /* wakeup mask */
116 unsigned int changed_bits; /* changed wakeup bits */
117 unsigned int changed_mask; /* changed wakeup mask */
118 int paint_count; /* pending paint messages count */
Robert Shearmana40ce392006-01-17 13:14:31 +0100119 int quit_message; /* is there a pending quit message? */
120 int exit_code; /* exit code of pending quit message */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000121 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
Alexandre Julliard039e1312003-07-26 20:36:43 +0000122 struct list send_result; /* stack of sent messages waiting for result */
123 struct list callback_result; /* list of callback messages waiting for result */
124 struct message_result *recv_result; /* stack of received messages waiting for result */
Alexandre Julliardff986a52004-11-29 18:08:18 +0000125 struct list pending_timers; /* list of pending timers */
126 struct list expired_timers; /* list of expired timers */
Alexandre Julliard31282b32008-12-10 16:01:50 +0100127 lparam_t next_timer_id; /* id for the next timer with a 0 window */
Alexandre Julliard039e1312003-07-26 20:36:43 +0000128 struct timeout_user *timeout; /* timeout for next timer to expire */
129 struct thread_input *input; /* thread input descriptor */
130 struct hook_table *hooks; /* hook table */
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200131 timeout_t last_get_msg; /* time of last get message call */
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000132};
133
134static void msg_queue_dump( struct object *obj, int verbose );
135static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
136static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
137static int msg_queue_signaled( struct object *obj, struct thread *thread );
138static int msg_queue_satisfied( struct object *obj, struct thread *thread );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000139static void msg_queue_destroy( struct object *obj );
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200140static void msg_queue_poll_event( struct fd *fd, int event );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000141static void thread_input_dump( struct object *obj, int verbose );
142static void thread_input_destroy( struct object *obj );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000143static void timer_callback( void *private );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000144
145static const struct object_ops msg_queue_ops =
146{
147 sizeof(struct msg_queue), /* size */
148 msg_queue_dump, /* dump */
Alexandre Julliard8382eb02007-12-05 18:16:42 +0100149 no_get_type, /* get_type */
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000150 msg_queue_add_queue, /* add_queue */
151 msg_queue_remove_queue, /* remove_queue */
152 msg_queue_signaled, /* signaled */
153 msg_queue_satisfied, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +0000154 no_signal, /* signal */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +0000155 no_get_fd, /* get_fd */
Alexandre Julliard28beba32005-12-12 14:57:40 +0100156 no_map_access, /* map_access */
Rob Shearmanc1707d82007-10-03 13:10:37 +0100157 default_get_sd, /* get_sd */
158 default_set_sd, /* set_sd */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000159 no_lookup_name, /* lookup_name */
Alexandre Julliard7e71c1d2007-03-22 11:44:29 +0100160 no_open_file, /* open_file */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000161 no_close_handle, /* close_handle */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000162 msg_queue_destroy /* destroy */
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000163};
164
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200165static const struct fd_ops msg_queue_fd_ops =
166{
167 NULL, /* get_poll_events */
168 msg_queue_poll_event, /* poll_event */
Alexandre Julliard3f057592007-04-12 20:21:53 +0200169 NULL, /* flush */
170 NULL, /* get_fd_type */
Alexandre Julliard63571432007-04-16 14:45:03 +0200171 NULL, /* ioctl */
Alexandre Julliard3f057592007-04-12 20:21:53 +0200172 NULL, /* queue_async */
Alexandre Julliard72bff2e2007-04-10 17:07:27 +0200173 NULL, /* reselect_async */
Alexandre Julliard3f057592007-04-12 20:21:53 +0200174 NULL /* cancel async */
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200175};
176
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000177
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000178static const struct object_ops thread_input_ops =
179{
180 sizeof(struct thread_input), /* size */
181 thread_input_dump, /* dump */
Alexandre Julliard8382eb02007-12-05 18:16:42 +0100182 no_get_type, /* get_type */
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000183 no_add_queue, /* add_queue */
184 NULL, /* remove_queue */
185 NULL, /* signaled */
186 NULL, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +0000187 no_signal, /* signal */
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000188 no_get_fd, /* get_fd */
Alexandre Julliard28beba32005-12-12 14:57:40 +0100189 no_map_access, /* map_access */
Rob Shearmanc1707d82007-10-03 13:10:37 +0100190 default_get_sd, /* get_sd */
191 default_set_sd, /* set_sd */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000192 no_lookup_name, /* lookup_name */
Alexandre Julliard7e71c1d2007-03-22 11:44:29 +0100193 no_open_file, /* open_file */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000194 no_close_handle, /* close_handle */
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000195 thread_input_destroy /* destroy */
196};
197
Alexandre Julliard242e3952003-01-08 00:27:58 +0000198/* pointer to input structure of foreground thread */
199static struct thread_input *foreground_input;
Mike McCormackabe70f72005-04-28 12:04:14 +0000200static unsigned int last_input_time;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000201
Alexandre Julliard3ad97982006-10-04 20:25:42 +0200202static void free_message( struct message *msg );
Alexandre Julliard11e35232002-10-17 01:24:33 +0000203
204/* set the caret window in a given thread input */
205static void set_caret_window( struct thread_input *input, user_handle_t win )
206{
Krzysztof Foltmanb8501722005-02-18 20:02:55 +0000207 if (!win || win != input->caret)
208 {
209 input->caret_rect.left = 0;
210 input->caret_rect.top = 0;
211 input->caret_rect.right = 0;
212 input->caret_rect.bottom = 0;
213 }
Alexandre Julliard11e35232002-10-17 01:24:33 +0000214 input->caret = win;
Alexandre Julliard11e35232002-10-17 01:24:33 +0000215 input->caret_hide = 1;
216 input->caret_state = 0;
217}
218
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000219/* create a thread input object */
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000220static struct thread_input *create_thread_input( struct thread *thread )
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000221{
222 struct thread_input *input;
223
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000224 if ((input = alloc_object( &thread_input_ops )))
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000225 {
226 input->focus = 0;
227 input->capture = 0;
228 input->active = 0;
229 input->menu_owner = 0;
230 input->move_size = 0;
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000231 list_init( &input->msg_list );
Alexandre Julliard11e35232002-10-17 01:24:33 +0000232 set_caret_window( input, 0 );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000233 memset( input->keystate, 0, sizeof(input->keystate) );
Robert Shearmand8058fb2006-04-07 11:15:19 +0100234
235 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
236 {
237 release_object( input );
238 return NULL;
239 }
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000240 }
241 return input;
242}
243
Alexandre Julliard15ca8202004-06-17 20:00:25 +0000244/* release the thread input data of a given thread */
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +0000245static inline void release_thread_input( struct thread *thread )
Alexandre Julliard15ca8202004-06-17 20:00:25 +0000246{
247 struct thread_input *input = thread->queue->input;
248
249 if (!input) return;
Alexandre Julliard15ca8202004-06-17 20:00:25 +0000250 release_object( input );
251 thread->queue->input = NULL;
252}
253
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000254/* create a message queue object */
255static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000256{
257 struct msg_queue *queue;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000258 int i;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000259
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000260 if (!input && !(input = create_thread_input( thread ))) return NULL;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000261 if ((queue = alloc_object( &msg_queue_ops )))
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000262 {
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200263 queue->fd = NULL;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000264 queue->wake_bits = 0;
265 queue->wake_mask = 0;
266 queue->changed_bits = 0;
267 queue->changed_mask = 0;
Alexandre Julliard4b0343d2001-06-20 22:55:31 +0000268 queue->paint_count = 0;
Robert Shearmana40ce392006-01-17 13:14:31 +0100269 queue->quit_message = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000270 queue->recv_result = NULL;
Dmitry Timoshkovb4227d72007-11-13 20:41:01 +0800271 queue->next_timer_id = 0x7fff;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000272 queue->timeout = NULL;
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000273 queue->input = (struct thread_input *)grab_object( input );
Alexandre Julliardd55e7f12003-07-03 18:16:48 +0000274 queue->hooks = NULL;
Alexandre Julliard753c8702006-08-10 16:42:09 +0200275 queue->last_get_msg = current_time;
Alexandre Julliard039e1312003-07-26 20:36:43 +0000276 list_init( &queue->send_result );
277 list_init( &queue->callback_result );
Alexandre Julliardff986a52004-11-29 18:08:18 +0000278 list_init( &queue->pending_timers );
279 list_init( &queue->expired_timers );
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000280 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000281
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000282 thread->queue = queue;
283 if (!thread->process->queue)
284 thread->process->queue = (struct msg_queue *)grab_object( queue );
285 }
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000286 release_object( input );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000287 return queue;
288}
289
Alexandre Julliard31022d62002-08-16 23:30:41 +0000290/* free the message queue of a thread at thread exit */
291void free_msg_queue( struct thread *thread )
292{
293 struct process *process = thread->process;
294
Alexandre Julliardca3ac8f2003-07-11 21:55:58 +0000295 remove_thread_hooks( thread );
Alexandre Julliard31022d62002-08-16 23:30:41 +0000296 if (!thread->queue) return;
297 if (process->queue == thread->queue) /* is it the process main queue? */
298 {
299 release_object( process->queue );
300 process->queue = NULL;
301 if (process->idle_event)
302 {
303 set_event( process->idle_event );
304 release_object( process->idle_event );
305 process->idle_event = NULL;
306 }
307 }
308 release_object( thread->queue );
309 thread->queue = NULL;
310}
311
Alexandre Julliardd55e7f12003-07-03 18:16:48 +0000312/* get the hook table for a given thread */
313struct hook_table *get_queue_hooks( struct thread *thread )
314{
315 if (!thread->queue) return NULL;
316 return thread->queue->hooks;
317}
318
319/* set the hook table for a given thread, allocating the queue if needed */
320void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
321{
322 struct msg_queue *queue = thread->queue;
Alexandre Julliardd70a2532005-04-26 14:31:33 +0000323 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
Alexandre Julliardd55e7f12003-07-03 18:16:48 +0000324 if (queue->hooks) release_object( queue->hooks );
325 queue->hooks = hooks;
326}
327
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000328/* check the queue status */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000329static inline int is_signaled( struct msg_queue *queue )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000330{
331 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
332}
333
Alexandre Julliardd253c582001-08-07 19:19:08 +0000334/* set some queue bits */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000335static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000336{
Alexandre Julliardd253c582001-08-07 19:19:08 +0000337 queue->wake_bits |= bits;
338 queue->changed_bits |= bits;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000339 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
340}
341
Alexandre Julliardd253c582001-08-07 19:19:08 +0000342/* clear some queue bits */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000343static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
Alexandre Julliardd253c582001-08-07 19:19:08 +0000344{
345 queue->wake_bits &= ~bits;
346 queue->changed_bits &= ~bits;
347}
348
Alexandre Julliard242e3952003-01-08 00:27:58 +0000349/* check whether msg is a keyboard message */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000350static inline int is_keyboard_msg( struct message *msg )
Alexandre Julliard242e3952003-01-08 00:27:58 +0000351{
352 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
353}
354
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +0000355/* check if message is matched by the filter */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000356static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +0000357{
358 return (msg >= first && msg <= last);
359}
360
Alexandre Julliardcf2f1422005-03-14 17:17:09 +0000361/* check whether a message filter contains at least one potential hardware message */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000362static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
Alexandre Julliardcf2f1422005-03-14 17:17:09 +0000363{
364 /* hardware message ranges are (in numerical order):
365 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
366 * WM_KEYFIRST .. WM_KEYLAST
367 * WM_MOUSEFIRST .. WM_MOUSELAST
368 */
369 if (last < WM_NCMOUSEFIRST) return 0;
370 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
371 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
372 if (first > WM_MOUSELAST) return 0;
373 return 1;
374}
375
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000376/* get the QS_* bit corresponding to a given hardware message */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000377static inline int get_hardware_msg_bit( struct message *msg )
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000378{
379 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000380 if (is_keyboard_msg( msg )) return QS_KEY;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000381 return QS_MOUSEBUTTON;
382}
383
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000384/* get the current thread queue, creating it if needed */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000385static inline struct msg_queue *get_current_queue(void)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000386{
387 struct msg_queue *queue = current->queue;
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000388 if (!queue) queue = create_msg_queue( current, NULL );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000389 return queue;
390}
391
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +0000392/* get a (pseudo-)unique id to tag hardware messages */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000393static inline unsigned int get_unique_id(void)
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +0000394{
395 static unsigned int id;
396 if (!++id) id = 1; /* avoid an id of 0 */
397 return id;
398}
399
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000400/* try to merge a message with the last in the list; return 1 if successful */
Alexandre Julliard242e3952003-01-08 00:27:58 +0000401static int merge_message( struct thread_input *input, const struct message *msg )
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000402{
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000403 struct message *prev;
404 struct list *ptr = list_tail( &input->msg_list );
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000405
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000406 if (!ptr) return 0;
407 prev = LIST_ENTRY( ptr, struct message, entry );
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000408 if (prev->result) return 0;
Alexandre Julliard942a70d2007-11-27 16:43:00 +0100409 if (prev->win && msg->win && prev->win != msg->win) return 0;
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000410 if (prev->msg != msg->msg) return 0;
411 if (prev->type != msg->type) return 0;
412 /* now we can merge it */
413 prev->wparam = msg->wparam;
414 prev->lparam = msg->lparam;
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000415 prev->time = msg->time;
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +0100416 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
417 {
418 struct hardware_msg_data *prev_data = prev->data;
419 struct hardware_msg_data *msg_data = msg->data;
420 prev_data->x = msg_data->x;
421 prev_data->y = msg_data->y;
422 prev_data->info = msg_data->info;
423 }
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000424 return 1;
425}
426
Alexandre Julliardd253c582001-08-07 19:19:08 +0000427/* free a result structure */
428static void free_result( struct message_result *result )
429{
430 if (result->timeout) remove_timeout_user( result->timeout );
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +0200431 free( result->data );
Alexandre Julliard3ad97982006-10-04 20:25:42 +0200432 if (result->callback_msg) free_message( result->callback_msg );
Alexandre Julliardd253c582001-08-07 19:19:08 +0000433 free( result );
434}
435
Alexandre Julliard039e1312003-07-26 20:36:43 +0000436/* remove the result from the sender list it is on */
437static inline void remove_result_from_sender( struct message_result *result )
438{
439 assert( result->sender );
440
441 list_remove( &result->sender_entry );
442 result->sender = NULL;
443 if (!result->receiver) free_result( result );
444}
445
Alexandre Julliardd253c582001-08-07 19:19:08 +0000446/* store the message result in the appropriate structure */
Alexandre Julliard31282b32008-12-10 16:01:50 +0100447static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
Alexandre Julliardd253c582001-08-07 19:19:08 +0000448{
449 res->result = result;
450 res->error = error;
451 res->replied = 1;
452 if (res->timeout)
453 {
454 remove_timeout_user( res->timeout );
455 res->timeout = NULL;
456 }
Alexandre Julliard039e1312003-07-26 20:36:43 +0000457 if (res->sender)
458 {
459 if (res->callback_msg)
460 {
461 /* queue the callback message in the sender queue */
Alexandre Julliard29a3ce92006-10-04 16:29:45 +0200462 struct callback_msg_data *data = res->callback_msg->data;
463 data->result = result;
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000464 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
Alexandre Julliard039e1312003-07-26 20:36:43 +0000465 set_queue_bits( res->sender, QS_SENDMESSAGE );
466 res->callback_msg = NULL;
467 remove_result_from_sender( res );
468 }
469 else
470 {
471 /* wake sender queue if waiting on this result */
472 if (list_head(&res->sender->send_result) == &res->sender_entry)
473 set_queue_bits( res->sender, QS_SMRESULT );
474 }
475 }
476
Alexandre Julliardd253c582001-08-07 19:19:08 +0000477}
478
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000479/* free a message when deleting a queue or window */
480static void free_message( struct message *msg )
481{
482 struct message_result *result = msg->result;
483 if (result)
484 {
Alexandre Julliard127127f2005-09-13 14:46:46 +0000485 result->msg = NULL;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000486 if (result->sender)
487 {
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000488 result->receiver = NULL;
Alexandre Julliardd253c582001-08-07 19:19:08 +0000489 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000490 }
Alexandre Julliardd253c582001-08-07 19:19:08 +0000491 else free_result( result );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000492 }
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +0200493 free( msg->data );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000494 free( msg );
495}
496
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000497/* remove (and free) a message from a message list */
498static void remove_queue_message( struct msg_queue *queue, struct message *msg,
499 enum message_kind kind )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000500{
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000501 list_remove( &msg->entry );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000502 switch(kind)
503 {
504 case SEND_MESSAGE:
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000505 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000506 break;
507 case POST_MESSAGE:
Robert Shearman22bd7a32006-05-22 22:16:53 +0100508 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
509 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000510 break;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000511 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000512 free_message( msg );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000513}
514
Alexandre Julliardd253c582001-08-07 19:19:08 +0000515/* message timed out without getting a reply */
516static void result_timeout( void *private )
517{
518 struct message_result *result = private;
519
520 assert( !result->replied );
521
522 result->timeout = NULL;
Alexandre Julliard127127f2005-09-13 14:46:46 +0000523
524 if (result->msg) /* not received yet */
525 {
526 struct message *msg = result->msg;
527
528 result->msg = NULL;
529 msg->result = NULL;
530 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
531 result->receiver = NULL;
532 if (!result->sender)
533 {
534 free_result( result );
535 return;
536 }
537 }
538
Alexandre Julliardd253c582001-08-07 19:19:08 +0000539 store_message_result( result, 0, STATUS_TIMEOUT );
540}
541
542/* allocate and fill a message result structure */
543static struct message_result *alloc_message_result( struct msg_queue *send_queue,
544 struct msg_queue *recv_queue,
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200545 struct message *msg, timeout_t timeout )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000546{
547 struct message_result *result = mem_alloc( sizeof(*result) );
Alexandre Julliardd253c582001-08-07 19:19:08 +0000548 if (result)
549 {
Alexandre Julliard127127f2005-09-13 14:46:46 +0000550 result->msg = msg;
Alexandre Julliardd253c582001-08-07 19:19:08 +0000551 result->sender = send_queue;
552 result->receiver = recv_queue;
553 result->replied = 0;
554 result->data = NULL;
555 result->data_size = 0;
556 result->timeout = NULL;
Alexandre Julliard039e1312003-07-26 20:36:43 +0000557
558 if (msg->type == MSG_CALLBACK)
559 {
560 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
Alexandre Julliard29a3ce92006-10-04 16:29:45 +0200561
Alexandre Julliard039e1312003-07-26 20:36:43 +0000562 if (!callback_msg)
563 {
564 free( result );
565 return NULL;
566 }
567 callback_msg->type = MSG_CALLBACK_RESULT;
568 callback_msg->win = msg->win;
569 callback_msg->msg = msg->msg;
Alexandre Julliard29a3ce92006-10-04 16:29:45 +0200570 callback_msg->wparam = 0;
Alexandre Julliard039e1312003-07-26 20:36:43 +0000571 callback_msg->lparam = 0;
572 callback_msg->time = get_tick_count();
Alexandre Julliard039e1312003-07-26 20:36:43 +0000573 callback_msg->result = NULL;
Alexandre Julliard3ad97982006-10-04 20:25:42 +0200574 /* steal the data from the original message */
575 callback_msg->data = msg->data;
576 callback_msg->data_size = msg->data_size;
577 msg->data = NULL;
578 msg->data_size = 0;
Alexandre Julliard039e1312003-07-26 20:36:43 +0000579
580 result->callback_msg = callback_msg;
581 list_add_head( &send_queue->callback_result, &result->sender_entry );
582 }
583 else
584 {
585 result->callback_msg = NULL;
586 list_add_head( &send_queue->send_result, &result->sender_entry );
587 }
588
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200589 if (timeout != TIMEOUT_INFINITE)
590 result->timeout = add_timeout_user( timeout, result_timeout, result );
Alexandre Julliardd253c582001-08-07 19:19:08 +0000591 }
592 return result;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000593}
594
595/* receive a message, removing it from the sent queue */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000596static void receive_message( struct msg_queue *queue, struct message *msg,
597 struct get_message_reply *reply )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000598{
599 struct message_result *result = msg->result;
600
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000601 reply->total = msg->data_size;
602 if (msg->data_size > get_reply_max_size())
603 {
604 set_error( STATUS_BUFFER_OVERFLOW );
605 return;
606 }
607 reply->type = msg->type;
608 reply->win = msg->win;
609 reply->msg = msg->msg;
610 reply->wparam = msg->wparam;
611 reply->lparam = msg->lparam;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000612 reply->time = msg->time;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000613
614 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
615
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000616 list_remove( &msg->entry );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000617 /* put the result on the receiver result stack */
Alexandre Julliardd253c582001-08-07 19:19:08 +0000618 if (result)
619 {
Alexandre Julliard127127f2005-09-13 14:46:46 +0000620 result->msg = NULL;
Alexandre Julliardd253c582001-08-07 19:19:08 +0000621 result->recv_next = queue->recv_result;
622 queue->recv_result = result;
623 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000624 free( msg );
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000625 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000626}
627
628/* set the result of the current received message */
Alexandre Julliard31282b32008-12-10 16:01:50 +0100629static void reply_message( struct msg_queue *queue, lparam_t result,
Alexandre Julliard0f273c12006-07-26 10:43:25 +0200630 unsigned int error, int remove, const void *data, data_size_t len )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000631{
632 struct message_result *res = queue->recv_result;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000633
634 if (remove)
635 {
636 queue->recv_result = res->recv_next;
637 res->receiver = NULL;
638 if (!res->sender) /* no one waiting for it */
639 {
Alexandre Julliardd253c582001-08-07 19:19:08 +0000640 free_result( res );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000641 return;
642 }
643 }
644 if (!res->replied)
645 {
Alexandre Julliardd253c582001-08-07 19:19:08 +0000646 if (len && (res->data = memdup( data, len ))) res->data_size = len;
647 store_message_result( res, result, error );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000648 }
649}
650
Dmitry Timoshkovacb05662009-06-11 21:49:31 +0900651static int match_window( user_handle_t win, user_handle_t msg_win )
652{
653 if (!win) return 1;
654 if (win == (user_handle_t)-1) return !msg_win;
655 if (msg_win == win) return 1;
656 return is_child_window( win, msg_win );
657}
658
Alexandre Julliard242e3952003-01-08 00:27:58 +0000659/* retrieve a posted message */
660static int get_posted_message( struct msg_queue *queue, user_handle_t win,
661 unsigned int first, unsigned int last, unsigned int flags,
662 struct get_message_reply *reply )
663{
664 struct message *msg;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000665
666 /* check against the filters */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000667 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
Alexandre Julliard242e3952003-01-08 00:27:58 +0000668 {
Dmitry Timoshkovacb05662009-06-11 21:49:31 +0900669 if (!match_window( win, msg->win )) continue;
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +0000670 if (!check_msg_filter( msg->msg, first, last )) continue;
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000671 goto found; /* found one */
Alexandre Julliard242e3952003-01-08 00:27:58 +0000672 }
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000673 return 0;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000674
675 /* return it to the app */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000676found:
Alexandre Julliard242e3952003-01-08 00:27:58 +0000677 reply->total = msg->data_size;
678 if (msg->data_size > get_reply_max_size())
679 {
680 set_error( STATUS_BUFFER_OVERFLOW );
681 return 1;
682 }
683 reply->type = msg->type;
684 reply->win = msg->win;
685 reply->msg = msg->msg;
686 reply->wparam = msg->wparam;
687 reply->lparam = msg->lparam;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000688 reply->time = msg->time;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000689
Alexandre Julliard89faee02007-02-21 15:21:05 +0100690 if (flags & PM_REMOVE)
Alexandre Julliard242e3952003-01-08 00:27:58 +0000691 {
692 if (msg->data)
693 {
694 set_reply_data_ptr( msg->data, msg->data_size );
695 msg->data = NULL;
696 msg->data_size = 0;
697 }
698 remove_queue_message( queue, msg, POST_MESSAGE );
699 }
700 else if (msg->data) set_reply_data( msg->data, msg->data_size );
701
702 return 1;
703}
704
Robert Shearmana40ce392006-01-17 13:14:31 +0100705static int get_quit_message( struct msg_queue *queue, unsigned int flags,
706 struct get_message_reply *reply )
707{
708 if (queue->quit_message)
709 {
710 reply->total = 0;
711 reply->type = MSG_POSTED;
Alexandre Julliardd7641072008-12-08 16:57:38 +0100712 reply->win = 0;
Robert Shearmana40ce392006-01-17 13:14:31 +0100713 reply->msg = WM_QUIT;
714 reply->wparam = queue->exit_code;
715 reply->lparam = 0;
Robert Shearmana40ce392006-01-17 13:14:31 +0100716 reply->time = get_tick_count();
Robert Shearmana40ce392006-01-17 13:14:31 +0100717
Alexandre Julliard89faee02007-02-21 15:21:05 +0100718 if (flags & PM_REMOVE)
Robert Shearman22bd7a32006-05-22 22:16:53 +0100719 {
Robert Shearmana40ce392006-01-17 13:14:31 +0100720 queue->quit_message = 0;
Robert Shearman22bd7a32006-05-22 22:16:53 +0100721 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
722 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
723 }
Robert Shearmana40ce392006-01-17 13:14:31 +0100724 return 1;
725 }
726 else
727 return 0;
728}
729
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000730/* empty a message list and free all the messages */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000731static void empty_msg_list( struct list *list )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000732{
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000733 struct list *ptr;
734
735 while ((ptr = list_head( list )) != NULL)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000736 {
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000737 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
738 list_remove( &msg->entry );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000739 free_message( msg );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000740 }
741}
742
743/* cleanup all pending results when deleting a queue */
744static void cleanup_results( struct msg_queue *queue )
745{
Alexandre Julliard039e1312003-07-26 20:36:43 +0000746 struct list *entry;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000747
Alexandre Julliard039e1312003-07-26 20:36:43 +0000748 while ((entry = list_head( &queue->send_result )) != NULL)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000749 {
Alexandre Julliard039e1312003-07-26 20:36:43 +0000750 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
751 }
752
753 while ((entry = list_head( &queue->callback_result )) != NULL)
754 {
755 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000756 }
757
Alexandre Julliardd253c582001-08-07 19:19:08 +0000758 while (queue->recv_result)
759 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000760}
761
Alexandre Julliard09029b22003-07-11 04:09:42 +0000762/* check if the thread owning the queue is hung (not checking for messages) */
763static int is_queue_hung( struct msg_queue *queue )
764{
Alexandre Julliard09029b22003-07-11 04:09:42 +0000765 struct wait_queue_entry *entry;
766
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200767 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
Alexandre Julliard09029b22003-07-11 04:09:42 +0000768 return 0; /* less than 5 seconds since last get message -> not hung */
769
Alexandre Julliardaa347682005-03-01 11:49:58 +0000770 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
Alexandre Julliard09029b22003-07-11 04:09:42 +0000771 {
772 if (entry->thread->queue == queue)
773 return 0; /* thread is waiting on queue -> not hung */
774 }
775 return 1;
776}
777
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000778static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
779{
780 struct msg_queue *queue = (struct msg_queue *)obj;
781 struct process *process = entry->thread->process;
782
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000783 /* a thread can only wait on its own queue */
784 if (entry->thread->queue != queue)
785 {
786 set_error( STATUS_ACCESS_DENIED );
787 return 0;
788 }
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000789 /* if waiting on the main process queue, set the idle event */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000790 if (process->queue == queue)
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000791 {
792 if (process->idle_event) set_event( process->idle_event );
793 }
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200794 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
795 set_fd_events( queue->fd, POLLIN );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000796 add_queue( obj, entry );
797 return 1;
798}
799
800static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
801{
802 struct msg_queue *queue = (struct msg_queue *)obj;
803 struct process *process = entry->thread->process;
804
805 remove_queue( obj, entry );
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200806 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
807 set_fd_events( queue->fd, 0 );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000808
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000809 assert( entry->thread->queue == queue );
810
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000811 /* if waiting on the main process queue, reset the idle event */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000812 if (process->queue == queue)
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000813 {
814 if (process->idle_event) reset_event( process->idle_event );
815 }
816}
817
818static void msg_queue_dump( struct object *obj, int verbose )
819{
820 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000821 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
822 queue->wake_bits, queue->wake_mask );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000823}
824
825static int msg_queue_signaled( struct object *obj, struct thread *thread )
826{
827 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200828 int ret = 0;
829
830 if (queue->fd)
831 {
832 if ((ret = check_fd_events( queue->fd, POLLIN )))
833 /* stop waiting on select() if we are signaled */
834 set_fd_events( queue->fd, 0 );
835 else if (!list_empty( &obj->wait_queue ))
836 /* restart waiting on poll() if we are no longer signaled */
837 set_fd_events( queue->fd, POLLIN );
838 }
839 return ret || is_signaled( queue );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000840}
841
842static int msg_queue_satisfied( struct object *obj, struct thread *thread )
843{
844 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000845 queue->wake_mask = 0;
846 queue->changed_mask = 0;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000847 return 0; /* Not abandoned */
848}
849
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000850static void msg_queue_destroy( struct object *obj )
851{
852 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliardff986a52004-11-29 18:08:18 +0000853 struct list *ptr;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000854 int i;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000855
856 cleanup_results( queue );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000857 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000858
Alexandre Julliardff986a52004-11-29 18:08:18 +0000859 while ((ptr = list_head( &queue->pending_timers )))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000860 {
Alexandre Julliardff986a52004-11-29 18:08:18 +0000861 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
862 list_remove( &timer->entry );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000863 free( timer );
Alexandre Julliardff986a52004-11-29 18:08:18 +0000864 }
865 while ((ptr = list_head( &queue->expired_timers )))
866 {
867 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
868 list_remove( &timer->entry );
869 free( timer );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000870 }
871 if (queue->timeout) remove_timeout_user( queue->timeout );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000872 if (queue->input) release_object( queue->input );
Alexandre Julliardd55e7f12003-07-03 18:16:48 +0000873 if (queue->hooks) release_object( queue->hooks );
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200874 if (queue->fd) release_object( queue->fd );
875}
876
877static void msg_queue_poll_event( struct fd *fd, int event )
878{
879 struct msg_queue *queue = get_fd_user( fd );
880 assert( queue->obj.ops == &msg_queue_ops );
881
882 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
Alexandre Julliard2d69ba42009-06-02 22:14:40 +0200883 else set_fd_events( queue->fd, 0 );
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200884 wake_up( &queue->obj, 0 );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000885}
886
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000887static void thread_input_dump( struct object *obj, int verbose )
888{
889 struct thread_input *input = (struct thread_input *)obj;
Alexandre Julliardd7641072008-12-08 16:57:38 +0100890 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000891 input->focus, input->capture, input->active );
892}
893
894static void thread_input_destroy( struct object *obj )
895{
896 struct thread_input *input = (struct thread_input *)obj;
897
898 if (foreground_input == input) foreground_input = NULL;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000899 empty_msg_list( &input->msg_list );
Robert Shearmand8058fb2006-04-07 11:15:19 +0100900 if (input->desktop) release_object( input->desktop );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000901}
902
903/* fix the thread input data when a window is destroyed */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000904static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000905{
906 struct thread_input *input = queue->input;
907
908 if (window == input->focus) input->focus = 0;
909 if (window == input->capture) input->capture = 0;
910 if (window == input->active) input->active = 0;
911 if (window == input->menu_owner) input->menu_owner = 0;
912 if (window == input->move_size) input->move_size = 0;
Alexandre Julliard11e35232002-10-17 01:24:33 +0000913 if (window == input->caret) set_caret_window( input, 0 );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000914}
915
Alexandre Julliard5030bda2002-10-11 23:41:06 +0000916/* check if the specified window can be set in the input data of a given queue */
917static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
918{
919 struct thread *thread;
920 int ret = 0;
921
922 if (!window) return 1; /* we can always clear the data */
923
924 if ((thread = get_window_thread( window )))
925 {
926 ret = (queue->input == thread->queue->input);
927 if (!ret) set_error( STATUS_ACCESS_DENIED );
928 release_object( thread );
929 }
930 else set_error( STATUS_INVALID_HANDLE );
931
932 return ret;
933}
934
Alexandre Julliardd70a2532005-04-26 14:31:33 +0000935/* make sure the specified thread has a queue */
936int init_thread_queue( struct thread *thread )
937{
938 if (thread->queue) return 1;
939 return (create_msg_queue( thread, NULL ) != NULL);
940}
941
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000942/* attach two thread input data structures */
943int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
944{
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000945 struct desktop *desktop;
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000946 struct thread_input *input;
947
948 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000949 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000950 input = (struct thread_input *)grab_object( thread_to->queue->input );
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000951 if (input->desktop != desktop)
952 {
953 set_error( STATUS_ACCESS_DENIED );
954 release_object( input );
955 release_object( desktop );
956 return 0;
957 }
958 release_object( desktop );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000959
960 if (thread_from->queue)
961 {
Alexandre Julliard15ca8202004-06-17 20:00:25 +0000962 release_thread_input( thread_from );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000963 thread_from->queue->input = input;
964 }
965 else
966 {
967 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
968 }
969 memset( input->keystate, 0, sizeof(input->keystate) );
970 return 1;
971}
972
973/* detach two thread input data structures */
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000974void detach_thread_input( struct thread *thread_from )
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000975{
976 struct thread_input *input;
977
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000978 if ((input = create_thread_input( thread_from )))
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000979 {
Alexandre Julliard15ca8202004-06-17 20:00:25 +0000980 release_thread_input( thread_from );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000981 thread_from->queue->input = input;
982 }
983}
984
985
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000986/* set the next timer to expire */
Alexandre Julliardff986a52004-11-29 18:08:18 +0000987static void set_next_timer( struct msg_queue *queue )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000988{
Alexandre Julliardff986a52004-11-29 18:08:18 +0000989 struct list *ptr;
990
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000991 if (queue->timeout)
992 {
993 remove_timeout_user( queue->timeout );
994 queue->timeout = NULL;
995 }
Alexandre Julliardff986a52004-11-29 18:08:18 +0000996 if ((ptr = list_head( &queue->pending_timers )))
997 {
998 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200999 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
Alexandre Julliardff986a52004-11-29 18:08:18 +00001000 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001001 /* set/clear QS_TIMER bit */
Alexandre Julliardff986a52004-11-29 18:08:18 +00001002 if (list_empty( &queue->expired_timers ))
Alexandre Julliardd253c582001-08-07 19:19:08 +00001003 clear_queue_bits( queue, QS_TIMER );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001004 else
Alexandre Julliardd253c582001-08-07 19:19:08 +00001005 set_queue_bits( queue, QS_TIMER );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001006}
1007
Alexandre Julliardff986a52004-11-29 18:08:18 +00001008/* find a timer from its window and id */
1009static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
Alexandre Julliard31282b32008-12-10 16:01:50 +01001010 unsigned int msg, lparam_t id )
Alexandre Julliardff986a52004-11-29 18:08:18 +00001011{
1012 struct list *ptr;
1013
1014 /* we need to search both lists */
1015
1016 LIST_FOR_EACH( ptr, &queue->pending_timers )
1017 {
1018 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1019 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1020 }
1021 LIST_FOR_EACH( ptr, &queue->expired_timers )
1022 {
1023 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1024 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1025 }
1026 return NULL;
1027}
1028
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001029/* callback for the next timer expiration */
1030static void timer_callback( void *private )
1031{
1032 struct msg_queue *queue = private;
Alexandre Julliardff986a52004-11-29 18:08:18 +00001033 struct list *ptr;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001034
1035 queue->timeout = NULL;
1036 /* move on to the next timer */
Alexandre Julliardff986a52004-11-29 18:08:18 +00001037 ptr = list_head( &queue->pending_timers );
1038 list_remove( ptr );
1039 list_add_tail( &queue->expired_timers, ptr );
1040 set_next_timer( queue );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001041}
1042
1043/* link a timer at its rightful place in the queue list */
1044static void link_timer( struct msg_queue *queue, struct timer *timer )
1045{
Alexandre Julliardff986a52004-11-29 18:08:18 +00001046 struct list *ptr;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001047
Alexandre Julliardff986a52004-11-29 18:08:18 +00001048 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001049 {
Alexandre Julliardff986a52004-11-29 18:08:18 +00001050 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
Alexandre Julliardaaf477f2007-04-17 20:08:59 +02001051 if (t->when >= timer->when) break;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001052 }
Alexandre Julliardff986a52004-11-29 18:08:18 +00001053 list_add_before( ptr, &timer->entry );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001054}
1055
Alexandre Julliardff986a52004-11-29 18:08:18 +00001056/* remove a timer from the queue timer list and free it */
1057static void free_timer( struct msg_queue *queue, struct timer *timer )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001058{
Alexandre Julliardff986a52004-11-29 18:08:18 +00001059 list_remove( &timer->entry );
1060 free( timer );
1061 set_next_timer( queue );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001062}
1063
1064/* restart an expired timer */
1065static void restart_timer( struct msg_queue *queue, struct timer *timer )
1066{
Alexandre Julliardff986a52004-11-29 18:08:18 +00001067 list_remove( &timer->entry );
Alexandre Julliardaaf477f2007-04-17 20:08:59 +02001068 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001069 link_timer( queue, timer );
Alexandre Julliardff986a52004-11-29 18:08:18 +00001070 set_next_timer( queue );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001071}
1072
1073/* find an expired timer matching the filtering parameters */
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001074static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001075 unsigned int get_first, unsigned int get_last,
1076 int remove )
1077{
Alexandre Julliardff986a52004-11-29 18:08:18 +00001078 struct list *ptr;
1079
1080 LIST_FOR_EACH( ptr, &queue->expired_timers )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001081 {
Alexandre Julliardff986a52004-11-29 18:08:18 +00001082 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001083 if (win && timer->win != win) continue;
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001084 if (check_msg_filter( timer->msg, get_first, get_last ))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001085 {
1086 if (remove) restart_timer( queue, timer );
1087 return timer;
1088 }
1089 }
1090 return NULL;
1091}
1092
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001093/* add a timer */
1094static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1095{
1096 struct timer *timer = mem_alloc( sizeof(*timer) );
1097 if (timer)
1098 {
Alexandre Julliard753c8702006-08-10 16:42:09 +02001099 timer->rate = max( rate, 1 );
Alexandre Julliardaaf477f2007-04-17 20:08:59 +02001100 timer->when = current_time + (timeout_t)timer->rate * 10000;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001101 link_timer( queue, timer );
Alexandre Julliardff986a52004-11-29 18:08:18 +00001102 /* check if we replaced the next timer */
1103 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001104 }
1105 return timer;
1106}
1107
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001108/* change the input key state for a given key */
1109static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1110{
1111 if (down)
1112 {
1113 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1114 input->keystate[key] |= 0x80;
1115 }
1116 else input->keystate[key] &= ~0x80;
1117}
1118
1119/* update the input key state for a keyboard message */
1120static void update_input_key_state( struct thread_input *input, const struct message *msg )
1121{
1122 unsigned char key;
Vitaliy Margolen9faf7e32008-04-01 23:13:45 -06001123 int down = 0;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001124
1125 switch (msg->msg)
1126 {
1127 case WM_LBUTTONDOWN:
1128 down = 1;
1129 /* fall through */
1130 case WM_LBUTTONUP:
1131 set_input_key_state( input, VK_LBUTTON, down );
1132 break;
1133 case WM_MBUTTONDOWN:
1134 down = 1;
1135 /* fall through */
1136 case WM_MBUTTONUP:
1137 set_input_key_state( input, VK_MBUTTON, down );
1138 break;
1139 case WM_RBUTTONDOWN:
1140 down = 1;
1141 /* fall through */
1142 case WM_RBUTTONUP:
1143 set_input_key_state( input, VK_RBUTTON, down );
1144 break;
Alexandre Julliardcb7aa872005-03-24 19:16:54 +00001145 case WM_XBUTTONDOWN:
1146 down = 1;
1147 /* fall through */
1148 case WM_XBUTTONUP:
1149 if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1150 else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1151 break;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001152 case WM_KEYDOWN:
1153 case WM_SYSKEYDOWN:
1154 down = 1;
1155 /* fall through */
1156 case WM_KEYUP:
1157 case WM_SYSKEYUP:
1158 key = (unsigned char)msg->wparam;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001159 set_input_key_state( input, key, down );
1160 switch(key)
1161 {
Thomas Kho0e814842006-04-12 11:13:46 -07001162 case VK_LCONTROL:
1163 case VK_RCONTROL:
Vitaliy Margolen9faf7e32008-04-01 23:13:45 -06001164 down = (input->keystate[VK_LCONTROL] | input->keystate[VK_RCONTROL]) & 0x80;
Thomas Kho0e814842006-04-12 11:13:46 -07001165 set_input_key_state( input, VK_CONTROL, down );
1166 break;
1167 case VK_LMENU:
1168 case VK_RMENU:
Vitaliy Margolen9faf7e32008-04-01 23:13:45 -06001169 down = (input->keystate[VK_LMENU] | input->keystate[VK_RMENU]) & 0x80;
Thomas Kho0e814842006-04-12 11:13:46 -07001170 set_input_key_state( input, VK_MENU, down );
1171 break;
1172 case VK_LSHIFT:
1173 case VK_RSHIFT:
Vitaliy Margolen9faf7e32008-04-01 23:13:45 -06001174 down = (input->keystate[VK_LSHIFT] | input->keystate[VK_RSHIFT]) & 0x80;
Thomas Kho0e814842006-04-12 11:13:46 -07001175 set_input_key_state( input, VK_SHIFT, down );
1176 break;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001177 }
1178 break;
1179 }
1180}
1181
Alexandre Julliard242e3952003-01-08 00:27:58 +00001182/* release the hardware message currently being processed by the given thread */
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001183static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1184 int remove, user_handle_t new_win )
Alexandre Julliard242e3952003-01-08 00:27:58 +00001185{
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001186 struct thread_input *input = queue->input;
1187 struct message *msg;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001188
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001189 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1190 {
1191 if (msg->unique_id == hw_id) break;
1192 }
1193 if (&msg->entry == &input->msg_list) return; /* not found */
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001194
1195 /* clear the queue bit for that message */
1196 if (remove || new_win)
Alexandre Julliard242e3952003-01-08 00:27:58 +00001197 {
1198 struct message *other;
1199 int clr_bit;
1200
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001201 clr_bit = get_hardware_msg_bit( msg );
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001202 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1203 {
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001204 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001205 {
1206 clr_bit = 0;
1207 break;
1208 }
1209 }
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001210 if (clr_bit) clear_queue_bits( queue, clr_bit );
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001211 }
1212
1213 if (new_win) /* set the new window */
1214 {
1215 struct thread *owner = get_window_thread( new_win );
1216 if (owner)
1217 {
Alexandre Julliard93938eb2007-11-08 13:53:26 +01001218 msg->win = new_win;
1219 if (owner->queue->input != input)
Alexandre Julliard90b48e92005-05-13 14:00:19 +00001220 {
Alexandre Julliard93938eb2007-11-08 13:53:26 +01001221 list_remove( &msg->entry );
1222 if (msg->msg == WM_MOUSEMOVE && merge_message( owner->queue->input, msg ))
1223 {
1224 free_message( msg );
1225 release_object( owner );
1226 return;
1227 }
1228 list_add_tail( &owner->queue->input->msg_list, &msg->entry );
Alexandre Julliard90b48e92005-05-13 14:00:19 +00001229 }
Alexandre Julliard93938eb2007-11-08 13:53:26 +01001230 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1231 remove = 0;
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001232 release_object( owner );
1233 }
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001234 }
Alexandre Julliard90b48e92005-05-13 14:00:19 +00001235 if (remove)
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001236 {
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001237 update_input_key_state( input, msg );
1238 list_remove( &msg->entry );
1239 free_message( msg );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001240 }
Alexandre Julliard242e3952003-01-08 00:27:58 +00001241}
1242
1243/* find the window that should receive a given hardware message */
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001244static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001245 struct hardware_msg_data *data, unsigned int *msg_code )
Alexandre Julliard242e3952003-01-08 00:27:58 +00001246{
1247 user_handle_t win = 0;
1248
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001249 *msg_code = msg->msg;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001250 if (is_keyboard_msg( msg ))
1251 {
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001252 if (input && !(win = input->focus))
1253 {
1254 win = input->active;
1255 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1256 }
Alexandre Julliard242e3952003-01-08 00:27:58 +00001257 }
1258 else /* mouse message */
1259 {
1260 if (!input || !(win = input->capture))
1261 {
Dmitry Timoshkov93a4e1e2004-10-27 21:55:00 +00001262 if (!(win = msg->win) || !is_window_visible( win ))
Alexandre Julliard3f31a102005-08-16 19:58:12 +00001263 {
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001264 if (input) win = window_from_point( input->desktop, data->x, data->y );
Alexandre Julliard3f31a102005-08-16 19:58:12 +00001265 }
Alexandre Julliard242e3952003-01-08 00:27:58 +00001266 }
1267 }
1268 return win;
1269}
1270
1271/* queue a hardware message into a given thread input */
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001272static void queue_hardware_message( struct msg_queue *queue, struct message *msg,
1273 struct hardware_msg_data *data )
Alexandre Julliard242e3952003-01-08 00:27:58 +00001274{
1275 user_handle_t win;
Alexandre Julliarde3fe6892005-03-17 20:51:53 +00001276 struct thread *thread;
Alexandre Julliard5d282dc2006-02-14 10:43:15 +01001277 struct thread_input *input = queue ? queue->input : foreground_input;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001278 unsigned int msg_code;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001279
Mike McCormackabe70f72005-04-28 12:04:14 +00001280 last_input_time = get_tick_count();
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001281 win = find_hardware_message_window( input, msg, data, &msg_code );
Alexandre Julliarde3fe6892005-03-17 20:51:53 +00001282 if (!win || !(thread = get_window_thread(win)))
Alexandre Julliard242e3952003-01-08 00:27:58 +00001283 {
Alexandre Julliard5d282dc2006-02-14 10:43:15 +01001284 if (input) update_input_key_state( input, msg );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001285 free( msg );
1286 return;
1287 }
Alexandre Julliarde3fe6892005-03-17 20:51:53 +00001288 input = thread->queue->input;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001289
1290 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1291 else
1292 {
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001293 msg->unique_id = 0; /* will be set once we return it to the app */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001294 list_add_tail( &input->msg_list, &msg->entry );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001295 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1296 }
Alexandre Julliarde3fe6892005-03-17 20:51:53 +00001297 release_object( thread );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001298}
1299
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001300/* check message filter for a hardware message */
1301static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1302 user_handle_t filter_win, unsigned int first, unsigned int last )
1303{
1304 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1305 {
1306 /* we can only test the window for a keyboard message since the
1307 * dest window for a mouse message depends on hittest */
1308 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1309 return 0;
1310 /* the message code is final for a keyboard message, we can simply check it */
1311 return check_msg_filter( msg_code, first, last );
1312 }
1313 else /* mouse message */
1314 {
1315 /* we need to check all possible values that the message can have in the end */
1316
1317 if (check_msg_filter( msg_code, first, last )) return 1;
1318 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1319
1320 /* all other messages can become non-client messages */
1321 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1322
1323 /* clicks can become double-clicks or non-client double-clicks */
1324 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1325 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1326 {
1327 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1328 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1329 }
1330 return 0;
1331 }
1332}
1333
1334
Alexandre Julliard242e3952003-01-08 00:27:58 +00001335/* find a hardware message for the given queue */
Michael Stefaniuca6249772006-07-25 10:30:04 +02001336static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001337 unsigned int first, unsigned int last, struct get_message_reply *reply )
Alexandre Julliard242e3952003-01-08 00:27:58 +00001338{
1339 struct thread_input *input = thread->queue->input;
1340 struct thread *win_thread;
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001341 struct list *ptr;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001342 user_handle_t win;
1343 int clear_bits, got_one = 0;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001344 unsigned int msg_code;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001345
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001346 ptr = list_head( &input->msg_list );
1347 if (hw_id)
1348 {
1349 while (ptr)
1350 {
1351 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1352 if (msg->unique_id == hw_id) break;
1353 ptr = list_next( &input->msg_list, ptr );
1354 }
1355 if (!ptr) ptr = list_head( &input->msg_list );
1356 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1357 }
Alexandre Julliard242e3952003-01-08 00:27:58 +00001358
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001359 if (ptr == list_head( &input->msg_list ))
Alexandre Julliard242e3952003-01-08 00:27:58 +00001360 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001361 else
Alexandre Julliard242e3952003-01-08 00:27:58 +00001362 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
Alexandre Julliard242e3952003-01-08 00:27:58 +00001363
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001364 while (ptr)
Alexandre Julliard242e3952003-01-08 00:27:58 +00001365 {
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001366 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001367 struct hardware_msg_data *data = msg->data;
1368
Alexandre Julliard55d449e2005-05-14 11:08:05 +00001369 ptr = list_next( &input->msg_list, ptr );
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001370 win = find_hardware_message_window( input, msg, data, &msg_code );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001371 if (!win || !(win_thread = get_window_thread( win )))
1372 {
1373 /* no window at all, remove it */
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001374 update_input_key_state( input, msg );
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001375 list_remove( &msg->entry );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001376 free_message( msg );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001377 continue;
1378 }
1379 if (win_thread != thread)
1380 {
Alexandre Julliard55d449e2005-05-14 11:08:05 +00001381 if (win_thread->queue->input == input)
1382 {
1383 /* wake the other thread */
1384 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1385 got_one = 1;
1386 }
1387 else
1388 {
1389 /* for another thread input, drop it */
1390 update_input_key_state( input, msg );
1391 list_remove( &msg->entry );
1392 free_message( msg );
1393 }
Alexandre Julliard242e3952003-01-08 00:27:58 +00001394 release_object( win_thread );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001395 continue;
1396 }
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001397 release_object( win_thread );
1398
Alexandre Julliard242e3952003-01-08 00:27:58 +00001399 /* if we already got a message for another thread, or if it doesn't
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001400 * match the filter we skip it */
1401 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
Alexandre Julliard242e3952003-01-08 00:27:58 +00001402 {
1403 clear_bits &= ~get_hardware_msg_bit( msg );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001404 continue;
1405 }
1406 /* now we can return it */
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001407 if (!msg->unique_id) msg->unique_id = get_unique_id();
Alexandre Julliard242e3952003-01-08 00:27:58 +00001408 reply->type = MSG_HARDWARE;
1409 reply->win = win;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001410 reply->msg = msg_code;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001411 reply->wparam = msg->wparam;
1412 reply->lparam = msg->lparam;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001413 reply->time = msg->time;
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001414
1415 data->hw_id = msg->unique_id;
1416 set_reply_data( msg->data, msg->data_size );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001417 return 1;
1418 }
1419 /* nothing found, clear the hardware queue bits */
1420 clear_queue_bits( thread->queue, clear_bits );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001421 return 0;
1422}
Alexandre Julliard805bdc52001-11-13 22:23:48 +00001423
1424/* increment (or decrement if 'incr' is negative) the queue paint count */
1425void inc_queue_paint_count( struct thread *thread, int incr )
1426{
1427 struct msg_queue *queue = thread->queue;
1428
1429 assert( queue );
1430
1431 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1432
1433 if (queue->paint_count)
1434 set_queue_bits( queue, QS_PAINT );
1435 else
1436 clear_queue_bits( queue, QS_PAINT );
1437}
1438
1439
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001440/* remove all messages and timers belonging to a certain window */
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001441void queue_cleanup_window( struct thread *thread, user_handle_t win )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001442{
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001443 struct msg_queue *queue = thread->queue;
Alexandre Julliardff986a52004-11-29 18:08:18 +00001444 struct list *ptr;
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001445 int i;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001446
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001447 if (!queue) return;
1448
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001449 /* remove timers */
Alexandre Julliardff986a52004-11-29 18:08:18 +00001450
1451 ptr = list_head( &queue->pending_timers );
1452 while (ptr)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001453 {
Alexandre Julliardff986a52004-11-29 18:08:18 +00001454 struct list *next = list_next( &queue->pending_timers, ptr );
1455 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1456 if (timer->win == win) free_timer( queue, timer );
1457 ptr = next;
1458 }
1459 ptr = list_head( &queue->expired_timers );
1460 while (ptr)
1461 {
1462 struct list *next = list_next( &queue->expired_timers, ptr );
1463 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1464 if (timer->win == win) free_timer( queue, timer );
1465 ptr = next;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001466 }
1467
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001468 /* remove messages */
1469 for (i = 0; i < NB_MSG_KINDS; i++)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001470 {
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001471 struct list *ptr, *next;
1472
1473 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001474 {
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001475 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001476 if (msg->win == win) remove_queue_message( queue, msg, i );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001477 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001478 }
Alexandre Julliardab5063b2002-10-11 18:50:15 +00001479
1480 thread_input_cleanup_window( queue, win );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001481}
1482
Alexandre Julliard81f2a732002-03-23 20:43:52 +00001483/* post a message to a window; used by socket handling */
Alexandre Julliard31282b32008-12-10 16:01:50 +01001484void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
Alexandre Julliard81f2a732002-03-23 20:43:52 +00001485{
1486 struct message *msg;
1487 struct thread *thread = get_window_thread( win );
1488
1489 if (!thread) return;
1490
1491 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1492 {
1493 msg->type = MSG_POSTED;
1494 msg->win = get_user_full_handle( win );
1495 msg->msg = message;
1496 msg->wparam = wparam;
1497 msg->lparam = lparam;
1498 msg->time = get_tick_count();
Alexandre Julliard81f2a732002-03-23 20:43:52 +00001499 msg->result = NULL;
1500 msg->data = NULL;
1501 msg->data_size = 0;
1502
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001503 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
Alexandre Julliard80b997a2005-11-14 15:17:09 +00001504 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
Alexandre Julliard81f2a732002-03-23 20:43:52 +00001505 }
1506 release_object( thread );
1507}
1508
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001509/* post a win event */
1510void post_win_event( struct thread *thread, unsigned int event,
1511 user_handle_t win, unsigned int object_id,
Alexandre Julliardcc55fd32008-12-29 17:35:35 +01001512 unsigned int child_id, client_ptr_t hook_proc,
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001513 const WCHAR *module, data_size_t module_size,
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001514 user_handle_t hook)
1515{
1516 struct message *msg;
1517
1518 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1519 {
Alexandre Julliard59dc4562006-10-04 16:04:53 +02001520 struct winevent_msg_data *data;
1521
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001522 msg->type = MSG_WINEVENT;
1523 msg->win = get_user_full_handle( win );
1524 msg->msg = event;
1525 msg->wparam = object_id;
1526 msg->lparam = child_id;
1527 msg->time = get_tick_count();
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001528 msg->result = NULL;
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001529
Alexandre Julliard59dc4562006-10-04 16:04:53 +02001530 if ((data = malloc( sizeof(*data) + module_size )))
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001531 {
Alexandre Julliard59dc4562006-10-04 16:04:53 +02001532 data->hook = hook;
1533 data->tid = get_thread_id( current );
1534 data->hook_proc = hook_proc;
1535 memcpy( data + 1, module, module_size );
1536
1537 msg->data = data;
1538 msg->data_size = sizeof(*data) + module_size;
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001539
1540 if (debug_level > 1)
Alexandre Julliardd7641072008-12-08 16:57:38 +01001541 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001542 get_thread_id(thread), event, win, object_id, child_id );
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001543 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001544 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1545 }
1546 else
1547 free( msg );
1548 }
1549}
Alexandre Julliard81f2a732002-03-23 20:43:52 +00001550
Dmitry Timoshkove735e192007-12-28 10:59:13 +08001551
1552/* check if the thread owning the window is hung */
1553DECL_HANDLER(is_window_hung)
1554{
1555 struct thread *thread;
1556
1557 thread = get_window_thread( req->win );
1558 if (thread)
1559 {
1560 reply->is_hung = is_queue_hung( thread->queue );
1561 release_object( thread );
1562 }
1563 else reply->is_hung = 0;
1564}
1565
1566
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001567/* get the message queue of the current thread */
1568DECL_HANDLER(get_msg_queue)
1569{
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001570 struct msg_queue *queue = get_current_queue();
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001571
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001572 reply->handle = 0;
1573 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001574}
1575
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001576
Alexandre Julliard0cb29f42007-04-04 18:02:01 +02001577/* set the file descriptor associated to the current thread queue */
1578DECL_HANDLER(set_queue_fd)
1579{
1580 struct msg_queue *queue = get_current_queue();
1581 struct file *file;
1582 int unix_fd;
1583
1584 if (queue->fd) /* fd can only be set once */
1585 {
1586 set_error( STATUS_ACCESS_DENIED );
1587 return;
1588 }
1589 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
1590
1591 if ((unix_fd = get_file_unix_fd( file )) != -1)
1592 {
1593 if ((unix_fd = dup( unix_fd )) != -1)
Alexandre Julliardf85437c2007-04-10 22:25:07 +02001594 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
Alexandre Julliard0cb29f42007-04-04 18:02:01 +02001595 else
1596 file_set_error();
1597 }
1598 release_object( file );
1599}
1600
1601
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001602/* set the current message queue wakeup mask */
1603DECL_HANDLER(set_queue_mask)
1604{
1605 struct msg_queue *queue = get_current_queue();
1606
1607 if (queue)
1608 {
1609 queue->wake_mask = req->wake_mask;
1610 queue->changed_mask = req->changed_mask;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001611 reply->wake_bits = queue->wake_bits;
1612 reply->changed_bits = queue->changed_bits;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001613 if (is_signaled( queue ))
1614 {
1615 /* if skip wait is set, do what would have been done in the subsequent wait */
1616 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1617 else wake_up( &queue->obj, 0 );
1618 }
1619 }
1620}
1621
1622
1623/* get the current message queue status */
1624DECL_HANDLER(get_queue_status)
1625{
1626 struct msg_queue *queue = current->queue;
1627 if (queue)
1628 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001629 reply->wake_bits = queue->wake_bits;
1630 reply->changed_bits = queue->changed_bits;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001631 if (req->clear) queue->changed_bits = 0;
1632 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001633 else reply->wake_bits = reply->changed_bits = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001634}
1635
1636
1637/* send a message to a thread queue */
1638DECL_HANDLER(send_message)
1639{
1640 struct message *msg;
1641 struct msg_queue *send_queue = get_current_queue();
Alexandre Julliard242e3952003-01-08 00:27:58 +00001642 struct msg_queue *recv_queue = NULL;
1643 struct thread *thread = NULL;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001644
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001645 if (!(thread = get_thread_from_id( req->id ))) return;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001646
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001647 if (!(recv_queue = thread->queue))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001648 {
1649 set_error( STATUS_INVALID_PARAMETER );
1650 release_object( thread );
1651 return;
1652 }
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001653 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
Alexandre Julliard09029b22003-07-11 04:09:42 +00001654 {
1655 set_error( STATUS_TIMEOUT );
1656 release_object( thread );
1657 return;
1658 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001659
1660 if ((msg = mem_alloc( sizeof(*msg) )))
1661 {
Alexandre Julliardd253c582001-08-07 19:19:08 +00001662 msg->type = req->type;
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001663 msg->win = get_user_full_handle( req->win );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001664 msg->msg = req->msg;
1665 msg->wparam = req->wparam;
1666 msg->lparam = req->lparam;
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001667 msg->time = get_tick_count();
Alexandre Julliardd253c582001-08-07 19:19:08 +00001668 msg->result = NULL;
1669 msg->data = NULL;
Alexandre Julliard3ad97982006-10-04 20:25:42 +02001670 msg->data_size = get_req_data_size();
1671
1672 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1673 {
1674 free( msg );
1675 release_object( thread );
1676 return;
1677 }
Alexandre Julliardd253c582001-08-07 19:19:08 +00001678
1679 switch(msg->type)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001680 {
Eric Pouech0faceb02002-01-18 19:22:55 +00001681 case MSG_OTHER_PROCESS:
Alexandre Julliardd253c582001-08-07 19:19:08 +00001682 case MSG_ASCII:
1683 case MSG_UNICODE:
1684 case MSG_CALLBACK:
Alexandre Julliard3ad97982006-10-04 20:25:42 +02001685 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
Alexandre Julliarde630aa02001-07-11 17:29:01 +00001686 {
Alexandre Julliard039e1312003-07-26 20:36:43 +00001687 free_message( msg );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001688 break;
Alexandre Julliarde630aa02001-07-11 17:29:01 +00001689 }
Alexandre Julliardd253c582001-08-07 19:19:08 +00001690 /* fall through */
1691 case MSG_NOTIFY:
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001692 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001693 set_queue_bits( recv_queue, QS_SENDMESSAGE );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001694 break;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001695 case MSG_POSTED:
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001696 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
Alexandre Julliard80b997a2005-11-14 15:17:09 +00001697 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001698 break;
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001699 case MSG_HARDWARE: /* should use send_hardware_message instead */
Alexandre Julliard039e1312003-07-26 20:36:43 +00001700 case MSG_CALLBACK_RESULT: /* cannot send this one */
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001701 default:
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001702 set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001703 free( msg );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001704 break;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001705 }
1706 }
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001707 release_object( thread );
1708}
1709
1710/* send a hardware message to a thread queue */
1711DECL_HANDLER(send_hardware_message)
1712{
1713 struct message *msg;
1714 struct msg_queue *recv_queue = NULL;
1715 struct thread *thread = NULL;
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001716 struct hardware_msg_data *data;
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001717
1718 if (req->id)
1719 {
1720 if (!(thread = get_thread_from_id( req->id ))) return;
1721 }
1722
1723 if (thread && !(recv_queue = thread->queue))
1724 {
1725 set_error( STATUS_INVALID_PARAMETER );
1726 release_object( thread );
1727 return;
1728 }
1729
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001730 if (!(data = mem_alloc( sizeof(*data) )))
1731 {
Marcus Meissner3b86f462009-01-28 23:58:38 +01001732 if (thread) release_object( thread );
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001733 return;
1734 }
Alexandre Julliard8c8c6652008-12-31 15:14:29 +01001735 memset( data, 0, sizeof(*data) );
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001736 data->x = req->x;
1737 data->y = req->y;
1738 data->info = req->info;
1739
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001740 if ((msg = mem_alloc( sizeof(*msg) )))
1741 {
1742 msg->type = MSG_HARDWARE;
1743 msg->win = get_user_full_handle( req->win );
1744 msg->msg = req->msg;
1745 msg->wparam = req->wparam;
1746 msg->lparam = req->lparam;
1747 msg->time = req->time;
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001748 msg->result = NULL;
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001749 msg->data = data;
1750 msg->data_size = sizeof(*data);
1751 queue_hardware_message( recv_queue, msg, data );
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001752 }
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001753 else free( data );
1754
Alexandre Julliard242e3952003-01-08 00:27:58 +00001755 if (thread) release_object( thread );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001756}
1757
Robert Shearmana40ce392006-01-17 13:14:31 +01001758/* post a quit message to the current queue */
1759DECL_HANDLER(post_quit_message)
1760{
1761 struct msg_queue *queue = get_current_queue();
1762
1763 if (!queue)
1764 return;
1765
1766 queue->quit_message = 1;
1767 queue->exit_code = req->exit_code;
1768 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1769}
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001770
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001771/* get a message from the current queue */
1772DECL_HANDLER(get_message)
1773{
1774 struct timer *timer;
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001775 struct list *ptr;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001776 struct msg_queue *queue = get_current_queue();
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001777 user_handle_t get_win = get_user_full_handle( req->get_win );
Alexandre Julliard89faee02007-02-21 15:21:05 +01001778 unsigned int filter = req->flags >> 16;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001779
Alexandre Julliard63342352005-05-11 13:03:15 +00001780 reply->active_hooks = get_active_hooks();
1781
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001782 if (!queue) return;
Alexandre Julliard753c8702006-08-10 16:42:09 +02001783 queue->last_get_msg = current_time;
Alexandre Julliard89faee02007-02-21 15:21:05 +01001784 if (!filter) filter = QS_ALLINPUT;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001785
1786 /* first check for sent messages */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001787 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001788 {
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001789 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001790 receive_message( queue, msg, reply );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001791 return;
1792 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001793
Alexandre Julliard9f55ae62001-06-28 04:37:22 +00001794 /* clear changed bits so we can wait on them if we don't find a message */
Alexandre Julliard89faee02007-02-21 15:21:05 +01001795 if (filter & QS_POSTMESSAGE)
1796 {
1797 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
1798 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
1799 }
1800 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
1801 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
Alexandre Julliard9f55ae62001-06-28 04:37:22 +00001802
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001803 /* then check for posted messages */
Alexandre Julliard89faee02007-02-21 15:21:05 +01001804 if ((filter & QS_POSTMESSAGE) &&
1805 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001806 return;
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001807
Robert Shearmana40ce392006-01-17 13:14:31 +01001808 /* only check for quit messages if not posted messages pending.
1809 * note: the quit message isn't filtered */
1810 if (get_quit_message( queue, req->flags, reply ))
1811 return;
1812
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001813 /* then check for any raw hardware message */
Alexandre Julliard89faee02007-02-21 15:21:05 +01001814 if ((filter & QS_INPUT) &&
1815 filter_contains_hw_range( req->get_first, req->get_last ) &&
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001816 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001817 return;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001818
1819 /* now check for WM_PAINT */
Alexandre Julliard89faee02007-02-21 15:21:05 +01001820 if ((filter & QS_PAINT) &&
1821 queue->paint_count &&
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001822 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001823 (reply->win = find_window_to_repaint( get_win, current )))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001824 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001825 reply->type = MSG_POSTED;
1826 reply->msg = WM_PAINT;
1827 reply->wparam = 0;
1828 reply->lparam = 0;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001829 reply->time = get_tick_count();
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001830 return;
1831 }
1832
1833 /* now check for timer */
Alexandre Julliard89faee02007-02-21 15:21:05 +01001834 if ((filter & QS_TIMER) &&
1835 (timer = find_expired_timer( queue, get_win, req->get_first,
1836 req->get_last, (req->flags & PM_REMOVE) )))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001837 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001838 reply->type = MSG_POSTED;
1839 reply->win = timer->win;
1840 reply->msg = timer->msg;
1841 reply->wparam = timer->id;
1842 reply->lparam = timer->lparam;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001843 reply->time = get_tick_count();
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001844 return;
1845 }
1846
Alexandre Julliard28965402007-08-29 18:13:13 +02001847 queue->wake_mask = req->wake_mask;
1848 queue->changed_mask = req->changed_mask;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001849 set_error( STATUS_PENDING ); /* FIXME */
1850}
1851
1852
1853/* reply to a sent message */
1854DECL_HANDLER(reply_message)
1855{
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001856 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
Alexandre Julliardb1095da2003-03-19 00:12:17 +00001857 else if (current->queue->recv_result)
1858 reply_message( current->queue, req->result, 0, req->remove,
1859 get_req_data(), get_req_data_size() );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001860}
1861
1862
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001863/* accept the current hardware message */
1864DECL_HANDLER(accept_hardware_message)
1865{
1866 if (current->queue)
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001867 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1868 else
1869 set_error( STATUS_ACCESS_DENIED );
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001870}
1871
1872
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001873/* retrieve the reply for the last message sent */
1874DECL_HANDLER(get_message_reply)
1875{
Alexandre Julliard039e1312003-07-26 20:36:43 +00001876 struct message_result *result;
1877 struct list *entry;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001878 struct msg_queue *queue = current->queue;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001879
Alexandre Julliardd253c582001-08-07 19:19:08 +00001880 if (queue)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001881 {
Alexandre Julliardd253c582001-08-07 19:19:08 +00001882 set_error( STATUS_PENDING );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001883 reply->result = 0;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001884
Alexandre Julliard039e1312003-07-26 20:36:43 +00001885 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1886
1887 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1888 if (result->replied || req->cancel)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001889 {
Alexandre Julliardd253c582001-08-07 19:19:08 +00001890 if (result->replied)
1891 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001892 reply->result = result->result;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001893 set_error( result->error );
1894 if (result->data)
1895 {
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001896 data_size_t data_len = min( result->data_size, get_reply_max_size() );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001897 set_reply_data_ptr( result->data, data_len );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001898 result->data = NULL;
1899 result->data_size = 0;
1900 }
1901 }
Alexandre Julliard039e1312003-07-26 20:36:43 +00001902 remove_result_from_sender( result );
1903
1904 entry = list_head( &queue->send_result );
1905 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1906 else
1907 {
1908 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1909 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1910 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001911 }
1912 }
Alexandre Julliardd253c582001-08-07 19:19:08 +00001913 else set_error( STATUS_ACCESS_DENIED );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001914}
1915
1916
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001917/* set a window timer */
1918DECL_HANDLER(set_win_timer)
1919{
1920 struct timer *timer;
Alexandre Julliardff986a52004-11-29 18:08:18 +00001921 struct msg_queue *queue;
1922 struct thread *thread = NULL;
1923 user_handle_t win = 0;
Alexandre Julliard31282b32008-12-10 16:01:50 +01001924 lparam_t id = req->id;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001925
Alexandre Julliardff986a52004-11-29 18:08:18 +00001926 if (req->win)
1927 {
1928 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1929 {
1930 set_error( STATUS_INVALID_HANDLE );
1931 return;
1932 }
1933 if (thread->process != current->process)
1934 {
1935 release_object( thread );
1936 set_error( STATUS_ACCESS_DENIED );
1937 return;
1938 }
1939 queue = thread->queue;
1940 /* remove it if it existed already */
1941 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1942 }
1943 else
1944 {
1945 queue = get_current_queue();
Trent Waddingtonfc635fa2007-08-30 19:05:50 +10001946 /* look for a timer with this id */
Alexandre Julliardd7641072008-12-08 16:57:38 +01001947 if (id && (timer = find_timer( queue, 0, req->msg, id )))
Alexandre Julliardff986a52004-11-29 18:08:18 +00001948 {
Trent Waddingtonfc635fa2007-08-30 19:05:50 +10001949 /* free and reuse id */
1950 free_timer( queue, timer );
Alexandre Julliardff986a52004-11-29 18:08:18 +00001951 }
Trent Waddingtonfc635fa2007-08-30 19:05:50 +10001952 else
1953 {
1954 /* find a free id for it */
1955 do
1956 {
1957 id = queue->next_timer_id;
Dmitry Timoshkovb4227d72007-11-13 20:41:01 +08001958 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
Trent Waddingtonfc635fa2007-08-30 19:05:50 +10001959 }
1960 while (find_timer( queue, 0, req->msg, id ));
1961 }
Alexandre Julliardff986a52004-11-29 18:08:18 +00001962 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001963
1964 if ((timer = set_timer( queue, req->rate )))
1965 {
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001966 timer->win = win;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001967 timer->msg = req->msg;
Alexandre Julliardff986a52004-11-29 18:08:18 +00001968 timer->id = id;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001969 timer->lparam = req->lparam;
Alexandre Julliardff986a52004-11-29 18:08:18 +00001970 reply->id = id;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001971 }
Alexandre Julliardff986a52004-11-29 18:08:18 +00001972 if (thread) release_object( thread );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001973}
1974
1975/* kill a window timer */
1976DECL_HANDLER(kill_win_timer)
1977{
Alexandre Julliardff986a52004-11-29 18:08:18 +00001978 struct timer *timer;
1979 struct thread *thread;
1980 user_handle_t win = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001981
Alexandre Julliardff986a52004-11-29 18:08:18 +00001982 if (req->win)
1983 {
1984 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1985 {
1986 set_error( STATUS_INVALID_HANDLE );
1987 return;
1988 }
1989 if (thread->process != current->process)
1990 {
1991 release_object( thread );
1992 set_error( STATUS_ACCESS_DENIED );
1993 return;
1994 }
1995 }
1996 else thread = (struct thread *)grab_object( current );
1997
1998 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1999 free_timer( thread->queue, timer );
2000 else
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00002001 set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliardff986a52004-11-29 18:08:18 +00002002
2003 release_object( thread );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00002004}
Alexandre Julliardab5063b2002-10-11 18:50:15 +00002005
2006
2007/* attach (or detach) thread inputs */
2008DECL_HANDLER(attach_thread_input)
2009{
2010 struct thread *thread_from = get_thread_from_id( req->tid_from );
2011 struct thread *thread_to = get_thread_from_id( req->tid_to );
2012
2013 if (!thread_from || !thread_to)
2014 {
2015 if (thread_from) release_object( thread_from );
2016 if (thread_to) release_object( thread_to );
2017 return;
2018 }
2019 if (thread_from != thread_to)
2020 {
2021 if (req->attach) attach_thread_input( thread_from, thread_to );
Alexandre Julliard5ad90c02005-07-11 13:30:23 +00002022 else
2023 {
2024 if (thread_from->queue && thread_to->queue &&
2025 thread_from->queue->input == thread_to->queue->input)
2026 detach_thread_input( thread_from );
2027 else
2028 set_error( STATUS_ACCESS_DENIED );
2029 }
Alexandre Julliardab5063b2002-10-11 18:50:15 +00002030 }
2031 else set_error( STATUS_ACCESS_DENIED );
2032 release_object( thread_from );
2033 release_object( thread_to );
2034}
2035
2036
2037/* get thread input data */
2038DECL_HANDLER(get_thread_input)
2039{
2040 struct thread *thread = NULL;
2041 struct thread_input *input;
2042
2043 if (req->tid)
2044 {
2045 if (!(thread = get_thread_from_id( req->tid ))) return;
2046 input = thread->queue ? thread->queue->input : NULL;
2047 }
2048 else input = foreground_input; /* get the foreground thread info */
2049
2050 if (input)
2051 {
2052 reply->focus = input->focus;
2053 reply->capture = input->capture;
2054 reply->active = input->active;
2055 reply->menu_owner = input->menu_owner;
2056 reply->move_size = input->move_size;
2057 reply->caret = input->caret;
Alexandre Julliard11e35232002-10-17 01:24:33 +00002058 reply->rect = input->caret_rect;
Alexandre Julliardab5063b2002-10-11 18:50:15 +00002059 }
2060 else
2061 {
2062 reply->focus = 0;
2063 reply->capture = 0;
2064 reply->active = 0;
2065 reply->menu_owner = 0;
2066 reply->move_size = 0;
2067 reply->caret = 0;
2068 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
2069 }
2070 /* foreground window is active window of foreground thread */
2071 reply->foreground = foreground_input ? foreground_input->active : 0;
2072 if (thread) release_object( thread );
2073}
Alexandre Julliard5030bda2002-10-11 23:41:06 +00002074
2075
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00002076/* retrieve queue keyboard state for a given thread */
2077DECL_HANDLER(get_key_state)
2078{
2079 struct thread *thread;
2080 struct thread_input *input;
2081
2082 if (!(thread = get_thread_from_id( req->tid ))) return;
2083 input = thread->queue ? thread->queue->input : NULL;
2084 if (input)
2085 {
2086 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
2087 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
2088 }
2089 release_object( thread );
2090}
2091
2092
2093/* set queue keyboard state for a given thread */
2094DECL_HANDLER(set_key_state)
2095{
2096 struct thread *thread = NULL;
2097 struct thread_input *input;
2098
2099 if (!(thread = get_thread_from_id( req->tid ))) return;
2100 input = thread->queue ? thread->queue->input : NULL;
2101 if (input)
2102 {
Alexandre Julliard0f273c12006-07-26 10:43:25 +02002103 data_size_t size = min( sizeof(input->keystate), get_req_data_size() );
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00002104 if (size) memcpy( input->keystate, get_req_data(), size );
2105 }
2106 release_object( thread );
2107}
2108
2109
Alexandre Julliard5030bda2002-10-11 23:41:06 +00002110/* set the system foreground window */
2111DECL_HANDLER(set_foreground_window)
2112{
Dmitry Timoshkov19e7fab2006-07-07 23:01:51 +09002113 struct thread *thread;
Alexandre Julliard5030bda2002-10-11 23:41:06 +00002114 struct msg_queue *queue = get_current_queue();
2115
2116 reply->previous = foreground_input ? foreground_input->active : 0;
2117 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
2118 reply->send_msg_new = FALSE;
2119
Dmitry Timoshkov19e7fab2006-07-07 23:01:51 +09002120 if (is_top_level_window( req->handle ) &&
2121 ((thread = get_window_thread( req->handle ))))
Alexandre Julliard5030bda2002-10-11 23:41:06 +00002122 {
Dmitry Timoshkov19e7fab2006-07-07 23:01:51 +09002123 foreground_input = thread->queue->input;
2124 reply->send_msg_new = (foreground_input != queue->input);
2125 release_object( thread );
Alexandre Julliard5030bda2002-10-11 23:41:06 +00002126 }
Dmitry Timoshkov19e7fab2006-07-07 23:01:51 +09002127 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
Alexandre Julliard5030bda2002-10-11 23:41:06 +00002128}
2129
2130
2131/* set the current thread focus window */
2132DECL_HANDLER(set_focus_window)
2133{
2134 struct msg_queue *queue = get_current_queue();
2135
2136 reply->previous = 0;
2137 if (queue && check_queue_input_window( queue, req->handle ))
2138 {
2139 reply->previous = queue->input->focus;
2140 queue->input->focus = get_user_full_handle( req->handle );
2141 }
2142}
2143
2144
2145/* set the current thread active window */
2146DECL_HANDLER(set_active_window)
2147{
2148 struct msg_queue *queue = get_current_queue();
2149
2150 reply->previous = 0;
2151 if (queue && check_queue_input_window( queue, req->handle ))
2152 {
2153 if (!req->handle || make_window_active( req->handle ))
2154 {
2155 reply->previous = queue->input->active;
2156 queue->input->active = get_user_full_handle( req->handle );
2157 }
2158 else set_error( STATUS_INVALID_HANDLE );
2159 }
2160}
Alexandre Julliarda9e8f592002-10-12 01:24:37 +00002161
2162
2163/* set the current thread capture window */
2164DECL_HANDLER(set_capture_window)
2165{
2166 struct msg_queue *queue = get_current_queue();
2167
2168 reply->previous = reply->full_handle = 0;
2169 if (queue && check_queue_input_window( queue, req->handle ))
2170 {
2171 struct thread_input *input = queue->input;
2172
2173 reply->previous = input->capture;
2174 input->capture = get_user_full_handle( req->handle );
2175 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2176 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2177 reply->full_handle = input->capture;
2178 }
2179}
Alexandre Julliard11e35232002-10-17 01:24:33 +00002180
2181
2182/* Set the current thread caret window */
2183DECL_HANDLER(set_caret_window)
2184{
2185 struct msg_queue *queue = get_current_queue();
2186
2187 reply->previous = 0;
2188 if (queue && check_queue_input_window( queue, req->handle ))
2189 {
2190 struct thread_input *input = queue->input;
2191
2192 reply->previous = input->caret;
2193 reply->old_rect = input->caret_rect;
2194 reply->old_hide = input->caret_hide;
2195 reply->old_state = input->caret_state;
2196
2197 set_caret_window( input, get_user_full_handle(req->handle) );
Krzysztof Foltmanb8501722005-02-18 20:02:55 +00002198 input->caret_rect.right = input->caret_rect.left + req->width;
2199 input->caret_rect.bottom = input->caret_rect.top + req->height;
Alexandre Julliard11e35232002-10-17 01:24:33 +00002200 }
2201}
2202
2203
2204/* Set the current thread caret information */
2205DECL_HANDLER(set_caret_info)
2206{
2207 struct msg_queue *queue = get_current_queue();
2208 struct thread_input *input;
2209
2210 if (!queue) return;
2211 input = queue->input;
2212 reply->full_handle = input->caret;
2213 reply->old_rect = input->caret_rect;
2214 reply->old_hide = input->caret_hide;
2215 reply->old_state = input->caret_state;
2216
2217 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2218 {
2219 set_error( STATUS_ACCESS_DENIED );
2220 return;
2221 }
2222 if (req->flags & SET_CARET_POS)
2223 {
2224 input->caret_rect.right += req->x - input->caret_rect.left;
2225 input->caret_rect.bottom += req->y - input->caret_rect.top;
2226 input->caret_rect.left = req->x;
2227 input->caret_rect.top = req->y;
2228 }
2229 if (req->flags & SET_CARET_HIDE)
2230 {
2231 input->caret_hide += req->hide;
2232 if (input->caret_hide < 0) input->caret_hide = 0;
2233 }
2234 if (req->flags & SET_CARET_STATE)
2235 {
2236 if (req->state == -1) input->caret_state = !input->caret_state;
2237 else input->caret_state = !!req->state;
2238 }
2239}
Mike McCormackabe70f72005-04-28 12:04:14 +00002240
2241
2242/* get the time of the last input event */
2243DECL_HANDLER(get_last_input_time)
2244{
2245 reply->time = last_input_time;
2246}