blob: 90499ccccaeb227c4f940d536b90c3ba8825e540 [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? */
59 unsigned int result; /* reply result */
60 unsigned int error; /* error code to pass back to sender */
61 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 */
Mike McCormackdb9b5572006-06-08 17:01:54 +090073 unsigned long wparam; /* parameters */
74 unsigned long lparam; /* parameters */
Alexandre Julliard672bfc22006-08-09 17:10:14 +020075 unsigned long info; /* extra info */
Alexandre Julliardd253c582001-08-07 19:19:08 +000076 int x; /* x position */
77 int y; /* y position */
Alexandre Julliard838d65a2001-06-19 19:16:41 +000078 unsigned int time; /* message time */
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +000079 user_handle_t hook; /* winevent hook handle */
80 void *hook_proc; /* winevent hook proc address */
Alexandre Julliardd253c582001-08-07 19:19:08 +000081 void *data; /* message data for sent messages */
82 unsigned int data_size; /* size of message data */
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +000083 unsigned int unique_id; /* unique id for nested hw message waits */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000084 struct message_result *result; /* result in sender queue */
85};
86
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000087struct timer
88{
Alexandre Julliardff986a52004-11-29 18:08:18 +000089 struct list entry; /* entry in timer list */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000090 struct timeval when; /* next expiration */
91 unsigned int rate; /* timer rate in ms */
Alexandre Julliard1a66d222001-08-28 18:44:52 +000092 user_handle_t win; /* window handle */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000093 unsigned int msg; /* message to post */
94 unsigned int id; /* timer id */
Ge van Geldorpa6df1632006-07-10 08:58:35 +020095 unsigned long lparam; /* lparam for message */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000096};
97
Alexandre Julliardab5063b2002-10-11 18:50:15 +000098struct thread_input
99{
100 struct object obj; /* object header */
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000101 struct desktop *desktop; /* desktop that this thread input belongs to */
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000102 user_handle_t focus; /* focus window */
103 user_handle_t capture; /* capture window */
104 user_handle_t active; /* active window */
105 user_handle_t menu_owner; /* current menu owner window */
106 user_handle_t move_size; /* current moving/resizing window */
107 user_handle_t caret; /* caret window */
Alexandre Julliard11e35232002-10-17 01:24:33 +0000108 rectangle_t caret_rect; /* caret rectangle */
109 int caret_hide; /* caret hide count */
110 int caret_state; /* caret on/off state */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000111 struct list msg_list; /* list of hardware messages */
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000112 unsigned char keystate[256]; /* state of each key */
113};
114
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000115struct msg_queue
116{
Alexandre Julliard039e1312003-07-26 20:36:43 +0000117 struct object obj; /* object header */
118 unsigned int wake_bits; /* wakeup bits */
119 unsigned int wake_mask; /* wakeup mask */
120 unsigned int changed_bits; /* changed wakeup bits */
121 unsigned int changed_mask; /* changed wakeup mask */
122 int paint_count; /* pending paint messages count */
Robert Shearmana40ce392006-01-17 13:14:31 +0100123 int quit_message; /* is there a pending quit message? */
124 int exit_code; /* exit code of pending quit message */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000125 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
Alexandre Julliard039e1312003-07-26 20:36:43 +0000126 struct list send_result; /* stack of sent messages waiting for result */
127 struct list callback_result; /* list of callback messages waiting for result */
128 struct message_result *recv_result; /* stack of received messages waiting for result */
Alexandre Julliardff986a52004-11-29 18:08:18 +0000129 struct list pending_timers; /* list of pending timers */
130 struct list expired_timers; /* list of expired timers */
131 unsigned int next_timer_id; /* id for the next timer with a 0 window */
Alexandre Julliard039e1312003-07-26 20:36:43 +0000132 struct timeout_user *timeout; /* timeout for next timer to expire */
133 struct thread_input *input; /* thread input descriptor */
134 struct hook_table *hooks; /* hook table */
135 struct timeval last_get_msg; /* time of last get message call */
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000136};
137
138static void msg_queue_dump( struct object *obj, int verbose );
139static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
140static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
141static int msg_queue_signaled( struct object *obj, struct thread *thread );
142static int msg_queue_satisfied( struct object *obj, struct thread *thread );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000143static void msg_queue_destroy( struct object *obj );
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 */
152 msg_queue_add_queue, /* add_queue */
153 msg_queue_remove_queue, /* remove_queue */
154 msg_queue_signaled, /* signaled */
155 msg_queue_satisfied, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +0000156 no_signal, /* signal */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +0000157 no_get_fd, /* get_fd */
Alexandre Julliard28beba32005-12-12 14:57:40 +0100158 no_map_access, /* map_access */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000159 no_lookup_name, /* lookup_name */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000160 no_close_handle, /* close_handle */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000161 msg_queue_destroy /* destroy */
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000162};
163
164
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000165static const struct object_ops thread_input_ops =
166{
167 sizeof(struct thread_input), /* size */
168 thread_input_dump, /* dump */
169 no_add_queue, /* add_queue */
170 NULL, /* remove_queue */
171 NULL, /* signaled */
172 NULL, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +0000173 no_signal, /* signal */
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000174 no_get_fd, /* get_fd */
Alexandre Julliard28beba32005-12-12 14:57:40 +0100175 no_map_access, /* map_access */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000176 no_lookup_name, /* lookup_name */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000177 no_close_handle, /* close_handle */
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000178 thread_input_destroy /* destroy */
179};
180
Alexandre Julliard242e3952003-01-08 00:27:58 +0000181/* pointer to input structure of foreground thread */
182static struct thread_input *foreground_input;
Mike McCormackabe70f72005-04-28 12:04:14 +0000183static unsigned int last_input_time;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000184
Alexandre Julliard11e35232002-10-17 01:24:33 +0000185
186/* set the caret window in a given thread input */
187static void set_caret_window( struct thread_input *input, user_handle_t win )
188{
Krzysztof Foltmanb8501722005-02-18 20:02:55 +0000189 if (!win || win != input->caret)
190 {
191 input->caret_rect.left = 0;
192 input->caret_rect.top = 0;
193 input->caret_rect.right = 0;
194 input->caret_rect.bottom = 0;
195 }
Alexandre Julliard11e35232002-10-17 01:24:33 +0000196 input->caret = win;
Alexandre Julliard11e35232002-10-17 01:24:33 +0000197 input->caret_hide = 1;
198 input->caret_state = 0;
199}
200
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000201/* create a thread input object */
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000202static struct thread_input *create_thread_input( struct thread *thread )
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000203{
204 struct thread_input *input;
205
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000206 if ((input = alloc_object( &thread_input_ops )))
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000207 {
208 input->focus = 0;
209 input->capture = 0;
210 input->active = 0;
211 input->menu_owner = 0;
212 input->move_size = 0;
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000213 list_init( &input->msg_list );
Alexandre Julliard11e35232002-10-17 01:24:33 +0000214 set_caret_window( input, 0 );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000215 memset( input->keystate, 0, sizeof(input->keystate) );
Robert Shearmand8058fb2006-04-07 11:15:19 +0100216
217 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
218 {
219 release_object( input );
220 return NULL;
221 }
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000222 }
223 return input;
224}
225
Alexandre Julliard15ca8202004-06-17 20:00:25 +0000226/* release the thread input data of a given thread */
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +0000227static inline void release_thread_input( struct thread *thread )
Alexandre Julliard15ca8202004-06-17 20:00:25 +0000228{
229 struct thread_input *input = thread->queue->input;
230
231 if (!input) return;
Alexandre Julliard15ca8202004-06-17 20:00:25 +0000232 release_object( input );
233 thread->queue->input = NULL;
234}
235
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000236/* create a message queue object */
237static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000238{
239 struct msg_queue *queue;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000240 int i;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000241
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000242 if (!input && !(input = create_thread_input( thread ))) return NULL;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000243 if ((queue = alloc_object( &msg_queue_ops )))
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000244 {
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000245 queue->wake_bits = 0;
246 queue->wake_mask = 0;
247 queue->changed_bits = 0;
248 queue->changed_mask = 0;
Alexandre Julliard4b0343d2001-06-20 22:55:31 +0000249 queue->paint_count = 0;
Robert Shearmana40ce392006-01-17 13:14:31 +0100250 queue->quit_message = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000251 queue->recv_result = NULL;
Alexandre Julliardff986a52004-11-29 18:08:18 +0000252 queue->next_timer_id = 1;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000253 queue->timeout = NULL;
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000254 queue->input = (struct thread_input *)grab_object( input );
Alexandre Julliardd55e7f12003-07-03 18:16:48 +0000255 queue->hooks = NULL;
Alexandre Julliard753c8702006-08-10 16:42:09 +0200256 queue->last_get_msg = current_time;
Alexandre Julliard039e1312003-07-26 20:36:43 +0000257 list_init( &queue->send_result );
258 list_init( &queue->callback_result );
Alexandre Julliardff986a52004-11-29 18:08:18 +0000259 list_init( &queue->pending_timers );
260 list_init( &queue->expired_timers );
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000261 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000262
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000263 thread->queue = queue;
264 if (!thread->process->queue)
265 thread->process->queue = (struct msg_queue *)grab_object( queue );
266 }
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000267 release_object( input );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000268 return queue;
269}
270
Alexandre Julliard31022d62002-08-16 23:30:41 +0000271/* free the message queue of a thread at thread exit */
272void free_msg_queue( struct thread *thread )
273{
274 struct process *process = thread->process;
275
Alexandre Julliardca3ac8f2003-07-11 21:55:58 +0000276 remove_thread_hooks( thread );
Alexandre Julliard31022d62002-08-16 23:30:41 +0000277 if (!thread->queue) return;
278 if (process->queue == thread->queue) /* is it the process main queue? */
279 {
280 release_object( process->queue );
281 process->queue = NULL;
282 if (process->idle_event)
283 {
284 set_event( process->idle_event );
285 release_object( process->idle_event );
286 process->idle_event = NULL;
287 }
288 }
289 release_object( thread->queue );
290 thread->queue = NULL;
291}
292
Alexandre Julliardd55e7f12003-07-03 18:16:48 +0000293/* get the hook table for a given thread */
294struct hook_table *get_queue_hooks( struct thread *thread )
295{
296 if (!thread->queue) return NULL;
297 return thread->queue->hooks;
298}
299
300/* set the hook table for a given thread, allocating the queue if needed */
301void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
302{
303 struct msg_queue *queue = thread->queue;
Alexandre Julliardd70a2532005-04-26 14:31:33 +0000304 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
Alexandre Julliardd55e7f12003-07-03 18:16:48 +0000305 if (queue->hooks) release_object( queue->hooks );
306 queue->hooks = hooks;
307}
308
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000309/* check the queue status */
310inline static int is_signaled( struct msg_queue *queue )
311{
312 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
313}
314
Alexandre Julliardd253c582001-08-07 19:19:08 +0000315/* set some queue bits */
316inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000317{
Alexandre Julliardd253c582001-08-07 19:19:08 +0000318 queue->wake_bits |= bits;
319 queue->changed_bits |= bits;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000320 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
321}
322
Alexandre Julliardd253c582001-08-07 19:19:08 +0000323/* clear some queue bits */
324inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
325{
326 queue->wake_bits &= ~bits;
327 queue->changed_bits &= ~bits;
328}
329
Alexandre Julliard242e3952003-01-08 00:27:58 +0000330/* check whether msg is a keyboard message */
331inline static int is_keyboard_msg( struct message *msg )
332{
333 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
334}
335
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +0000336/* check if message is matched by the filter */
337inline static int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
338{
339 return (msg >= first && msg <= last);
340}
341
Alexandre Julliardcf2f1422005-03-14 17:17:09 +0000342/* check whether a message filter contains at least one potential hardware message */
343inline static int filter_contains_hw_range( unsigned int first, unsigned int last )
344{
345 /* hardware message ranges are (in numerical order):
346 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
347 * WM_KEYFIRST .. WM_KEYLAST
348 * WM_MOUSEFIRST .. WM_MOUSELAST
349 */
350 if (last < WM_NCMOUSEFIRST) return 0;
351 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
352 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
353 if (first > WM_MOUSELAST) return 0;
354 return 1;
355}
356
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000357/* get the QS_* bit corresponding to a given hardware message */
358inline static int get_hardware_msg_bit( struct message *msg )
359{
360 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000361 if (is_keyboard_msg( msg )) return QS_KEY;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000362 return QS_MOUSEBUTTON;
363}
364
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000365/* get the current thread queue, creating it if needed */
Alexandre Julliard805bdc52001-11-13 22:23:48 +0000366inline static struct msg_queue *get_current_queue(void)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000367{
368 struct msg_queue *queue = current->queue;
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000369 if (!queue) queue = create_msg_queue( current, NULL );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000370 return queue;
371}
372
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +0000373/* get a (pseudo-)unique id to tag hardware messages */
374inline static unsigned int get_unique_id(void)
375{
376 static unsigned int id;
377 if (!++id) id = 1; /* avoid an id of 0 */
378 return id;
379}
380
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000381/* try to merge a message with the last in the list; return 1 if successful */
Alexandre Julliard242e3952003-01-08 00:27:58 +0000382static int merge_message( struct thread_input *input, const struct message *msg )
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000383{
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000384 struct message *prev;
385 struct list *ptr = list_tail( &input->msg_list );
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000386
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000387 if (!ptr) return 0;
388 prev = LIST_ENTRY( ptr, struct message, entry );
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +0000389 if (prev->unique_id) return 0;
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000390 if (prev->result) return 0;
391 if (prev->win != msg->win) return 0;
392 if (prev->msg != msg->msg) return 0;
393 if (prev->type != msg->type) return 0;
394 /* now we can merge it */
395 prev->wparam = msg->wparam;
396 prev->lparam = msg->lparam;
397 prev->x = msg->x;
398 prev->y = msg->y;
399 prev->time = msg->time;
400 prev->info = msg->info;
401 return 1;
402}
403
Alexandre Julliardd253c582001-08-07 19:19:08 +0000404/* free a result structure */
405static void free_result( struct message_result *result )
406{
407 if (result->timeout) remove_timeout_user( result->timeout );
408 if (result->data) free( result->data );
Alexandre Julliard039e1312003-07-26 20:36:43 +0000409 if (result->callback_msg) free( result->callback_msg );
Alexandre Julliardd253c582001-08-07 19:19:08 +0000410 free( result );
411}
412
Alexandre Julliard039e1312003-07-26 20:36:43 +0000413/* remove the result from the sender list it is on */
414static inline void remove_result_from_sender( struct message_result *result )
415{
416 assert( result->sender );
417
418 list_remove( &result->sender_entry );
419 result->sender = NULL;
420 if (!result->receiver) free_result( result );
421}
422
Alexandre Julliardd253c582001-08-07 19:19:08 +0000423/* store the message result in the appropriate structure */
424static void store_message_result( struct message_result *res, unsigned int result,
425 unsigned int error )
426{
427 res->result = result;
428 res->error = error;
429 res->replied = 1;
430 if (res->timeout)
431 {
432 remove_timeout_user( res->timeout );
433 res->timeout = NULL;
434 }
Alexandre Julliard039e1312003-07-26 20:36:43 +0000435 if (res->sender)
436 {
437 if (res->callback_msg)
438 {
439 /* queue the callback message in the sender queue */
440 res->callback_msg->lparam = result;
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000441 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
Alexandre Julliard039e1312003-07-26 20:36:43 +0000442 set_queue_bits( res->sender, QS_SENDMESSAGE );
443 res->callback_msg = NULL;
444 remove_result_from_sender( res );
445 }
446 else
447 {
448 /* wake sender queue if waiting on this result */
449 if (list_head(&res->sender->send_result) == &res->sender_entry)
450 set_queue_bits( res->sender, QS_SMRESULT );
451 }
452 }
453
Alexandre Julliardd253c582001-08-07 19:19:08 +0000454}
455
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000456/* free a message when deleting a queue or window */
457static void free_message( struct message *msg )
458{
459 struct message_result *result = msg->result;
460 if (result)
461 {
Alexandre Julliard127127f2005-09-13 14:46:46 +0000462 result->msg = NULL;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000463 if (result->sender)
464 {
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000465 result->receiver = NULL;
Alexandre Julliardd253c582001-08-07 19:19:08 +0000466 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000467 }
Alexandre Julliardd253c582001-08-07 19:19:08 +0000468 else free_result( result );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000469 }
Alexandre Julliardd253c582001-08-07 19:19:08 +0000470 if (msg->data) free( msg->data );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000471 free( msg );
472}
473
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000474/* remove (and free) a message from a message list */
475static void remove_queue_message( struct msg_queue *queue, struct message *msg,
476 enum message_kind kind )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000477{
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000478 list_remove( &msg->entry );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000479 switch(kind)
480 {
481 case SEND_MESSAGE:
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000482 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000483 break;
484 case POST_MESSAGE:
Robert Shearman22bd7a32006-05-22 22:16:53 +0100485 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
486 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000487 break;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000488 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000489 free_message( msg );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000490}
491
Alexandre Julliardd253c582001-08-07 19:19:08 +0000492/* message timed out without getting a reply */
493static void result_timeout( void *private )
494{
495 struct message_result *result = private;
496
497 assert( !result->replied );
498
499 result->timeout = NULL;
Alexandre Julliard127127f2005-09-13 14:46:46 +0000500
501 if (result->msg) /* not received yet */
502 {
503 struct message *msg = result->msg;
504
505 result->msg = NULL;
506 msg->result = NULL;
507 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
508 result->receiver = NULL;
509 if (!result->sender)
510 {
511 free_result( result );
512 return;
513 }
514 }
515
Alexandre Julliardd253c582001-08-07 19:19:08 +0000516 store_message_result( result, 0, STATUS_TIMEOUT );
517}
518
519/* allocate and fill a message result structure */
520static struct message_result *alloc_message_result( struct msg_queue *send_queue,
521 struct msg_queue *recv_queue,
Alexandre Julliard127127f2005-09-13 14:46:46 +0000522 struct message *msg, int timeout,
Alexandre Julliard672bfc22006-08-09 17:10:14 +0200523 void *callback, unsigned long callback_data )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000524{
525 struct message_result *result = mem_alloc( sizeof(*result) );
Alexandre Julliardd253c582001-08-07 19:19:08 +0000526 if (result)
527 {
Alexandre Julliard127127f2005-09-13 14:46:46 +0000528 result->msg = msg;
Alexandre Julliardd253c582001-08-07 19:19:08 +0000529 result->sender = send_queue;
530 result->receiver = recv_queue;
531 result->replied = 0;
532 result->data = NULL;
533 result->data_size = 0;
534 result->timeout = NULL;
Alexandre Julliard039e1312003-07-26 20:36:43 +0000535
536 if (msg->type == MSG_CALLBACK)
537 {
538 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
539 if (!callback_msg)
540 {
541 free( result );
542 return NULL;
543 }
544 callback_msg->type = MSG_CALLBACK_RESULT;
545 callback_msg->win = msg->win;
546 callback_msg->msg = msg->msg;
Mike McCormackdb9b5572006-06-08 17:01:54 +0900547 callback_msg->wparam = (unsigned long)callback;
Alexandre Julliard039e1312003-07-26 20:36:43 +0000548 callback_msg->lparam = 0;
549 callback_msg->time = get_tick_count();
550 callback_msg->x = 0;
551 callback_msg->y = 0;
552 callback_msg->info = callback_data;
Alexandre Julliardd6885fe2005-03-05 10:51:35 +0000553 callback_msg->hook = 0;
554 callback_msg->hook_proc = NULL;
Alexandre Julliard039e1312003-07-26 20:36:43 +0000555 callback_msg->result = NULL;
556 callback_msg->data = NULL;
557 callback_msg->data_size = 0;
558
559 result->callback_msg = callback_msg;
560 list_add_head( &send_queue->callback_result, &result->sender_entry );
561 }
562 else
563 {
564 result->callback_msg = NULL;
565 list_add_head( &send_queue->send_result, &result->sender_entry );
566 }
567
Alexandre Julliard127127f2005-09-13 14:46:46 +0000568 if (timeout)
Alexandre Julliardd253c582001-08-07 19:19:08 +0000569 {
Alexandre Julliard753c8702006-08-10 16:42:09 +0200570 struct timeval when = current_time;
Alexandre Julliardd253c582001-08-07 19:19:08 +0000571 add_timeout( &when, timeout );
572 result->timeout = add_timeout_user( &when, result_timeout, result );
573 }
574 }
575 return result;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000576}
577
578/* receive a message, removing it from the sent queue */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000579static void receive_message( struct msg_queue *queue, struct message *msg,
580 struct get_message_reply *reply )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000581{
582 struct message_result *result = msg->result;
583
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000584 reply->total = msg->data_size;
585 if (msg->data_size > get_reply_max_size())
586 {
587 set_error( STATUS_BUFFER_OVERFLOW );
588 return;
589 }
590 reply->type = msg->type;
591 reply->win = msg->win;
592 reply->msg = msg->msg;
593 reply->wparam = msg->wparam;
594 reply->lparam = msg->lparam;
595 reply->x = msg->x;
596 reply->y = msg->y;
597 reply->time = msg->time;
598 reply->info = msg->info;
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +0000599 reply->hook = msg->hook;
600 reply->hook_proc = msg->hook_proc;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000601
602 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
603
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000604 list_remove( &msg->entry );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000605 /* put the result on the receiver result stack */
Alexandre Julliardd253c582001-08-07 19:19:08 +0000606 if (result)
607 {
Alexandre Julliard127127f2005-09-13 14:46:46 +0000608 result->msg = NULL;
Alexandre Julliardd253c582001-08-07 19:19:08 +0000609 result->recv_next = queue->recv_result;
610 queue->recv_result = result;
611 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000612 free( msg );
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000613 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000614}
615
616/* set the result of the current received message */
617static void reply_message( struct msg_queue *queue, unsigned int result,
Alexandre Julliard0f273c12006-07-26 10:43:25 +0200618 unsigned int error, int remove, const void *data, data_size_t len )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000619{
620 struct message_result *res = queue->recv_result;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000621
622 if (remove)
623 {
624 queue->recv_result = res->recv_next;
625 res->receiver = NULL;
626 if (!res->sender) /* no one waiting for it */
627 {
Alexandre Julliardd253c582001-08-07 19:19:08 +0000628 free_result( res );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000629 return;
630 }
631 }
632 if (!res->replied)
633 {
Alexandre Julliardd253c582001-08-07 19:19:08 +0000634 if (len && (res->data = memdup( data, len ))) res->data_size = len;
635 store_message_result( res, result, error );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000636 }
637}
638
Alexandre Julliard242e3952003-01-08 00:27:58 +0000639/* retrieve a posted message */
640static int get_posted_message( struct msg_queue *queue, user_handle_t win,
641 unsigned int first, unsigned int last, unsigned int flags,
642 struct get_message_reply *reply )
643{
644 struct message *msg;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000645
646 /* check against the filters */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000647 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
Alexandre Julliard242e3952003-01-08 00:27:58 +0000648 {
Alexandre Julliard242e3952003-01-08 00:27:58 +0000649 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +0000650 if (!check_msg_filter( msg->msg, first, last )) continue;
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000651 goto found; /* found one */
Alexandre Julliard242e3952003-01-08 00:27:58 +0000652 }
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000653 return 0;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000654
655 /* return it to the app */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000656found:
Alexandre Julliard242e3952003-01-08 00:27:58 +0000657 reply->total = msg->data_size;
658 if (msg->data_size > get_reply_max_size())
659 {
660 set_error( STATUS_BUFFER_OVERFLOW );
661 return 1;
662 }
663 reply->type = msg->type;
664 reply->win = msg->win;
665 reply->msg = msg->msg;
666 reply->wparam = msg->wparam;
667 reply->lparam = msg->lparam;
668 reply->x = msg->x;
669 reply->y = msg->y;
670 reply->time = msg->time;
671 reply->info = msg->info;
672
673 if (flags & GET_MSG_REMOVE)
674 {
675 if (msg->data)
676 {
677 set_reply_data_ptr( msg->data, msg->data_size );
678 msg->data = NULL;
679 msg->data_size = 0;
680 }
681 remove_queue_message( queue, msg, POST_MESSAGE );
682 }
683 else if (msg->data) set_reply_data( msg->data, msg->data_size );
684
685 return 1;
686}
687
Robert Shearmana40ce392006-01-17 13:14:31 +0100688static int get_quit_message( struct msg_queue *queue, unsigned int flags,
689 struct get_message_reply *reply )
690{
691 if (queue->quit_message)
692 {
693 reply->total = 0;
694 reply->type = MSG_POSTED;
695 reply->win = NULL;
696 reply->msg = WM_QUIT;
697 reply->wparam = queue->exit_code;
698 reply->lparam = 0;
699 reply->x = 0;
700 reply->y = 0;
701 reply->time = get_tick_count();
702 reply->info = 0;
703
704 if (flags & GET_MSG_REMOVE)
Robert Shearman22bd7a32006-05-22 22:16:53 +0100705 {
Robert Shearmana40ce392006-01-17 13:14:31 +0100706 queue->quit_message = 0;
Robert Shearman22bd7a32006-05-22 22:16:53 +0100707 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
708 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
709 }
Robert Shearmana40ce392006-01-17 13:14:31 +0100710 return 1;
711 }
712 else
713 return 0;
714}
715
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000716/* empty a message list and free all the messages */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000717static void empty_msg_list( struct list *list )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000718{
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000719 struct list *ptr;
720
721 while ((ptr = list_head( list )) != NULL)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000722 {
Alexandre Julliard8e3b0722005-02-25 21:05:11 +0000723 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
724 list_remove( &msg->entry );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000725 free_message( msg );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000726 }
727}
728
729/* cleanup all pending results when deleting a queue */
730static void cleanup_results( struct msg_queue *queue )
731{
Alexandre Julliard039e1312003-07-26 20:36:43 +0000732 struct list *entry;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000733
Alexandre Julliard039e1312003-07-26 20:36:43 +0000734 while ((entry = list_head( &queue->send_result )) != NULL)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000735 {
Alexandre Julliard039e1312003-07-26 20:36:43 +0000736 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
737 }
738
739 while ((entry = list_head( &queue->callback_result )) != NULL)
740 {
741 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000742 }
743
Alexandre Julliardd253c582001-08-07 19:19:08 +0000744 while (queue->recv_result)
745 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000746}
747
Alexandre Julliard09029b22003-07-11 04:09:42 +0000748/* check if the thread owning the queue is hung (not checking for messages) */
749static int is_queue_hung( struct msg_queue *queue )
750{
Alexandre Julliard09029b22003-07-11 04:09:42 +0000751 struct wait_queue_entry *entry;
752
Alexandre Julliard753c8702006-08-10 16:42:09 +0200753 if (current_time.tv_sec - queue->last_get_msg.tv_sec <= 5)
Alexandre Julliard09029b22003-07-11 04:09:42 +0000754 return 0; /* less than 5 seconds since last get message -> not hung */
755
Alexandre Julliardaa347682005-03-01 11:49:58 +0000756 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
Alexandre Julliard09029b22003-07-11 04:09:42 +0000757 {
758 if (entry->thread->queue == queue)
759 return 0; /* thread is waiting on queue -> not hung */
760 }
761 return 1;
762}
763
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000764static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
765{
766 struct msg_queue *queue = (struct msg_queue *)obj;
767 struct process *process = entry->thread->process;
768
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000769 /* a thread can only wait on its own queue */
770 if (entry->thread->queue != queue)
771 {
772 set_error( STATUS_ACCESS_DENIED );
773 return 0;
774 }
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000775 /* if waiting on the main process queue, set the idle event */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000776 if (process->queue == queue)
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000777 {
778 if (process->idle_event) set_event( process->idle_event );
779 }
780 add_queue( obj, entry );
781 return 1;
782}
783
784static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
785{
786 struct msg_queue *queue = (struct msg_queue *)obj;
787 struct process *process = entry->thread->process;
788
789 remove_queue( obj, entry );
790
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000791 assert( entry->thread->queue == queue );
792
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000793 /* if waiting on the main process queue, reset the idle event */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000794 if (process->queue == queue)
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000795 {
796 if (process->idle_event) reset_event( process->idle_event );
797 }
798}
799
800static void msg_queue_dump( struct object *obj, int verbose )
801{
802 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000803 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
804 queue->wake_bits, queue->wake_mask );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000805}
806
807static int msg_queue_signaled( struct object *obj, struct thread *thread )
808{
809 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000810 return is_signaled( queue );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000811}
812
813static int msg_queue_satisfied( struct object *obj, struct thread *thread )
814{
815 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000816 queue->wake_mask = 0;
817 queue->changed_mask = 0;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000818 return 0; /* Not abandoned */
819}
820
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000821static void msg_queue_destroy( struct object *obj )
822{
823 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliardff986a52004-11-29 18:08:18 +0000824 struct list *ptr;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000825 int i;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000826
827 cleanup_results( queue );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000828 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000829
Alexandre Julliardff986a52004-11-29 18:08:18 +0000830 while ((ptr = list_head( &queue->pending_timers )))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000831 {
Alexandre Julliardff986a52004-11-29 18:08:18 +0000832 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
833 list_remove( &timer->entry );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000834 free( timer );
Alexandre Julliardff986a52004-11-29 18:08:18 +0000835 }
836 while ((ptr = list_head( &queue->expired_timers )))
837 {
838 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
839 list_remove( &timer->entry );
840 free( timer );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000841 }
842 if (queue->timeout) remove_timeout_user( queue->timeout );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000843 if (queue->input) release_object( queue->input );
Alexandre Julliardd55e7f12003-07-03 18:16:48 +0000844 if (queue->hooks) release_object( queue->hooks );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000845}
846
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000847static void thread_input_dump( struct object *obj, int verbose )
848{
849 struct thread_input *input = (struct thread_input *)obj;
Alexandre Julliardb3332d72002-10-19 01:00:59 +0000850 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000851 input->focus, input->capture, input->active );
852}
853
854static void thread_input_destroy( struct object *obj )
855{
856 struct thread_input *input = (struct thread_input *)obj;
857
858 if (foreground_input == input) foreground_input = NULL;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000859 empty_msg_list( &input->msg_list );
Robert Shearmand8058fb2006-04-07 11:15:19 +0100860 if (input->desktop) release_object( input->desktop );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000861}
862
863/* fix the thread input data when a window is destroyed */
864inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
865{
866 struct thread_input *input = queue->input;
867
868 if (window == input->focus) input->focus = 0;
869 if (window == input->capture) input->capture = 0;
870 if (window == input->active) input->active = 0;
871 if (window == input->menu_owner) input->menu_owner = 0;
872 if (window == input->move_size) input->move_size = 0;
Alexandre Julliard11e35232002-10-17 01:24:33 +0000873 if (window == input->caret) set_caret_window( input, 0 );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000874}
875
Alexandre Julliard5030bda2002-10-11 23:41:06 +0000876/* check if the specified window can be set in the input data of a given queue */
877static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
878{
879 struct thread *thread;
880 int ret = 0;
881
882 if (!window) return 1; /* we can always clear the data */
883
884 if ((thread = get_window_thread( window )))
885 {
886 ret = (queue->input == thread->queue->input);
887 if (!ret) set_error( STATUS_ACCESS_DENIED );
888 release_object( thread );
889 }
890 else set_error( STATUS_INVALID_HANDLE );
891
892 return ret;
893}
894
Alexandre Julliardd70a2532005-04-26 14:31:33 +0000895/* make sure the specified thread has a queue */
896int init_thread_queue( struct thread *thread )
897{
898 if (thread->queue) return 1;
899 return (create_msg_queue( thread, NULL ) != NULL);
900}
901
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000902/* attach two thread input data structures */
903int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
904{
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000905 struct desktop *desktop;
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000906 struct thread_input *input;
907
908 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000909 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000910 input = (struct thread_input *)grab_object( thread_to->queue->input );
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000911 if (input->desktop != desktop)
912 {
913 set_error( STATUS_ACCESS_DENIED );
914 release_object( input );
915 release_object( desktop );
916 return 0;
917 }
918 release_object( desktop );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000919
920 if (thread_from->queue)
921 {
Alexandre Julliard15ca8202004-06-17 20:00:25 +0000922 release_thread_input( thread_from );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000923 thread_from->queue->input = input;
924 }
925 else
926 {
927 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
928 }
929 memset( input->keystate, 0, sizeof(input->keystate) );
930 return 1;
931}
932
933/* detach two thread input data structures */
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000934void detach_thread_input( struct thread *thread_from )
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000935{
936 struct thread_input *input;
937
Alexandre Julliard5ad90c02005-07-11 13:30:23 +0000938 if ((input = create_thread_input( thread_from )))
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000939 {
Alexandre Julliard15ca8202004-06-17 20:00:25 +0000940 release_thread_input( thread_from );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000941 thread_from->queue->input = input;
942 }
943}
944
945
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000946/* set the next timer to expire */
Alexandre Julliardff986a52004-11-29 18:08:18 +0000947static void set_next_timer( struct msg_queue *queue )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000948{
Alexandre Julliardff986a52004-11-29 18:08:18 +0000949 struct list *ptr;
950
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000951 if (queue->timeout)
952 {
953 remove_timeout_user( queue->timeout );
954 queue->timeout = NULL;
955 }
Alexandre Julliardff986a52004-11-29 18:08:18 +0000956 if ((ptr = list_head( &queue->pending_timers )))
957 {
958 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000959 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
Alexandre Julliardff986a52004-11-29 18:08:18 +0000960 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000961 /* set/clear QS_TIMER bit */
Alexandre Julliardff986a52004-11-29 18:08:18 +0000962 if (list_empty( &queue->expired_timers ))
Alexandre Julliardd253c582001-08-07 19:19:08 +0000963 clear_queue_bits( queue, QS_TIMER );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000964 else
Alexandre Julliardd253c582001-08-07 19:19:08 +0000965 set_queue_bits( queue, QS_TIMER );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000966}
967
Alexandre Julliardff986a52004-11-29 18:08:18 +0000968/* find a timer from its window and id */
969static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
970 unsigned int msg, unsigned int id )
971{
972 struct list *ptr;
973
974 /* we need to search both lists */
975
976 LIST_FOR_EACH( ptr, &queue->pending_timers )
977 {
978 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
979 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
980 }
981 LIST_FOR_EACH( ptr, &queue->expired_timers )
982 {
983 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
984 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
985 }
986 return NULL;
987}
988
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000989/* callback for the next timer expiration */
990static void timer_callback( void *private )
991{
992 struct msg_queue *queue = private;
Alexandre Julliardff986a52004-11-29 18:08:18 +0000993 struct list *ptr;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000994
995 queue->timeout = NULL;
996 /* move on to the next timer */
Alexandre Julliardff986a52004-11-29 18:08:18 +0000997 ptr = list_head( &queue->pending_timers );
998 list_remove( ptr );
999 list_add_tail( &queue->expired_timers, ptr );
1000 set_next_timer( queue );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001001}
1002
1003/* link a timer at its rightful place in the queue list */
1004static void link_timer( struct msg_queue *queue, struct timer *timer )
1005{
Alexandre Julliardff986a52004-11-29 18:08:18 +00001006 struct list *ptr;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001007
Alexandre Julliardff986a52004-11-29 18:08:18 +00001008 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001009 {
Alexandre Julliardff986a52004-11-29 18:08:18 +00001010 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1011 if (!time_before( &t->when, &timer->when )) break;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001012 }
Alexandre Julliardff986a52004-11-29 18:08:18 +00001013 list_add_before( ptr, &timer->entry );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001014}
1015
Alexandre Julliardff986a52004-11-29 18:08:18 +00001016/* remove a timer from the queue timer list and free it */
1017static void free_timer( struct msg_queue *queue, struct timer *timer )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001018{
Alexandre Julliardff986a52004-11-29 18:08:18 +00001019 list_remove( &timer->entry );
1020 free( timer );
1021 set_next_timer( queue );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001022}
1023
1024/* restart an expired timer */
1025static void restart_timer( struct msg_queue *queue, struct timer *timer )
1026{
Alexandre Julliardff986a52004-11-29 18:08:18 +00001027 list_remove( &timer->entry );
Alexandre Julliard753c8702006-08-10 16:42:09 +02001028 while (!time_before( &current_time, &timer->when )) add_timeout( &timer->when, timer->rate );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001029 link_timer( queue, timer );
Alexandre Julliardff986a52004-11-29 18:08:18 +00001030 set_next_timer( queue );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001031}
1032
1033/* find an expired timer matching the filtering parameters */
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001034static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001035 unsigned int get_first, unsigned int get_last,
1036 int remove )
1037{
Alexandre Julliardff986a52004-11-29 18:08:18 +00001038 struct list *ptr;
1039
1040 LIST_FOR_EACH( ptr, &queue->expired_timers )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001041 {
Alexandre Julliardff986a52004-11-29 18:08:18 +00001042 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001043 if (win && timer->win != win) continue;
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001044 if (check_msg_filter( timer->msg, get_first, get_last ))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001045 {
1046 if (remove) restart_timer( queue, timer );
1047 return timer;
1048 }
1049 }
1050 return NULL;
1051}
1052
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001053/* add a timer */
1054static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1055{
1056 struct timer *timer = mem_alloc( sizeof(*timer) );
1057 if (timer)
1058 {
Alexandre Julliard753c8702006-08-10 16:42:09 +02001059 timer->rate = max( rate, 1 );
1060 timer->when = current_time;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001061 add_timeout( &timer->when, rate );
1062 link_timer( queue, timer );
Alexandre Julliardff986a52004-11-29 18:08:18 +00001063 /* check if we replaced the next timer */
1064 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001065 }
1066 return timer;
1067}
1068
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001069/* change the input key state for a given key */
1070static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1071{
1072 if (down)
1073 {
1074 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1075 input->keystate[key] |= 0x80;
1076 }
1077 else input->keystate[key] &= ~0x80;
1078}
1079
1080/* update the input key state for a keyboard message */
1081static void update_input_key_state( struct thread_input *input, const struct message *msg )
1082{
1083 unsigned char key;
1084 int down = 0, extended;
1085
1086 switch (msg->msg)
1087 {
1088 case WM_LBUTTONDOWN:
1089 down = 1;
1090 /* fall through */
1091 case WM_LBUTTONUP:
1092 set_input_key_state( input, VK_LBUTTON, down );
1093 break;
1094 case WM_MBUTTONDOWN:
1095 down = 1;
1096 /* fall through */
1097 case WM_MBUTTONUP:
1098 set_input_key_state( input, VK_MBUTTON, down );
1099 break;
1100 case WM_RBUTTONDOWN:
1101 down = 1;
1102 /* fall through */
1103 case WM_RBUTTONUP:
1104 set_input_key_state( input, VK_RBUTTON, down );
1105 break;
Alexandre Julliardcb7aa872005-03-24 19:16:54 +00001106 case WM_XBUTTONDOWN:
1107 down = 1;
1108 /* fall through */
1109 case WM_XBUTTONUP:
1110 if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1111 else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1112 break;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001113 case WM_KEYDOWN:
1114 case WM_SYSKEYDOWN:
1115 down = 1;
1116 /* fall through */
1117 case WM_KEYUP:
1118 case WM_SYSKEYUP:
1119 key = (unsigned char)msg->wparam;
1120 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1121 set_input_key_state( input, key, down );
1122 switch(key)
1123 {
1124 case VK_SHIFT:
1125 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1126 break;
1127 case VK_CONTROL:
1128 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1129 break;
1130 case VK_MENU:
1131 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1132 break;
Thomas Kho0e814842006-04-12 11:13:46 -07001133 case VK_LCONTROL:
1134 case VK_RCONTROL:
1135 set_input_key_state( input, VK_CONTROL, down );
1136 break;
1137 case VK_LMENU:
1138 case VK_RMENU:
1139 set_input_key_state( input, VK_MENU, down );
1140 break;
1141 case VK_LSHIFT:
1142 case VK_RSHIFT:
1143 set_input_key_state( input, VK_SHIFT, down );
1144 break;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001145 }
1146 break;
1147 }
1148}
1149
Alexandre Julliard242e3952003-01-08 00:27:58 +00001150/* release the hardware message currently being processed by the given thread */
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001151static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1152 int remove, user_handle_t new_win )
Alexandre Julliard242e3952003-01-08 00:27:58 +00001153{
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001154 struct thread_input *input = queue->input;
1155 struct message *msg;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001156
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001157 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1158 {
1159 if (msg->unique_id == hw_id) break;
1160 }
1161 if (&msg->entry == &input->msg_list) return; /* not found */
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001162
1163 /* clear the queue bit for that message */
1164 if (remove || new_win)
Alexandre Julliard242e3952003-01-08 00:27:58 +00001165 {
1166 struct message *other;
1167 int clr_bit;
1168
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001169 clr_bit = get_hardware_msg_bit( msg );
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001170 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1171 {
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001172 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001173 {
1174 clr_bit = 0;
1175 break;
1176 }
1177 }
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001178 if (clr_bit) clear_queue_bits( queue, clr_bit );
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001179 }
1180
1181 if (new_win) /* set the new window */
1182 {
1183 struct thread *owner = get_window_thread( new_win );
1184 if (owner)
1185 {
Alexandre Julliard90b48e92005-05-13 14:00:19 +00001186 if (owner->queue->input == input)
1187 {
1188 msg->win = new_win;
1189 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1190 remove = 0;
1191 }
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001192 release_object( owner );
1193 }
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001194 }
Alexandre Julliard90b48e92005-05-13 14:00:19 +00001195 if (remove)
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001196 {
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001197 update_input_key_state( input, msg );
1198 list_remove( &msg->entry );
1199 free_message( msg );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001200 }
Alexandre Julliard242e3952003-01-08 00:27:58 +00001201}
1202
1203/* find the window that should receive a given hardware message */
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001204static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1205 unsigned int *msg_code )
Alexandre Julliard242e3952003-01-08 00:27:58 +00001206{
1207 user_handle_t win = 0;
1208
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001209 *msg_code = msg->msg;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001210 if (is_keyboard_msg( msg ))
1211 {
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001212 if (input && !(win = input->focus))
1213 {
1214 win = input->active;
1215 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1216 }
Alexandre Julliard242e3952003-01-08 00:27:58 +00001217 }
1218 else /* mouse message */
1219 {
1220 if (!input || !(win = input->capture))
1221 {
Dmitry Timoshkov93a4e1e2004-10-27 21:55:00 +00001222 if (!(win = msg->win) || !is_window_visible( win ))
Alexandre Julliard3f31a102005-08-16 19:58:12 +00001223 {
1224 if (input) win = window_from_point( input->desktop, msg->x, msg->y );
1225 }
Alexandre Julliard242e3952003-01-08 00:27:58 +00001226 }
1227 }
1228 return win;
1229}
1230
1231/* queue a hardware message into a given thread input */
1232static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1233{
1234 user_handle_t win;
Alexandre Julliarde3fe6892005-03-17 20:51:53 +00001235 struct thread *thread;
Alexandre Julliard5d282dc2006-02-14 10:43:15 +01001236 struct thread_input *input = queue ? queue->input : foreground_input;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001237 unsigned int msg_code;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001238
Mike McCormackabe70f72005-04-28 12:04:14 +00001239 last_input_time = get_tick_count();
Alexandre Julliard5d282dc2006-02-14 10:43:15 +01001240 win = find_hardware_message_window( input, msg, &msg_code );
Alexandre Julliarde3fe6892005-03-17 20:51:53 +00001241 if (!win || !(thread = get_window_thread(win)))
Alexandre Julliard242e3952003-01-08 00:27:58 +00001242 {
Alexandre Julliard5d282dc2006-02-14 10:43:15 +01001243 if (input) update_input_key_state( input, msg );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001244 free( msg );
1245 return;
1246 }
Alexandre Julliarde3fe6892005-03-17 20:51:53 +00001247 input = thread->queue->input;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001248
1249 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1250 else
1251 {
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001252 msg->unique_id = 0; /* will be set once we return it to the app */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001253 list_add_tail( &input->msg_list, &msg->entry );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001254 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1255 }
Alexandre Julliarde3fe6892005-03-17 20:51:53 +00001256 release_object( thread );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001257}
1258
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001259/* check message filter for a hardware message */
1260static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1261 user_handle_t filter_win, unsigned int first, unsigned int last )
1262{
1263 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1264 {
1265 /* we can only test the window for a keyboard message since the
1266 * dest window for a mouse message depends on hittest */
1267 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1268 return 0;
1269 /* the message code is final for a keyboard message, we can simply check it */
1270 return check_msg_filter( msg_code, first, last );
1271 }
1272 else /* mouse message */
1273 {
1274 /* we need to check all possible values that the message can have in the end */
1275
1276 if (check_msg_filter( msg_code, first, last )) return 1;
1277 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1278
1279 /* all other messages can become non-client messages */
1280 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1281
1282 /* clicks can become double-clicks or non-client double-clicks */
1283 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1284 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1285 {
1286 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1287 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1288 }
1289 return 0;
1290 }
1291}
1292
1293
Alexandre Julliard242e3952003-01-08 00:27:58 +00001294/* find a hardware message for the given queue */
Michael Stefaniuca6249772006-07-25 10:30:04 +02001295static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001296 unsigned int first, unsigned int last, struct get_message_reply *reply )
Alexandre Julliard242e3952003-01-08 00:27:58 +00001297{
1298 struct thread_input *input = thread->queue->input;
1299 struct thread *win_thread;
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001300 struct list *ptr;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001301 user_handle_t win;
1302 int clear_bits, got_one = 0;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001303 unsigned int msg_code;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001304
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001305 ptr = list_head( &input->msg_list );
1306 if (hw_id)
1307 {
1308 while (ptr)
1309 {
1310 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1311 if (msg->unique_id == hw_id) break;
1312 ptr = list_next( &input->msg_list, ptr );
1313 }
1314 if (!ptr) ptr = list_head( &input->msg_list );
1315 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1316 }
Alexandre Julliard242e3952003-01-08 00:27:58 +00001317
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001318 if (ptr == list_head( &input->msg_list ))
Alexandre Julliard242e3952003-01-08 00:27:58 +00001319 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001320 else
Alexandre Julliard242e3952003-01-08 00:27:58 +00001321 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
Alexandre Julliard242e3952003-01-08 00:27:58 +00001322
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001323 while (ptr)
Alexandre Julliard242e3952003-01-08 00:27:58 +00001324 {
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001325 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
Alexandre Julliard55d449e2005-05-14 11:08:05 +00001326 ptr = list_next( &input->msg_list, ptr );
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001327 win = find_hardware_message_window( input, msg, &msg_code );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001328 if (!win || !(win_thread = get_window_thread( win )))
1329 {
1330 /* no window at all, remove it */
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001331 update_input_key_state( input, msg );
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001332 list_remove( &msg->entry );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001333 free_message( msg );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001334 continue;
1335 }
1336 if (win_thread != thread)
1337 {
Alexandre Julliard55d449e2005-05-14 11:08:05 +00001338 if (win_thread->queue->input == input)
1339 {
1340 /* wake the other thread */
1341 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1342 got_one = 1;
1343 }
1344 else
1345 {
1346 /* for another thread input, drop it */
1347 update_input_key_state( input, msg );
1348 list_remove( &msg->entry );
1349 free_message( msg );
1350 }
Alexandre Julliard242e3952003-01-08 00:27:58 +00001351 release_object( win_thread );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001352 continue;
1353 }
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001354 release_object( win_thread );
1355
Alexandre Julliard242e3952003-01-08 00:27:58 +00001356 /* if we already got a message for another thread, or if it doesn't
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001357 * match the filter we skip it */
1358 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
Alexandre Julliard242e3952003-01-08 00:27:58 +00001359 {
1360 clear_bits &= ~get_hardware_msg_bit( msg );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001361 continue;
1362 }
1363 /* now we can return it */
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001364 if (!msg->unique_id) msg->unique_id = get_unique_id();
Alexandre Julliard242e3952003-01-08 00:27:58 +00001365 reply->type = MSG_HARDWARE;
1366 reply->win = win;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001367 reply->msg = msg_code;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001368 reply->wparam = msg->wparam;
1369 reply->lparam = msg->lparam;
1370 reply->x = msg->x;
1371 reply->y = msg->y;
1372 reply->time = msg->time;
1373 reply->info = msg->info;
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001374 reply->hw_id = msg->unique_id;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001375 return 1;
1376 }
1377 /* nothing found, clear the hardware queue bits */
1378 clear_queue_bits( thread->queue, clear_bits );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001379 return 0;
1380}
Alexandre Julliard805bdc52001-11-13 22:23:48 +00001381
1382/* increment (or decrement if 'incr' is negative) the queue paint count */
1383void inc_queue_paint_count( struct thread *thread, int incr )
1384{
1385 struct msg_queue *queue = thread->queue;
1386
1387 assert( queue );
1388
1389 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1390
1391 if (queue->paint_count)
1392 set_queue_bits( queue, QS_PAINT );
1393 else
1394 clear_queue_bits( queue, QS_PAINT );
1395}
1396
1397
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001398/* remove all messages and timers belonging to a certain window */
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001399void queue_cleanup_window( struct thread *thread, user_handle_t win )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001400{
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001401 struct msg_queue *queue = thread->queue;
Alexandre Julliardff986a52004-11-29 18:08:18 +00001402 struct list *ptr;
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001403 int i;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001404
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001405 if (!queue) return;
1406
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001407 /* remove timers */
Alexandre Julliardff986a52004-11-29 18:08:18 +00001408
1409 ptr = list_head( &queue->pending_timers );
1410 while (ptr)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001411 {
Alexandre Julliardff986a52004-11-29 18:08:18 +00001412 struct list *next = list_next( &queue->pending_timers, ptr );
1413 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1414 if (timer->win == win) free_timer( queue, timer );
1415 ptr = next;
1416 }
1417 ptr = list_head( &queue->expired_timers );
1418 while (ptr)
1419 {
1420 struct list *next = list_next( &queue->expired_timers, ptr );
1421 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1422 if (timer->win == win) free_timer( queue, timer );
1423 ptr = next;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001424 }
1425
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001426 /* remove messages */
1427 for (i = 0; i < NB_MSG_KINDS; i++)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001428 {
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001429 struct list *ptr, *next;
1430
1431 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001432 {
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001433 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001434 if (msg->win == win) remove_queue_message( queue, msg, i );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001435 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001436 }
Alexandre Julliardab5063b2002-10-11 18:50:15 +00001437
1438 thread_input_cleanup_window( queue, win );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001439}
1440
Alexandre Julliard81f2a732002-03-23 20:43:52 +00001441/* post a message to a window; used by socket handling */
1442void post_message( user_handle_t win, unsigned int message,
Mike McCormackdb9b5572006-06-08 17:01:54 +09001443 unsigned long wparam, unsigned long lparam )
Alexandre Julliard81f2a732002-03-23 20:43:52 +00001444{
1445 struct message *msg;
1446 struct thread *thread = get_window_thread( win );
1447
1448 if (!thread) return;
1449
1450 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1451 {
1452 msg->type = MSG_POSTED;
1453 msg->win = get_user_full_handle( win );
1454 msg->msg = message;
1455 msg->wparam = wparam;
1456 msg->lparam = lparam;
1457 msg->time = get_tick_count();
1458 msg->x = 0;
1459 msg->y = 0;
1460 msg->info = 0;
Alexandre Julliardd6885fe2005-03-05 10:51:35 +00001461 msg->hook = 0;
1462 msg->hook_proc = NULL;
Alexandre Julliard81f2a732002-03-23 20:43:52 +00001463 msg->result = NULL;
1464 msg->data = NULL;
1465 msg->data_size = 0;
1466
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001467 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
Alexandre Julliard80b997a2005-11-14 15:17:09 +00001468 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
Alexandre Julliard81f2a732002-03-23 20:43:52 +00001469 }
1470 release_object( thread );
1471}
1472
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001473/* post a win event */
1474void post_win_event( struct thread *thread, unsigned int event,
1475 user_handle_t win, unsigned int object_id,
1476 unsigned int child_id, void *hook_proc,
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001477 const WCHAR *module, data_size_t module_size,
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001478 user_handle_t hook)
1479{
1480 struct message *msg;
1481
1482 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1483 {
1484 msg->type = MSG_WINEVENT;
1485 msg->win = get_user_full_handle( win );
1486 msg->msg = event;
1487 msg->wparam = object_id;
1488 msg->lparam = child_id;
1489 msg->time = get_tick_count();
1490 msg->x = 0;
1491 msg->y = 0;
1492 msg->info = get_thread_id( current );
1493 msg->result = NULL;
1494 msg->hook = hook;
1495 msg->hook_proc = hook_proc;
1496
1497 if ((msg->data = malloc( module_size )))
1498 {
1499 msg->data_size = module_size;
1500 memcpy( msg->data, module, module_size );
1501
1502 if (debug_level > 1)
1503 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1504 get_thread_id(thread), event, win, object_id, child_id );
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001505 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
Dmitry Timoshkov6dba0a72005-02-03 16:40:20 +00001506 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1507 }
1508 else
1509 free( msg );
1510 }
1511}
Alexandre Julliard81f2a732002-03-23 20:43:52 +00001512
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001513/* get the message queue of the current thread */
1514DECL_HANDLER(get_msg_queue)
1515{
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001516 struct msg_queue *queue = get_current_queue();
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001517
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001518 reply->handle = 0;
1519 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001520}
1521
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001522
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001523/* set the current message queue wakeup mask */
1524DECL_HANDLER(set_queue_mask)
1525{
1526 struct msg_queue *queue = get_current_queue();
1527
1528 if (queue)
1529 {
1530 queue->wake_mask = req->wake_mask;
1531 queue->changed_mask = req->changed_mask;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001532 reply->wake_bits = queue->wake_bits;
1533 reply->changed_bits = queue->changed_bits;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001534 if (is_signaled( queue ))
1535 {
1536 /* if skip wait is set, do what would have been done in the subsequent wait */
1537 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1538 else wake_up( &queue->obj, 0 );
1539 }
1540 }
1541}
1542
1543
1544/* get the current message queue status */
1545DECL_HANDLER(get_queue_status)
1546{
1547 struct msg_queue *queue = current->queue;
1548 if (queue)
1549 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001550 reply->wake_bits = queue->wake_bits;
1551 reply->changed_bits = queue->changed_bits;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001552 if (req->clear) queue->changed_bits = 0;
1553 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001554 else reply->wake_bits = reply->changed_bits = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001555}
1556
1557
1558/* send a message to a thread queue */
1559DECL_HANDLER(send_message)
1560{
1561 struct message *msg;
1562 struct msg_queue *send_queue = get_current_queue();
Alexandre Julliard242e3952003-01-08 00:27:58 +00001563 struct msg_queue *recv_queue = NULL;
1564 struct thread *thread = NULL;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001565
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001566 if (!(thread = get_thread_from_id( req->id ))) return;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001567
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001568 if (!(recv_queue = thread->queue))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001569 {
1570 set_error( STATUS_INVALID_PARAMETER );
1571 release_object( thread );
1572 return;
1573 }
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001574 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
Alexandre Julliard09029b22003-07-11 04:09:42 +00001575 {
1576 set_error( STATUS_TIMEOUT );
1577 release_object( thread );
1578 return;
1579 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001580
1581 if ((msg = mem_alloc( sizeof(*msg) )))
1582 {
Alexandre Julliardd253c582001-08-07 19:19:08 +00001583 msg->type = req->type;
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001584 msg->win = get_user_full_handle( req->win );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001585 msg->msg = req->msg;
1586 msg->wparam = req->wparam;
1587 msg->lparam = req->lparam;
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001588 msg->time = get_tick_count();
1589 msg->x = 0;
1590 msg->y = 0;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001591 msg->info = req->info;
Alexandre Julliardd6885fe2005-03-05 10:51:35 +00001592 msg->hook = 0;
1593 msg->hook_proc = NULL;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001594 msg->result = NULL;
1595 msg->data = NULL;
1596 msg->data_size = 0;
1597
1598 switch(msg->type)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001599 {
Eric Pouech0faceb02002-01-18 19:22:55 +00001600 case MSG_OTHER_PROCESS:
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001601 msg->data_size = get_req_data_size();
1602 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
Alexandre Julliarde630aa02001-07-11 17:29:01 +00001603 {
1604 free( msg );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001605 break;
Alexandre Julliarde630aa02001-07-11 17:29:01 +00001606 }
Alexandre Julliardd253c582001-08-07 19:19:08 +00001607 /* fall through */
1608 case MSG_ASCII:
1609 case MSG_UNICODE:
1610 case MSG_CALLBACK:
Alexandre Julliard039e1312003-07-26 20:36:43 +00001611 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1612 req->timeout, req->callback, req->info )))
Alexandre Julliarde630aa02001-07-11 17:29:01 +00001613 {
Alexandre Julliard039e1312003-07-26 20:36:43 +00001614 free_message( msg );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001615 break;
Alexandre Julliarde630aa02001-07-11 17:29:01 +00001616 }
Alexandre Julliardd253c582001-08-07 19:19:08 +00001617 /* fall through */
1618 case MSG_NOTIFY:
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001619 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001620 set_queue_bits( recv_queue, QS_SENDMESSAGE );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001621 break;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001622 case MSG_POSTED:
Eric Pouech0faceb02002-01-18 19:22:55 +00001623 /* needed for posted DDE messages */
1624 msg->data_size = get_req_data_size();
1625 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1626 {
1627 free( msg );
1628 break;
1629 }
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001630 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
Alexandre Julliard80b997a2005-11-14 15:17:09 +00001631 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001632 break;
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001633 case MSG_HARDWARE: /* should use send_hardware_message instead */
Alexandre Julliard039e1312003-07-26 20:36:43 +00001634 case MSG_CALLBACK_RESULT: /* cannot send this one */
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001635 default:
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001636 set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001637 free( msg );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001638 break;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001639 }
1640 }
Alexandre Julliardd3b30962006-08-09 16:45:26 +02001641 release_object( thread );
1642}
1643
1644/* send a hardware message to a thread queue */
1645DECL_HANDLER(send_hardware_message)
1646{
1647 struct message *msg;
1648 struct msg_queue *recv_queue = NULL;
1649 struct thread *thread = NULL;
1650
1651 if (req->id)
1652 {
1653 if (!(thread = get_thread_from_id( req->id ))) return;
1654 }
1655
1656 if (thread && !(recv_queue = thread->queue))
1657 {
1658 set_error( STATUS_INVALID_PARAMETER );
1659 release_object( thread );
1660 return;
1661 }
1662
1663 if ((msg = mem_alloc( sizeof(*msg) )))
1664 {
1665 msg->type = MSG_HARDWARE;
1666 msg->win = get_user_full_handle( req->win );
1667 msg->msg = req->msg;
1668 msg->wparam = req->wparam;
1669 msg->lparam = req->lparam;
1670 msg->time = req->time;
1671 msg->x = req->x;
1672 msg->y = req->y;
1673 msg->info = req->info;
1674 msg->hook = 0;
1675 msg->hook_proc = NULL;
1676 msg->result = NULL;
1677 msg->data = NULL;
1678 msg->data_size = 0;
1679 queue_hardware_message( recv_queue, msg );
1680 }
Alexandre Julliard242e3952003-01-08 00:27:58 +00001681 if (thread) release_object( thread );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001682}
1683
Robert Shearmana40ce392006-01-17 13:14:31 +01001684/* post a quit message to the current queue */
1685DECL_HANDLER(post_quit_message)
1686{
1687 struct msg_queue *queue = get_current_queue();
1688
1689 if (!queue)
1690 return;
1691
1692 queue->quit_message = 1;
1693 queue->exit_code = req->exit_code;
1694 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1695}
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001696
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001697/* get a message from the current queue */
1698DECL_HANDLER(get_message)
1699{
1700 struct timer *timer;
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001701 struct list *ptr;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001702 struct msg_queue *queue = get_current_queue();
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001703 user_handle_t get_win = get_user_full_handle( req->get_win );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001704
Alexandre Julliard63342352005-05-11 13:03:15 +00001705 reply->active_hooks = get_active_hooks();
1706
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001707 if (!queue) return;
Alexandre Julliard753c8702006-08-10 16:42:09 +02001708 queue->last_get_msg = current_time;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001709
1710 /* first check for sent messages */
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001711 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001712 {
Alexandre Julliard8e3b0722005-02-25 21:05:11 +00001713 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001714 receive_message( queue, msg, reply );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001715 return;
1716 }
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001717 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001718
Alexandre Julliard9f55ae62001-06-28 04:37:22 +00001719 /* clear changed bits so we can wait on them if we don't find a message */
Alexandre Julliard80b997a2005-11-14 15:17:09 +00001720 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits = 0;
1721 else queue->changed_bits &= QS_ALLPOSTMESSAGE;
Alexandre Julliard9f55ae62001-06-28 04:37:22 +00001722
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001723 /* then check for posted messages */
Alexandre Julliard242e3952003-01-08 00:27:58 +00001724 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001725 return;
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001726
Robert Shearmana40ce392006-01-17 13:14:31 +01001727 /* only check for quit messages if not posted messages pending.
1728 * note: the quit message isn't filtered */
1729 if (get_quit_message( queue, req->flags, reply ))
1730 return;
1731
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001732 /* then check for any raw hardware message */
Alexandre Julliardcf2f1422005-03-14 17:17:09 +00001733 if (filter_contains_hw_range( req->get_first, req->get_last ) &&
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001734 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001735 return;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001736
1737 /* now check for WM_PAINT */
Alexandre Julliard47f98212001-11-14 21:28:36 +00001738 if (queue->paint_count &&
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001739 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001740 (reply->win = find_window_to_repaint( get_win, current )))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001741 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001742 reply->type = MSG_POSTED;
1743 reply->msg = WM_PAINT;
1744 reply->wparam = 0;
1745 reply->lparam = 0;
1746 reply->x = 0;
1747 reply->y = 0;
1748 reply->time = get_tick_count();
1749 reply->info = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001750 return;
1751 }
1752
1753 /* now check for timer */
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001754 if ((timer = find_expired_timer( queue, get_win, req->get_first,
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001755 req->get_last, (req->flags & GET_MSG_REMOVE) )))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001756 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001757 reply->type = MSG_POSTED;
1758 reply->win = timer->win;
1759 reply->msg = timer->msg;
1760 reply->wparam = timer->id;
1761 reply->lparam = timer->lparam;
1762 reply->x = 0;
1763 reply->y = 0;
1764 reply->time = get_tick_count();
1765 reply->info = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001766 return;
1767 }
1768
1769 done:
1770 set_error( STATUS_PENDING ); /* FIXME */
1771}
1772
1773
1774/* reply to a sent message */
1775DECL_HANDLER(reply_message)
1776{
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001777 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
Alexandre Julliardb1095da2003-03-19 00:12:17 +00001778 else if (current->queue->recv_result)
1779 reply_message( current->queue, req->result, 0, req->remove,
1780 get_req_data(), get_req_data_size() );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001781}
1782
1783
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001784/* accept the current hardware message */
1785DECL_HANDLER(accept_hardware_message)
1786{
1787 if (current->queue)
Alexandre Julliard3e2f2a52005-04-20 13:03:59 +00001788 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1789 else
1790 set_error( STATUS_ACCESS_DENIED );
Alexandre Julliard0bc83772005-03-23 10:33:17 +00001791}
1792
1793
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001794/* retrieve the reply for the last message sent */
1795DECL_HANDLER(get_message_reply)
1796{
Alexandre Julliard039e1312003-07-26 20:36:43 +00001797 struct message_result *result;
1798 struct list *entry;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001799 struct msg_queue *queue = current->queue;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001800
Alexandre Julliardd253c582001-08-07 19:19:08 +00001801 if (queue)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001802 {
Alexandre Julliardd253c582001-08-07 19:19:08 +00001803 set_error( STATUS_PENDING );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001804 reply->result = 0;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001805
Alexandre Julliard039e1312003-07-26 20:36:43 +00001806 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1807
1808 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1809 if (result->replied || req->cancel)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001810 {
Alexandre Julliardd253c582001-08-07 19:19:08 +00001811 if (result->replied)
1812 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001813 reply->result = result->result;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001814 set_error( result->error );
1815 if (result->data)
1816 {
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001817 data_size_t data_len = min( result->data_size, get_reply_max_size() );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001818 set_reply_data_ptr( result->data, data_len );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001819 result->data = NULL;
1820 result->data_size = 0;
1821 }
1822 }
Alexandre Julliard039e1312003-07-26 20:36:43 +00001823 remove_result_from_sender( result );
1824
1825 entry = list_head( &queue->send_result );
1826 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1827 else
1828 {
1829 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1830 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1831 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001832 }
1833 }
Alexandre Julliardd253c582001-08-07 19:19:08 +00001834 else set_error( STATUS_ACCESS_DENIED );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001835}
1836
1837
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001838/* set a window timer */
1839DECL_HANDLER(set_win_timer)
1840{
1841 struct timer *timer;
Alexandre Julliardff986a52004-11-29 18:08:18 +00001842 struct msg_queue *queue;
1843 struct thread *thread = NULL;
1844 user_handle_t win = 0;
1845 unsigned int id = req->id;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001846
Alexandre Julliardff986a52004-11-29 18:08:18 +00001847 if (req->win)
1848 {
1849 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1850 {
1851 set_error( STATUS_INVALID_HANDLE );
1852 return;
1853 }
1854 if (thread->process != current->process)
1855 {
1856 release_object( thread );
1857 set_error( STATUS_ACCESS_DENIED );
1858 return;
1859 }
1860 queue = thread->queue;
1861 /* remove it if it existed already */
1862 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1863 }
1864 else
1865 {
1866 queue = get_current_queue();
1867 /* find a free id for it */
1868 do
1869 {
1870 id = queue->next_timer_id;
1871 if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
1872 }
1873 while (find_timer( queue, 0, req->msg, id ));
1874 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001875
1876 if ((timer = set_timer( queue, req->rate )))
1877 {
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001878 timer->win = win;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001879 timer->msg = req->msg;
Alexandre Julliardff986a52004-11-29 18:08:18 +00001880 timer->id = id;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001881 timer->lparam = req->lparam;
Alexandre Julliardff986a52004-11-29 18:08:18 +00001882 reply->id = id;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001883 }
Alexandre Julliardff986a52004-11-29 18:08:18 +00001884 if (thread) release_object( thread );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001885}
1886
1887/* kill a window timer */
1888DECL_HANDLER(kill_win_timer)
1889{
Alexandre Julliardff986a52004-11-29 18:08:18 +00001890 struct timer *timer;
1891 struct thread *thread;
1892 user_handle_t win = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001893
Alexandre Julliardff986a52004-11-29 18:08:18 +00001894 if (req->win)
1895 {
1896 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1897 {
1898 set_error( STATUS_INVALID_HANDLE );
1899 return;
1900 }
1901 if (thread->process != current->process)
1902 {
1903 release_object( thread );
1904 set_error( STATUS_ACCESS_DENIED );
1905 return;
1906 }
1907 }
1908 else thread = (struct thread *)grab_object( current );
1909
1910 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1911 free_timer( thread->queue, timer );
1912 else
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001913 set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliardff986a52004-11-29 18:08:18 +00001914
1915 release_object( thread );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001916}
Alexandre Julliardab5063b2002-10-11 18:50:15 +00001917
1918
1919/* attach (or detach) thread inputs */
1920DECL_HANDLER(attach_thread_input)
1921{
1922 struct thread *thread_from = get_thread_from_id( req->tid_from );
1923 struct thread *thread_to = get_thread_from_id( req->tid_to );
1924
1925 if (!thread_from || !thread_to)
1926 {
1927 if (thread_from) release_object( thread_from );
1928 if (thread_to) release_object( thread_to );
1929 return;
1930 }
1931 if (thread_from != thread_to)
1932 {
1933 if (req->attach) attach_thread_input( thread_from, thread_to );
Alexandre Julliard5ad90c02005-07-11 13:30:23 +00001934 else
1935 {
1936 if (thread_from->queue && thread_to->queue &&
1937 thread_from->queue->input == thread_to->queue->input)
1938 detach_thread_input( thread_from );
1939 else
1940 set_error( STATUS_ACCESS_DENIED );
1941 }
Alexandre Julliardab5063b2002-10-11 18:50:15 +00001942 }
1943 else set_error( STATUS_ACCESS_DENIED );
1944 release_object( thread_from );
1945 release_object( thread_to );
1946}
1947
1948
1949/* get thread input data */
1950DECL_HANDLER(get_thread_input)
1951{
1952 struct thread *thread = NULL;
1953 struct thread_input *input;
1954
1955 if (req->tid)
1956 {
1957 if (!(thread = get_thread_from_id( req->tid ))) return;
1958 input = thread->queue ? thread->queue->input : NULL;
1959 }
1960 else input = foreground_input; /* get the foreground thread info */
1961
1962 if (input)
1963 {
1964 reply->focus = input->focus;
1965 reply->capture = input->capture;
1966 reply->active = input->active;
1967 reply->menu_owner = input->menu_owner;
1968 reply->move_size = input->move_size;
1969 reply->caret = input->caret;
Alexandre Julliard11e35232002-10-17 01:24:33 +00001970 reply->rect = input->caret_rect;
Alexandre Julliardab5063b2002-10-11 18:50:15 +00001971 }
1972 else
1973 {
1974 reply->focus = 0;
1975 reply->capture = 0;
1976 reply->active = 0;
1977 reply->menu_owner = 0;
1978 reply->move_size = 0;
1979 reply->caret = 0;
1980 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1981 }
1982 /* foreground window is active window of foreground thread */
1983 reply->foreground = foreground_input ? foreground_input->active : 0;
1984 if (thread) release_object( thread );
1985}
Alexandre Julliard5030bda2002-10-11 23:41:06 +00001986
1987
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001988/* retrieve queue keyboard state for a given thread */
1989DECL_HANDLER(get_key_state)
1990{
1991 struct thread *thread;
1992 struct thread_input *input;
1993
1994 if (!(thread = get_thread_from_id( req->tid ))) return;
1995 input = thread->queue ? thread->queue->input : NULL;
1996 if (input)
1997 {
1998 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1999 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
2000 }
2001 release_object( thread );
2002}
2003
2004
2005/* set queue keyboard state for a given thread */
2006DECL_HANDLER(set_key_state)
2007{
2008 struct thread *thread = NULL;
2009 struct thread_input *input;
2010
2011 if (!(thread = get_thread_from_id( req->tid ))) return;
2012 input = thread->queue ? thread->queue->input : NULL;
2013 if (input)
2014 {
Alexandre Julliard0f273c12006-07-26 10:43:25 +02002015 data_size_t size = min( sizeof(input->keystate), get_req_data_size() );
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00002016 if (size) memcpy( input->keystate, get_req_data(), size );
2017 }
2018 release_object( thread );
2019}
2020
2021
Alexandre Julliard5030bda2002-10-11 23:41:06 +00002022/* set the system foreground window */
2023DECL_HANDLER(set_foreground_window)
2024{
Dmitry Timoshkov19e7fab2006-07-07 23:01:51 +09002025 struct thread *thread;
Alexandre Julliard5030bda2002-10-11 23:41:06 +00002026 struct msg_queue *queue = get_current_queue();
2027
2028 reply->previous = foreground_input ? foreground_input->active : 0;
2029 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
2030 reply->send_msg_new = FALSE;
2031
Dmitry Timoshkov19e7fab2006-07-07 23:01:51 +09002032 if (is_top_level_window( req->handle ) &&
2033 ((thread = get_window_thread( req->handle ))))
Alexandre Julliard5030bda2002-10-11 23:41:06 +00002034 {
Dmitry Timoshkov19e7fab2006-07-07 23:01:51 +09002035 foreground_input = thread->queue->input;
2036 reply->send_msg_new = (foreground_input != queue->input);
2037 release_object( thread );
Alexandre Julliard5030bda2002-10-11 23:41:06 +00002038 }
Dmitry Timoshkov19e7fab2006-07-07 23:01:51 +09002039 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
Alexandre Julliard5030bda2002-10-11 23:41:06 +00002040}
2041
2042
2043/* set the current thread focus window */
2044DECL_HANDLER(set_focus_window)
2045{
2046 struct msg_queue *queue = get_current_queue();
2047
2048 reply->previous = 0;
2049 if (queue && check_queue_input_window( queue, req->handle ))
2050 {
2051 reply->previous = queue->input->focus;
2052 queue->input->focus = get_user_full_handle( req->handle );
2053 }
2054}
2055
2056
2057/* set the current thread active window */
2058DECL_HANDLER(set_active_window)
2059{
2060 struct msg_queue *queue = get_current_queue();
2061
2062 reply->previous = 0;
2063 if (queue && check_queue_input_window( queue, req->handle ))
2064 {
2065 if (!req->handle || make_window_active( req->handle ))
2066 {
2067 reply->previous = queue->input->active;
2068 queue->input->active = get_user_full_handle( req->handle );
2069 }
2070 else set_error( STATUS_INVALID_HANDLE );
2071 }
2072}
Alexandre Julliarda9e8f592002-10-12 01:24:37 +00002073
2074
2075/* set the current thread capture window */
2076DECL_HANDLER(set_capture_window)
2077{
2078 struct msg_queue *queue = get_current_queue();
2079
2080 reply->previous = reply->full_handle = 0;
2081 if (queue && check_queue_input_window( queue, req->handle ))
2082 {
2083 struct thread_input *input = queue->input;
2084
2085 reply->previous = input->capture;
2086 input->capture = get_user_full_handle( req->handle );
2087 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2088 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2089 reply->full_handle = input->capture;
2090 }
2091}
Alexandre Julliard11e35232002-10-17 01:24:33 +00002092
2093
2094/* Set the current thread caret window */
2095DECL_HANDLER(set_caret_window)
2096{
2097 struct msg_queue *queue = get_current_queue();
2098
2099 reply->previous = 0;
2100 if (queue && check_queue_input_window( queue, req->handle ))
2101 {
2102 struct thread_input *input = queue->input;
2103
2104 reply->previous = input->caret;
2105 reply->old_rect = input->caret_rect;
2106 reply->old_hide = input->caret_hide;
2107 reply->old_state = input->caret_state;
2108
2109 set_caret_window( input, get_user_full_handle(req->handle) );
Krzysztof Foltmanb8501722005-02-18 20:02:55 +00002110 input->caret_rect.right = input->caret_rect.left + req->width;
2111 input->caret_rect.bottom = input->caret_rect.top + req->height;
Alexandre Julliard11e35232002-10-17 01:24:33 +00002112 }
2113}
2114
2115
2116/* Set the current thread caret information */
2117DECL_HANDLER(set_caret_info)
2118{
2119 struct msg_queue *queue = get_current_queue();
2120 struct thread_input *input;
2121
2122 if (!queue) return;
2123 input = queue->input;
2124 reply->full_handle = input->caret;
2125 reply->old_rect = input->caret_rect;
2126 reply->old_hide = input->caret_hide;
2127 reply->old_state = input->caret_state;
2128
2129 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2130 {
2131 set_error( STATUS_ACCESS_DENIED );
2132 return;
2133 }
2134 if (req->flags & SET_CARET_POS)
2135 {
2136 input->caret_rect.right += req->x - input->caret_rect.left;
2137 input->caret_rect.bottom += req->y - input->caret_rect.top;
2138 input->caret_rect.left = req->x;
2139 input->caret_rect.top = req->y;
2140 }
2141 if (req->flags & SET_CARET_HIDE)
2142 {
2143 input->caret_hide += req->hide;
2144 if (input->caret_hide < 0) input->caret_hide = 0;
2145 }
2146 if (req->flags & SET_CARET_STATE)
2147 {
2148 if (req->state == -1) input->caret_state = !input->caret_state;
2149 else input->caret_state = !!req->state;
2150 }
2151}
Mike McCormackabe70f72005-04-28 12:04:14 +00002152
2153
2154/* get the time of the last input event */
2155DECL_HANDLER(get_last_input_time)
2156{
2157 reply->time = last_input_time;
2158}