blob: 4868a23310fa0d738a015c64fa7e2b262737d7ce [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>
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
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000029#include "windef.h"
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000030#include "winbase.h"
31#include "wingdi.h"
32#include "winuser.h"
33
Alexandre Julliardc5e433a2000-05-30 19:48:18 +000034#include "handle.h"
Alexandre Julliarde66207e2003-02-19 00:33:32 +000035#include "file.h"
Alexandre Julliardc5e433a2000-05-30 19:48:18 +000036#include "thread.h"
37#include "process.h"
38#include "request.h"
Alexandre Julliard1a66d222001-08-28 18:44:52 +000039#include "user.h"
Alexandre Julliardc5e433a2000-05-30 19:48:18 +000040
Alexandre Julliard242e3952003-01-08 00:27:58 +000041enum message_kind { SEND_MESSAGE, POST_MESSAGE };
42#define NB_MSG_KINDS (POST_MESSAGE+1)
Alexandre Julliardd253c582001-08-07 19:19:08 +000043
44
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000045struct message_result
46{
Alexandre Julliard039e1312003-07-26 20:36:43 +000047 struct list sender_entry; /* entry in sender list */
48 struct message_result *recv_next; /* next in receiver list */
49 struct msg_queue *sender; /* sender queue */
50 struct msg_queue *receiver; /* receiver queue */
51 int replied; /* has it been replied to? */
52 unsigned int result; /* reply result */
53 unsigned int error; /* error code to pass back to sender */
54 struct message *callback_msg; /* message to queue for callback */
55 void *data; /* message reply data */
56 unsigned int data_size; /* size of message reply data */
57 struct timeout_user *timeout; /* result timeout */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000058};
59
60struct message
61{
62 struct message *next; /* next message in list */
63 struct message *prev; /* prev message in list */
Alexandre Julliardd253c582001-08-07 19:19:08 +000064 enum message_type type; /* message type */
Alexandre Julliard1a66d222001-08-28 18:44:52 +000065 user_handle_t win; /* window handle */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000066 unsigned int msg; /* message code */
67 unsigned int wparam; /* parameters */
68 unsigned int lparam; /* parameters */
Alexandre Julliardd253c582001-08-07 19:19:08 +000069 int x; /* x position */
70 int y; /* y position */
Alexandre Julliard838d65a2001-06-19 19:16:41 +000071 unsigned int time; /* message time */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000072 unsigned int info; /* extra info */
Alexandre Julliardd253c582001-08-07 19:19:08 +000073 void *data; /* message data for sent messages */
74 unsigned int data_size; /* size of message data */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000075 struct message_result *result; /* result in sender queue */
76};
77
78struct message_list
79{
80 struct message *first; /* head of list */
81 struct message *last; /* tail of list */
82};
83
84struct timer
85{
86 struct timer *next; /* next timer in list */
87 struct timer *prev; /* prev timer in list */
88 struct timeval when; /* next expiration */
89 unsigned int rate; /* timer rate in ms */
Alexandre Julliard1a66d222001-08-28 18:44:52 +000090 user_handle_t win; /* window handle */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +000091 unsigned int msg; /* message to post */
92 unsigned int id; /* timer id */
93 unsigned int lparam; /* lparam for message */
94};
95
Alexandre Julliardab5063b2002-10-11 18:50:15 +000096struct thread_input
97{
98 struct object obj; /* object header */
99 user_handle_t focus; /* focus window */
100 user_handle_t capture; /* capture window */
101 user_handle_t active; /* active window */
102 user_handle_t menu_owner; /* current menu owner window */
103 user_handle_t move_size; /* current moving/resizing window */
104 user_handle_t caret; /* caret window */
Alexandre Julliard11e35232002-10-17 01:24:33 +0000105 rectangle_t caret_rect; /* caret rectangle */
106 int caret_hide; /* caret hide count */
107 int caret_state; /* caret on/off state */
Alexandre Julliard242e3952003-01-08 00:27:58 +0000108 struct message *msg; /* message currently processed */
109 struct thread *msg_thread; /* thread processing the message */
110 struct message_list msg_list; /* list of hardware messages */
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000111 unsigned char keystate[256]; /* state of each key */
112};
113
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000114struct msg_queue
115{
Alexandre Julliard039e1312003-07-26 20:36:43 +0000116 struct object obj; /* object header */
117 unsigned int wake_bits; /* wakeup bits */
118 unsigned int wake_mask; /* wakeup mask */
119 unsigned int changed_bits; /* changed wakeup bits */
120 unsigned int changed_mask; /* changed wakeup mask */
121 int paint_count; /* pending paint messages count */
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000122 struct message_list msg_list[NB_MSG_KINDS]; /* lists of messages */
Alexandre Julliard039e1312003-07-26 20:36:43 +0000123 struct list send_result; /* stack of sent messages waiting for result */
124 struct list callback_result; /* list of callback messages waiting for result */
125 struct message_result *recv_result; /* stack of received messages waiting for result */
126 struct timer *first_timer; /* head of timer list */
127 struct timer *last_timer; /* tail of timer list */
128 struct timer *next_timer; /* next timer to expire */
129 struct timeout_user *timeout; /* timeout for next timer to expire */
130 struct thread_input *input; /* thread input descriptor */
131 struct hook_table *hooks; /* hook table */
132 struct timeval last_get_msg; /* time of last get message call */
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000133};
134
135static void msg_queue_dump( struct object *obj, int verbose );
136static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
137static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
138static int msg_queue_signaled( struct object *obj, struct thread *thread );
139static int msg_queue_satisfied( struct object *obj, struct thread *thread );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000140static void msg_queue_destroy( struct object *obj );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000141static void thread_input_dump( struct object *obj, int verbose );
142static void thread_input_destroy( struct object *obj );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000143static void timer_callback( void *private );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000144
145static const struct object_ops msg_queue_ops =
146{
147 sizeof(struct msg_queue), /* size */
148 msg_queue_dump, /* dump */
149 msg_queue_add_queue, /* add_queue */
150 msg_queue_remove_queue, /* remove_queue */
151 msg_queue_signaled, /* signaled */
152 msg_queue_satisfied, /* satisfied */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +0000153 no_get_fd, /* get_fd */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000154 msg_queue_destroy /* destroy */
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000155};
156
157
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000158static const struct object_ops thread_input_ops =
159{
160 sizeof(struct thread_input), /* size */
161 thread_input_dump, /* dump */
162 no_add_queue, /* add_queue */
163 NULL, /* remove_queue */
164 NULL, /* signaled */
165 NULL, /* satisfied */
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000166 no_get_fd, /* get_fd */
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000167 thread_input_destroy /* destroy */
168};
169
Alexandre Julliard242e3952003-01-08 00:27:58 +0000170/* pointer to input structure of foreground thread */
171static struct thread_input *foreground_input;
172
Alexandre Julliard11e35232002-10-17 01:24:33 +0000173
174/* set the caret window in a given thread input */
175static void set_caret_window( struct thread_input *input, user_handle_t win )
176{
177 input->caret = win;
178 input->caret_rect.left = 0;
179 input->caret_rect.top = 0;
180 input->caret_rect.right = 0;
181 input->caret_rect.bottom = 0;
182 input->caret_hide = 1;
183 input->caret_state = 0;
184}
185
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000186/* create a thread input object */
187static struct thread_input *create_thread_input(void)
188{
189 struct thread_input *input;
190
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000191 if ((input = alloc_object( &thread_input_ops )))
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000192 {
193 input->focus = 0;
194 input->capture = 0;
195 input->active = 0;
196 input->menu_owner = 0;
197 input->move_size = 0;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000198 input->msg = NULL;
199 input->msg_thread = NULL;
200 input->msg_list.first = input->msg_list.last = NULL;
Alexandre Julliard11e35232002-10-17 01:24:33 +0000201 set_caret_window( input, 0 );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000202 memset( input->keystate, 0, sizeof(input->keystate) );
203 }
204 return input;
205}
206
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000207/* create a message queue object */
208static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000209{
210 struct msg_queue *queue;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000211 int i;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000212
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000213 if (!input && !(input = create_thread_input())) return NULL;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000214 if ((queue = alloc_object( &msg_queue_ops )))
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000215 {
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000216 queue->wake_bits = 0;
217 queue->wake_mask = 0;
218 queue->changed_bits = 0;
219 queue->changed_mask = 0;
Alexandre Julliard4b0343d2001-06-20 22:55:31 +0000220 queue->paint_count = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000221 queue->recv_result = NULL;
222 queue->first_timer = NULL;
223 queue->last_timer = NULL;
224 queue->next_timer = NULL;
225 queue->timeout = NULL;
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000226 queue->input = (struct thread_input *)grab_object( input );
Alexandre Julliardd55e7f12003-07-03 18:16:48 +0000227 queue->hooks = NULL;
Alexandre Julliard09029b22003-07-11 04:09:42 +0000228 gettimeofday( &queue->last_get_msg, NULL );
Alexandre Julliard039e1312003-07-26 20:36:43 +0000229 list_init( &queue->send_result );
230 list_init( &queue->callback_result );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000231 for (i = 0; i < NB_MSG_KINDS; i++)
232 queue->msg_list[i].first = queue->msg_list[i].last = NULL;
233
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000234 thread->queue = queue;
235 if (!thread->process->queue)
236 thread->process->queue = (struct msg_queue *)grab_object( queue );
237 }
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000238 release_object( input );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000239 return queue;
240}
241
Alexandre Julliard31022d62002-08-16 23:30:41 +0000242/* free the message queue of a thread at thread exit */
243void free_msg_queue( struct thread *thread )
244{
245 struct process *process = thread->process;
246
Alexandre Julliardca3ac8f2003-07-11 21:55:58 +0000247 remove_thread_hooks( thread );
Alexandre Julliard31022d62002-08-16 23:30:41 +0000248 if (!thread->queue) return;
249 if (process->queue == thread->queue) /* is it the process main queue? */
250 {
251 release_object( process->queue );
252 process->queue = NULL;
253 if (process->idle_event)
254 {
255 set_event( process->idle_event );
256 release_object( process->idle_event );
257 process->idle_event = NULL;
258 }
259 }
260 release_object( thread->queue );
261 thread->queue = NULL;
262}
263
Alexandre Julliardd55e7f12003-07-03 18:16:48 +0000264/* get the hook table for a given thread */
265struct hook_table *get_queue_hooks( struct thread *thread )
266{
267 if (!thread->queue) return NULL;
268 return thread->queue->hooks;
269}
270
271/* set the hook table for a given thread, allocating the queue if needed */
272void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
273{
274 struct msg_queue *queue = thread->queue;
275 if (!queue) queue = create_msg_queue( thread, NULL );
276 if (queue->hooks) release_object( queue->hooks );
277 queue->hooks = hooks;
278}
279
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000280/* check the queue status */
281inline static int is_signaled( struct msg_queue *queue )
282{
283 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
284}
285
Alexandre Julliardd253c582001-08-07 19:19:08 +0000286/* set some queue bits */
287inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000288{
Alexandre Julliardd253c582001-08-07 19:19:08 +0000289 queue->wake_bits |= bits;
290 queue->changed_bits |= bits;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000291 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
292}
293
Alexandre Julliardd253c582001-08-07 19:19:08 +0000294/* clear some queue bits */
295inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
296{
297 queue->wake_bits &= ~bits;
298 queue->changed_bits &= ~bits;
299}
300
Alexandre Julliard242e3952003-01-08 00:27:58 +0000301/* check whether msg is a keyboard message */
302inline static int is_keyboard_msg( struct message *msg )
303{
304 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
305}
306
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000307/* get the QS_* bit corresponding to a given hardware message */
308inline static int get_hardware_msg_bit( struct message *msg )
309{
310 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000311 if (is_keyboard_msg( msg )) return QS_KEY;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000312 return QS_MOUSEBUTTON;
313}
314
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000315/* get the current thread queue, creating it if needed */
Alexandre Julliard805bdc52001-11-13 22:23:48 +0000316inline static struct msg_queue *get_current_queue(void)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000317{
318 struct msg_queue *queue = current->queue;
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000319 if (!queue) queue = create_msg_queue( current, NULL );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000320 return queue;
321}
322
323/* append a message to the end of a list */
324inline static void append_message( struct message_list *list, struct message *msg )
325{
326 msg->next = NULL;
327 if ((msg->prev = list->last)) msg->prev->next = msg;
328 else list->first = msg;
329 list->last = msg;
330}
331
332/* unlink a message from a list it */
333inline static void unlink_message( struct message_list *list, struct message *msg )
334{
335 if (msg->next) msg->next->prev = msg->prev;
336 else list->last = msg->prev;
337 if (msg->prev) msg->prev->next = msg->next;
338 else list->first = msg->next;
339}
340
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000341/* try to merge a message with the last in the list; return 1 if successful */
Alexandre Julliard242e3952003-01-08 00:27:58 +0000342static int merge_message( struct thread_input *input, const struct message *msg )
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000343{
Alexandre Julliard242e3952003-01-08 00:27:58 +0000344 struct message *prev = input->msg_list.last;
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000345
346 if (!prev) return 0;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000347 if (input->msg == prev) return 0;
Alexandre Julliarde630aa02001-07-11 17:29:01 +0000348 if (prev->result) return 0;
349 if (prev->win != msg->win) return 0;
350 if (prev->msg != msg->msg) return 0;
351 if (prev->type != msg->type) return 0;
352 /* now we can merge it */
353 prev->wparam = msg->wparam;
354 prev->lparam = msg->lparam;
355 prev->x = msg->x;
356 prev->y = msg->y;
357 prev->time = msg->time;
358 prev->info = msg->info;
359 return 1;
360}
361
Alexandre Julliardd253c582001-08-07 19:19:08 +0000362/* free a result structure */
363static void free_result( struct message_result *result )
364{
365 if (result->timeout) remove_timeout_user( result->timeout );
366 if (result->data) free( result->data );
Alexandre Julliard039e1312003-07-26 20:36:43 +0000367 if (result->callback_msg) free( result->callback_msg );
Alexandre Julliardd253c582001-08-07 19:19:08 +0000368 free( result );
369}
370
Alexandre Julliard039e1312003-07-26 20:36:43 +0000371/* remove the result from the sender list it is on */
372static inline void remove_result_from_sender( struct message_result *result )
373{
374 assert( result->sender );
375
376 list_remove( &result->sender_entry );
377 result->sender = NULL;
378 if (!result->receiver) free_result( result );
379}
380
Alexandre Julliardd253c582001-08-07 19:19:08 +0000381/* store the message result in the appropriate structure */
382static void store_message_result( struct message_result *res, unsigned int result,
383 unsigned int error )
384{
385 res->result = result;
386 res->error = error;
387 res->replied = 1;
388 if (res->timeout)
389 {
390 remove_timeout_user( res->timeout );
391 res->timeout = NULL;
392 }
Alexandre Julliard039e1312003-07-26 20:36:43 +0000393 if (res->sender)
394 {
395 if (res->callback_msg)
396 {
397 /* queue the callback message in the sender queue */
398 res->callback_msg->lparam = result;
399 append_message( &res->sender->msg_list[SEND_MESSAGE], res->callback_msg );
400 set_queue_bits( res->sender, QS_SENDMESSAGE );
401 res->callback_msg = NULL;
402 remove_result_from_sender( res );
403 }
404 else
405 {
406 /* wake sender queue if waiting on this result */
407 if (list_head(&res->sender->send_result) == &res->sender_entry)
408 set_queue_bits( res->sender, QS_SMRESULT );
409 }
410 }
411
Alexandre Julliardd253c582001-08-07 19:19:08 +0000412}
413
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000414/* free a message when deleting a queue or window */
415static void free_message( struct message *msg )
416{
417 struct message_result *result = msg->result;
418 if (result)
419 {
420 if (result->sender)
421 {
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000422 result->receiver = NULL;
Alexandre Julliardd253c582001-08-07 19:19:08 +0000423 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000424 }
Alexandre Julliardd253c582001-08-07 19:19:08 +0000425 else free_result( result );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000426 }
Alexandre Julliardd253c582001-08-07 19:19:08 +0000427 if (msg->data) free( msg->data );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000428 free( msg );
429}
430
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000431/* remove (and free) a message from a message list */
432static void remove_queue_message( struct msg_queue *queue, struct message *msg,
433 enum message_kind kind )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000434{
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000435 unlink_message( &queue->msg_list[kind], msg );
436 switch(kind)
437 {
438 case SEND_MESSAGE:
Alexandre Julliardd253c582001-08-07 19:19:08 +0000439 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_SENDMESSAGE );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000440 break;
441 case POST_MESSAGE:
Alexandre Julliardd253c582001-08-07 19:19:08 +0000442 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_POSTMESSAGE );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000443 break;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000444 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000445 free_message( msg );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000446}
447
Alexandre Julliardd253c582001-08-07 19:19:08 +0000448/* message timed out without getting a reply */
449static void result_timeout( void *private )
450{
451 struct message_result *result = private;
452
453 assert( !result->replied );
454
455 result->timeout = NULL;
456 store_message_result( result, 0, STATUS_TIMEOUT );
457}
458
459/* allocate and fill a message result structure */
460static struct message_result *alloc_message_result( struct msg_queue *send_queue,
461 struct msg_queue *recv_queue,
Alexandre Julliard039e1312003-07-26 20:36:43 +0000462 struct message *msg, unsigned int timeout,
463 void *callback, unsigned int callback_data )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000464{
465 struct message_result *result = mem_alloc( sizeof(*result) );
Alexandre Julliardd253c582001-08-07 19:19:08 +0000466 if (result)
467 {
Alexandre Julliardd253c582001-08-07 19:19:08 +0000468 result->sender = send_queue;
469 result->receiver = recv_queue;
470 result->replied = 0;
471 result->data = NULL;
472 result->data_size = 0;
473 result->timeout = NULL;
Alexandre Julliard039e1312003-07-26 20:36:43 +0000474
475 if (msg->type == MSG_CALLBACK)
476 {
477 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
478 if (!callback_msg)
479 {
480 free( result );
481 return NULL;
482 }
483 callback_msg->type = MSG_CALLBACK_RESULT;
484 callback_msg->win = msg->win;
485 callback_msg->msg = msg->msg;
486 callback_msg->wparam = (unsigned int)callback;
487 callback_msg->lparam = 0;
488 callback_msg->time = get_tick_count();
489 callback_msg->x = 0;
490 callback_msg->y = 0;
491 callback_msg->info = callback_data;
492 callback_msg->result = NULL;
493 callback_msg->data = NULL;
494 callback_msg->data_size = 0;
495
496 result->callback_msg = callback_msg;
497 list_add_head( &send_queue->callback_result, &result->sender_entry );
498 }
499 else
500 {
501 result->callback_msg = NULL;
502 list_add_head( &send_queue->send_result, &result->sender_entry );
503 }
504
Alexandre Julliardd253c582001-08-07 19:19:08 +0000505 if (timeout != -1)
506 {
507 struct timeval when;
508 gettimeofday( &when, 0 );
509 add_timeout( &when, timeout );
510 result->timeout = add_timeout_user( &when, result_timeout, result );
511 }
512 }
513 return result;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000514}
515
516/* receive a message, removing it from the sent queue */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000517static void receive_message( struct msg_queue *queue, struct message *msg,
518 struct get_message_reply *reply )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000519{
520 struct message_result *result = msg->result;
521
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000522 reply->total = msg->data_size;
523 if (msg->data_size > get_reply_max_size())
524 {
525 set_error( STATUS_BUFFER_OVERFLOW );
526 return;
527 }
528 reply->type = msg->type;
529 reply->win = msg->win;
530 reply->msg = msg->msg;
531 reply->wparam = msg->wparam;
532 reply->lparam = msg->lparam;
533 reply->x = msg->x;
534 reply->y = msg->y;
535 reply->time = msg->time;
536 reply->info = msg->info;
537
538 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
539
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000540 unlink_message( &queue->msg_list[SEND_MESSAGE], msg );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000541 /* put the result on the receiver result stack */
Alexandre Julliardd253c582001-08-07 19:19:08 +0000542 if (result)
543 {
544 result->recv_next = queue->recv_result;
545 queue->recv_result = result;
546 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000547 free( msg );
Alexandre Julliardd253c582001-08-07 19:19:08 +0000548 if (!queue->msg_list[SEND_MESSAGE].first) clear_queue_bits( queue, QS_SENDMESSAGE );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000549}
550
551/* set the result of the current received message */
552static void reply_message( struct msg_queue *queue, unsigned int result,
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000553 unsigned int error, int remove, const void *data, size_t len )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000554{
555 struct message_result *res = queue->recv_result;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000556
557 if (remove)
558 {
559 queue->recv_result = res->recv_next;
560 res->receiver = NULL;
561 if (!res->sender) /* no one waiting for it */
562 {
Alexandre Julliardd253c582001-08-07 19:19:08 +0000563 free_result( res );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000564 return;
565 }
566 }
567 if (!res->replied)
568 {
Alexandre Julliardd253c582001-08-07 19:19:08 +0000569 if (len && (res->data = memdup( data, len ))) res->data_size = len;
570 store_message_result( res, result, error );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000571 }
572}
573
Alexandre Julliard242e3952003-01-08 00:27:58 +0000574/* retrieve a posted message */
575static int get_posted_message( struct msg_queue *queue, user_handle_t win,
576 unsigned int first, unsigned int last, unsigned int flags,
577 struct get_message_reply *reply )
578{
579 struct message *msg;
580 struct message_list *list = &queue->msg_list[POST_MESSAGE];
581
582 /* check against the filters */
583 for (msg = list->first; msg; msg = msg->next)
584 {
585 if (msg->msg == WM_QUIT) break; /* WM_QUIT is never filtered */
586 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
587 if (msg->msg < first) continue;
588 if (msg->msg > last) continue;
589 break; /* found one */
590 }
591 if (!msg) return 0;
592
593 /* return it to the app */
594
595 reply->total = msg->data_size;
596 if (msg->data_size > get_reply_max_size())
597 {
598 set_error( STATUS_BUFFER_OVERFLOW );
599 return 1;
600 }
601 reply->type = msg->type;
602 reply->win = msg->win;
603 reply->msg = msg->msg;
604 reply->wparam = msg->wparam;
605 reply->lparam = msg->lparam;
606 reply->x = msg->x;
607 reply->y = msg->y;
608 reply->time = msg->time;
609 reply->info = msg->info;
610
611 if (flags & GET_MSG_REMOVE)
612 {
613 if (msg->data)
614 {
615 set_reply_data_ptr( msg->data, msg->data_size );
616 msg->data = NULL;
617 msg->data_size = 0;
618 }
619 remove_queue_message( queue, msg, POST_MESSAGE );
620 }
621 else if (msg->data) set_reply_data( msg->data, msg->data_size );
622
623 return 1;
624}
625
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000626/* empty a message list and free all the messages */
627static void empty_msg_list( struct message_list *list )
628{
629 struct message *msg = list->first;
630 while (msg)
631 {
632 struct message *next = msg->next;
633 free_message( msg );
634 msg = next;
635 }
636}
637
638/* cleanup all pending results when deleting a queue */
639static void cleanup_results( struct msg_queue *queue )
640{
Alexandre Julliard039e1312003-07-26 20:36:43 +0000641 struct list *entry;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000642
Alexandre Julliard039e1312003-07-26 20:36:43 +0000643 while ((entry = list_head( &queue->send_result )) != NULL)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000644 {
Alexandre Julliard039e1312003-07-26 20:36:43 +0000645 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
646 }
647
648 while ((entry = list_head( &queue->callback_result )) != NULL)
649 {
650 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000651 }
652
Alexandre Julliardd253c582001-08-07 19:19:08 +0000653 while (queue->recv_result)
654 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000655}
656
Alexandre Julliard09029b22003-07-11 04:09:42 +0000657/* check if the thread owning the queue is hung (not checking for messages) */
658static int is_queue_hung( struct msg_queue *queue )
659{
660 struct timeval now;
661 struct wait_queue_entry *entry;
662
663 gettimeofday( &now, NULL );
664 if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
665 return 0; /* less than 5 seconds since last get message -> not hung */
666
667 for (entry = queue->obj.head; entry; entry = entry->next)
668 {
669 if (entry->thread->queue == queue)
670 return 0; /* thread is waiting on queue -> not hung */
671 }
672 return 1;
673}
674
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000675static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
676{
677 struct msg_queue *queue = (struct msg_queue *)obj;
678 struct process *process = entry->thread->process;
679
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000680 /* a thread can only wait on its own queue */
681 if (entry->thread->queue != queue)
682 {
683 set_error( STATUS_ACCESS_DENIED );
684 return 0;
685 }
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000686 /* if waiting on the main process queue, set the idle event */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000687 if (process->queue == queue)
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000688 {
689 if (process->idle_event) set_event( process->idle_event );
690 }
691 add_queue( obj, entry );
692 return 1;
693}
694
695static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
696{
697 struct msg_queue *queue = (struct msg_queue *)obj;
698 struct process *process = entry->thread->process;
699
700 remove_queue( obj, entry );
701
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000702 assert( entry->thread->queue == queue );
703
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000704 /* if waiting on the main process queue, reset the idle event */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000705 if (process->queue == queue)
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000706 {
707 if (process->idle_event) reset_event( process->idle_event );
708 }
709}
710
711static void msg_queue_dump( struct object *obj, int verbose )
712{
713 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000714 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
715 queue->wake_bits, queue->wake_mask );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000716}
717
718static int msg_queue_signaled( struct object *obj, struct thread *thread )
719{
720 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000721 return is_signaled( queue );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000722}
723
724static int msg_queue_satisfied( struct object *obj, struct thread *thread )
725{
726 struct msg_queue *queue = (struct msg_queue *)obj;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000727 queue->wake_mask = 0;
728 queue->changed_mask = 0;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +0000729 return 0; /* Not abandoned */
730}
731
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000732static void msg_queue_destroy( struct object *obj )
733{
734 struct msg_queue *queue = (struct msg_queue *)obj;
735 struct timer *timer = queue->first_timer;
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000736 int i;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000737
738 cleanup_results( queue );
Alexandre Julliard838d65a2001-06-19 19:16:41 +0000739 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000740
741 while (timer)
742 {
743 struct timer *next = timer->next;
744 free( timer );
745 timer = next;
746 }
747 if (queue->timeout) remove_timeout_user( queue->timeout );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000748 if (queue->input) release_object( queue->input );
Alexandre Julliardd55e7f12003-07-03 18:16:48 +0000749 if (queue->hooks) release_object( queue->hooks );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000750}
751
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000752static void thread_input_dump( struct object *obj, int verbose )
753{
754 struct thread_input *input = (struct thread_input *)obj;
Alexandre Julliardb3332d72002-10-19 01:00:59 +0000755 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000756 input->focus, input->capture, input->active );
757}
758
759static void thread_input_destroy( struct object *obj )
760{
761 struct thread_input *input = (struct thread_input *)obj;
762
763 if (foreground_input == input) foreground_input = NULL;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000764 if (input->msg_thread) release_object( input->msg_thread );
765 empty_msg_list( &input->msg_list );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000766}
767
768/* fix the thread input data when a window is destroyed */
769inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
770{
771 struct thread_input *input = queue->input;
772
773 if (window == input->focus) input->focus = 0;
774 if (window == input->capture) input->capture = 0;
775 if (window == input->active) input->active = 0;
776 if (window == input->menu_owner) input->menu_owner = 0;
777 if (window == input->move_size) input->move_size = 0;
Alexandre Julliard11e35232002-10-17 01:24:33 +0000778 if (window == input->caret) set_caret_window( input, 0 );
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000779}
780
Alexandre Julliard5030bda2002-10-11 23:41:06 +0000781/* check if the specified window can be set in the input data of a given queue */
782static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
783{
784 struct thread *thread;
785 int ret = 0;
786
787 if (!window) return 1; /* we can always clear the data */
788
789 if ((thread = get_window_thread( window )))
790 {
791 ret = (queue->input == thread->queue->input);
792 if (!ret) set_error( STATUS_ACCESS_DENIED );
793 release_object( thread );
794 }
795 else set_error( STATUS_INVALID_HANDLE );
796
797 return ret;
798}
799
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000800/* attach two thread input data structures */
801int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
802{
803 struct thread_input *input;
804
805 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
806 input = (struct thread_input *)grab_object( thread_to->queue->input );
807
808 if (thread_from->queue)
809 {
810 release_object( thread_from->queue->input );
811 thread_from->queue->input = input;
812 }
813 else
814 {
815 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
816 }
817 memset( input->keystate, 0, sizeof(input->keystate) );
818 return 1;
819}
820
821/* detach two thread input data structures */
822static void detach_thread_input( struct thread *thread_from, struct thread *thread_to )
823{
824 struct thread_input *input;
825
826 if (!thread_from->queue || !thread_to->queue ||
827 thread_from->queue->input != thread_to->queue->input)
828 {
829 set_error( STATUS_ACCESS_DENIED );
830 return;
831 }
832 if ((input = create_thread_input()))
833 {
834 release_object( thread_from->queue->input );
835 thread_from->queue->input = input;
836 }
837}
838
839
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000840/* set the next timer to expire */
841static void set_next_timer( struct msg_queue *queue, struct timer *timer )
842{
843 if (queue->timeout)
844 {
845 remove_timeout_user( queue->timeout );
846 queue->timeout = NULL;
847 }
848 if ((queue->next_timer = timer))
849 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
850
851 /* set/clear QS_TIMER bit */
852 if (queue->next_timer == queue->first_timer)
Alexandre Julliardd253c582001-08-07 19:19:08 +0000853 clear_queue_bits( queue, QS_TIMER );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000854 else
Alexandre Julliardd253c582001-08-07 19:19:08 +0000855 set_queue_bits( queue, QS_TIMER );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000856}
857
858/* callback for the next timer expiration */
859static void timer_callback( void *private )
860{
861 struct msg_queue *queue = private;
862
863 queue->timeout = NULL;
864 /* move on to the next timer */
865 set_next_timer( queue, queue->next_timer->next );
866}
867
868/* link a timer at its rightful place in the queue list */
869static void link_timer( struct msg_queue *queue, struct timer *timer )
870{
871 struct timer *pos = queue->next_timer;
872
873 while (pos && time_before( &pos->when, &timer->when )) pos = pos->next;
874
875 if (pos) /* insert before pos */
876 {
877 if ((timer->prev = pos->prev)) timer->prev->next = timer;
878 else queue->first_timer = timer;
879 timer->next = pos;
880 pos->prev = timer;
881 }
882 else /* insert at end */
883 {
884 timer->next = NULL;
885 timer->prev = queue->last_timer;
886 if (queue->last_timer) queue->last_timer->next = timer;
887 else queue->first_timer = timer;
888 queue->last_timer = timer;
889 }
890 /* check if we replaced the next timer */
891 if (pos == queue->next_timer) set_next_timer( queue, timer );
892}
893
894/* remove a timer from the queue timer list */
895static void unlink_timer( struct msg_queue *queue, struct timer *timer )
896{
897 if (timer->next) timer->next->prev = timer->prev;
898 else queue->last_timer = timer->prev;
899 if (timer->prev) timer->prev->next = timer->next;
900 else queue->first_timer = timer->next;
901 /* check if we removed the next timer */
902 if (queue->next_timer == timer) set_next_timer( queue, timer->next );
Alexandre Julliardd253c582001-08-07 19:19:08 +0000903 else if (queue->next_timer == queue->first_timer) clear_queue_bits( queue, QS_TIMER );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000904}
905
906/* restart an expired timer */
907static void restart_timer( struct msg_queue *queue, struct timer *timer )
908{
909 struct timeval now;
910 unlink_timer( queue, timer );
911 gettimeofday( &now, 0 );
912 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
913 link_timer( queue, timer );
914}
915
916/* find an expired timer matching the filtering parameters */
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000917static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000918 unsigned int get_first, unsigned int get_last,
919 int remove )
920{
921 struct timer *timer;
922 for (timer = queue->first_timer; (timer && timer != queue->next_timer); timer = timer->next)
923 {
924 if (win && timer->win != win) continue;
925 if (timer->msg >= get_first && timer->msg <= get_last)
926 {
927 if (remove) restart_timer( queue, timer );
928 return timer;
929 }
930 }
931 return NULL;
932}
933
934/* kill a timer */
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000935static int kill_timer( struct msg_queue *queue, user_handle_t win,
936 unsigned int msg, unsigned int id )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +0000937{
938 struct timer *timer;
939
940 for (timer = queue->first_timer; timer; timer = timer->next)
941 {
942 if (timer->win != win || timer->msg != msg || timer->id != id) continue;
943 unlink_timer( queue, timer );
944 free( timer );
945 return 1;
946 }
947 return 0;
948}
949
950/* add a timer */
951static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
952{
953 struct timer *timer = mem_alloc( sizeof(*timer) );
954 if (timer)
955 {
956 timer->rate = rate;
957 gettimeofday( &timer->when, 0 );
958 add_timeout( &timer->when, rate );
959 link_timer( queue, timer );
960 }
961 return timer;
962}
963
Alexandre Julliard8ba666f2003-01-08 19:56:31 +0000964/* change the input key state for a given key */
965static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
966{
967 if (down)
968 {
969 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
970 input->keystate[key] |= 0x80;
971 }
972 else input->keystate[key] &= ~0x80;
973}
974
975/* update the input key state for a keyboard message */
976static void update_input_key_state( struct thread_input *input, const struct message *msg )
977{
978 unsigned char key;
979 int down = 0, extended;
980
981 switch (msg->msg)
982 {
983 case WM_LBUTTONDOWN:
984 down = 1;
985 /* fall through */
986 case WM_LBUTTONUP:
987 set_input_key_state( input, VK_LBUTTON, down );
988 break;
989 case WM_MBUTTONDOWN:
990 down = 1;
991 /* fall through */
992 case WM_MBUTTONUP:
993 set_input_key_state( input, VK_MBUTTON, down );
994 break;
995 case WM_RBUTTONDOWN:
996 down = 1;
997 /* fall through */
998 case WM_RBUTTONUP:
999 set_input_key_state( input, VK_RBUTTON, down );
1000 break;
1001 case WM_KEYDOWN:
1002 case WM_SYSKEYDOWN:
1003 down = 1;
1004 /* fall through */
1005 case WM_KEYUP:
1006 case WM_SYSKEYUP:
1007 key = (unsigned char)msg->wparam;
1008 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1009 set_input_key_state( input, key, down );
1010 switch(key)
1011 {
1012 case VK_SHIFT:
1013 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1014 break;
1015 case VK_CONTROL:
1016 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1017 break;
1018 case VK_MENU:
1019 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1020 break;
1021 }
1022 break;
1023 }
1024}
1025
Alexandre Julliard242e3952003-01-08 00:27:58 +00001026/* release the hardware message currently being processed by the given thread */
1027static void release_hardware_message( struct thread *thread, int remove )
1028{
1029 struct thread_input *input = thread->queue->input;
1030
1031 if (input->msg_thread != thread) return;
1032 if (remove)
1033 {
1034 struct message *other;
1035 int clr_bit;
1036
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001037 update_input_key_state( input, input->msg );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001038 unlink_message( &input->msg_list, input->msg );
1039 clr_bit = get_hardware_msg_bit( input->msg );
1040 for (other = input->msg_list.first; other; other = other->next)
1041 if (get_hardware_msg_bit( other ) == clr_bit) break;
1042 if (!other) clear_queue_bits( thread->queue, clr_bit );
1043 free_message( input->msg );
1044 }
1045 release_object( input->msg_thread );
1046 input->msg = NULL;
1047 input->msg_thread = NULL;
1048}
1049
1050/* find the window that should receive a given hardware message */
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001051static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1052 unsigned int *msg_code )
Alexandre Julliard242e3952003-01-08 00:27:58 +00001053{
1054 user_handle_t win = 0;
1055
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001056 *msg_code = msg->msg;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001057 if (is_keyboard_msg( msg ))
1058 {
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001059 if (input && !(win = input->focus))
1060 {
1061 win = input->active;
1062 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1063 }
Alexandre Julliard242e3952003-01-08 00:27:58 +00001064 }
1065 else /* mouse message */
1066 {
1067 if (!input || !(win = input->capture))
1068 {
1069 if (!(win = msg->win)) win = window_from_point( msg->x, msg->y );
1070 }
1071 }
1072 return win;
1073}
1074
1075/* queue a hardware message into a given thread input */
1076static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1077{
1078 user_handle_t win;
1079 struct thread *thread;
1080 struct thread_input *input;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001081 unsigned int msg_code;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001082
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001083 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001084 if (!win || !(thread = get_window_thread(win)))
1085 {
1086 free( msg );
1087 return;
1088 }
1089 input = thread->queue->input;
1090
1091 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1092 else
1093 {
1094 append_message( &input->msg_list, msg );
1095 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1096 }
1097 release_object( thread );
1098}
1099
1100/* find a hardware message for the given queue */
1101static int get_hardware_message( struct thread *thread, struct message *first,
1102 user_handle_t filter_win, struct get_message_reply *reply )
1103{
1104 struct thread_input *input = thread->queue->input;
1105 struct thread *win_thread;
1106 struct message *msg;
1107 user_handle_t win;
1108 int clear_bits, got_one = 0;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001109 unsigned int msg_code;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001110
1111 if (input->msg_thread && input->msg_thread != thread)
1112 return 0; /* locked by another thread */
1113
1114 if (!first)
1115 {
1116 msg = input->msg_list.first;
1117 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1118 }
1119 else
1120 {
1121 msg = first->next;
1122 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1123 }
1124
1125 while (msg)
1126 {
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001127 win = find_hardware_message_window( input, msg, &msg_code );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001128 if (!win || !(win_thread = get_window_thread( win )))
1129 {
1130 /* no window at all, remove it */
1131 struct message *next = msg->next;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001132 update_input_key_state( input, msg );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001133 unlink_message( &input->msg_list, msg );
1134 free_message( msg );
1135 msg = next;
1136 continue;
1137 }
1138 if (win_thread != thread)
1139 {
1140 /* wake the other thread */
1141 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1142 release_object( win_thread );
1143 got_one = 1;
1144 msg = msg->next;
1145 continue;
1146 }
1147 /* if we already got a message for another thread, or if it doesn't
1148 * match the filter we skip it (filter is only checked for keyboard
1149 * messages since the dest window for a mouse message depends on hittest)
1150 */
1151 if (got_one ||
1152 (filter_win && is_keyboard_msg(msg) &&
1153 win != filter_win && !is_child_window( filter_win, win )))
1154 {
1155 clear_bits &= ~get_hardware_msg_bit( msg );
Mike McCormack7034e8b2003-02-19 00:38:12 +00001156 release_object( win_thread );
Alexandre Julliard242e3952003-01-08 00:27:58 +00001157 msg = msg->next;
1158 continue;
1159 }
1160 /* now we can return it */
1161 if (!input->msg_thread) input->msg_thread = win_thread;
1162 else release_object( win_thread );
1163 input->msg = msg;
1164
1165 reply->type = MSG_HARDWARE;
1166 reply->win = win;
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001167 reply->msg = msg_code;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001168 reply->wparam = msg->wparam;
1169 reply->lparam = msg->lparam;
1170 reply->x = msg->x;
1171 reply->y = msg->y;
1172 reply->time = msg->time;
1173 reply->info = msg->info;
1174 return 1;
1175 }
1176 /* nothing found, clear the hardware queue bits */
1177 clear_queue_bits( thread->queue, clear_bits );
1178 if (input->msg_thread) release_object( input->msg_thread );
1179 input->msg = NULL;
1180 input->msg_thread = NULL;
1181 return 0;
1182}
Alexandre Julliard805bdc52001-11-13 22:23:48 +00001183
1184/* increment (or decrement if 'incr' is negative) the queue paint count */
1185void inc_queue_paint_count( struct thread *thread, int incr )
1186{
1187 struct msg_queue *queue = thread->queue;
1188
1189 assert( queue );
1190
1191 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1192
1193 if (queue->paint_count)
1194 set_queue_bits( queue, QS_PAINT );
1195 else
1196 clear_queue_bits( queue, QS_PAINT );
1197}
1198
1199
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001200/* remove all messages and timers belonging to a certain window */
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001201void queue_cleanup_window( struct thread *thread, user_handle_t win )
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001202{
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001203 struct msg_queue *queue = thread->queue;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001204 struct timer *timer;
1205 struct message *msg;
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001206 int i;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001207
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001208 if (!queue) return;
1209
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001210 /* remove timers */
1211 timer = queue->first_timer;
1212 while (timer)
1213 {
1214 struct timer *next = timer->next;
1215 if (timer->win == win)
1216 {
1217 unlink_timer( queue, timer );
1218 free( timer );
1219 }
1220 timer = next;
1221 }
1222
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001223 /* remove messages */
1224 for (i = 0; i < NB_MSG_KINDS; i++)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001225 {
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001226 msg = queue->msg_list[i].first;
1227 while (msg)
1228 {
1229 struct message *next = msg->next;
1230 if (msg->win == win) remove_queue_message( queue, msg, i );
1231 msg = next;
1232 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001233 }
Alexandre Julliardab5063b2002-10-11 18:50:15 +00001234
1235 thread_input_cleanup_window( queue, win );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001236}
1237
Alexandre Julliard81f2a732002-03-23 20:43:52 +00001238/* post a message to a window; used by socket handling */
1239void post_message( user_handle_t win, unsigned int message,
1240 unsigned int wparam, unsigned int lparam )
1241{
1242 struct message *msg;
1243 struct thread *thread = get_window_thread( win );
1244
1245 if (!thread) return;
1246
1247 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1248 {
1249 msg->type = MSG_POSTED;
1250 msg->win = get_user_full_handle( win );
1251 msg->msg = message;
1252 msg->wparam = wparam;
1253 msg->lparam = lparam;
1254 msg->time = get_tick_count();
1255 msg->x = 0;
1256 msg->y = 0;
1257 msg->info = 0;
1258 msg->result = NULL;
1259 msg->data = NULL;
1260 msg->data_size = 0;
1261
1262 append_message( &thread->queue->msg_list[POST_MESSAGE], msg );
1263 set_queue_bits( thread->queue, QS_POSTMESSAGE );
1264 }
1265 release_object( thread );
1266}
1267
1268
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001269/* get the message queue of the current thread */
1270DECL_HANDLER(get_msg_queue)
1271{
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001272 struct msg_queue *queue = get_current_queue();
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001273
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001274 reply->handle = 0;
1275 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
Alexandre Julliardc5e433a2000-05-30 19:48:18 +00001276}
1277
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001278
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001279/* set the current message queue wakeup mask */
1280DECL_HANDLER(set_queue_mask)
1281{
1282 struct msg_queue *queue = get_current_queue();
1283
1284 if (queue)
1285 {
1286 queue->wake_mask = req->wake_mask;
1287 queue->changed_mask = req->changed_mask;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001288 reply->wake_bits = queue->wake_bits;
1289 reply->changed_bits = queue->changed_bits;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001290 if (is_signaled( queue ))
1291 {
1292 /* if skip wait is set, do what would have been done in the subsequent wait */
1293 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1294 else wake_up( &queue->obj, 0 );
1295 }
1296 }
1297}
1298
1299
1300/* get the current message queue status */
1301DECL_HANDLER(get_queue_status)
1302{
1303 struct msg_queue *queue = current->queue;
1304 if (queue)
1305 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001306 reply->wake_bits = queue->wake_bits;
1307 reply->changed_bits = queue->changed_bits;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001308 if (req->clear) queue->changed_bits = 0;
1309 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001310 else reply->wake_bits = reply->changed_bits = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001311}
1312
1313
1314/* send a message to a thread queue */
1315DECL_HANDLER(send_message)
1316{
1317 struct message *msg;
1318 struct msg_queue *send_queue = get_current_queue();
Alexandre Julliard242e3952003-01-08 00:27:58 +00001319 struct msg_queue *recv_queue = NULL;
1320 struct thread *thread = NULL;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001321
Alexandre Julliard242e3952003-01-08 00:27:58 +00001322 if (req->id)
1323 {
1324 if (!(thread = get_thread_from_id( req->id ))) return;
1325 }
1326 else if (req->type != MSG_HARDWARE)
1327 {
1328 /* only hardware messages are allowed without destination thread */
1329 set_error( STATUS_INVALID_PARAMETER );
1330 return;
1331 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001332
Alexandre Julliard242e3952003-01-08 00:27:58 +00001333 if (thread && !(recv_queue = thread->queue))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001334 {
1335 set_error( STATUS_INVALID_PARAMETER );
1336 release_object( thread );
1337 return;
1338 }
Alexandre Julliard09029b22003-07-11 04:09:42 +00001339 if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1340 {
1341 set_error( STATUS_TIMEOUT );
1342 release_object( thread );
1343 return;
1344 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001345
1346 if ((msg = mem_alloc( sizeof(*msg) )))
1347 {
Alexandre Julliardd253c582001-08-07 19:19:08 +00001348 msg->type = req->type;
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001349 msg->win = get_user_full_handle( req->win );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001350 msg->msg = req->msg;
1351 msg->wparam = req->wparam;
1352 msg->lparam = req->lparam;
1353 msg->time = req->time;
1354 msg->x = req->x;
1355 msg->y = req->y;
1356 msg->info = req->info;
1357 msg->result = NULL;
1358 msg->data = NULL;
1359 msg->data_size = 0;
1360
1361 switch(msg->type)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001362 {
Eric Pouech0faceb02002-01-18 19:22:55 +00001363 case MSG_OTHER_PROCESS:
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001364 msg->data_size = get_req_data_size();
1365 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
Alexandre Julliarde630aa02001-07-11 17:29:01 +00001366 {
1367 free( msg );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001368 break;
Alexandre Julliarde630aa02001-07-11 17:29:01 +00001369 }
Alexandre Julliardd253c582001-08-07 19:19:08 +00001370 /* fall through */
1371 case MSG_ASCII:
1372 case MSG_UNICODE:
1373 case MSG_CALLBACK:
Alexandre Julliard039e1312003-07-26 20:36:43 +00001374 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1375 req->timeout, req->callback, req->info )))
Alexandre Julliarde630aa02001-07-11 17:29:01 +00001376 {
Alexandre Julliard039e1312003-07-26 20:36:43 +00001377 free_message( msg );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001378 break;
Alexandre Julliarde630aa02001-07-11 17:29:01 +00001379 }
Alexandre Julliardd253c582001-08-07 19:19:08 +00001380 /* fall through */
1381 case MSG_NOTIFY:
1382 append_message( &recv_queue->msg_list[SEND_MESSAGE], msg );
1383 set_queue_bits( recv_queue, QS_SENDMESSAGE );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001384 break;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001385 case MSG_POSTED:
Eric Pouech0faceb02002-01-18 19:22:55 +00001386 /* needed for posted DDE messages */
1387 msg->data_size = get_req_data_size();
1388 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1389 {
1390 free( msg );
1391 break;
1392 }
Alexandre Julliardd253c582001-08-07 19:19:08 +00001393 append_message( &recv_queue->msg_list[POST_MESSAGE], msg );
1394 set_queue_bits( recv_queue, QS_POSTMESSAGE );
1395 break;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001396 case MSG_HARDWARE:
1397 queue_hardware_message( recv_queue, msg );
1398 break;
Alexandre Julliard039e1312003-07-26 20:36:43 +00001399 case MSG_CALLBACK_RESULT: /* cannot send this one */
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001400 default:
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001401 set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001402 free( msg );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001403 break;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001404 }
1405 }
Alexandre Julliard242e3952003-01-08 00:27:58 +00001406 if (thread) release_object( thread );
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001407}
1408
1409
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001410/* get a message from the current queue */
1411DECL_HANDLER(get_message)
1412{
1413 struct timer *timer;
1414 struct message *msg;
Alexandre Julliard242e3952003-01-08 00:27:58 +00001415 struct message *first_hw_msg = NULL;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001416 struct msg_queue *queue = get_current_queue();
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001417 user_handle_t get_win = get_user_full_handle( req->get_win );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001418
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001419 if (!queue) return;
Alexandre Julliard09029b22003-07-11 04:09:42 +00001420 gettimeofday( &queue->last_get_msg, NULL );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001421
Alexandre Julliard242e3952003-01-08 00:27:58 +00001422 /* first of all release the hardware input lock if we own it */
1423 /* we'll grab it again if we find a hardware message */
1424 if (queue->input->msg_thread == current)
1425 {
1426 first_hw_msg = queue->input->msg;
1427 release_hardware_message( current, 0 );
1428 }
1429
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001430 /* first check for sent messages */
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001431 if ((msg = queue->msg_list[SEND_MESSAGE].first))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001432 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001433 receive_message( queue, msg, reply );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001434 return;
1435 }
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001436 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001437
Alexandre Julliard9f55ae62001-06-28 04:37:22 +00001438 /* clear changed bits so we can wait on them if we don't find a message */
1439 queue->changed_bits = 0;
1440
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001441 /* then check for posted messages */
Alexandre Julliard242e3952003-01-08 00:27:58 +00001442 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001443 return;
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001444
1445 /* then check for any raw hardware message */
Alexandre Julliard242e3952003-01-08 00:27:58 +00001446 if (get_hardware_message( current, first_hw_msg, get_win, reply ))
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001447 return;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001448
1449 /* now check for WM_PAINT */
Alexandre Julliard47f98212001-11-14 21:28:36 +00001450 if (queue->paint_count &&
1451 (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) &&
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001452 (reply->win = find_window_to_repaint( get_win, current )))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001453 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001454 reply->type = MSG_POSTED;
1455 reply->msg = WM_PAINT;
1456 reply->wparam = 0;
1457 reply->lparam = 0;
1458 reply->x = 0;
1459 reply->y = 0;
1460 reply->time = get_tick_count();
1461 reply->info = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001462 return;
1463 }
1464
1465 /* now check for timer */
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001466 if ((timer = find_expired_timer( queue, get_win, req->get_first,
Alexandre Julliard838d65a2001-06-19 19:16:41 +00001467 req->get_last, (req->flags & GET_MSG_REMOVE) )))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001468 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001469 reply->type = MSG_POSTED;
1470 reply->win = timer->win;
1471 reply->msg = timer->msg;
1472 reply->wparam = timer->id;
1473 reply->lparam = timer->lparam;
1474 reply->x = 0;
1475 reply->y = 0;
1476 reply->time = get_tick_count();
1477 reply->info = 0;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001478 return;
1479 }
1480
1481 done:
1482 set_error( STATUS_PENDING ); /* FIXME */
1483}
1484
1485
1486/* reply to a sent message */
1487DECL_HANDLER(reply_message)
1488{
Alexandre Julliard242e3952003-01-08 00:27:58 +00001489 if (!current->queue)
1490 {
1491 set_error( STATUS_ACCESS_DENIED );
1492 return;
1493 }
Alexandre Julliardb1095da2003-03-19 00:12:17 +00001494 if (req->type == MSG_HARDWARE)
Alexandre Julliard242e3952003-01-08 00:27:58 +00001495 {
1496 struct thread_input *input = current->queue->input;
1497 if (input->msg_thread == current) release_hardware_message( current, req->remove );
1498 else set_error( STATUS_ACCESS_DENIED );
1499 }
Alexandre Julliardb1095da2003-03-19 00:12:17 +00001500 else if (current->queue->recv_result)
1501 reply_message( current->queue, req->result, 0, req->remove,
1502 get_req_data(), get_req_data_size() );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001503}
1504
1505
1506/* retrieve the reply for the last message sent */
1507DECL_HANDLER(get_message_reply)
1508{
Alexandre Julliard039e1312003-07-26 20:36:43 +00001509 struct message_result *result;
1510 struct list *entry;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001511 struct msg_queue *queue = current->queue;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001512
Alexandre Julliardd253c582001-08-07 19:19:08 +00001513 if (queue)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001514 {
Alexandre Julliardd253c582001-08-07 19:19:08 +00001515 set_error( STATUS_PENDING );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001516 reply->result = 0;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001517
Alexandre Julliard039e1312003-07-26 20:36:43 +00001518 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1519
1520 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1521 if (result->replied || req->cancel)
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001522 {
Alexandre Julliardd253c582001-08-07 19:19:08 +00001523 if (result->replied)
1524 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001525 reply->result = result->result;
Alexandre Julliardd253c582001-08-07 19:19:08 +00001526 set_error( result->error );
1527 if (result->data)
1528 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001529 size_t data_len = min( result->data_size, get_reply_max_size() );
1530 set_reply_data_ptr( result->data, data_len );
Alexandre Julliardd253c582001-08-07 19:19:08 +00001531 result->data = NULL;
1532 result->data_size = 0;
1533 }
1534 }
Alexandre Julliard039e1312003-07-26 20:36:43 +00001535 remove_result_from_sender( result );
1536
1537 entry = list_head( &queue->send_result );
1538 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1539 else
1540 {
1541 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1542 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1543 }
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001544 }
1545 }
Alexandre Julliardd253c582001-08-07 19:19:08 +00001546 else set_error( STATUS_ACCESS_DENIED );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001547}
1548
1549
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001550/* set a window timer */
1551DECL_HANDLER(set_win_timer)
1552{
1553 struct timer *timer;
1554 struct msg_queue *queue = get_current_queue();
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001555 user_handle_t win = get_user_full_handle( req->win );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001556
1557 if (!queue) return;
1558
1559 /* remove it if it existed already */
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001560 if (win) kill_timer( queue, win, req->msg, req->id );
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001561
1562 if ((timer = set_timer( queue, req->rate )))
1563 {
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001564 timer->win = win;
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001565 timer->msg = req->msg;
1566 timer->id = req->id;
1567 timer->lparam = req->lparam;
1568 }
1569}
1570
1571/* kill a window timer */
1572DECL_HANDLER(kill_win_timer)
1573{
1574 struct msg_queue *queue = current->queue;
1575
Alexandre Julliardbc878ef2001-09-12 17:09:24 +00001576 if (!queue || !kill_timer( queue, get_user_full_handle(req->win), req->msg, req->id ))
Alexandre Julliard51ab43b2001-05-18 22:51:56 +00001577 set_error( STATUS_INVALID_PARAMETER );
1578}
Alexandre Julliardab5063b2002-10-11 18:50:15 +00001579
1580
1581/* attach (or detach) thread inputs */
1582DECL_HANDLER(attach_thread_input)
1583{
1584 struct thread *thread_from = get_thread_from_id( req->tid_from );
1585 struct thread *thread_to = get_thread_from_id( req->tid_to );
1586
1587 if (!thread_from || !thread_to)
1588 {
1589 if (thread_from) release_object( thread_from );
1590 if (thread_to) release_object( thread_to );
1591 return;
1592 }
1593 if (thread_from != thread_to)
1594 {
1595 if (req->attach) attach_thread_input( thread_from, thread_to );
1596 else detach_thread_input( thread_from, thread_to );
1597 }
1598 else set_error( STATUS_ACCESS_DENIED );
1599 release_object( thread_from );
1600 release_object( thread_to );
1601}
1602
1603
1604/* get thread input data */
1605DECL_HANDLER(get_thread_input)
1606{
1607 struct thread *thread = NULL;
1608 struct thread_input *input;
1609
1610 if (req->tid)
1611 {
1612 if (!(thread = get_thread_from_id( req->tid ))) return;
1613 input = thread->queue ? thread->queue->input : NULL;
1614 }
1615 else input = foreground_input; /* get the foreground thread info */
1616
1617 if (input)
1618 {
1619 reply->focus = input->focus;
1620 reply->capture = input->capture;
1621 reply->active = input->active;
1622 reply->menu_owner = input->menu_owner;
1623 reply->move_size = input->move_size;
1624 reply->caret = input->caret;
Alexandre Julliard11e35232002-10-17 01:24:33 +00001625 reply->rect = input->caret_rect;
Alexandre Julliardab5063b2002-10-11 18:50:15 +00001626 }
1627 else
1628 {
1629 reply->focus = 0;
1630 reply->capture = 0;
1631 reply->active = 0;
1632 reply->menu_owner = 0;
1633 reply->move_size = 0;
1634 reply->caret = 0;
1635 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1636 }
1637 /* foreground window is active window of foreground thread */
1638 reply->foreground = foreground_input ? foreground_input->active : 0;
1639 if (thread) release_object( thread );
1640}
Alexandre Julliard5030bda2002-10-11 23:41:06 +00001641
1642
Alexandre Julliard8ba666f2003-01-08 19:56:31 +00001643/* retrieve queue keyboard state for a given thread */
1644DECL_HANDLER(get_key_state)
1645{
1646 struct thread *thread;
1647 struct thread_input *input;
1648
1649 if (!(thread = get_thread_from_id( req->tid ))) return;
1650 input = thread->queue ? thread->queue->input : NULL;
1651 if (input)
1652 {
1653 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1654 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1655 }
1656 release_object( thread );
1657}
1658
1659
1660/* set queue keyboard state for a given thread */
1661DECL_HANDLER(set_key_state)
1662{
1663 struct thread *thread = NULL;
1664 struct thread_input *input;
1665
1666 if (!(thread = get_thread_from_id( req->tid ))) return;
1667 input = thread->queue ? thread->queue->input : NULL;
1668 if (input)
1669 {
1670 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1671 if (size) memcpy( input->keystate, get_req_data(), size );
1672 }
1673 release_object( thread );
1674}
1675
1676
Alexandre Julliard5030bda2002-10-11 23:41:06 +00001677/* set the system foreground window */
1678DECL_HANDLER(set_foreground_window)
1679{
1680 struct msg_queue *queue = get_current_queue();
1681
1682 reply->previous = foreground_input ? foreground_input->active : 0;
1683 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1684 reply->send_msg_new = FALSE;
1685
1686 if (req->handle)
1687 {
1688 struct thread *thread;
1689
1690 if (is_top_level_window( req->handle ) &&
1691 ((thread = get_window_thread( req->handle ))))
1692 {
1693 foreground_input = thread->queue->input;
1694 reply->send_msg_new = (foreground_input != queue->input);
1695 release_object( thread );
1696 }
1697 else set_error( STATUS_INVALID_HANDLE );
1698 }
1699 else foreground_input = NULL;
1700}
1701
1702
1703/* set the current thread focus window */
1704DECL_HANDLER(set_focus_window)
1705{
1706 struct msg_queue *queue = get_current_queue();
1707
1708 reply->previous = 0;
1709 if (queue && check_queue_input_window( queue, req->handle ))
1710 {
1711 reply->previous = queue->input->focus;
1712 queue->input->focus = get_user_full_handle( req->handle );
1713 }
1714}
1715
1716
1717/* set the current thread active window */
1718DECL_HANDLER(set_active_window)
1719{
1720 struct msg_queue *queue = get_current_queue();
1721
1722 reply->previous = 0;
1723 if (queue && check_queue_input_window( queue, req->handle ))
1724 {
1725 if (!req->handle || make_window_active( req->handle ))
1726 {
1727 reply->previous = queue->input->active;
1728 queue->input->active = get_user_full_handle( req->handle );
1729 }
1730 else set_error( STATUS_INVALID_HANDLE );
1731 }
1732}
Alexandre Julliarda9e8f592002-10-12 01:24:37 +00001733
1734
1735/* set the current thread capture window */
1736DECL_HANDLER(set_capture_window)
1737{
1738 struct msg_queue *queue = get_current_queue();
1739
1740 reply->previous = reply->full_handle = 0;
1741 if (queue && check_queue_input_window( queue, req->handle ))
1742 {
1743 struct thread_input *input = queue->input;
1744
1745 reply->previous = input->capture;
1746 input->capture = get_user_full_handle( req->handle );
1747 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1748 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1749 reply->full_handle = input->capture;
1750 }
1751}
Alexandre Julliard11e35232002-10-17 01:24:33 +00001752
1753
1754/* Set the current thread caret window */
1755DECL_HANDLER(set_caret_window)
1756{
1757 struct msg_queue *queue = get_current_queue();
1758
1759 reply->previous = 0;
1760 if (queue && check_queue_input_window( queue, req->handle ))
1761 {
1762 struct thread_input *input = queue->input;
1763
1764 reply->previous = input->caret;
1765 reply->old_rect = input->caret_rect;
1766 reply->old_hide = input->caret_hide;
1767 reply->old_state = input->caret_state;
1768
1769 set_caret_window( input, get_user_full_handle(req->handle) );
1770 input->caret_rect.right = req->width;
1771 input->caret_rect.bottom = req->height;
1772 }
1773}
1774
1775
1776/* Set the current thread caret information */
1777DECL_HANDLER(set_caret_info)
1778{
1779 struct msg_queue *queue = get_current_queue();
1780 struct thread_input *input;
1781
1782 if (!queue) return;
1783 input = queue->input;
1784 reply->full_handle = input->caret;
1785 reply->old_rect = input->caret_rect;
1786 reply->old_hide = input->caret_hide;
1787 reply->old_state = input->caret_state;
1788
1789 if (req->handle && get_user_full_handle(req->handle) != input->caret)
1790 {
1791 set_error( STATUS_ACCESS_DENIED );
1792 return;
1793 }
1794 if (req->flags & SET_CARET_POS)
1795 {
1796 input->caret_rect.right += req->x - input->caret_rect.left;
1797 input->caret_rect.bottom += req->y - input->caret_rect.top;
1798 input->caret_rect.left = req->x;
1799 input->caret_rect.top = req->y;
1800 }
1801 if (req->flags & SET_CARET_HIDE)
1802 {
1803 input->caret_hide += req->hide;
1804 if (input->caret_hide < 0) input->caret_hide = 0;
1805 }
1806 if (req->flags & SET_CARET_STATE)
1807 {
1808 if (req->state == -1) input->caret_state = !input->caret_state;
1809 else input->caret_state = !!req->state;
1810 }
1811}