blob: 5a08668563a46b26790abd014c6f87bd8288b150 [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
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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>
25#include <stdio.h>
26#include <stdlib.h>
27
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000028#include "winbase.h"
29#include "wingdi.h"
30#include "winuser.h"
31
Alexandre Julliardc5e433a2000-05-30 19:48:18 +000032#include "handle.h"
33#include "thread.h"
34#include "process.h"
35#include "request.h"
Alexandre Julliard1a66d222001-08-28 18:44:52 +000036#include "user.h"
Alexandre Julliardc5e433a2000-05-30 19:48:18 +000037
Alexandre Julliardd253c582001-08-07 19:19:08 +000038enum message_kind { SEND_MESSAGE, POST_MESSAGE, COOKED_HW_MESSAGE, RAW_HW_MESSAGE };
39#define NB_MSG_KINDS (RAW_HW_MESSAGE+1)
40
41
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000042struct message_result
43{
44 struct message_result *send_next; /* next in sender list */
45 struct message_result *recv_next; /* next in receiver list */
46 struct msg_queue *sender; /* sender queue */
47 struct msg_queue *receiver; /* receiver queue */
48 int replied; /* has it been replied to? */
49 unsigned int result; /* reply result */
50 unsigned int error; /* error code to pass back to sender */
Alexandre Julliardd253c582001-08-07 19:19:08 +000051 void *data; /* message reply data */
52 unsigned int data_size; /* size of message reply data */
53 struct timeout_user *timeout; /* result timeout */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000054};
55
56struct message
57{
58 struct message *next; /* next message in list */
59 struct message *prev; /* prev message in list */
Alexandre Julliardd253c582001-08-07 19:19:08 +000060 enum message_type type; /* message type */
Alexandre Julliard1a66d222001-08-28 18:44:52 +000061 user_handle_t win; /* window handle */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000062 unsigned int msg; /* message code */
63 unsigned int wparam; /* parameters */
64 unsigned int lparam; /* parameters */
Alexandre Julliardd253c582001-08-07 19:19:08 +000065 int x; /* x position */
66 int y; /* y position */
Alexandre Julliard838d65a2001-06-19 19:16:41 +000067 unsigned int time; /* message time */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000068 unsigned int info; /* extra info */
Alexandre Julliardd253c582001-08-07 19:19:08 +000069 void *data; /* message data for sent messages */
70 unsigned int data_size; /* size of message data */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000071 struct message_result *result; /* result in sender queue */
72};
73
74struct message_list
75{
76 struct message *first; /* head of list */
77 struct message *last; /* tail of list */
78};
79
80struct timer
81{
82 struct timer *next; /* next timer in list */
83 struct timer *prev; /* prev timer in list */
84 struct timeval when; /* next expiration */
85 unsigned int rate; /* timer rate in ms */
Alexandre Julliard1a66d222001-08-28 18:44:52 +000086 user_handle_t win; /* window handle */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000087 unsigned int msg; /* message to post */
88 unsigned int id; /* timer id */
89 unsigned int lparam; /* lparam for message */
90};
91
Alexandre Julliardab5063b2002-10-11 18:50:15 +000092struct thread_input
93{
94 struct object obj; /* object header */
95 user_handle_t focus; /* focus window */
96 user_handle_t capture; /* capture window */
97 user_handle_t active; /* active window */
98 user_handle_t menu_owner; /* current menu owner window */
99 user_handle_t move_size; /* current moving/resizing window */
100 user_handle_t caret; /* caret window */
101 rectangle_t rect; /* caret rectangle */
102 unsigned char keystate[256]; /* state of each key */
103};
104
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000105struct msg_queue
106{
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000107 struct object obj; /* object header */
108 unsigned int wake_bits; /* wakeup bits */
109 unsigned int wake_mask; /* wakeup mask */
110 unsigned int changed_bits; /* changed wakeup bits */
111 unsigned int changed_mask; /* changed wakeup mask */
Alexandre Julliard4b0343d2001-06-20 22:55:31 +0000112 int paint_count; /* pending paint messages count */
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000113 struct message_list msg_list[NB_MSG_KINDS]; /* lists of messages */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000114 struct message_result *send_result; /* stack of sent messages waiting for result */
115 struct message_result *recv_result; /* stack of received messages waiting for result */
Alexandre Julliard9f55ae62001-06-28 04:37:22 +0000116 struct message *last_msg; /* last msg returned to the app and not removed */
117 enum message_kind last_msg_kind; /* message kind of last_msg */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000118 struct timer *first_timer; /* head of timer list */
119 struct timer *last_timer; /* tail of timer list */
120 struct timer *next_timer; /* next timer to expire */
121 struct timeout_user *timeout; /* timeout for next timer to expire */
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000122 struct thread_input *input; /* thread input descriptor */
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000123};
124
125static void msg_queue_dump( struct object *obj, int verbose );
126static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
127static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
128static int msg_queue_signaled( struct object *obj, struct thread *thread );
129static int msg_queue_satisfied( struct object *obj, struct thread *thread );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000130static void msg_queue_destroy( struct object *obj );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000131static void thread_input_dump( struct object *obj, int verbose );
132static void thread_input_destroy( struct object *obj );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000133static void timer_callback( void *private );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000134
135static const struct object_ops msg_queue_ops =
136{
137 sizeof(struct msg_queue), /* size */
138 msg_queue_dump, /* dump */
139 msg_queue_add_queue, /* add_queue */
140 msg_queue_remove_queue, /* remove_queue */
141 msg_queue_signaled, /* signaled */
142 msg_queue_satisfied, /* satisfied */
143 NULL, /* get_poll_events */
144 NULL, /* poll_event */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +0000145 no_get_fd, /* get_fd */
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000146 no_flush, /* flush */
147 no_get_file_info, /* get_file_info */
Mike McCormack6f011c02001-12-20 00:07:05 +0000148 NULL, /* queue_async */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000149 msg_queue_destroy /* destroy */
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000150};
151
152
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000153static const struct object_ops thread_input_ops =
154{
155 sizeof(struct thread_input), /* size */
156 thread_input_dump, /* dump */
157 no_add_queue, /* add_queue */
158 NULL, /* remove_queue */
159 NULL, /* signaled */
160 NULL, /* satisfied */
161 NULL, /* get_poll_events */
162 NULL, /* poll_event */
163 no_get_fd, /* get_fd */
164 no_flush, /* flush */
165 no_get_file_info, /* get_file_info */
166 NULL, /* queue_async */
167 thread_input_destroy /* destroy */
168};
169
170/* create a thread input object */
171static struct thread_input *create_thread_input(void)
172{
173 struct thread_input *input;
174
175 if ((input = alloc_object( &thread_input_ops, -1 )))
176 {
177 input->focus = 0;
178 input->capture = 0;
179 input->active = 0;
180 input->menu_owner = 0;
181 input->move_size = 0;
182 input->caret = 0;
183 input->rect.left = 0;
184 input->rect.top = 0;
185 input->rect.right = 0;
186 input->rect.bottom = 0;
187 memset( input->keystate, 0, sizeof(input->keystate) );
188 }
189 return input;
190}
191
192/* pointer to input structure of foreground thread */
193static struct thread_input *foreground_input;
194
195/* create a message queue object */
196static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000197{
198 struct msg_queue *queue;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000199 int i;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000200
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000201 if (!input && !(input = create_thread_input())) return NULL;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000202 if ((queue = alloc_object( &msg_queue_ops, -1 )))
203 {
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000204 queue->wake_bits = 0;
205 queue->wake_mask = 0;
206 queue->changed_bits = 0;
207 queue->changed_mask = 0;
Alexandre Julliard4b0343d2001-06-20 22:55:31 +0000208 queue->paint_count = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000209 queue->send_result = NULL;
210 queue->recv_result = NULL;
Alexandre Julliard9f55ae62001-06-28 04:37:22 +0000211 queue->last_msg = NULL;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000212 queue->first_timer = NULL;
213 queue->last_timer = NULL;
214 queue->next_timer = NULL;
215 queue->timeout = NULL;
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000216 queue->input = (struct thread_input *)grab_object( input );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000217 for (i = 0; i < NB_MSG_KINDS; i++)
218 queue->msg_list[i].first = queue->msg_list[i].last = NULL;
219
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000220 thread->queue = queue;
221 if (!thread->process->queue)
222 thread->process->queue = (struct msg_queue *)grab_object( queue );
223 }
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000224 release_object( input );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000225 return queue;
226}
227
Alexandre Julliard31022d62002-08-16 23:30:41 +0000228/* free the message queue of a thread at thread exit */
229void free_msg_queue( struct thread *thread )
230{
231 struct process *process = thread->process;
232
233 if (!thread->queue) return;
234 if (process->queue == thread->queue) /* is it the process main queue? */
235 {
236 release_object( process->queue );
237 process->queue = NULL;
238 if (process->idle_event)
239 {
240 set_event( process->idle_event );
241 release_object( process->idle_event );
242 process->idle_event = NULL;
243 }
244 }
245 release_object( thread->queue );
246 thread->queue = NULL;
247}
248
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000249/* check the queue status */
250inline static int is_signaled( struct msg_queue *queue )
251{
252 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
253}
254
Alexandre Julliardd253c582001-08-07 19:19:08 +0000255/* set some queue bits */
256inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000257{
Alexandre Julliardd253c582001-08-07 19:19:08 +0000258 queue->wake_bits |= bits;
259 queue->changed_bits |= bits;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000260 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
261}
262
Alexandre Julliardd253c582001-08-07 19:19:08 +0000263/* clear some queue bits */
264inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
265{
266 queue->wake_bits &= ~bits;
267 queue->changed_bits &= ~bits;
268}
269
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000270/* get the QS_* bit corresponding to a given hardware message */
271inline static int get_hardware_msg_bit( struct message *msg )
272{
273 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
274 if (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST) return QS_KEY;
275 return QS_MOUSEBUTTON;
276}
277
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000278/* get the current thread queue, creating it if needed */
Alexandre Julliard805bdc52001-11-13 22:23:48 +0000279inline static struct msg_queue *get_current_queue(void)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000280{
281 struct msg_queue *queue = current->queue;
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000282 if (!queue) queue = create_msg_queue( current, NULL );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000283 return queue;
284}
285
286/* append a message to the end of a list */
287inline static void append_message( struct message_list *list, struct message *msg )
288{
289 msg->next = NULL;
290 if ((msg->prev = list->last)) msg->prev->next = msg;
291 else list->first = msg;
292 list->last = msg;
293}
294
295/* unlink a message from a list it */
296inline static void unlink_message( struct message_list *list, struct message *msg )
297{
298 if (msg->next) msg->next->prev = msg->prev;
299 else list->last = msg->prev;
300 if (msg->prev) msg->prev->next = msg->next;
301 else list->first = msg->next;
302}
303
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000304/* try to merge a message with the last in the list; return 1 if successful */
305static int merge_message( struct message_list *list, const struct message *msg )
306{
307 struct message *prev = list->last;
308
309 if (!prev) return 0;
310 if (prev->result) return 0;
311 if (prev->win != msg->win) return 0;
312 if (prev->msg != msg->msg) return 0;
313 if (prev->type != msg->type) return 0;
314 /* now we can merge it */
315 prev->wparam = msg->wparam;
316 prev->lparam = msg->lparam;
317 prev->x = msg->x;
318 prev->y = msg->y;
319 prev->time = msg->time;
320 prev->info = msg->info;
321 return 1;
322}
323
Alexandre Julliardd253c582001-08-07 19:19:08 +0000324/* free a result structure */
325static void free_result( struct message_result *result )
326{
327 if (result->timeout) remove_timeout_user( result->timeout );
328 if (result->data) free( result->data );
329 free( result );
330}
331
332/* store the message result in the appropriate structure */
333static void store_message_result( struct message_result *res, unsigned int result,
334 unsigned int error )
335{
336 res->result = result;
337 res->error = error;
338 res->replied = 1;
339 if (res->timeout)
340 {
341 remove_timeout_user( res->timeout );
342 res->timeout = NULL;
343 }
344 /* wake sender queue if waiting on this result */
345 if (res->sender && res->sender->send_result == res)
346 set_queue_bits( res->sender, QS_SMRESULT );
347}
348
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000349/* free a message when deleting a queue or window */
350static void free_message( struct message *msg )
351{
352 struct message_result *result = msg->result;
353 if (result)
354 {
355 if (result->sender)
356 {
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000357 result->receiver = NULL;
Alexandre Julliardd253c582001-08-07 19:19:08 +0000358 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000359 }
Alexandre Julliardd253c582001-08-07 19:19:08 +0000360 else free_result( result );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000361 }
Alexandre Julliardd253c582001-08-07 19:19:08 +0000362 if (msg->data) free( msg->data );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000363 free( msg );
364}
365
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000366/* remove (and free) a message from a message list */
367static void remove_queue_message( struct msg_queue *queue, struct message *msg,
368 enum message_kind kind )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000369{
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000370 int clr_bit;
371 struct message *other;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000372
Alexandre Julliard9f55ae62001-06-28 04:37:22 +0000373 if (queue->last_msg == msg) queue->last_msg = NULL;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000374 unlink_message( &queue->msg_list[kind], msg );
375 switch(kind)
376 {
377 case SEND_MESSAGE:
Alexandre Julliardd253c582001-08-07 19:19:08 +0000378 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_SENDMESSAGE );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000379 break;
380 case POST_MESSAGE:
Alexandre Julliardd253c582001-08-07 19:19:08 +0000381 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_POSTMESSAGE );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000382 break;
383 case COOKED_HW_MESSAGE:
384 case RAW_HW_MESSAGE:
385 clr_bit = get_hardware_msg_bit( msg );
386 for (other = queue->msg_list[kind].first; other; other = other->next)
387 if (get_hardware_msg_bit( other ) == clr_bit) break;
Alexandre Julliardd253c582001-08-07 19:19:08 +0000388 if (!other) clear_queue_bits( queue, clr_bit );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000389 break;
390 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000391 free_message( msg );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000392}
393
Alexandre Julliardd253c582001-08-07 19:19:08 +0000394/* message timed out without getting a reply */
395static void result_timeout( void *private )
396{
397 struct message_result *result = private;
398
399 assert( !result->replied );
400
401 result->timeout = NULL;
402 store_message_result( result, 0, STATUS_TIMEOUT );
403}
404
405/* allocate and fill a message result structure */
406static struct message_result *alloc_message_result( struct msg_queue *send_queue,
407 struct msg_queue *recv_queue,
408 unsigned int timeout )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000409{
410 struct message_result *result = mem_alloc( sizeof(*result) );
Alexandre Julliardd253c582001-08-07 19:19:08 +0000411 if (result)
412 {
413 /* put the result on the sender result stack */
414 result->sender = send_queue;
415 result->receiver = recv_queue;
416 result->replied = 0;
417 result->data = NULL;
418 result->data_size = 0;
419 result->timeout = NULL;
420 result->send_next = send_queue->send_result;
421 send_queue->send_result = result;
422 if (timeout != -1)
423 {
424 struct timeval when;
425 gettimeofday( &when, 0 );
426 add_timeout( &when, timeout );
427 result->timeout = add_timeout_user( &when, result_timeout, result );
428 }
429 }
430 return result;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000431}
432
433/* receive a message, removing it from the sent queue */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000434static void receive_message( struct msg_queue *queue, struct message *msg,
435 struct get_message_reply *reply )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000436{
437 struct message_result *result = msg->result;
438
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000439 reply->total = msg->data_size;
440 if (msg->data_size > get_reply_max_size())
441 {
442 set_error( STATUS_BUFFER_OVERFLOW );
443 return;
444 }
445 reply->type = msg->type;
446 reply->win = msg->win;
447 reply->msg = msg->msg;
448 reply->wparam = msg->wparam;
449 reply->lparam = msg->lparam;
450 reply->x = msg->x;
451 reply->y = msg->y;
452 reply->time = msg->time;
453 reply->info = msg->info;
454
455 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
456
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000457 unlink_message( &queue->msg_list[SEND_MESSAGE], msg );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000458 /* put the result on the receiver result stack */
Alexandre Julliardd253c582001-08-07 19:19:08 +0000459 if (result)
460 {
461 result->recv_next = queue->recv_result;
462 queue->recv_result = result;
463 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000464 free( msg );
Alexandre Julliardd253c582001-08-07 19:19:08 +0000465 if (!queue->msg_list[SEND_MESSAGE].first) clear_queue_bits( queue, QS_SENDMESSAGE );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000466}
467
468/* set the result of the current received message */
469static void reply_message( struct msg_queue *queue, unsigned int result,
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000470 unsigned int error, int remove, const void *data, size_t len )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000471{
472 struct message_result *res = queue->recv_result;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000473
474 if (remove)
475 {
476 queue->recv_result = res->recv_next;
477 res->receiver = NULL;
478 if (!res->sender) /* no one waiting for it */
479 {
Alexandre Julliardd253c582001-08-07 19:19:08 +0000480 free_result( res );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000481 return;
482 }
483 }
484 if (!res->replied)
485 {
Alexandre Julliardd253c582001-08-07 19:19:08 +0000486 if (len && (res->data = memdup( data, len ))) res->data_size = len;
487 store_message_result( res, result, error );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000488 }
489}
490
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000491/* empty a message list and free all the messages */
492static void empty_msg_list( struct message_list *list )
493{
494 struct message *msg = list->first;
495 while (msg)
496 {
497 struct message *next = msg->next;
498 free_message( msg );
499 msg = next;
500 }
501}
502
503/* cleanup all pending results when deleting a queue */
504static void cleanup_results( struct msg_queue *queue )
505{
506 struct message_result *result, *next;
507
508 result = queue->send_result;
509 while (result)
510 {
511 next = result->send_next;
512 result->sender = NULL;
Alexandre Julliardd253c582001-08-07 19:19:08 +0000513 if (!result->receiver) free_result( result );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000514 result = next;
515 }
516
Alexandre Julliardd253c582001-08-07 19:19:08 +0000517 while (queue->recv_result)
518 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000519}
520
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000521static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
522{
523 struct msg_queue *queue = (struct msg_queue *)obj;
524 struct process *process = entry->thread->process;
525
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000526 /* a thread can only wait on its own queue */
527 if (entry->thread->queue != queue)
528 {
529 set_error( STATUS_ACCESS_DENIED );
530 return 0;
531 }
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000532 /* if waiting on the main process queue, set the idle event */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000533 if (process->queue == queue)
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000534 {
535 if (process->idle_event) set_event( process->idle_event );
536 }
537 add_queue( obj, entry );
538 return 1;
539}
540
541static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
542{
543 struct msg_queue *queue = (struct msg_queue *)obj;
544 struct process *process = entry->thread->process;
545
546 remove_queue( obj, entry );
547
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000548 assert( entry->thread->queue == queue );
549
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000550 /* if waiting on the main process queue, reset the idle event */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000551 if (process->queue == queue)
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000552 {
553 if (process->idle_event) reset_event( process->idle_event );
554 }
555}
556
557static void msg_queue_dump( struct object *obj, int verbose )
558{
559 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000560 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
561 queue->wake_bits, queue->wake_mask );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000562}
563
564static int msg_queue_signaled( struct object *obj, struct thread *thread )
565{
566 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000567 return is_signaled( queue );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000568}
569
570static int msg_queue_satisfied( struct object *obj, struct thread *thread )
571{
572 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000573 queue->wake_mask = 0;
574 queue->changed_mask = 0;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000575 return 0; /* Not abandoned */
576}
577
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000578static void msg_queue_destroy( struct object *obj )
579{
580 struct msg_queue *queue = (struct msg_queue *)obj;
581 struct timer *timer = queue->first_timer;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000582 int i;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000583
584 cleanup_results( queue );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000585 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000586
587 while (timer)
588 {
589 struct timer *next = timer->next;
590 free( timer );
591 timer = next;
592 }
593 if (queue->timeout) remove_timeout_user( queue->timeout );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000594 if (queue->input) release_object( queue->input );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000595}
596
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000597static void thread_input_dump( struct object *obj, int verbose )
598{
599 struct thread_input *input = (struct thread_input *)obj;
600 fprintf( stderr, "Thread input focus=%x capture=%x active=%x\n",
601 input->focus, input->capture, input->active );
602}
603
604static void thread_input_destroy( struct object *obj )
605{
606 struct thread_input *input = (struct thread_input *)obj;
607
608 if (foreground_input == input) foreground_input = NULL;
609}
610
611/* fix the thread input data when a window is destroyed */
612inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
613{
614 struct thread_input *input = queue->input;
615
616 if (window == input->focus) input->focus = 0;
617 if (window == input->capture) input->capture = 0;
618 if (window == input->active) input->active = 0;
619 if (window == input->menu_owner) input->menu_owner = 0;
620 if (window == input->move_size) input->move_size = 0;
621 if (window == input->caret) input->caret = 0;
622}
623
624/* attach two thread input data structures */
625int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
626{
627 struct thread_input *input;
628
629 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
630 input = (struct thread_input *)grab_object( thread_to->queue->input );
631
632 if (thread_from->queue)
633 {
634 release_object( thread_from->queue->input );
635 thread_from->queue->input = input;
636 }
637 else
638 {
639 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
640 }
641 memset( input->keystate, 0, sizeof(input->keystate) );
642 return 1;
643}
644
645/* detach two thread input data structures */
646static void detach_thread_input( struct thread *thread_from, struct thread *thread_to )
647{
648 struct thread_input *input;
649
650 if (!thread_from->queue || !thread_to->queue ||
651 thread_from->queue->input != thread_to->queue->input)
652 {
653 set_error( STATUS_ACCESS_DENIED );
654 return;
655 }
656 if ((input = create_thread_input()))
657 {
658 release_object( thread_from->queue->input );
659 thread_from->queue->input = input;
660 }
661}
662
663
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000664/* set the next timer to expire */
665static void set_next_timer( struct msg_queue *queue, struct timer *timer )
666{
667 if (queue->timeout)
668 {
669 remove_timeout_user( queue->timeout );
670 queue->timeout = NULL;
671 }
672 if ((queue->next_timer = timer))
673 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
674
675 /* set/clear QS_TIMER bit */
676 if (queue->next_timer == queue->first_timer)
Alexandre Julliardd253c582001-08-07 19:19:08 +0000677 clear_queue_bits( queue, QS_TIMER );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000678 else
Alexandre Julliardd253c582001-08-07 19:19:08 +0000679 set_queue_bits( queue, QS_TIMER );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000680}
681
682/* callback for the next timer expiration */
683static void timer_callback( void *private )
684{
685 struct msg_queue *queue = private;
686
687 queue->timeout = NULL;
688 /* move on to the next timer */
689 set_next_timer( queue, queue->next_timer->next );
690}
691
692/* link a timer at its rightful place in the queue list */
693static void link_timer( struct msg_queue *queue, struct timer *timer )
694{
695 struct timer *pos = queue->next_timer;
696
697 while (pos && time_before( &pos->when, &timer->when )) pos = pos->next;
698
699 if (pos) /* insert before pos */
700 {
701 if ((timer->prev = pos->prev)) timer->prev->next = timer;
702 else queue->first_timer = timer;
703 timer->next = pos;
704 pos->prev = timer;
705 }
706 else /* insert at end */
707 {
708 timer->next = NULL;
709 timer->prev = queue->last_timer;
710 if (queue->last_timer) queue->last_timer->next = timer;
711 else queue->first_timer = timer;
712 queue->last_timer = timer;
713 }
714 /* check if we replaced the next timer */
715 if (pos == queue->next_timer) set_next_timer( queue, timer );
716}
717
718/* remove a timer from the queue timer list */
719static void unlink_timer( struct msg_queue *queue, struct timer *timer )
720{
721 if (timer->next) timer->next->prev = timer->prev;
722 else queue->last_timer = timer->prev;
723 if (timer->prev) timer->prev->next = timer->next;
724 else queue->first_timer = timer->next;
725 /* check if we removed the next timer */
726 if (queue->next_timer == timer) set_next_timer( queue, timer->next );
Alexandre Julliardd253c582001-08-07 19:19:08 +0000727 else if (queue->next_timer == queue->first_timer) clear_queue_bits( queue, QS_TIMER );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000728}
729
730/* restart an expired timer */
731static void restart_timer( struct msg_queue *queue, struct timer *timer )
732{
733 struct timeval now;
734 unlink_timer( queue, timer );
735 gettimeofday( &now, 0 );
736 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
737 link_timer( queue, timer );
738}
739
740/* find an expired timer matching the filtering parameters */
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000741static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000742 unsigned int get_first, unsigned int get_last,
743 int remove )
744{
745 struct timer *timer;
746 for (timer = queue->first_timer; (timer && timer != queue->next_timer); timer = timer->next)
747 {
748 if (win && timer->win != win) continue;
749 if (timer->msg >= get_first && timer->msg <= get_last)
750 {
751 if (remove) restart_timer( queue, timer );
752 return timer;
753 }
754 }
755 return NULL;
756}
757
758/* kill a timer */
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000759static int kill_timer( struct msg_queue *queue, user_handle_t win,
760 unsigned int msg, unsigned int id )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000761{
762 struct timer *timer;
763
764 for (timer = queue->first_timer; timer; timer = timer->next)
765 {
766 if (timer->win != win || timer->msg != msg || timer->id != id) continue;
767 unlink_timer( queue, timer );
768 free( timer );
769 return 1;
770 }
771 return 0;
772}
773
774/* add a timer */
775static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
776{
777 struct timer *timer = mem_alloc( sizeof(*timer) );
778 if (timer)
779 {
780 timer->rate = rate;
781 gettimeofday( &timer->when, 0 );
782 add_timeout( &timer->when, rate );
783 link_timer( queue, timer );
784 }
785 return timer;
786}
787
Alexandre Julliard805bdc52001-11-13 22:23:48 +0000788
789/* increment (or decrement if 'incr' is negative) the queue paint count */
790void inc_queue_paint_count( struct thread *thread, int incr )
791{
792 struct msg_queue *queue = thread->queue;
793
794 assert( queue );
795
796 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
797
798 if (queue->paint_count)
799 set_queue_bits( queue, QS_PAINT );
800 else
801 clear_queue_bits( queue, QS_PAINT );
802}
803
804
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000805/* remove all messages and timers belonging to a certain window */
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000806void queue_cleanup_window( struct thread *thread, user_handle_t win )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000807{
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000808 struct msg_queue *queue = thread->queue;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000809 struct timer *timer;
810 struct message *msg;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000811 int i;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000812
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000813 if (!queue) return;
814
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000815 /* remove timers */
816 timer = queue->first_timer;
817 while (timer)
818 {
819 struct timer *next = timer->next;
820 if (timer->win == win)
821 {
822 unlink_timer( queue, timer );
823 free( timer );
824 }
825 timer = next;
826 }
827
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000828 /* remove messages */
829 for (i = 0; i < NB_MSG_KINDS; i++)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000830 {
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000831 msg = queue->msg_list[i].first;
832 while (msg)
833 {
834 struct message *next = msg->next;
835 if (msg->win == win) remove_queue_message( queue, msg, i );
836 msg = next;
837 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000838 }
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000839
840 thread_input_cleanup_window( queue, win );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000841}
842
Alexandre Julliard81f2a732002-03-23 20:43:52 +0000843/* post a message to a window; used by socket handling */
844void post_message( user_handle_t win, unsigned int message,
845 unsigned int wparam, unsigned int lparam )
846{
847 struct message *msg;
848 struct thread *thread = get_window_thread( win );
849
850 if (!thread) return;
851
852 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
853 {
854 msg->type = MSG_POSTED;
855 msg->win = get_user_full_handle( win );
856 msg->msg = message;
857 msg->wparam = wparam;
858 msg->lparam = lparam;
859 msg->time = get_tick_count();
860 msg->x = 0;
861 msg->y = 0;
862 msg->info = 0;
863 msg->result = NULL;
864 msg->data = NULL;
865 msg->data_size = 0;
866
867 append_message( &thread->queue->msg_list[POST_MESSAGE], msg );
868 set_queue_bits( thread->queue, QS_POSTMESSAGE );
869 }
870 release_object( thread );
871}
872
873
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000874/* get the message queue of the current thread */
875DECL_HANDLER(get_msg_queue)
876{
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000877 struct msg_queue *queue = get_current_queue();
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000878
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000879 reply->handle = 0;
880 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000881}
882
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000883
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000884/* set the current message queue wakeup mask */
885DECL_HANDLER(set_queue_mask)
886{
887 struct msg_queue *queue = get_current_queue();
888
889 if (queue)
890 {
891 queue->wake_mask = req->wake_mask;
892 queue->changed_mask = req->changed_mask;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000893 reply->wake_bits = queue->wake_bits;
894 reply->changed_bits = queue->changed_bits;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000895 if (is_signaled( queue ))
896 {
897 /* if skip wait is set, do what would have been done in the subsequent wait */
898 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
899 else wake_up( &queue->obj, 0 );
900 }
901 }
902}
903
904
905/* get the current message queue status */
906DECL_HANDLER(get_queue_status)
907{
908 struct msg_queue *queue = current->queue;
909 if (queue)
910 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000911 reply->wake_bits = queue->wake_bits;
912 reply->changed_bits = queue->changed_bits;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000913 if (req->clear) queue->changed_bits = 0;
914 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000915 else reply->wake_bits = reply->changed_bits = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000916}
917
918
919/* send a message to a thread queue */
920DECL_HANDLER(send_message)
921{
922 struct message *msg;
923 struct msg_queue *send_queue = get_current_queue();
924 struct msg_queue *recv_queue;
925 struct thread *thread = get_thread_from_id( req->id );
926
927 if (!thread) return;
928
929 if (!(recv_queue = thread->queue))
930 {
931 set_error( STATUS_INVALID_PARAMETER );
932 release_object( thread );
933 return;
934 }
935
936 if ((msg = mem_alloc( sizeof(*msg) )))
937 {
Alexandre Julliardd253c582001-08-07 19:19:08 +0000938 msg->type = req->type;
Alexandre Julliardbc878ef2001-09-12 17:09:24 +0000939 msg->win = get_user_full_handle( req->win );
Alexandre Julliardd253c582001-08-07 19:19:08 +0000940 msg->msg = req->msg;
941 msg->wparam = req->wparam;
942 msg->lparam = req->lparam;
943 msg->time = req->time;
944 msg->x = req->x;
945 msg->y = req->y;
946 msg->info = req->info;
947 msg->result = NULL;
948 msg->data = NULL;
949 msg->data_size = 0;
950
951 switch(msg->type)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000952 {
Eric Pouech0faceb02002-01-18 19:22:55 +0000953 case MSG_OTHER_PROCESS:
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000954 msg->data_size = get_req_data_size();
955 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000956 {
957 free( msg );
Alexandre Julliardd253c582001-08-07 19:19:08 +0000958 break;
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000959 }
Alexandre Julliardd253c582001-08-07 19:19:08 +0000960 /* fall through */
961 case MSG_ASCII:
962 case MSG_UNICODE:
963 case MSG_CALLBACK:
964 if (!(msg->result = alloc_message_result( send_queue, recv_queue, req->timeout )))
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000965 {
Alexandre Julliardd253c582001-08-07 19:19:08 +0000966 free( msg );
967 break;
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000968 }
Alexandre Julliardd253c582001-08-07 19:19:08 +0000969 /* fall through */
970 case MSG_NOTIFY:
971 append_message( &recv_queue->msg_list[SEND_MESSAGE], msg );
972 set_queue_bits( recv_queue, QS_SENDMESSAGE );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000973 break;
Alexandre Julliardd253c582001-08-07 19:19:08 +0000974 case MSG_POSTED:
Eric Pouech0faceb02002-01-18 19:22:55 +0000975 /* needed for posted DDE messages */
976 msg->data_size = get_req_data_size();
977 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
978 {
979 free( msg );
980 break;
981 }
Alexandre Julliardd253c582001-08-07 19:19:08 +0000982 append_message( &recv_queue->msg_list[POST_MESSAGE], msg );
983 set_queue_bits( recv_queue, QS_POSTMESSAGE );
984 break;
985 case MSG_HARDWARE_RAW:
986 case MSG_HARDWARE_COOKED:
987 {
988 struct message_list *list = ((msg->type == MSG_HARDWARE_RAW) ?
989 &recv_queue->msg_list[RAW_HW_MESSAGE] :
990 &recv_queue->msg_list[COOKED_HW_MESSAGE]);
991 if (msg->msg == WM_MOUSEMOVE && merge_message( list, msg ))
992 {
993 free( msg );
994 break;
995 }
996 append_message( list, msg );
997 set_queue_bits( recv_queue, get_hardware_msg_bit(msg) );
998 break;
999 }
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001000 default:
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001001 set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001002 free( msg );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001003 break;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001004 }
1005 }
1006 release_object( thread );
1007}
1008
Alexandre Julliard9f55ae62001-06-28 04:37:22 +00001009/* return a message to the application, removing it from the queue if needed */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001010static void return_message_to_app( struct msg_queue *queue, int flags,
1011 struct get_message_reply *reply,
Alexandre Julliard9f55ae62001-06-28 04:37:22 +00001012 struct message *msg, enum message_kind kind )
1013{
Eric Pouech0faceb02002-01-18 19:22:55 +00001014 reply->total = msg->data_size;
1015 if (msg->data_size > get_reply_max_size())
1016 {
1017 set_error( STATUS_BUFFER_OVERFLOW );
1018 return;
1019 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001020 reply->type = msg->type;
1021 reply->win = msg->win;
1022 reply->msg = msg->msg;
1023 reply->wparam = msg->wparam;
1024 reply->lparam = msg->lparam;
1025 reply->x = msg->x;
1026 reply->y = msg->y;
1027 reply->time = msg->time;
1028 reply->info = msg->info;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001029
Alexandre Julliard9f55ae62001-06-28 04:37:22 +00001030 /* raw messages always get removed */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001031 if ((msg->type == MSG_HARDWARE_RAW) || (flags & GET_MSG_REMOVE))
Alexandre Julliard9f55ae62001-06-28 04:37:22 +00001032 {
1033 queue->last_msg = NULL;
Eric Pouech0faceb02002-01-18 19:22:55 +00001034 if (msg->data)
1035 {
1036 set_reply_data_ptr( msg->data, msg->data_size );
1037 msg->data = NULL;
1038 msg->data_size = 0;
1039 }
Alexandre Julliard9f55ae62001-06-28 04:37:22 +00001040 remove_queue_message( queue, msg, kind );
1041 }
1042 else /* remember it as the last returned message */
1043 {
Eric Pouech0faceb02002-01-18 19:22:55 +00001044 if (msg->data) set_reply_data( msg->data, msg->data_size );
Alexandre Julliard9f55ae62001-06-28 04:37:22 +00001045 queue->last_msg = msg;
1046 queue->last_msg_kind = kind;
1047 }
1048}
1049
1050
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001051inline static struct message *find_matching_message( const struct message_list *list,
1052 user_handle_t win,
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001053 unsigned int first, unsigned int last )
1054{
1055 struct message *msg;
1056
1057 for (msg = list->first; msg; msg = msg->next)
1058 {
1059 /* check against the filters */
Alexandre Julliard9f55ae62001-06-28 04:37:22 +00001060 if (msg->msg == WM_QUIT) break; /* WM_QUIT is never filtered */
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001061 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001062 if (msg->msg < first) continue;
1063 if (msg->msg > last) continue;
1064 break; /* found one */
1065 }
1066 return msg;
1067}
1068
1069
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001070/* get a message from the current queue */
1071DECL_HANDLER(get_message)
1072{
1073 struct timer *timer;
1074 struct message *msg;
1075 struct msg_queue *queue = get_current_queue();
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001076 user_handle_t get_win = get_user_full_handle( req->get_win );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001077
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001078 if (!queue) return;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001079
1080 /* first check for sent messages */
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001081 if ((msg = queue->msg_list[SEND_MESSAGE].first))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001082 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001083 receive_message( queue, msg, reply );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001084 return;
1085 }
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001086 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001087
Alexandre Julliard9f55ae62001-06-28 04:37:22 +00001088 /* if requested, remove the last returned but not yet removed message */
1089 if ((req->flags & GET_MSG_REMOVE_LAST) && queue->last_msg)
1090 remove_queue_message( queue, queue->last_msg, queue->last_msg_kind );
1091 queue->last_msg = NULL;
1092
1093 /* clear changed bits so we can wait on them if we don't find a message */
1094 queue->changed_bits = 0;
1095
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001096 /* then check for posted messages */
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001097 if ((msg = find_matching_message( &queue->msg_list[POST_MESSAGE], get_win,
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001098 req->get_first, req->get_last )))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001099 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001100 return_message_to_app( queue, req->flags, reply, msg, POST_MESSAGE );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001101 return;
1102 }
1103
1104 /* then check for cooked hardware messages */
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001105 if ((msg = find_matching_message( &queue->msg_list[COOKED_HW_MESSAGE], get_win,
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001106 req->get_first, req->get_last )))
1107 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001108 return_message_to_app( queue, req->flags, reply, msg, COOKED_HW_MESSAGE );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001109 return;
1110 }
1111
1112 /* then check for any raw hardware message */
1113 if ((msg = queue->msg_list[RAW_HW_MESSAGE].first))
1114 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001115 return_message_to_app( queue, req->flags, reply, msg, RAW_HW_MESSAGE );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001116 return;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001117 }
1118
1119 /* now check for WM_PAINT */
Alexandre Julliard47f98212001-11-14 21:28:36 +00001120 if (queue->paint_count &&
1121 (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) &&
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001122 (reply->win = find_window_to_repaint( get_win, current )))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001123 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001124 reply->type = MSG_POSTED;
1125 reply->msg = WM_PAINT;
1126 reply->wparam = 0;
1127 reply->lparam = 0;
1128 reply->x = 0;
1129 reply->y = 0;
1130 reply->time = get_tick_count();
1131 reply->info = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001132 return;
1133 }
1134
1135 /* now check for timer */
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001136 if ((timer = find_expired_timer( queue, get_win, req->get_first,
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001137 req->get_last, (req->flags & GET_MSG_REMOVE) )))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001138 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001139 reply->type = MSG_POSTED;
1140 reply->win = timer->win;
1141 reply->msg = timer->msg;
1142 reply->wparam = timer->id;
1143 reply->lparam = timer->lparam;
1144 reply->x = 0;
1145 reply->y = 0;
1146 reply->time = get_tick_count();
1147 reply->info = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001148 return;
1149 }
1150
1151 done:
1152 set_error( STATUS_PENDING ); /* FIXME */
1153}
1154
1155
1156/* reply to a sent message */
1157DECL_HANDLER(reply_message)
1158{
Eric Pouech476c2b42001-05-19 17:38:21 +00001159 if (current->queue && current->queue->recv_result)
Alexandre Julliardd253c582001-08-07 19:19:08 +00001160 reply_message( current->queue, req->result, 0, req->remove,
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001161 get_req_data(), get_req_data_size() );
Eric Pouech476c2b42001-05-19 17:38:21 +00001162 else
1163 set_error( STATUS_ACCESS_DENIED );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001164}
1165
1166
1167/* retrieve the reply for the last message sent */
1168DECL_HANDLER(get_message_reply)
1169{
Alexandre Julliardd253c582001-08-07 19:19:08 +00001170 struct msg_queue *queue = current->queue;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001171
Alexandre Julliardd253c582001-08-07 19:19:08 +00001172 if (queue)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001173 {
Alexandre Julliardd253c582001-08-07 19:19:08 +00001174 struct message_result *result = queue->send_result;
1175
1176 set_error( STATUS_PENDING );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001177 reply->result = 0;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001178
1179 if (result && (result->replied || req->cancel))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001180 {
Alexandre Julliardd253c582001-08-07 19:19:08 +00001181 if (result->replied)
1182 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001183 reply->result = result->result;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001184 set_error( result->error );
1185 if (result->data)
1186 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001187 size_t data_len = min( result->data_size, get_reply_max_size() );
1188 set_reply_data_ptr( result->data, data_len );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001189 result->data = NULL;
1190 result->data_size = 0;
1191 }
1192 }
1193 queue->send_result = result->send_next;
1194 result->sender = NULL;
1195 if (!result->receiver) free_result( result );
1196 if (!queue->send_result || !queue->send_result->replied)
1197 clear_queue_bits( queue, QS_SMRESULT );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001198 }
1199 }
Alexandre Julliardd253c582001-08-07 19:19:08 +00001200 else set_error( STATUS_ACCESS_DENIED );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001201}
1202
1203
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001204/* set a window timer */
1205DECL_HANDLER(set_win_timer)
1206{
1207 struct timer *timer;
1208 struct msg_queue *queue = get_current_queue();
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001209 user_handle_t win = get_user_full_handle( req->win );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001210
1211 if (!queue) return;
1212
1213 /* remove it if it existed already */
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001214 if (win) kill_timer( queue, win, req->msg, req->id );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001215
1216 if ((timer = set_timer( queue, req->rate )))
1217 {
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001218 timer->win = win;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001219 timer->msg = req->msg;
1220 timer->id = req->id;
1221 timer->lparam = req->lparam;
1222 }
1223}
1224
1225/* kill a window timer */
1226DECL_HANDLER(kill_win_timer)
1227{
1228 struct msg_queue *queue = current->queue;
1229
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001230 if (!queue || !kill_timer( queue, get_user_full_handle(req->win), req->msg, req->id ))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001231 set_error( STATUS_INVALID_PARAMETER );
1232}
Alexandre Julliardab5063b2002-10-11 18:50:15 +00001233
1234
1235/* attach (or detach) thread inputs */
1236DECL_HANDLER(attach_thread_input)
1237{
1238 struct thread *thread_from = get_thread_from_id( req->tid_from );
1239 struct thread *thread_to = get_thread_from_id( req->tid_to );
1240
1241 if (!thread_from || !thread_to)
1242 {
1243 if (thread_from) release_object( thread_from );
1244 if (thread_to) release_object( thread_to );
1245 return;
1246 }
1247 if (thread_from != thread_to)
1248 {
1249 if (req->attach) attach_thread_input( thread_from, thread_to );
1250 else detach_thread_input( thread_from, thread_to );
1251 }
1252 else set_error( STATUS_ACCESS_DENIED );
1253 release_object( thread_from );
1254 release_object( thread_to );
1255}
1256
1257
1258/* get thread input data */
1259DECL_HANDLER(get_thread_input)
1260{
1261 struct thread *thread = NULL;
1262 struct thread_input *input;
1263
1264 if (req->tid)
1265 {
1266 if (!(thread = get_thread_from_id( req->tid ))) return;
1267 input = thread->queue ? thread->queue->input : NULL;
1268 }
1269 else input = foreground_input; /* get the foreground thread info */
1270
1271 if (input)
1272 {
1273 reply->focus = input->focus;
1274 reply->capture = input->capture;
1275 reply->active = input->active;
1276 reply->menu_owner = input->menu_owner;
1277 reply->move_size = input->move_size;
1278 reply->caret = input->caret;
1279 reply->rect = input->rect;
1280 }
1281 else
1282 {
1283 reply->focus = 0;
1284 reply->capture = 0;
1285 reply->active = 0;
1286 reply->menu_owner = 0;
1287 reply->move_size = 0;
1288 reply->caret = 0;
1289 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1290 }
1291 /* foreground window is active window of foreground thread */
1292 reply->foreground = foreground_input ? foreground_input->active : 0;
1293 if (thread) release_object( thread );
1294}