blob: 755d3c19cbffddd93e96625f18aa839b703b0def [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 Julliard8159d4e2010-03-22 17:52:23 +0100106 user_handle_t cursor; /* current cursor */
107 int cursor_count; /* cursor show count */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000108 struct list msg_list; /* list of hardware messages */
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000109 unsigned char keystate[256]; /* state of each key */
110};
111
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000112struct msg_queue
113{
Alexandre Julliard039e1312003-07-26 20:36:43 +0000114 struct object obj; /* object header */
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200115 struct fd *fd; /* optional file descriptor to poll */
Alexandre Julliard039e1312003-07-26 20:36:43 +0000116 unsigned int wake_bits; /* wakeup bits */
117 unsigned int wake_mask; /* wakeup mask */
118 unsigned int changed_bits; /* changed wakeup bits */
119 unsigned int changed_mask; /* changed wakeup mask */
120 int paint_count; /* pending paint messages count */
Robert Shearmana40ce392006-01-17 13:14:31 +0100121 int quit_message; /* is there a pending quit message? */
122 int exit_code; /* exit code of pending quit message */
Alexandre Julliard8159d4e2010-03-22 17:52:23 +0100123 int cursor_count; /* per-queue cursor show count */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000124 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
Alexandre Julliard039e1312003-07-26 20:36:43 +0000125 struct list send_result; /* stack of sent messages waiting for result */
126 struct list callback_result; /* list of callback messages waiting for result */
127 struct message_result *recv_result; /* stack of received messages waiting for result */
Alexandre Julliardff986a52004-11-29 18:08:18 +0000128 struct list pending_timers; /* list of pending timers */
129 struct list expired_timers; /* list of expired timers */
Alexandre Julliard31282b32008-12-10 16:01:50 +0100130 lparam_t next_timer_id; /* id for the next timer with a 0 window */
Alexandre Julliard039e1312003-07-26 20:36:43 +0000131 struct timeout_user *timeout; /* timeout for next timer to expire */
132 struct thread_input *input; /* thread input descriptor */
133 struct hook_table *hooks; /* hook table */
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200134 timeout_t last_get_msg; /* time of last get message call */
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000135};
136
137static void msg_queue_dump( struct object *obj, int verbose );
138static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
139static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
140static int msg_queue_signaled( struct object *obj, struct thread *thread );
141static int msg_queue_satisfied( struct object *obj, struct thread *thread );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000142static void msg_queue_destroy( struct object *obj );
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200143static void msg_queue_poll_event( struct fd *fd, int event );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000144static void thread_input_dump( struct object *obj, int verbose );
145static void thread_input_destroy( struct object *obj );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000146static void timer_callback( void *private );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000147
148static const struct object_ops msg_queue_ops =
149{
150 sizeof(struct msg_queue), /* size */
151 msg_queue_dump, /* dump */
Alexandre Julliard8382eb02007-12-05 18:16:42 +0100152 no_get_type, /* get_type */
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000153 msg_queue_add_queue, /* add_queue */
154 msg_queue_remove_queue, /* remove_queue */
155 msg_queue_signaled, /* signaled */
156 msg_queue_satisfied, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +0000157 no_signal, /* signal */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +0000158 no_get_fd, /* get_fd */
Alexandre Julliard28beba32005-12-12 14:57:40 +0100159 no_map_access, /* map_access */
Rob Shearmanc1707d82007-10-03 13:10:37 +0100160 default_get_sd, /* get_sd */
161 default_set_sd, /* set_sd */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000162 no_lookup_name, /* lookup_name */
Alexandre Julliard7e71c1d2007-03-22 11:44:29 +0100163 no_open_file, /* open_file */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000164 no_close_handle, /* close_handle */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000165 msg_queue_destroy /* destroy */
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000166};
167
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200168static const struct fd_ops msg_queue_fd_ops =
169{
170 NULL, /* get_poll_events */
171 msg_queue_poll_event, /* poll_event */
Alexandre Julliard3f057592007-04-12 20:21:53 +0200172 NULL, /* flush */
173 NULL, /* get_fd_type */
Alexandre Julliard63571432007-04-16 14:45:03 +0200174 NULL, /* ioctl */
Alexandre Julliard3f057592007-04-12 20:21:53 +0200175 NULL, /* queue_async */
Alexandre Julliard72bff2e2007-04-10 17:07:27 +0200176 NULL, /* reselect_async */
Alexandre Julliard3f057592007-04-12 20:21:53 +0200177 NULL /* cancel async */
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200178};
179
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000180
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000181static const struct object_ops thread_input_ops =
182{
183 sizeof(struct thread_input), /* size */
184 thread_input_dump, /* dump */
Alexandre Julliard8382eb02007-12-05 18:16:42 +0100185 no_get_type, /* get_type */
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000186 no_add_queue, /* add_queue */
187 NULL, /* remove_queue */
188 NULL, /* signaled */
189 NULL, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +0000190 no_signal, /* signal */
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000191 no_get_fd, /* get_fd */
Alexandre Julliard28beba32005-12-12 14:57:40 +0100192 no_map_access, /* map_access */
Rob Shearmanc1707d82007-10-03 13:10:37 +0100193 default_get_sd, /* get_sd */
194 default_set_sd, /* set_sd */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000195 no_lookup_name, /* lookup_name */
Alexandre Julliard7e71c1d2007-03-22 11:44:29 +0100196 no_open_file, /* open_file */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000197 no_close_handle, /* close_handle */
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000198 thread_input_destroy /* destroy */
199};
200
Alexandre Julliard242e3952003-01-08 00:27:58 +0000201/* pointer to input structure of foreground thread */
202static struct thread_input *foreground_input;
Mike McCormackabe70f72005-04-28 12:04:14 +0000203static unsigned int last_input_time;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000204
Alexandre Julliard3ad97982006-10-04 20:25:42 +0200205static void free_message( struct message *msg );
Alexandre Julliard11e35232002-10-17 01:24:33 +0000206
207/* set the caret window in a given thread input */
208static void set_caret_window( struct thread_input *input, user_handle_t win )
209{
Krzysztof Foltmanb8501722005-02-18 20:02:55 +0000210 if (!win || win != input->caret)
211 {
212 input->caret_rect.left = 0;
213 input->caret_rect.top = 0;
214 input->caret_rect.right = 0;
215 input->caret_rect.bottom = 0;
216 }
Alexandre Julliard11e35232002-10-17 01:24:33 +0000217 input->caret = win;
Alexandre Julliard11e35232002-10-17 01:24:33 +0000218 input->caret_hide = 1;
219 input->caret_state = 0;
220}
221
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000222/* create a thread input object */
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000223static struct thread_input *create_thread_input( struct thread *thread )
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000224{
225 struct thread_input *input;
226
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000227 if ((input = alloc_object( &thread_input_ops )))
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000228 {
Alexandre Julliard8159d4e2010-03-22 17:52:23 +0100229 input->focus = 0;
230 input->capture = 0;
231 input->active = 0;
232 input->menu_owner = 0;
233 input->move_size = 0;
234 input->cursor = 0;
235 input->cursor_count = 0;
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000236 list_init( &input->msg_list );
Alexandre Julliard11e35232002-10-17 01:24:33 +0000237 set_caret_window( input, 0 );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000238 memset( input->keystate, 0, sizeof(input->keystate) );
Robert Shearmand8058fb2006-04-07 11:15:19 +0100239
240 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
241 {
242 release_object( input );
243 return NULL;
244 }
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000245 }
246 return input;
247}
248
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000249/* create a message queue object */
250static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000251{
Alexandre Julliard5efe9962010-03-22 17:48:22 +0100252 struct thread_input *new_input = NULL;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000253 struct msg_queue *queue;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000254 int i;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000255
Alexandre Julliard5efe9962010-03-22 17:48:22 +0100256 if (!input)
257 {
258 if (!(new_input = create_thread_input( thread ))) return NULL;
259 input = new_input;
260 }
261
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000262 if ((queue = alloc_object( &msg_queue_ops )))
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000263 {
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200264 queue->fd = NULL;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000265 queue->wake_bits = 0;
266 queue->wake_mask = 0;
267 queue->changed_bits = 0;
268 queue->changed_mask = 0;
Alexandre Julliard4b0343d2001-06-20 22:55:31 +0000269 queue->paint_count = 0;
Robert Shearmana40ce392006-01-17 13:14:31 +0100270 queue->quit_message = 0;
Alexandre Julliard8159d4e2010-03-22 17:52:23 +0100271 queue->cursor_count = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000272 queue->recv_result = NULL;
Dmitry Timoshkovb4227d72007-11-13 20:41:01 +0800273 queue->next_timer_id = 0x7fff;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000274 queue->timeout = NULL;
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000275 queue->input = (struct thread_input *)grab_object( input );
Alexandre Julliardd55e7f12003-07-03 18:16:48 +0000276 queue->hooks = NULL;
Alexandre Julliard753c8702006-08-10 16:42:09 +0200277 queue->last_get_msg = current_time;
Alexandre Julliard039e1312003-07-26 20:36:43 +0000278 list_init( &queue->send_result );
279 list_init( &queue->callback_result );
Alexandre Julliardff986a52004-11-29 18:08:18 +0000280 list_init( &queue->pending_timers );
281 list_init( &queue->expired_timers );
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000282 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000283
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000284 thread->queue = queue;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000285 }
Alexandre Julliard5efe9962010-03-22 17:48:22 +0100286 if (new_input) release_object( new_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{
Alexandre Julliardca3ac8f2003-07-11 21:55:58 +0000293 remove_thread_hooks( thread );
Alexandre Julliard31022d62002-08-16 23:30:41 +0000294 if (!thread->queue) return;
Alexandre Julliard31022d62002-08-16 23:30:41 +0000295 release_object( thread->queue );
296 thread->queue = NULL;
297}
298
Alexandre Julliard5efe9962010-03-22 17:48:22 +0100299/* change the thread input data of a given thread */
300static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
301{
Alexandre Julliard8159d4e2010-03-22 17:52:23 +0100302 struct msg_queue *queue = thread->queue;
303
304 if (!queue)
Alexandre Julliard5efe9962010-03-22 17:48:22 +0100305 {
306 thread->queue = create_msg_queue( thread, new_input );
307 return thread->queue != NULL;
308 }
Alexandre Julliard8159d4e2010-03-22 17:52:23 +0100309 if (queue->input)
310 {
311 queue->input->cursor_count -= queue->cursor_count;
312 release_object( queue->input );
313 }
314 queue->input = (struct thread_input *)grab_object( new_input );
315 new_input->cursor_count += queue->cursor_count;
Alexandre Julliard5efe9962010-03-22 17:48:22 +0100316 return 1;
317}
318
Alexandre Julliardd55e7f12003-07-03 18:16:48 +0000319/* get the hook table for a given thread */
320struct hook_table *get_queue_hooks( struct thread *thread )
321{
322 if (!thread->queue) return NULL;
323 return thread->queue->hooks;
324}
325
326/* set the hook table for a given thread, allocating the queue if needed */
327void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
328{
329 struct msg_queue *queue = thread->queue;
Alexandre Julliardd70a2532005-04-26 14:31:33 +0000330 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
Alexandre Julliardd55e7f12003-07-03 18:16:48 +0000331 if (queue->hooks) release_object( queue->hooks );
332 queue->hooks = hooks;
333}
334
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000335/* check the queue status */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000336static inline int is_signaled( struct msg_queue *queue )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000337{
338 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
339}
340
Alexandre Julliardd253c582001-08-07 19:19:08 +0000341/* set some queue bits */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000342static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000343{
Alexandre Julliardd253c582001-08-07 19:19:08 +0000344 queue->wake_bits |= bits;
345 queue->changed_bits |= bits;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000346 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
347}
348
Alexandre Julliardd253c582001-08-07 19:19:08 +0000349/* clear some queue bits */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000350static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
Alexandre Julliardd253c582001-08-07 19:19:08 +0000351{
352 queue->wake_bits &= ~bits;
353 queue->changed_bits &= ~bits;
354}
355
Alexandre Julliard242e3952003-01-08 00:27:58 +0000356/* check whether msg is a keyboard message */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000357static inline int is_keyboard_msg( struct message *msg )
Alexandre Julliard242e3952003-01-08 00:27:58 +0000358{
359 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
360}
361
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +0000362/* check if message is matched by the filter */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000363static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +0000364{
365 return (msg >= first && msg <= last);
366}
367
Alexandre Julliardcf2f1422005-03-14 17:17:09 +0000368/* check whether a message filter contains at least one potential hardware message */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000369static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
Alexandre Julliardcf2f1422005-03-14 17:17:09 +0000370{
371 /* hardware message ranges are (in numerical order):
372 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
373 * WM_KEYFIRST .. WM_KEYLAST
374 * WM_MOUSEFIRST .. WM_MOUSELAST
375 */
376 if (last < WM_NCMOUSEFIRST) return 0;
377 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
378 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
379 if (first > WM_MOUSELAST) return 0;
380 return 1;
381}
382
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000383/* get the QS_* bit corresponding to a given hardware message */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000384static inline int get_hardware_msg_bit( struct message *msg )
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000385{
386 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000387 if (is_keyboard_msg( msg )) return QS_KEY;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000388 return QS_MOUSEBUTTON;
389}
390
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000391/* get the current thread queue, creating it if needed */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000392static inline struct msg_queue *get_current_queue(void)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000393{
394 struct msg_queue *queue = current->queue;
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000395 if (!queue) queue = create_msg_queue( current, NULL );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000396 return queue;
397}
398
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +0000399/* get a (pseudo-)unique id to tag hardware messages */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000400static inline unsigned int get_unique_id(void)
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +0000401{
402 static unsigned int id;
403 if (!++id) id = 1; /* avoid an id of 0 */
404 return id;
405}
406
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000407/* try to merge a message with the last in the list; return 1 if successful */
Alexandre Julliard242e3952003-01-08 00:27:58 +0000408static int merge_message( struct thread_input *input, const struct message *msg )
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000409{
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000410 struct message *prev;
411 struct list *ptr = list_tail( &input->msg_list );
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000412
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000413 if (!ptr) return 0;
414 prev = LIST_ENTRY( ptr, struct message, entry );
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000415 if (prev->result) return 0;
Alexandre Julliard942a70d2007-11-27 16:43:00 +0100416 if (prev->win && msg->win && prev->win != msg->win) return 0;
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000417 if (prev->msg != msg->msg) return 0;
418 if (prev->type != msg->type) return 0;
419 /* now we can merge it */
420 prev->wparam = msg->wparam;
421 prev->lparam = msg->lparam;
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000422 prev->time = msg->time;
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +0100423 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
424 {
425 struct hardware_msg_data *prev_data = prev->data;
426 struct hardware_msg_data *msg_data = msg->data;
427 prev_data->x = msg_data->x;
428 prev_data->y = msg_data->y;
429 prev_data->info = msg_data->info;
430 }
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000431 return 1;
432}
433
Alexandre Julliardd253c582001-08-07 19:19:08 +0000434/* free a result structure */
435static void free_result( struct message_result *result )
436{
437 if (result->timeout) remove_timeout_user( result->timeout );
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +0200438 free( result->data );
Alexandre Julliard3ad97982006-10-04 20:25:42 +0200439 if (result->callback_msg) free_message( result->callback_msg );
Alexandre Julliardd253c582001-08-07 19:19:08 +0000440 free( result );
441}
442
Alexandre Julliard039e1312003-07-26 20:36:43 +0000443/* remove the result from the sender list it is on */
444static inline void remove_result_from_sender( struct message_result *result )
445{
446 assert( result->sender );
447
448 list_remove( &result->sender_entry );
449 result->sender = NULL;
450 if (!result->receiver) free_result( result );
451}
452
Alexandre Julliardd253c582001-08-07 19:19:08 +0000453/* store the message result in the appropriate structure */
Alexandre Julliard31282b32008-12-10 16:01:50 +0100454static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
Alexandre Julliardd253c582001-08-07 19:19:08 +0000455{
456 res->result = result;
457 res->error = error;
458 res->replied = 1;
459 if (res->timeout)
460 {
461 remove_timeout_user( res->timeout );
462 res->timeout = NULL;
463 }
Alexandre Julliard039e1312003-07-26 20:36:43 +0000464 if (res->sender)
465 {
466 if (res->callback_msg)
467 {
468 /* queue the callback message in the sender queue */
Alexandre Julliard29a3ce92006-10-04 16:29:45 +0200469 struct callback_msg_data *data = res->callback_msg->data;
470 data->result = result;
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000471 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
Alexandre Julliard039e1312003-07-26 20:36:43 +0000472 set_queue_bits( res->sender, QS_SENDMESSAGE );
473 res->callback_msg = NULL;
474 remove_result_from_sender( res );
475 }
476 else
477 {
478 /* wake sender queue if waiting on this result */
479 if (list_head(&res->sender->send_result) == &res->sender_entry)
480 set_queue_bits( res->sender, QS_SMRESULT );
481 }
482 }
483
Alexandre Julliardd253c582001-08-07 19:19:08 +0000484}
485
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000486/* free a message when deleting a queue or window */
487static void free_message( struct message *msg )
488{
489 struct message_result *result = msg->result;
490 if (result)
491 {
Alexandre Julliard127127f2005-09-13 14:46:46 +0000492 result->msg = NULL;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000493 if (result->sender)
494 {
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000495 result->receiver = NULL;
Alexandre Julliardd253c582001-08-07 19:19:08 +0000496 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000497 }
Alexandre Julliardd253c582001-08-07 19:19:08 +0000498 else free_result( result );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000499 }
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +0200500 free( msg->data );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000501 free( msg );
502}
503
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000504/* remove (and free) a message from a message list */
505static void remove_queue_message( struct msg_queue *queue, struct message *msg,
506 enum message_kind kind )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000507{
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000508 list_remove( &msg->entry );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000509 switch(kind)
510 {
511 case SEND_MESSAGE:
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000512 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000513 break;
514 case POST_MESSAGE:
Robert Shearman22bd7a32006-05-22 22:16:53 +0100515 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
516 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000517 break;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000518 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000519 free_message( msg );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000520}
521
Alexandre Julliardd253c582001-08-07 19:19:08 +0000522/* message timed out without getting a reply */
523static void result_timeout( void *private )
524{
525 struct message_result *result = private;
526
527 assert( !result->replied );
528
529 result->timeout = NULL;
Alexandre Julliard127127f2005-09-13 14:46:46 +0000530
531 if (result->msg) /* not received yet */
532 {
533 struct message *msg = result->msg;
534
535 result->msg = NULL;
536 msg->result = NULL;
537 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
538 result->receiver = NULL;
539 if (!result->sender)
540 {
541 free_result( result );
542 return;
543 }
544 }
545
Alexandre Julliardd253c582001-08-07 19:19:08 +0000546 store_message_result( result, 0, STATUS_TIMEOUT );
547}
548
549/* allocate and fill a message result structure */
550static struct message_result *alloc_message_result( struct msg_queue *send_queue,
551 struct msg_queue *recv_queue,
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200552 struct message *msg, timeout_t timeout )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000553{
554 struct message_result *result = mem_alloc( sizeof(*result) );
Alexandre Julliardd253c582001-08-07 19:19:08 +0000555 if (result)
556 {
Alexandre Julliard127127f2005-09-13 14:46:46 +0000557 result->msg = msg;
Alexandre Julliardd253c582001-08-07 19:19:08 +0000558 result->sender = send_queue;
559 result->receiver = recv_queue;
560 result->replied = 0;
561 result->data = NULL;
562 result->data_size = 0;
563 result->timeout = NULL;
Alexandre Julliard039e1312003-07-26 20:36:43 +0000564
565 if (msg->type == MSG_CALLBACK)
566 {
567 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
Alexandre Julliard29a3ce92006-10-04 16:29:45 +0200568
Alexandre Julliard039e1312003-07-26 20:36:43 +0000569 if (!callback_msg)
570 {
571 free( result );
572 return NULL;
573 }
574 callback_msg->type = MSG_CALLBACK_RESULT;
575 callback_msg->win = msg->win;
576 callback_msg->msg = msg->msg;
Alexandre Julliard29a3ce92006-10-04 16:29:45 +0200577 callback_msg->wparam = 0;
Alexandre Julliard039e1312003-07-26 20:36:43 +0000578 callback_msg->lparam = 0;
579 callback_msg->time = get_tick_count();
Alexandre Julliard039e1312003-07-26 20:36:43 +0000580 callback_msg->result = NULL;
Alexandre Julliard3ad97982006-10-04 20:25:42 +0200581 /* steal the data from the original message */
582 callback_msg->data = msg->data;
583 callback_msg->data_size = msg->data_size;
584 msg->data = NULL;
585 msg->data_size = 0;
Alexandre Julliard039e1312003-07-26 20:36:43 +0000586
587 result->callback_msg = callback_msg;
588 list_add_head( &send_queue->callback_result, &result->sender_entry );
589 }
590 else
591 {
592 result->callback_msg = NULL;
593 list_add_head( &send_queue->send_result, &result->sender_entry );
594 }
595
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200596 if (timeout != TIMEOUT_INFINITE)
597 result->timeout = add_timeout_user( timeout, result_timeout, result );
Alexandre Julliardd253c582001-08-07 19:19:08 +0000598 }
599 return result;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000600}
601
602/* receive a message, removing it from the sent queue */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000603static void receive_message( struct msg_queue *queue, struct message *msg,
604 struct get_message_reply *reply )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000605{
606 struct message_result *result = msg->result;
607
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000608 reply->total = msg->data_size;
609 if (msg->data_size > get_reply_max_size())
610 {
611 set_error( STATUS_BUFFER_OVERFLOW );
612 return;
613 }
614 reply->type = msg->type;
615 reply->win = msg->win;
616 reply->msg = msg->msg;
617 reply->wparam = msg->wparam;
618 reply->lparam = msg->lparam;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000619 reply->time = msg->time;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000620
621 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
622
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000623 list_remove( &msg->entry );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000624 /* put the result on the receiver result stack */
Alexandre Julliardd253c582001-08-07 19:19:08 +0000625 if (result)
626 {
Alexandre Julliard127127f2005-09-13 14:46:46 +0000627 result->msg = NULL;
Alexandre Julliardd253c582001-08-07 19:19:08 +0000628 result->recv_next = queue->recv_result;
629 queue->recv_result = result;
630 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000631 free( msg );
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000632 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000633}
634
635/* set the result of the current received message */
Alexandre Julliard31282b32008-12-10 16:01:50 +0100636static void reply_message( struct msg_queue *queue, lparam_t result,
Alexandre Julliard0f273c12006-07-26 10:43:25 +0200637 unsigned int error, int remove, const void *data, data_size_t len )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000638{
639 struct message_result *res = queue->recv_result;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000640
641 if (remove)
642 {
643 queue->recv_result = res->recv_next;
644 res->receiver = NULL;
645 if (!res->sender) /* no one waiting for it */
646 {
Alexandre Julliardd253c582001-08-07 19:19:08 +0000647 free_result( res );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000648 return;
649 }
650 }
651 if (!res->replied)
652 {
Alexandre Julliardd253c582001-08-07 19:19:08 +0000653 if (len && (res->data = memdup( data, len ))) res->data_size = len;
654 store_message_result( res, result, error );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000655 }
656}
657
Dmitry Timoshkovacb05662009-06-11 21:49:31 +0900658static int match_window( user_handle_t win, user_handle_t msg_win )
659{
660 if (!win) return 1;
Alexandre Julliard4d33d492010-03-03 13:18:33 +0100661 if (win == -1 || win == 1) return !msg_win;
Dmitry Timoshkovacb05662009-06-11 21:49:31 +0900662 if (msg_win == win) return 1;
663 return is_child_window( win, msg_win );
664}
665
Alexandre Julliard242e3952003-01-08 00:27:58 +0000666/* retrieve a posted message */
667static int get_posted_message( struct msg_queue *queue, user_handle_t win,
668 unsigned int first, unsigned int last, unsigned int flags,
669 struct get_message_reply *reply )
670{
671 struct message *msg;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000672
673 /* check against the filters */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000674 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
Alexandre Julliard242e3952003-01-08 00:27:58 +0000675 {
Dmitry Timoshkovacb05662009-06-11 21:49:31 +0900676 if (!match_window( win, msg->win )) continue;
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +0000677 if (!check_msg_filter( msg->msg, first, last )) continue;
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000678 goto found; /* found one */
Alexandre Julliard242e3952003-01-08 00:27:58 +0000679 }
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000680 return 0;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000681
682 /* return it to the app */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000683found:
Alexandre Julliard242e3952003-01-08 00:27:58 +0000684 reply->total = msg->data_size;
685 if (msg->data_size > get_reply_max_size())
686 {
687 set_error( STATUS_BUFFER_OVERFLOW );
688 return 1;
689 }
690 reply->type = msg->type;
691 reply->win = msg->win;
692 reply->msg = msg->msg;
693 reply->wparam = msg->wparam;
694 reply->lparam = msg->lparam;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000695 reply->time = msg->time;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000696
Alexandre Julliard89faee02007-02-21 15:21:05 +0100697 if (flags & PM_REMOVE)
Alexandre Julliard242e3952003-01-08 00:27:58 +0000698 {
699 if (msg->data)
700 {
701 set_reply_data_ptr( msg->data, msg->data_size );
702 msg->data = NULL;
703 msg->data_size = 0;
704 }
705 remove_queue_message( queue, msg, POST_MESSAGE );
706 }
707 else if (msg->data) set_reply_data( msg->data, msg->data_size );
708
709 return 1;
710}
711
Robert Shearmana40ce392006-01-17 13:14:31 +0100712static int get_quit_message( struct msg_queue *queue, unsigned int flags,
713 struct get_message_reply *reply )
714{
715 if (queue->quit_message)
716 {
717 reply->total = 0;
718 reply->type = MSG_POSTED;
Alexandre Julliardd7641072008-12-08 16:57:38 +0100719 reply->win = 0;
Robert Shearmana40ce392006-01-17 13:14:31 +0100720 reply->msg = WM_QUIT;
721 reply->wparam = queue->exit_code;
722 reply->lparam = 0;
Robert Shearmana40ce392006-01-17 13:14:31 +0100723 reply->time = get_tick_count();
Robert Shearmana40ce392006-01-17 13:14:31 +0100724
Alexandre Julliard89faee02007-02-21 15:21:05 +0100725 if (flags & PM_REMOVE)
Robert Shearman22bd7a32006-05-22 22:16:53 +0100726 {
Robert Shearmana40ce392006-01-17 13:14:31 +0100727 queue->quit_message = 0;
Robert Shearman22bd7a32006-05-22 22:16:53 +0100728 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
729 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
730 }
Robert Shearmana40ce392006-01-17 13:14:31 +0100731 return 1;
732 }
733 else
734 return 0;
735}
736
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000737/* empty a message list and free all the messages */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000738static void empty_msg_list( struct list *list )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000739{
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000740 struct list *ptr;
741
742 while ((ptr = list_head( list )) != NULL)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000743 {
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000744 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
745 list_remove( &msg->entry );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000746 free_message( msg );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000747 }
748}
749
750/* cleanup all pending results when deleting a queue */
751static void cleanup_results( struct msg_queue *queue )
752{
Alexandre Julliard039e1312003-07-26 20:36:43 +0000753 struct list *entry;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000754
Alexandre Julliard039e1312003-07-26 20:36:43 +0000755 while ((entry = list_head( &queue->send_result )) != NULL)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000756 {
Alexandre Julliard039e1312003-07-26 20:36:43 +0000757 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
758 }
759
760 while ((entry = list_head( &queue->callback_result )) != NULL)
761 {
762 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000763 }
764
Alexandre Julliardd253c582001-08-07 19:19:08 +0000765 while (queue->recv_result)
766 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000767}
768
Alexandre Julliard09029b22003-07-11 04:09:42 +0000769/* check if the thread owning the queue is hung (not checking for messages) */
770static int is_queue_hung( struct msg_queue *queue )
771{
Alexandre Julliard09029b22003-07-11 04:09:42 +0000772 struct wait_queue_entry *entry;
773
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200774 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
Alexandre Julliard09029b22003-07-11 04:09:42 +0000775 return 0; /* less than 5 seconds since last get message -> not hung */
776
Alexandre Julliardaa347682005-03-01 11:49:58 +0000777 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
Alexandre Julliard09029b22003-07-11 04:09:42 +0000778 {
779 if (entry->thread->queue == queue)
780 return 0; /* thread is waiting on queue -> not hung */
781 }
782 return 1;
783}
784
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000785static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
786{
787 struct msg_queue *queue = (struct msg_queue *)obj;
788 struct process *process = entry->thread->process;
789
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000790 /* a thread can only wait on its own queue */
791 if (entry->thread->queue != queue)
792 {
793 set_error( STATUS_ACCESS_DENIED );
794 return 0;
795 }
Alexandre Julliard92e704e2009-12-16 17:32:15 +0100796 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
Alexandre Julliard7d4e2842009-12-16 17:25:01 +0100797
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200798 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
799 set_fd_events( queue->fd, POLLIN );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000800 add_queue( obj, entry );
801 return 1;
802}
803
804static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
805{
806 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000807
808 remove_queue( obj, entry );
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200809 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
810 set_fd_events( queue->fd, 0 );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000811}
812
813static void msg_queue_dump( struct object *obj, int verbose )
814{
815 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000816 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
817 queue->wake_bits, queue->wake_mask );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000818}
819
820static int msg_queue_signaled( struct object *obj, struct thread *thread )
821{
822 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200823 int ret = 0;
824
825 if (queue->fd)
826 {
827 if ((ret = check_fd_events( queue->fd, POLLIN )))
828 /* stop waiting on select() if we are signaled */
829 set_fd_events( queue->fd, 0 );
830 else if (!list_empty( &obj->wait_queue ))
831 /* restart waiting on poll() if we are no longer signaled */
832 set_fd_events( queue->fd, POLLIN );
833 }
834 return ret || is_signaled( queue );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000835}
836
837static int msg_queue_satisfied( struct object *obj, struct thread *thread )
838{
839 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000840 queue->wake_mask = 0;
841 queue->changed_mask = 0;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000842 return 0; /* Not abandoned */
843}
844
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000845static void msg_queue_destroy( struct object *obj )
846{
847 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliardff986a52004-11-29 18:08:18 +0000848 struct list *ptr;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000849 int i;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000850
851 cleanup_results( queue );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000852 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000853
Alexandre Julliardff986a52004-11-29 18:08:18 +0000854 while ((ptr = list_head( &queue->pending_timers )))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000855 {
Alexandre Julliardff986a52004-11-29 18:08:18 +0000856 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
857 list_remove( &timer->entry );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000858 free( timer );
Alexandre Julliardff986a52004-11-29 18:08:18 +0000859 }
860 while ((ptr = list_head( &queue->expired_timers )))
861 {
862 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
863 list_remove( &timer->entry );
864 free( timer );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000865 }
866 if (queue->timeout) remove_timeout_user( queue->timeout );
Alexandre Julliard8159d4e2010-03-22 17:52:23 +0100867 if (queue->input)
868 {
869 queue->input->cursor_count -= queue->cursor_count;
870 release_object( queue->input );
871 }
Alexandre Julliardd55e7f12003-07-03 18:16:48 +0000872 if (queue->hooks) release_object( queue->hooks );
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200873 if (queue->fd) release_object( queue->fd );
874}
875
876static void msg_queue_poll_event( struct fd *fd, int event )
877{
878 struct msg_queue *queue = get_fd_user( fd );
879 assert( queue->obj.ops == &msg_queue_ops );
880
881 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
Alexandre Julliard2d69ba42009-06-02 22:14:40 +0200882 else set_fd_events( queue->fd, 0 );
Alexandre Julliard0cb29f42007-04-04 18:02:01 +0200883 wake_up( &queue->obj, 0 );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000884}
885
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000886static void thread_input_dump( struct object *obj, int verbose )
887{
888 struct thread_input *input = (struct thread_input *)obj;
Alexandre Julliardd7641072008-12-08 16:57:38 +0100889 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000890 input->focus, input->capture, input->active );
891}
892
893static void thread_input_destroy( struct object *obj )
894{
895 struct thread_input *input = (struct thread_input *)obj;
896
897 if (foreground_input == input) foreground_input = NULL;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000898 empty_msg_list( &input->msg_list );
Robert Shearmand8058fb2006-04-07 11:15:19 +0100899 if (input->desktop) release_object( input->desktop );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000900}
901
902/* fix the thread input data when a window is destroyed */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000903static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000904{
905 struct thread_input *input = queue->input;
906
907 if (window == input->focus) input->focus = 0;
908 if (window == input->capture) input->capture = 0;
909 if (window == input->active) input->active = 0;
910 if (window == input->menu_owner) input->menu_owner = 0;
911 if (window == input->move_size) input->move_size = 0;
Alexandre Julliard11e35232002-10-17 01:24:33 +0000912 if (window == input->caret) set_caret_window( input, 0 );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000913}
914
Alexandre Julliard5030bda2002-10-11 23:41:06 +0000915/* check if the specified window can be set in the input data of a given queue */
916static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
917{
918 struct thread *thread;
919 int ret = 0;
920
921 if (!window) return 1; /* we can always clear the data */
922
923 if ((thread = get_window_thread( window )))
924 {
925 ret = (queue->input == thread->queue->input);
926 if (!ret) set_error( STATUS_ACCESS_DENIED );
927 release_object( thread );
928 }
929 else set_error( STATUS_INVALID_HANDLE );
930
931 return ret;
932}
933
Alexandre Julliardd70a2532005-04-26 14:31:33 +0000934/* make sure the specified thread has a queue */
935int init_thread_queue( struct thread *thread )
936{
937 if (thread->queue) return 1;
938 return (create_msg_queue( thread, NULL ) != NULL);
939}
940
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000941/* attach two thread input data structures */
942int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
943{
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000944 struct desktop *desktop;
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000945 struct thread_input *input;
Alexandre Julliard5efe9962010-03-22 17:48:22 +0100946 int ret;
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000947
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
Alexandre Julliard5efe9962010-03-22 17:48:22 +0100960 ret = assign_thread_input( thread_from, input );
961 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
962 release_object( input );
963 return ret;
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000964}
965
966/* detach two thread input data structures */
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000967void detach_thread_input( struct thread *thread_from )
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000968{
969 struct thread_input *input;
970
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000971 if ((input = create_thread_input( thread_from )))
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000972 {
Alexandre Julliard5efe9962010-03-22 17:48:22 +0100973 assign_thread_input( thread_from, input );
974 release_object( input );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000975 }
976}
977
978
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000979/* set the next timer to expire */
Alexandre Julliardff986a52004-11-29 18:08:18 +0000980static void set_next_timer( struct msg_queue *queue )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000981{
Alexandre Julliardff986a52004-11-29 18:08:18 +0000982 struct list *ptr;
983
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000984 if (queue->timeout)
985 {
986 remove_timeout_user( queue->timeout );
987 queue->timeout = NULL;
988 }
Alexandre Julliardff986a52004-11-29 18:08:18 +0000989 if ((ptr = list_head( &queue->pending_timers )))
990 {
991 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200992 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
Alexandre Julliardff986a52004-11-29 18:08:18 +0000993 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000994 /* set/clear QS_TIMER bit */
Alexandre Julliardff986a52004-11-29 18:08:18 +0000995 if (list_empty( &queue->expired_timers ))
Alexandre Julliardd253c582001-08-07 19:19:08 +0000996 clear_queue_bits( queue, QS_TIMER );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000997 else
Alexandre Julliardd253c582001-08-07 19:19:08 +0000998 set_queue_bits( queue, QS_TIMER );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000999}
1000
Alexandre Julliardff986a52004-11-29 18:08:18 +00001001/* find a timer from its window and id */
1002static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
Alexandre Julliard31282b32008-12-10 16:01:50 +01001003 unsigned int msg, lparam_t id )
Alexandre Julliardff986a52004-11-29 18:08:18 +00001004{
1005 struct list *ptr;
1006
1007 /* we need to search both lists */
1008
1009 LIST_FOR_EACH( ptr, &queue->pending_timers )
1010 {
1011 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1012 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1013 }
1014 LIST_FOR_EACH( ptr, &queue->expired_timers )
1015 {
1016 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1017 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1018 }
1019 return NULL;
1020}
1021
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001022/* callback for the next timer expiration */
1023static void timer_callback( void *private )
1024{
1025 struct msg_queue *queue = private;
Alexandre Julliardff986a52004-11-29 18:08:18 +00001026 struct list *ptr;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001027
1028 queue->timeout = NULL;
1029 /* move on to the next timer */
Alexandre Julliardff986a52004-11-29 18:08:18 +00001030 ptr = list_head( &queue->pending_timers );
1031 list_remove( ptr );
1032 list_add_tail( &queue->expired_timers, ptr );
1033 set_next_timer( queue );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001034}
1035
1036/* link a timer at its rightful place in the queue list */
1037static void link_timer( struct msg_queue *queue, struct timer *timer )
1038{
Alexandre Julliardff986a52004-11-29 18:08:18 +00001039 struct list *ptr;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001040
Alexandre Julliardff986a52004-11-29 18:08:18 +00001041 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001042 {
Alexandre Julliardff986a52004-11-29 18:08:18 +00001043 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
Alexandre Julliardaaf477f2007-04-17 20:08:59 +02001044 if (t->when >= timer->when) break;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001045 }
Alexandre Julliardff986a52004-11-29 18:08:18 +00001046 list_add_before( ptr, &timer->entry );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001047}
1048
Alexandre Julliardff986a52004-11-29 18:08:18 +00001049/* remove a timer from the queue timer list and free it */
1050static void free_timer( struct msg_queue *queue, struct timer *timer )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001051{
Alexandre Julliardff986a52004-11-29 18:08:18 +00001052 list_remove( &timer->entry );
1053 free( timer );
1054 set_next_timer( queue );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001055}
1056
1057/* restart an expired timer */
1058static void restart_timer( struct msg_queue *queue, struct timer *timer )
1059{
Alexandre Julliardff986a52004-11-29 18:08:18 +00001060 list_remove( &timer->entry );
Alexandre Julliardaaf477f2007-04-17 20:08:59 +02001061 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001062 link_timer( queue, timer );
Alexandre Julliardff986a52004-11-29 18:08:18 +00001063 set_next_timer( queue );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001064}
1065
1066/* find an expired timer matching the filtering parameters */
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001067static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001068 unsigned int get_first, unsigned int get_last,
1069 int remove )
1070{
Alexandre Julliardff986a52004-11-29 18:08:18 +00001071 struct list *ptr;
1072
1073 LIST_FOR_EACH( ptr, &queue->expired_timers )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001074 {
Alexandre Julliardff986a52004-11-29 18:08:18 +00001075 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001076 if (win && timer->win != win) continue;
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001077 if (check_msg_filter( timer->msg, get_first, get_last ))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001078 {
1079 if (remove) restart_timer( queue, timer );
1080 return timer;
1081 }
1082 }
1083 return NULL;
1084}
1085
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001086/* add a timer */
1087static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1088{
1089 struct timer *timer = mem_alloc( sizeof(*timer) );
1090 if (timer)
1091 {
Alexandre Julliard753c8702006-08-10 16:42:09 +02001092 timer->rate = max( rate, 1 );
Alexandre Julliardaaf477f2007-04-17 20:08:59 +02001093 timer->when = current_time + (timeout_t)timer->rate * 10000;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001094 link_timer( queue, timer );
Alexandre Julliardff986a52004-11-29 18:08:18 +00001095 /* check if we replaced the next timer */
1096 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001097 }
1098 return timer;
1099}
1100
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001101/* change the input key state for a given key */
1102static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1103{
1104 if (down)
1105 {
1106 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1107 input->keystate[key] |= 0x80;
1108 }
1109 else input->keystate[key] &= ~0x80;
1110}
1111
1112/* update the input key state for a keyboard message */
1113static void update_input_key_state( struct thread_input *input, const struct message *msg )
1114{
1115 unsigned char key;
Vitaliy Margolen9faf7e32008-04-01 23:13:45 -06001116 int down = 0;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001117
1118 switch (msg->msg)
1119 {
1120 case WM_LBUTTONDOWN:
1121 down = 1;
1122 /* fall through */
1123 case WM_LBUTTONUP:
1124 set_input_key_state( input, VK_LBUTTON, down );
1125 break;
1126 case WM_MBUTTONDOWN:
1127 down = 1;
1128 /* fall through */
1129 case WM_MBUTTONUP:
1130 set_input_key_state( input, VK_MBUTTON, down );
1131 break;
1132 case WM_RBUTTONDOWN:
1133 down = 1;
1134 /* fall through */
1135 case WM_RBUTTONUP:
1136 set_input_key_state( input, VK_RBUTTON, down );
1137 break;
Alexandre Julliardcb7aa872005-03-24 19:16:54 +00001138 case WM_XBUTTONDOWN:
1139 down = 1;
1140 /* fall through */
1141 case WM_XBUTTONUP:
1142 if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1143 else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1144 break;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001145 case WM_KEYDOWN:
1146 case WM_SYSKEYDOWN:
1147 down = 1;
1148 /* fall through */
1149 case WM_KEYUP:
1150 case WM_SYSKEYUP:
1151 key = (unsigned char)msg->wparam;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001152 set_input_key_state( input, key, down );
1153 switch(key)
1154 {
Thomas Kho0e814842006-04-12 11:13:46 -07001155 case VK_LCONTROL:
1156 case VK_RCONTROL:
Vitaliy Margolen9faf7e32008-04-01 23:13:45 -06001157 down = (input->keystate[VK_LCONTROL] | input->keystate[VK_RCONTROL]) & 0x80;
Thomas Kho0e814842006-04-12 11:13:46 -07001158 set_input_key_state( input, VK_CONTROL, down );
1159 break;
1160 case VK_LMENU:
1161 case VK_RMENU:
Vitaliy Margolen9faf7e32008-04-01 23:13:45 -06001162 down = (input->keystate[VK_LMENU] | input->keystate[VK_RMENU]) & 0x80;
Thomas Kho0e814842006-04-12 11:13:46 -07001163 set_input_key_state( input, VK_MENU, down );
1164 break;
1165 case VK_LSHIFT:
1166 case VK_RSHIFT:
Vitaliy Margolen9faf7e32008-04-01 23:13:45 -06001167 down = (input->keystate[VK_LSHIFT] | input->keystate[VK_RSHIFT]) & 0x80;
Thomas Kho0e814842006-04-12 11:13:46 -07001168 set_input_key_state( input, VK_SHIFT, down );
1169 break;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001170 }
1171 break;
1172 }
1173}
1174
Alexandre Julliard242e3952003-01-08 00:27:58 +00001175/* release the hardware message currently being processed by the given thread */
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001176static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1177 int remove, user_handle_t new_win )
Alexandre Julliard242e3952003-01-08 00:27:58 +00001178{
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001179 struct thread_input *input = queue->input;
1180 struct message *msg;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001181
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001182 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1183 {
1184 if (msg->unique_id == hw_id) break;
1185 }
1186 if (&msg->entry == &input->msg_list) return; /* not found */
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001187
1188 /* clear the queue bit for that message */
1189 if (remove || new_win)
Alexandre Julliard242e3952003-01-08 00:27:58 +00001190 {
1191 struct message *other;
1192 int clr_bit;
1193
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001194 clr_bit = get_hardware_msg_bit( msg );
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001195 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1196 {
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001197 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001198 {
1199 clr_bit = 0;
1200 break;
1201 }
1202 }
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001203 if (clr_bit) clear_queue_bits( queue, clr_bit );
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001204 }
1205
1206 if (new_win) /* set the new window */
1207 {
1208 struct thread *owner = get_window_thread( new_win );
1209 if (owner)
1210 {
Alexandre Julliard93938eb2007-11-08 13:53:26 +01001211 msg->win = new_win;
1212 if (owner->queue->input != input)
Alexandre Julliard90b48e92005-05-13 14:00:19 +00001213 {
Alexandre Julliard93938eb2007-11-08 13:53:26 +01001214 list_remove( &msg->entry );
1215 if (msg->msg == WM_MOUSEMOVE && merge_message( owner->queue->input, msg ))
1216 {
1217 free_message( msg );
1218 release_object( owner );
1219 return;
1220 }
1221 list_add_tail( &owner->queue->input->msg_list, &msg->entry );
Alexandre Julliard90b48e92005-05-13 14:00:19 +00001222 }
Alexandre Julliard93938eb2007-11-08 13:53:26 +01001223 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1224 remove = 0;
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001225 release_object( owner );
1226 }
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001227 }
Alexandre Julliard90b48e92005-05-13 14:00:19 +00001228 if (remove)
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001229 {
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001230 update_input_key_state( input, msg );
1231 list_remove( &msg->entry );
1232 free_message( msg );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001233 }
Alexandre Julliard242e3952003-01-08 00:27:58 +00001234}
1235
1236/* find the window that should receive a given hardware message */
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001237static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001238 struct hardware_msg_data *data, unsigned int *msg_code )
Alexandre Julliard242e3952003-01-08 00:27:58 +00001239{
1240 user_handle_t win = 0;
1241
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001242 *msg_code = msg->msg;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001243 if (is_keyboard_msg( msg ))
1244 {
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001245 if (input && !(win = input->focus))
1246 {
1247 win = input->active;
1248 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1249 }
Alexandre Julliard242e3952003-01-08 00:27:58 +00001250 }
1251 else /* mouse message */
1252 {
1253 if (!input || !(win = input->capture))
1254 {
Dmitry Timoshkov93a4e1e2004-10-27 21:55:00 +00001255 if (!(win = msg->win) || !is_window_visible( win ))
Alexandre Julliard3f31a102005-08-16 19:58:12 +00001256 {
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001257 if (input) win = window_from_point( input->desktop, data->x, data->y );
Alexandre Julliard3f31a102005-08-16 19:58:12 +00001258 }
Alexandre Julliard242e3952003-01-08 00:27:58 +00001259 }
1260 }
1261 return win;
1262}
1263
1264/* queue a hardware message into a given thread input */
Alexandre Julliardcd28e2b2010-04-20 20:51:45 +02001265static void queue_hardware_message( struct thread_input *input, struct message *msg,
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001266 struct hardware_msg_data *data )
Alexandre Julliard242e3952003-01-08 00:27:58 +00001267{
1268 user_handle_t win;
Alexandre Julliarde3fe6892005-03-17 20:51:53 +00001269 struct thread *thread;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001270 unsigned int msg_code;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001271
Mike McCormackabe70f72005-04-28 12:04:14 +00001272 last_input_time = get_tick_count();
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001273 win = find_hardware_message_window( input, msg, data, &msg_code );
Alexandre Julliarde3fe6892005-03-17 20:51:53 +00001274 if (!win || !(thread = get_window_thread(win)))
Alexandre Julliard242e3952003-01-08 00:27:58 +00001275 {
Alexandre Julliard5d282dc2006-02-14 10:43:15 +01001276 if (input) update_input_key_state( input, msg );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001277 free( msg );
1278 return;
1279 }
Alexandre Julliarde3fe6892005-03-17 20:51:53 +00001280 input = thread->queue->input;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001281
1282 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1283 else
1284 {
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001285 msg->unique_id = 0; /* will be set once we return it to the app */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001286 list_add_tail( &input->msg_list, &msg->entry );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001287 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1288 }
Alexandre Julliarde3fe6892005-03-17 20:51:53 +00001289 release_object( thread );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001290}
1291
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001292/* check message filter for a hardware message */
1293static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1294 user_handle_t filter_win, unsigned int first, unsigned int last )
1295{
1296 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1297 {
1298 /* we can only test the window for a keyboard message since the
1299 * dest window for a mouse message depends on hittest */
1300 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1301 return 0;
1302 /* the message code is final for a keyboard message, we can simply check it */
1303 return check_msg_filter( msg_code, first, last );
1304 }
1305 else /* mouse message */
1306 {
1307 /* we need to check all possible values that the message can have in the end */
1308
1309 if (check_msg_filter( msg_code, first, last )) return 1;
1310 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1311
1312 /* all other messages can become non-client messages */
1313 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1314
1315 /* clicks can become double-clicks or non-client double-clicks */
1316 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1317 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1318 {
1319 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1320 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1321 }
1322 return 0;
1323 }
1324}
1325
1326
Alexandre Julliard242e3952003-01-08 00:27:58 +00001327/* find a hardware message for the given queue */
Michael Stefaniuca6249772006-07-25 10:30:04 +02001328static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001329 unsigned int first, unsigned int last, struct get_message_reply *reply )
Alexandre Julliard242e3952003-01-08 00:27:58 +00001330{
1331 struct thread_input *input = thread->queue->input;
1332 struct thread *win_thread;
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001333 struct list *ptr;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001334 user_handle_t win;
1335 int clear_bits, got_one = 0;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001336 unsigned int msg_code;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001337
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001338 ptr = list_head( &input->msg_list );
1339 if (hw_id)
1340 {
1341 while (ptr)
1342 {
1343 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1344 if (msg->unique_id == hw_id) break;
1345 ptr = list_next( &input->msg_list, ptr );
1346 }
1347 if (!ptr) ptr = list_head( &input->msg_list );
1348 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1349 }
Alexandre Julliard242e3952003-01-08 00:27:58 +00001350
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001351 if (ptr == list_head( &input->msg_list ))
Alexandre Julliard242e3952003-01-08 00:27:58 +00001352 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001353 else
Alexandre Julliard242e3952003-01-08 00:27:58 +00001354 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
Alexandre Julliard242e3952003-01-08 00:27:58 +00001355
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001356 while (ptr)
Alexandre Julliard242e3952003-01-08 00:27:58 +00001357 {
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001358 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001359 struct hardware_msg_data *data = msg->data;
1360
Alexandre Julliard55d449e2005-05-14 11:08:05 +00001361 ptr = list_next( &input->msg_list, ptr );
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001362 win = find_hardware_message_window( input, msg, data, &msg_code );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001363 if (!win || !(win_thread = get_window_thread( win )))
1364 {
1365 /* no window at all, remove it */
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001366 update_input_key_state( input, msg );
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001367 list_remove( &msg->entry );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001368 free_message( msg );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001369 continue;
1370 }
1371 if (win_thread != thread)
1372 {
Alexandre Julliard55d449e2005-05-14 11:08:05 +00001373 if (win_thread->queue->input == input)
1374 {
1375 /* wake the other thread */
1376 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1377 got_one = 1;
1378 }
1379 else
1380 {
1381 /* for another thread input, drop it */
1382 update_input_key_state( input, msg );
1383 list_remove( &msg->entry );
1384 free_message( msg );
1385 }
Alexandre Julliard242e3952003-01-08 00:27:58 +00001386 release_object( win_thread );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001387 continue;
1388 }
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001389 release_object( win_thread );
1390
Alexandre Julliard242e3952003-01-08 00:27:58 +00001391 /* if we already got a message for another thread, or if it doesn't
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001392 * match the filter we skip it */
1393 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
Alexandre Julliard242e3952003-01-08 00:27:58 +00001394 {
1395 clear_bits &= ~get_hardware_msg_bit( msg );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001396 continue;
1397 }
1398 /* now we can return it */
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001399 if (!msg->unique_id) msg->unique_id = get_unique_id();
Alexandre Julliard242e3952003-01-08 00:27:58 +00001400 reply->type = MSG_HARDWARE;
1401 reply->win = win;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001402 reply->msg = msg_code;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001403 reply->wparam = msg->wparam;
1404 reply->lparam = msg->lparam;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001405 reply->time = msg->time;
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001406
1407 data->hw_id = msg->unique_id;
1408 set_reply_data( msg->data, msg->data_size );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001409 return 1;
1410 }
1411 /* nothing found, clear the hardware queue bits */
1412 clear_queue_bits( thread->queue, clear_bits );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001413 return 0;
1414}
Alexandre Julliard805bdc52001-11-13 22:23:48 +00001415
1416/* increment (or decrement if 'incr' is negative) the queue paint count */
1417void inc_queue_paint_count( struct thread *thread, int incr )
1418{
1419 struct msg_queue *queue = thread->queue;
1420
1421 assert( queue );
1422
1423 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1424
1425 if (queue->paint_count)
1426 set_queue_bits( queue, QS_PAINT );
1427 else
1428 clear_queue_bits( queue, QS_PAINT );
1429}
1430
1431
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001432/* remove all messages and timers belonging to a certain window */
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001433void queue_cleanup_window( struct thread *thread, user_handle_t win )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001434{
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001435 struct msg_queue *queue = thread->queue;
Alexandre Julliardff986a52004-11-29 18:08:18 +00001436 struct list *ptr;
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001437 int i;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001438
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001439 if (!queue) return;
1440
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001441 /* remove timers */
Alexandre Julliardff986a52004-11-29 18:08:18 +00001442
1443 ptr = list_head( &queue->pending_timers );
1444 while (ptr)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001445 {
Alexandre Julliardff986a52004-11-29 18:08:18 +00001446 struct list *next = list_next( &queue->pending_timers, ptr );
1447 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1448 if (timer->win == win) free_timer( queue, timer );
1449 ptr = next;
1450 }
1451 ptr = list_head( &queue->expired_timers );
1452 while (ptr)
1453 {
1454 struct list *next = list_next( &queue->expired_timers, ptr );
1455 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1456 if (timer->win == win) free_timer( queue, timer );
1457 ptr = next;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001458 }
1459
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001460 /* remove messages */
1461 for (i = 0; i < NB_MSG_KINDS; i++)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001462 {
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001463 struct list *ptr, *next;
1464
1465 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001466 {
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001467 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001468 if (msg->win == win) remove_queue_message( queue, msg, i );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001469 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001470 }
Alexandre Julliardab5063b2002-10-11 18:50:15 +00001471
1472 thread_input_cleanup_window( queue, win );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001473}
1474
Alexandre Julliard81f2a732002-03-23 20:43:52 +00001475/* post a message to a window; used by socket handling */
Alexandre Julliard31282b32008-12-10 16:01:50 +01001476void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
Alexandre Julliard81f2a732002-03-23 20:43:52 +00001477{
1478 struct message *msg;
1479 struct thread *thread = get_window_thread( win );
1480
1481 if (!thread) return;
1482
1483 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1484 {
1485 msg->type = MSG_POSTED;
1486 msg->win = get_user_full_handle( win );
1487 msg->msg = message;
1488 msg->wparam = wparam;
1489 msg->lparam = lparam;
1490 msg->time = get_tick_count();
Alexandre Julliard81f2a732002-03-23 20:43:52 +00001491 msg->result = NULL;
1492 msg->data = NULL;
1493 msg->data_size = 0;
1494
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001495 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
Alexandre Julliard80b997a2005-11-14 15:17:09 +00001496 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
Alexandre Julliard81f2a732002-03-23 20:43:52 +00001497 }
1498 release_object( thread );
1499}
1500
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001501/* post a win event */
1502void post_win_event( struct thread *thread, unsigned int event,
1503 user_handle_t win, unsigned int object_id,
Alexandre Julliardcc55fd32008-12-29 17:35:35 +01001504 unsigned int child_id, client_ptr_t hook_proc,
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001505 const WCHAR *module, data_size_t module_size,
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001506 user_handle_t hook)
1507{
1508 struct message *msg;
1509
1510 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1511 {
Alexandre Julliard59dc4562006-10-04 16:04:53 +02001512 struct winevent_msg_data *data;
1513
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001514 msg->type = MSG_WINEVENT;
1515 msg->win = get_user_full_handle( win );
1516 msg->msg = event;
1517 msg->wparam = object_id;
1518 msg->lparam = child_id;
1519 msg->time = get_tick_count();
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001520 msg->result = NULL;
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001521
Alexandre Julliard59dc4562006-10-04 16:04:53 +02001522 if ((data = malloc( sizeof(*data) + module_size )))
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001523 {
Alexandre Julliard59dc4562006-10-04 16:04:53 +02001524 data->hook = hook;
1525 data->tid = get_thread_id( current );
1526 data->hook_proc = hook_proc;
1527 memcpy( data + 1, module, module_size );
1528
1529 msg->data = data;
1530 msg->data_size = sizeof(*data) + module_size;
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001531
1532 if (debug_level > 1)
Alexandre Julliardd7641072008-12-08 16:57:38 +01001533 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 +00001534 get_thread_id(thread), event, win, object_id, child_id );
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001535 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001536 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1537 }
1538 else
1539 free( msg );
1540 }
1541}
Alexandre Julliard81f2a732002-03-23 20:43:52 +00001542
Dmitry Timoshkove735e192007-12-28 10:59:13 +08001543
1544/* check if the thread owning the window is hung */
1545DECL_HANDLER(is_window_hung)
1546{
1547 struct thread *thread;
1548
1549 thread = get_window_thread( req->win );
1550 if (thread)
1551 {
1552 reply->is_hung = is_queue_hung( thread->queue );
1553 release_object( thread );
1554 }
1555 else reply->is_hung = 0;
1556}
1557
1558
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001559/* get the message queue of the current thread */
1560DECL_HANDLER(get_msg_queue)
1561{
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001562 struct msg_queue *queue = get_current_queue();
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001563
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001564 reply->handle = 0;
1565 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001566}
1567
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001568
Alexandre Julliard0cb29f42007-04-04 18:02:01 +02001569/* set the file descriptor associated to the current thread queue */
1570DECL_HANDLER(set_queue_fd)
1571{
1572 struct msg_queue *queue = get_current_queue();
1573 struct file *file;
1574 int unix_fd;
1575
1576 if (queue->fd) /* fd can only be set once */
1577 {
1578 set_error( STATUS_ACCESS_DENIED );
1579 return;
1580 }
1581 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
1582
1583 if ((unix_fd = get_file_unix_fd( file )) != -1)
1584 {
1585 if ((unix_fd = dup( unix_fd )) != -1)
Alexandre Julliardf85437c2007-04-10 22:25:07 +02001586 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
Alexandre Julliard0cb29f42007-04-04 18:02:01 +02001587 else
1588 file_set_error();
1589 }
1590 release_object( file );
1591}
1592
1593
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001594/* set the current message queue wakeup mask */
1595DECL_HANDLER(set_queue_mask)
1596{
1597 struct msg_queue *queue = get_current_queue();
1598
1599 if (queue)
1600 {
1601 queue->wake_mask = req->wake_mask;
1602 queue->changed_mask = req->changed_mask;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001603 reply->wake_bits = queue->wake_bits;
1604 reply->changed_bits = queue->changed_bits;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001605 if (is_signaled( queue ))
1606 {
1607 /* if skip wait is set, do what would have been done in the subsequent wait */
1608 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1609 else wake_up( &queue->obj, 0 );
1610 }
1611 }
1612}
1613
1614
1615/* get the current message queue status */
1616DECL_HANDLER(get_queue_status)
1617{
1618 struct msg_queue *queue = current->queue;
1619 if (queue)
1620 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001621 reply->wake_bits = queue->wake_bits;
1622 reply->changed_bits = queue->changed_bits;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001623 if (req->clear) queue->changed_bits = 0;
1624 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001625 else reply->wake_bits = reply->changed_bits = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001626}
1627
1628
1629/* send a message to a thread queue */
1630DECL_HANDLER(send_message)
1631{
1632 struct message *msg;
1633 struct msg_queue *send_queue = get_current_queue();
Alexandre Julliard242e3952003-01-08 00:27:58 +00001634 struct msg_queue *recv_queue = NULL;
1635 struct thread *thread = NULL;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001636
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001637 if (!(thread = get_thread_from_id( req->id ))) return;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001638
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001639 if (!(recv_queue = thread->queue))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001640 {
1641 set_error( STATUS_INVALID_PARAMETER );
1642 release_object( thread );
1643 return;
1644 }
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001645 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
Alexandre Julliard09029b22003-07-11 04:09:42 +00001646 {
1647 set_error( STATUS_TIMEOUT );
1648 release_object( thread );
1649 return;
1650 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001651
1652 if ((msg = mem_alloc( sizeof(*msg) )))
1653 {
Alexandre Julliardd253c582001-08-07 19:19:08 +00001654 msg->type = req->type;
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001655 msg->win = get_user_full_handle( req->win );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001656 msg->msg = req->msg;
1657 msg->wparam = req->wparam;
1658 msg->lparam = req->lparam;
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001659 msg->time = get_tick_count();
Alexandre Julliardd253c582001-08-07 19:19:08 +00001660 msg->result = NULL;
1661 msg->data = NULL;
Alexandre Julliard3ad97982006-10-04 20:25:42 +02001662 msg->data_size = get_req_data_size();
1663
1664 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1665 {
1666 free( msg );
1667 release_object( thread );
1668 return;
1669 }
Alexandre Julliardd253c582001-08-07 19:19:08 +00001670
1671 switch(msg->type)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001672 {
Eric Pouech0faceb02002-01-18 19:22:55 +00001673 case MSG_OTHER_PROCESS:
Alexandre Julliardd253c582001-08-07 19:19:08 +00001674 case MSG_ASCII:
1675 case MSG_UNICODE:
1676 case MSG_CALLBACK:
Alexandre Julliard3ad97982006-10-04 20:25:42 +02001677 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
Alexandre Julliarde630aa02001-07-11 17:29:01 +00001678 {
Alexandre Julliard039e1312003-07-26 20:36:43 +00001679 free_message( msg );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001680 break;
Alexandre Julliarde630aa02001-07-11 17:29:01 +00001681 }
Alexandre Julliardd253c582001-08-07 19:19:08 +00001682 /* fall through */
1683 case MSG_NOTIFY:
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001684 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001685 set_queue_bits( recv_queue, QS_SENDMESSAGE );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001686 break;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001687 case MSG_POSTED:
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001688 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
Alexandre Julliard80b997a2005-11-14 15:17:09 +00001689 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001690 break;
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001691 case MSG_HARDWARE: /* should use send_hardware_message instead */
Alexandre Julliard039e1312003-07-26 20:36:43 +00001692 case MSG_CALLBACK_RESULT: /* cannot send this one */
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001693 default:
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001694 set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001695 free( msg );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001696 break;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001697 }
1698 }
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001699 release_object( thread );
1700}
1701
1702/* send a hardware message to a thread queue */
1703DECL_HANDLER(send_hardware_message)
1704{
1705 struct message *msg;
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001706 struct thread *thread = NULL;
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001707 struct hardware_msg_data *data;
Alexandre Julliardcd28e2b2010-04-20 20:51:45 +02001708 struct thread_input *input = foreground_input;
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001709
1710 if (req->id)
1711 {
1712 if (!(thread = get_thread_from_id( req->id ))) return;
Alexandre Julliardcd28e2b2010-04-20 20:51:45 +02001713 if (!thread->queue)
1714 {
1715 set_error( STATUS_INVALID_PARAMETER );
1716 release_object( thread );
1717 return;
1718 }
1719 input = thread->queue->input;
Alexandre Julliard5ee89aa2010-05-12 13:48:00 +02001720 reply->cursor = input->cursor;
1721 reply->count = input->cursor_count;
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001722 }
1723
Alexandre Julliard5ee89aa2010-05-12 13:48:00 +02001724 if (!req->msg || !(data = mem_alloc( sizeof(*data) )))
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001725 {
Marcus Meissner3b86f462009-01-28 23:58:38 +01001726 if (thread) release_object( thread );
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001727 return;
1728 }
Alexandre Julliard8c8c6652008-12-31 15:14:29 +01001729 memset( data, 0, sizeof(*data) );
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001730 data->x = req->x;
1731 data->y = req->y;
1732 data->info = req->info;
1733
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001734 if ((msg = mem_alloc( sizeof(*msg) )))
1735 {
1736 msg->type = MSG_HARDWARE;
1737 msg->win = get_user_full_handle( req->win );
1738 msg->msg = req->msg;
1739 msg->wparam = req->wparam;
1740 msg->lparam = req->lparam;
1741 msg->time = req->time;
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001742 msg->result = NULL;
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001743 msg->data = data;
1744 msg->data_size = sizeof(*data);
Alexandre Julliardcd28e2b2010-04-20 20:51:45 +02001745 queue_hardware_message( input, msg, data );
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001746 }
Alexandre Julliardd1d7b9f2008-12-24 11:59:09 +01001747 else free( data );
1748
Alexandre Julliard5ee89aa2010-05-12 13:48:00 +02001749 if (thread) release_object( thread );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001750}
1751
Robert Shearmana40ce392006-01-17 13:14:31 +01001752/* post a quit message to the current queue */
1753DECL_HANDLER(post_quit_message)
1754{
1755 struct msg_queue *queue = get_current_queue();
1756
1757 if (!queue)
1758 return;
1759
1760 queue->quit_message = 1;
1761 queue->exit_code = req->exit_code;
1762 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1763}
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001764
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001765/* get a message from the current queue */
1766DECL_HANDLER(get_message)
1767{
1768 struct timer *timer;
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001769 struct list *ptr;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001770 struct msg_queue *queue = get_current_queue();
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001771 user_handle_t get_win = get_user_full_handle( req->get_win );
Alexandre Julliard89faee02007-02-21 15:21:05 +01001772 unsigned int filter = req->flags >> 16;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001773
Alexandre Julliard63342352005-05-11 13:03:15 +00001774 reply->active_hooks = get_active_hooks();
1775
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001776 if (!queue) return;
Alexandre Julliard753c8702006-08-10 16:42:09 +02001777 queue->last_get_msg = current_time;
Alexandre Julliard89faee02007-02-21 15:21:05 +01001778 if (!filter) filter = QS_ALLINPUT;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001779
1780 /* first check for sent messages */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001781 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001782 {
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001783 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001784 receive_message( queue, msg, reply );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001785 return;
1786 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001787
Alexandre Julliard9f55ae62001-06-28 04:37:22 +00001788 /* clear changed bits so we can wait on them if we don't find a message */
Alexandre Julliard89faee02007-02-21 15:21:05 +01001789 if (filter & QS_POSTMESSAGE)
1790 {
1791 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
1792 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
1793 }
1794 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
1795 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
Alexandre Julliard9f55ae62001-06-28 04:37:22 +00001796
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001797 /* then check for posted messages */
Alexandre Julliard89faee02007-02-21 15:21:05 +01001798 if ((filter & QS_POSTMESSAGE) &&
1799 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001800 return;
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001801
Robert Shearmana40ce392006-01-17 13:14:31 +01001802 /* only check for quit messages if not posted messages pending.
1803 * note: the quit message isn't filtered */
1804 if (get_quit_message( queue, req->flags, reply ))
1805 return;
1806
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001807 /* then check for any raw hardware message */
Alexandre Julliard89faee02007-02-21 15:21:05 +01001808 if ((filter & QS_INPUT) &&
1809 filter_contains_hw_range( req->get_first, req->get_last ) &&
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001810 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001811 return;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001812
1813 /* now check for WM_PAINT */
Alexandre Julliard89faee02007-02-21 15:21:05 +01001814 if ((filter & QS_PAINT) &&
1815 queue->paint_count &&
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001816 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001817 (reply->win = find_window_to_repaint( get_win, current )))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001818 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001819 reply->type = MSG_POSTED;
1820 reply->msg = WM_PAINT;
1821 reply->wparam = 0;
1822 reply->lparam = 0;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001823 reply->time = get_tick_count();
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001824 return;
1825 }
1826
1827 /* now check for timer */
Alexandre Julliard89faee02007-02-21 15:21:05 +01001828 if ((filter & QS_TIMER) &&
1829 (timer = find_expired_timer( queue, get_win, req->get_first,
1830 req->get_last, (req->flags & PM_REMOVE) )))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001831 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001832 reply->type = MSG_POSTED;
1833 reply->win = timer->win;
1834 reply->msg = timer->msg;
1835 reply->wparam = timer->id;
1836 reply->lparam = timer->lparam;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001837 reply->time = get_tick_count();
Alexandre Julliard7265e892009-12-16 18:18:07 +01001838 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
1839 set_event( current->process->idle_event );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001840 return;
1841 }
1842
Alexandre Julliardb0760fc2010-03-03 13:19:13 +01001843 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
Alexandre Julliard28965402007-08-29 18:13:13 +02001844 queue->wake_mask = req->wake_mask;
1845 queue->changed_mask = req->changed_mask;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001846 set_error( STATUS_PENDING ); /* FIXME */
1847}
1848
1849
1850/* reply to a sent message */
1851DECL_HANDLER(reply_message)
1852{
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001853 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
Alexandre Julliardb1095da2003-03-19 00:12:17 +00001854 else if (current->queue->recv_result)
1855 reply_message( current->queue, req->result, 0, req->remove,
1856 get_req_data(), get_req_data_size() );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001857}
1858
1859
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001860/* accept the current hardware message */
1861DECL_HANDLER(accept_hardware_message)
1862{
1863 if (current->queue)
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001864 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1865 else
1866 set_error( STATUS_ACCESS_DENIED );
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001867}
1868
1869
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001870/* retrieve the reply for the last message sent */
1871DECL_HANDLER(get_message_reply)
1872{
Alexandre Julliard039e1312003-07-26 20:36:43 +00001873 struct message_result *result;
1874 struct list *entry;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001875 struct msg_queue *queue = current->queue;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001876
Alexandre Julliardd253c582001-08-07 19:19:08 +00001877 if (queue)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001878 {
Alexandre Julliardd253c582001-08-07 19:19:08 +00001879 set_error( STATUS_PENDING );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001880 reply->result = 0;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001881
Alexandre Julliard039e1312003-07-26 20:36:43 +00001882 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1883
1884 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1885 if (result->replied || req->cancel)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001886 {
Alexandre Julliardd253c582001-08-07 19:19:08 +00001887 if (result->replied)
1888 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001889 reply->result = result->result;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001890 set_error( result->error );
1891 if (result->data)
1892 {
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001893 data_size_t data_len = min( result->data_size, get_reply_max_size() );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001894 set_reply_data_ptr( result->data, data_len );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001895 result->data = NULL;
1896 result->data_size = 0;
1897 }
1898 }
Alexandre Julliard039e1312003-07-26 20:36:43 +00001899 remove_result_from_sender( result );
1900
1901 entry = list_head( &queue->send_result );
1902 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1903 else
1904 {
1905 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1906 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1907 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001908 }
1909 }
Alexandre Julliardd253c582001-08-07 19:19:08 +00001910 else set_error( STATUS_ACCESS_DENIED );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001911}
1912
1913
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001914/* set a window timer */
1915DECL_HANDLER(set_win_timer)
1916{
1917 struct timer *timer;
Alexandre Julliardff986a52004-11-29 18:08:18 +00001918 struct msg_queue *queue;
1919 struct thread *thread = NULL;
1920 user_handle_t win = 0;
Alexandre Julliard31282b32008-12-10 16:01:50 +01001921 lparam_t id = req->id;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001922
Alexandre Julliardff986a52004-11-29 18:08:18 +00001923 if (req->win)
1924 {
1925 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1926 {
1927 set_error( STATUS_INVALID_HANDLE );
1928 return;
1929 }
1930 if (thread->process != current->process)
1931 {
1932 release_object( thread );
1933 set_error( STATUS_ACCESS_DENIED );
1934 return;
1935 }
1936 queue = thread->queue;
1937 /* remove it if it existed already */
1938 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1939 }
1940 else
1941 {
1942 queue = get_current_queue();
Trent Waddingtonfc635fa2007-08-30 19:05:50 +10001943 /* look for a timer with this id */
Alexandre Julliardd7641072008-12-08 16:57:38 +01001944 if (id && (timer = find_timer( queue, 0, req->msg, id )))
Alexandre Julliardff986a52004-11-29 18:08:18 +00001945 {
Trent Waddingtonfc635fa2007-08-30 19:05:50 +10001946 /* free and reuse id */
1947 free_timer( queue, timer );
Alexandre Julliardff986a52004-11-29 18:08:18 +00001948 }
Trent Waddingtonfc635fa2007-08-30 19:05:50 +10001949 else
1950 {
1951 /* find a free id for it */
1952 do
1953 {
1954 id = queue->next_timer_id;
Dmitry Timoshkovb4227d72007-11-13 20:41:01 +08001955 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
Trent Waddingtonfc635fa2007-08-30 19:05:50 +10001956 }
1957 while (find_timer( queue, 0, req->msg, id ));
1958 }
Alexandre Julliardff986a52004-11-29 18:08:18 +00001959 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001960
1961 if ((timer = set_timer( queue, req->rate )))
1962 {
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001963 timer->win = win;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001964 timer->msg = req->msg;
Alexandre Julliardff986a52004-11-29 18:08:18 +00001965 timer->id = id;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001966 timer->lparam = req->lparam;
Alexandre Julliardff986a52004-11-29 18:08:18 +00001967 reply->id = id;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001968 }
Alexandre Julliardff986a52004-11-29 18:08:18 +00001969 if (thread) release_object( thread );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001970}
1971
1972/* kill a window timer */
1973DECL_HANDLER(kill_win_timer)
1974{
Alexandre Julliardff986a52004-11-29 18:08:18 +00001975 struct timer *timer;
1976 struct thread *thread;
1977 user_handle_t win = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001978
Alexandre Julliardff986a52004-11-29 18:08:18 +00001979 if (req->win)
1980 {
1981 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1982 {
1983 set_error( STATUS_INVALID_HANDLE );
1984 return;
1985 }
1986 if (thread->process != current->process)
1987 {
1988 release_object( thread );
1989 set_error( STATUS_ACCESS_DENIED );
1990 return;
1991 }
1992 }
1993 else thread = (struct thread *)grab_object( current );
1994
1995 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1996 free_timer( thread->queue, timer );
1997 else
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001998 set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliardff986a52004-11-29 18:08:18 +00001999
2000 release_object( thread );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00002001}
Alexandre Julliardab5063b2002-10-11 18:50:15 +00002002
2003
2004/* attach (or detach) thread inputs */
2005DECL_HANDLER(attach_thread_input)
2006{
2007 struct thread *thread_from = get_thread_from_id( req->tid_from );
2008 struct thread *thread_to = get_thread_from_id( req->tid_to );
2009
2010 if (!thread_from || !thread_to)
2011 {
2012 if (thread_from) release_object( thread_from );
2013 if (thread_to) release_object( thread_to );
2014 return;
2015 }
2016 if (thread_from != thread_to)
2017 {
2018 if (req->attach) attach_thread_input( thread_from, thread_to );
Alexandre Julliard5ad90c02005-07-11 13:30:23 +00002019 else
2020 {
2021 if (thread_from->queue && thread_to->queue &&
2022 thread_from->queue->input == thread_to->queue->input)
2023 detach_thread_input( thread_from );
2024 else
2025 set_error( STATUS_ACCESS_DENIED );
2026 }
Alexandre Julliardab5063b2002-10-11 18:50:15 +00002027 }
2028 else set_error( STATUS_ACCESS_DENIED );
2029 release_object( thread_from );
2030 release_object( thread_to );
2031}
2032
2033
2034/* get thread input data */
2035DECL_HANDLER(get_thread_input)
2036{
2037 struct thread *thread = NULL;
2038 struct thread_input *input;
2039
2040 if (req->tid)
2041 {
2042 if (!(thread = get_thread_from_id( req->tid ))) return;
2043 input = thread->queue ? thread->queue->input : NULL;
2044 }
2045 else input = foreground_input; /* get the foreground thread info */
2046
2047 if (input)
2048 {
2049 reply->focus = input->focus;
2050 reply->capture = input->capture;
2051 reply->active = input->active;
2052 reply->menu_owner = input->menu_owner;
2053 reply->move_size = input->move_size;
2054 reply->caret = input->caret;
Alexandre Julliardc5459822010-05-11 11:21:03 +02002055 reply->cursor = input->cursor;
2056 reply->show_count = input->cursor_count;
Alexandre Julliard11e35232002-10-17 01:24:33 +00002057 reply->rect = input->caret_rect;
Alexandre Julliardab5063b2002-10-11 18:50:15 +00002058 }
Alexandre Julliardc5459822010-05-11 11:21:03 +02002059
Alexandre Julliardab5063b2002-10-11 18:50:15 +00002060 /* foreground window is active window of foreground thread */
2061 reply->foreground = foreground_input ? foreground_input->active : 0;
2062 if (thread) release_object( thread );
2063}
Alexandre Julliard5030bda2002-10-11 23:41:06 +00002064
2065
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00002066/* retrieve queue keyboard state for a given thread */
2067DECL_HANDLER(get_key_state)
2068{
2069 struct thread *thread;
2070 struct thread_input *input;
2071
2072 if (!(thread = get_thread_from_id( req->tid ))) return;
2073 input = thread->queue ? thread->queue->input : NULL;
2074 if (input)
2075 {
2076 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
2077 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
2078 }
2079 release_object( thread );
2080}
2081
2082
2083/* set queue keyboard state for a given thread */
2084DECL_HANDLER(set_key_state)
2085{
2086 struct thread *thread = NULL;
2087 struct thread_input *input;
2088
2089 if (!(thread = get_thread_from_id( req->tid ))) return;
2090 input = thread->queue ? thread->queue->input : NULL;
2091 if (input)
2092 {
Alexandre Julliard0f273c12006-07-26 10:43:25 +02002093 data_size_t size = min( sizeof(input->keystate), get_req_data_size() );
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00002094 if (size) memcpy( input->keystate, get_req_data(), size );
2095 }
2096 release_object( thread );
2097}
2098
2099
Alexandre Julliard5030bda2002-10-11 23:41:06 +00002100/* set the system foreground window */
2101DECL_HANDLER(set_foreground_window)
2102{
Dmitry Timoshkov19e7fab2006-07-07 23:01:51 +09002103 struct thread *thread;
Alexandre Julliard5030bda2002-10-11 23:41:06 +00002104 struct msg_queue *queue = get_current_queue();
2105
2106 reply->previous = foreground_input ? foreground_input->active : 0;
2107 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
2108 reply->send_msg_new = FALSE;
2109
Dmitry Timoshkov19e7fab2006-07-07 23:01:51 +09002110 if (is_top_level_window( req->handle ) &&
2111 ((thread = get_window_thread( req->handle ))))
Alexandre Julliard5030bda2002-10-11 23:41:06 +00002112 {
Dmitry Timoshkov19e7fab2006-07-07 23:01:51 +09002113 foreground_input = thread->queue->input;
2114 reply->send_msg_new = (foreground_input != queue->input);
2115 release_object( thread );
Alexandre Julliard5030bda2002-10-11 23:41:06 +00002116 }
Dmitry Timoshkov19e7fab2006-07-07 23:01:51 +09002117 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
Alexandre Julliard5030bda2002-10-11 23:41:06 +00002118}
2119
2120
2121/* set the current thread focus window */
2122DECL_HANDLER(set_focus_window)
2123{
2124 struct msg_queue *queue = get_current_queue();
2125
2126 reply->previous = 0;
2127 if (queue && check_queue_input_window( queue, req->handle ))
2128 {
2129 reply->previous = queue->input->focus;
2130 queue->input->focus = get_user_full_handle( req->handle );
2131 }
2132}
2133
2134
2135/* set the current thread active window */
2136DECL_HANDLER(set_active_window)
2137{
2138 struct msg_queue *queue = get_current_queue();
2139
2140 reply->previous = 0;
2141 if (queue && check_queue_input_window( queue, req->handle ))
2142 {
2143 if (!req->handle || make_window_active( req->handle ))
2144 {
2145 reply->previous = queue->input->active;
2146 queue->input->active = get_user_full_handle( req->handle );
2147 }
2148 else set_error( STATUS_INVALID_HANDLE );
2149 }
2150}
Alexandre Julliarda9e8f592002-10-12 01:24:37 +00002151
2152
2153/* set the current thread capture window */
2154DECL_HANDLER(set_capture_window)
2155{
2156 struct msg_queue *queue = get_current_queue();
2157
2158 reply->previous = reply->full_handle = 0;
2159 if (queue && check_queue_input_window( queue, req->handle ))
2160 {
2161 struct thread_input *input = queue->input;
2162
Peter Dons Tychsend21c1312010-01-11 21:47:43 +01002163 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2164 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2165 {
2166 set_error(STATUS_ACCESS_DENIED);
2167 return;
2168 }
Alexandre Julliarda9e8f592002-10-12 01:24:37 +00002169 reply->previous = input->capture;
2170 input->capture = get_user_full_handle( req->handle );
2171 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2172 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2173 reply->full_handle = input->capture;
2174 }
2175}
Alexandre Julliard11e35232002-10-17 01:24:33 +00002176
2177
2178/* Set the current thread caret window */
2179DECL_HANDLER(set_caret_window)
2180{
2181 struct msg_queue *queue = get_current_queue();
2182
2183 reply->previous = 0;
2184 if (queue && check_queue_input_window( queue, req->handle ))
2185 {
2186 struct thread_input *input = queue->input;
2187
2188 reply->previous = input->caret;
2189 reply->old_rect = input->caret_rect;
2190 reply->old_hide = input->caret_hide;
2191 reply->old_state = input->caret_state;
2192
2193 set_caret_window( input, get_user_full_handle(req->handle) );
Krzysztof Foltmanb8501722005-02-18 20:02:55 +00002194 input->caret_rect.right = input->caret_rect.left + req->width;
2195 input->caret_rect.bottom = input->caret_rect.top + req->height;
Alexandre Julliard11e35232002-10-17 01:24:33 +00002196 }
2197}
2198
2199
2200/* Set the current thread caret information */
2201DECL_HANDLER(set_caret_info)
2202{
2203 struct msg_queue *queue = get_current_queue();
2204 struct thread_input *input;
2205
2206 if (!queue) return;
2207 input = queue->input;
2208 reply->full_handle = input->caret;
2209 reply->old_rect = input->caret_rect;
2210 reply->old_hide = input->caret_hide;
2211 reply->old_state = input->caret_state;
2212
2213 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2214 {
2215 set_error( STATUS_ACCESS_DENIED );
2216 return;
2217 }
2218 if (req->flags & SET_CARET_POS)
2219 {
2220 input->caret_rect.right += req->x - input->caret_rect.left;
2221 input->caret_rect.bottom += req->y - input->caret_rect.top;
2222 input->caret_rect.left = req->x;
2223 input->caret_rect.top = req->y;
2224 }
2225 if (req->flags & SET_CARET_HIDE)
2226 {
2227 input->caret_hide += req->hide;
2228 if (input->caret_hide < 0) input->caret_hide = 0;
2229 }
2230 if (req->flags & SET_CARET_STATE)
2231 {
2232 if (req->state == -1) input->caret_state = !input->caret_state;
2233 else input->caret_state = !!req->state;
2234 }
2235}
Mike McCormackabe70f72005-04-28 12:04:14 +00002236
2237
2238/* get the time of the last input event */
2239DECL_HANDLER(get_last_input_time)
2240{
2241 reply->time = last_input_time;
2242}
Alexandre Julliard8159d4e2010-03-22 17:52:23 +01002243
2244/* set/get the current cursor */
2245DECL_HANDLER(set_cursor)
2246{
2247 struct msg_queue *queue = get_current_queue();
2248 struct thread_input *input;
2249
2250 if (!queue) return;
2251 input = queue->input;
2252
2253 reply->prev_handle = input->cursor;
2254 reply->prev_count = input->cursor_count;
2255
2256 if (req->flags & SET_CURSOR_HANDLE)
2257 {
2258 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
2259 {
2260 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
2261 return;
2262 }
2263 input->cursor = req->handle;
2264 }
2265
2266 if (req->flags & SET_CURSOR_COUNT)
2267 {
2268 queue->cursor_count += req->show_count;
2269 input->cursor_count += req->show_count;
2270 }
2271}