Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side message queues |
| 3 | * |
| 4 | * Copyright (C) 2000 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 5 | * |
| 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 Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Alexandre Julliard | 5769d1d | 2002-04-26 19:05:15 +0000 | [diff] [blame] | 21 | #include "config.h" |
| 22 | #include "wine/port.h" |
| 23 | |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 24 | #include <assert.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 28 | #include "winbase.h" |
| 29 | #include "wingdi.h" |
| 30 | #include "winuser.h" |
| 31 | |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 32 | #include "handle.h" |
| 33 | #include "thread.h" |
| 34 | #include "process.h" |
| 35 | #include "request.h" |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 36 | #include "user.h" |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 37 | |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 38 | enum message_kind { SEND_MESSAGE, POST_MESSAGE, COOKED_HW_MESSAGE, RAW_HW_MESSAGE }; |
| 39 | #define NB_MSG_KINDS (RAW_HW_MESSAGE+1) |
| 40 | |
| 41 | |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 42 | struct 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 Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 51 | void *data; /* message reply data */ |
| 52 | unsigned int data_size; /* size of message reply data */ |
| 53 | struct timeout_user *timeout; /* result timeout */ |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | struct message |
| 57 | { |
| 58 | struct message *next; /* next message in list */ |
| 59 | struct message *prev; /* prev message in list */ |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 60 | enum message_type type; /* message type */ |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 61 | user_handle_t win; /* window handle */ |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 62 | unsigned int msg; /* message code */ |
| 63 | unsigned int wparam; /* parameters */ |
| 64 | unsigned int lparam; /* parameters */ |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 65 | int x; /* x position */ |
| 66 | int y; /* y position */ |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 67 | unsigned int time; /* message time */ |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 68 | unsigned int info; /* extra info */ |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 69 | void *data; /* message data for sent messages */ |
| 70 | unsigned int data_size; /* size of message data */ |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 71 | struct message_result *result; /* result in sender queue */ |
| 72 | }; |
| 73 | |
| 74 | struct message_list |
| 75 | { |
| 76 | struct message *first; /* head of list */ |
| 77 | struct message *last; /* tail of list */ |
| 78 | }; |
| 79 | |
| 80 | struct 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 Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 86 | user_handle_t win; /* window handle */ |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 87 | unsigned int msg; /* message to post */ |
| 88 | unsigned int id; /* timer id */ |
| 89 | unsigned int lparam; /* lparam for message */ |
| 90 | }; |
| 91 | |
Alexandre Julliard | ab5063b | 2002-10-11 18:50:15 +0000 | [diff] [blame] | 92 | struct 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 Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 105 | struct msg_queue |
| 106 | { |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 107 | 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 Julliard | 4b0343d | 2001-06-20 22:55:31 +0000 | [diff] [blame] | 112 | int paint_count; /* pending paint messages count */ |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 113 | struct message_list msg_list[NB_MSG_KINDS]; /* lists of messages */ |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 114 | 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 Julliard | 9f55ae6 | 2001-06-28 04:37:22 +0000 | [diff] [blame] | 116 | 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 Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 118 | 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 Julliard | ab5063b | 2002-10-11 18:50:15 +0000 | [diff] [blame] | 122 | struct thread_input *input; /* thread input descriptor */ |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | static void msg_queue_dump( struct object *obj, int verbose ); |
| 126 | static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry ); |
| 127 | static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry ); |
| 128 | static int msg_queue_signaled( struct object *obj, struct thread *thread ); |
| 129 | static int msg_queue_satisfied( struct object *obj, struct thread *thread ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 130 | static void msg_queue_destroy( struct object *obj ); |
Alexandre Julliard | ab5063b | 2002-10-11 18:50:15 +0000 | [diff] [blame] | 131 | static void thread_input_dump( struct object *obj, int verbose ); |
| 132 | static void thread_input_destroy( struct object *obj ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 133 | static void timer_callback( void *private ); |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 134 | |
| 135 | static 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 Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 145 | no_get_fd, /* get_fd */ |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 146 | no_flush, /* flush */ |
| 147 | no_get_file_info, /* get_file_info */ |
Mike McCormack | 6f011c0 | 2001-12-20 00:07:05 +0000 | [diff] [blame] | 148 | NULL, /* queue_async */ |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 149 | msg_queue_destroy /* destroy */ |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | |
Alexandre Julliard | ab5063b | 2002-10-11 18:50:15 +0000 | [diff] [blame] | 153 | static 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 */ |
| 171 | static 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 */ |
| 193 | static struct thread_input *foreground_input; |
| 194 | |
| 195 | /* create a message queue object */ |
| 196 | static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input ) |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 197 | { |
| 198 | struct msg_queue *queue; |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 199 | int i; |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 200 | |
Alexandre Julliard | ab5063b | 2002-10-11 18:50:15 +0000 | [diff] [blame] | 201 | if (!input && !(input = create_thread_input())) return NULL; |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 202 | if ((queue = alloc_object( &msg_queue_ops, -1 ))) |
| 203 | { |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 204 | queue->wake_bits = 0; |
| 205 | queue->wake_mask = 0; |
| 206 | queue->changed_bits = 0; |
| 207 | queue->changed_mask = 0; |
Alexandre Julliard | 4b0343d | 2001-06-20 22:55:31 +0000 | [diff] [blame] | 208 | queue->paint_count = 0; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 209 | queue->send_result = NULL; |
| 210 | queue->recv_result = NULL; |
Alexandre Julliard | 9f55ae6 | 2001-06-28 04:37:22 +0000 | [diff] [blame] | 211 | queue->last_msg = NULL; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 212 | queue->first_timer = NULL; |
| 213 | queue->last_timer = NULL; |
| 214 | queue->next_timer = NULL; |
| 215 | queue->timeout = NULL; |
Alexandre Julliard | ab5063b | 2002-10-11 18:50:15 +0000 | [diff] [blame] | 216 | queue->input = (struct thread_input *)grab_object( input ); |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 217 | for (i = 0; i < NB_MSG_KINDS; i++) |
| 218 | queue->msg_list[i].first = queue->msg_list[i].last = NULL; |
| 219 | |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 220 | thread->queue = queue; |
| 221 | if (!thread->process->queue) |
| 222 | thread->process->queue = (struct msg_queue *)grab_object( queue ); |
| 223 | } |
Alexandre Julliard | ab5063b | 2002-10-11 18:50:15 +0000 | [diff] [blame] | 224 | release_object( input ); |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 225 | return queue; |
| 226 | } |
| 227 | |
Alexandre Julliard | 31022d6 | 2002-08-16 23:30:41 +0000 | [diff] [blame] | 228 | /* free the message queue of a thread at thread exit */ |
| 229 | void 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 Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 249 | /* check the queue status */ |
| 250 | inline 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 Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 255 | /* set some queue bits */ |
| 256 | inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits ) |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 257 | { |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 258 | queue->wake_bits |= bits; |
| 259 | queue->changed_bits |= bits; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 260 | if (is_signaled( queue )) wake_up( &queue->obj, 0 ); |
| 261 | } |
| 262 | |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 263 | /* clear some queue bits */ |
| 264 | inline 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 Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 270 | /* get the QS_* bit corresponding to a given hardware message */ |
| 271 | inline 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 Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 278 | /* get the current thread queue, creating it if needed */ |
Alexandre Julliard | 805bdc5 | 2001-11-13 22:23:48 +0000 | [diff] [blame] | 279 | inline static struct msg_queue *get_current_queue(void) |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 280 | { |
| 281 | struct msg_queue *queue = current->queue; |
Alexandre Julliard | ab5063b | 2002-10-11 18:50:15 +0000 | [diff] [blame] | 282 | if (!queue) queue = create_msg_queue( current, NULL ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 283 | return queue; |
| 284 | } |
| 285 | |
| 286 | /* append a message to the end of a list */ |
| 287 | inline 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 */ |
| 296 | inline 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 Julliard | e630aa0 | 2001-07-11 17:29:01 +0000 | [diff] [blame] | 304 | /* try to merge a message with the last in the list; return 1 if successful */ |
| 305 | static 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 Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 324 | /* free a result structure */ |
| 325 | static 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 */ |
| 333 | static 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 Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 349 | /* free a message when deleting a queue or window */ |
| 350 | static void free_message( struct message *msg ) |
| 351 | { |
| 352 | struct message_result *result = msg->result; |
| 353 | if (result) |
| 354 | { |
| 355 | if (result->sender) |
| 356 | { |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 357 | result->receiver = NULL; |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 358 | store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 359 | } |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 360 | else free_result( result ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 361 | } |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 362 | if (msg->data) free( msg->data ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 363 | free( msg ); |
| 364 | } |
| 365 | |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 366 | /* remove (and free) a message from a message list */ |
| 367 | static void remove_queue_message( struct msg_queue *queue, struct message *msg, |
| 368 | enum message_kind kind ) |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 369 | { |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 370 | int clr_bit; |
| 371 | struct message *other; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 372 | |
Alexandre Julliard | 9f55ae6 | 2001-06-28 04:37:22 +0000 | [diff] [blame] | 373 | if (queue->last_msg == msg) queue->last_msg = NULL; |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 374 | unlink_message( &queue->msg_list[kind], msg ); |
| 375 | switch(kind) |
| 376 | { |
| 377 | case SEND_MESSAGE: |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 378 | if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_SENDMESSAGE ); |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 379 | break; |
| 380 | case POST_MESSAGE: |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 381 | if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_POSTMESSAGE ); |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 382 | 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 Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 388 | if (!other) clear_queue_bits( queue, clr_bit ); |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 389 | break; |
| 390 | } |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 391 | free_message( msg ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 394 | /* message timed out without getting a reply */ |
| 395 | static 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 */ |
| 406 | static struct message_result *alloc_message_result( struct msg_queue *send_queue, |
| 407 | struct msg_queue *recv_queue, |
| 408 | unsigned int timeout ) |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 409 | { |
| 410 | struct message_result *result = mem_alloc( sizeof(*result) ); |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 411 | 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 Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /* receive a message, removing it from the sent queue */ |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 434 | static void receive_message( struct msg_queue *queue, struct message *msg, |
| 435 | struct get_message_reply *reply ) |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 436 | { |
| 437 | struct message_result *result = msg->result; |
| 438 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 439 | 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 Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 457 | unlink_message( &queue->msg_list[SEND_MESSAGE], msg ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 458 | /* put the result on the receiver result stack */ |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 459 | if (result) |
| 460 | { |
| 461 | result->recv_next = queue->recv_result; |
| 462 | queue->recv_result = result; |
| 463 | } |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 464 | free( msg ); |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 465 | if (!queue->msg_list[SEND_MESSAGE].first) clear_queue_bits( queue, QS_SENDMESSAGE ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /* set the result of the current received message */ |
| 469 | static void reply_message( struct msg_queue *queue, unsigned int result, |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 470 | unsigned int error, int remove, const void *data, size_t len ) |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 471 | { |
| 472 | struct message_result *res = queue->recv_result; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 473 | |
| 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 Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 480 | free_result( res ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 481 | return; |
| 482 | } |
| 483 | } |
| 484 | if (!res->replied) |
| 485 | { |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 486 | if (len && (res->data = memdup( data, len ))) res->data_size = len; |
| 487 | store_message_result( res, result, error ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 491 | /* empty a message list and free all the messages */ |
| 492 | static 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 */ |
| 504 | static 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 Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 513 | if (!result->receiver) free_result( result ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 514 | result = next; |
| 515 | } |
| 516 | |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 517 | while (queue->recv_result) |
| 518 | reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 521 | static 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 Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 526 | /* 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 Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 532 | /* if waiting on the main process queue, set the idle event */ |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 533 | if (process->queue == queue) |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 534 | { |
| 535 | if (process->idle_event) set_event( process->idle_event ); |
| 536 | } |
| 537 | add_queue( obj, entry ); |
| 538 | return 1; |
| 539 | } |
| 540 | |
| 541 | static 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 Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 548 | assert( entry->thread->queue == queue ); |
| 549 | |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 550 | /* if waiting on the main process queue, reset the idle event */ |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 551 | if (process->queue == queue) |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 552 | { |
| 553 | if (process->idle_event) reset_event( process->idle_event ); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | static void msg_queue_dump( struct object *obj, int verbose ) |
| 558 | { |
| 559 | struct msg_queue *queue = (struct msg_queue *)obj; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 560 | fprintf( stderr, "Msg queue bits=%x mask=%x\n", |
| 561 | queue->wake_bits, queue->wake_mask ); |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | static int msg_queue_signaled( struct object *obj, struct thread *thread ) |
| 565 | { |
| 566 | struct msg_queue *queue = (struct msg_queue *)obj; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 567 | return is_signaled( queue ); |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | static int msg_queue_satisfied( struct object *obj, struct thread *thread ) |
| 571 | { |
| 572 | struct msg_queue *queue = (struct msg_queue *)obj; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 573 | queue->wake_mask = 0; |
| 574 | queue->changed_mask = 0; |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 575 | return 0; /* Not abandoned */ |
| 576 | } |
| 577 | |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 578 | static 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 Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 582 | int i; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 583 | |
| 584 | cleanup_results( queue ); |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 585 | for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 586 | |
| 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 Julliard | ab5063b | 2002-10-11 18:50:15 +0000 | [diff] [blame] | 594 | if (queue->input) release_object( queue->input ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Alexandre Julliard | ab5063b | 2002-10-11 18:50:15 +0000 | [diff] [blame] | 597 | static 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 | |
| 604 | static 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 */ |
| 612 | inline 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 */ |
| 625 | int 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 */ |
| 646 | static 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 Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 664 | /* set the next timer to expire */ |
| 665 | static 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 Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 677 | clear_queue_bits( queue, QS_TIMER ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 678 | else |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 679 | set_queue_bits( queue, QS_TIMER ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /* callback for the next timer expiration */ |
| 683 | static 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 */ |
| 693 | static 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 */ |
| 719 | static 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 Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 727 | else if (queue->next_timer == queue->first_timer) clear_queue_bits( queue, QS_TIMER ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | /* restart an expired timer */ |
| 731 | static 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 Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 741 | static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win, |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 742 | 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 Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 759 | static int kill_timer( struct msg_queue *queue, user_handle_t win, |
| 760 | unsigned int msg, unsigned int id ) |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 761 | { |
| 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 */ |
| 775 | static 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 Julliard | 805bdc5 | 2001-11-13 22:23:48 +0000 | [diff] [blame] | 788 | |
| 789 | /* increment (or decrement if 'incr' is negative) the queue paint count */ |
| 790 | void 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 Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 805 | /* remove all messages and timers belonging to a certain window */ |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 806 | void queue_cleanup_window( struct thread *thread, user_handle_t win ) |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 807 | { |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 808 | struct msg_queue *queue = thread->queue; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 809 | struct timer *timer; |
| 810 | struct message *msg; |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 811 | int i; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 812 | |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 813 | if (!queue) return; |
| 814 | |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 815 | /* 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 Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 828 | /* remove messages */ |
| 829 | for (i = 0; i < NB_MSG_KINDS; i++) |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 830 | { |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 831 | 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 Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 838 | } |
Alexandre Julliard | ab5063b | 2002-10-11 18:50:15 +0000 | [diff] [blame] | 839 | |
| 840 | thread_input_cleanup_window( queue, win ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Alexandre Julliard | 81f2a73 | 2002-03-23 20:43:52 +0000 | [diff] [blame] | 843 | /* post a message to a window; used by socket handling */ |
| 844 | void 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 Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 874 | /* get the message queue of the current thread */ |
| 875 | DECL_HANDLER(get_msg_queue) |
| 876 | { |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 877 | struct msg_queue *queue = get_current_queue(); |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 878 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 879 | reply->handle = 0; |
| 880 | if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 ); |
Alexandre Julliard | c5e433a | 2000-05-30 19:48:18 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 883 | |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 884 | /* set the current message queue wakeup mask */ |
| 885 | DECL_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 Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 893 | reply->wake_bits = queue->wake_bits; |
| 894 | reply->changed_bits = queue->changed_bits; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 895 | 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 */ |
| 906 | DECL_HANDLER(get_queue_status) |
| 907 | { |
| 908 | struct msg_queue *queue = current->queue; |
| 909 | if (queue) |
| 910 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 911 | reply->wake_bits = queue->wake_bits; |
| 912 | reply->changed_bits = queue->changed_bits; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 913 | if (req->clear) queue->changed_bits = 0; |
| 914 | } |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 915 | else reply->wake_bits = reply->changed_bits = 0; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | |
| 919 | /* send a message to a thread queue */ |
| 920 | DECL_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 Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 938 | msg->type = req->type; |
Alexandre Julliard | bc878ef | 2001-09-12 17:09:24 +0000 | [diff] [blame] | 939 | msg->win = get_user_full_handle( req->win ); |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 940 | 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 Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 952 | { |
Eric Pouech | 0faceb0 | 2002-01-18 19:22:55 +0000 | [diff] [blame] | 953 | case MSG_OTHER_PROCESS: |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 954 | msg->data_size = get_req_data_size(); |
| 955 | if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size ))) |
Alexandre Julliard | e630aa0 | 2001-07-11 17:29:01 +0000 | [diff] [blame] | 956 | { |
| 957 | free( msg ); |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 958 | break; |
Alexandre Julliard | e630aa0 | 2001-07-11 17:29:01 +0000 | [diff] [blame] | 959 | } |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 960 | /* 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 Julliard | e630aa0 | 2001-07-11 17:29:01 +0000 | [diff] [blame] | 965 | { |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 966 | free( msg ); |
| 967 | break; |
Alexandre Julliard | e630aa0 | 2001-07-11 17:29:01 +0000 | [diff] [blame] | 968 | } |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 969 | /* 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 Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 973 | break; |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 974 | case MSG_POSTED: |
Eric Pouech | 0faceb0 | 2002-01-18 19:22:55 +0000 | [diff] [blame] | 975 | /* 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 Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 982 | 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 Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 1000 | default: |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 1001 | set_error( STATUS_INVALID_PARAMETER ); |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 1002 | free( msg ); |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 1003 | break; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | release_object( thread ); |
| 1007 | } |
| 1008 | |
Alexandre Julliard | 9f55ae6 | 2001-06-28 04:37:22 +0000 | [diff] [blame] | 1009 | /* return a message to the application, removing it from the queue if needed */ |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1010 | static void return_message_to_app( struct msg_queue *queue, int flags, |
| 1011 | struct get_message_reply *reply, |
Alexandre Julliard | 9f55ae6 | 2001-06-28 04:37:22 +0000 | [diff] [blame] | 1012 | struct message *msg, enum message_kind kind ) |
| 1013 | { |
Eric Pouech | 0faceb0 | 2002-01-18 19:22:55 +0000 | [diff] [blame] | 1014 | 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 Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1020 | 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 Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1029 | |
Alexandre Julliard | 9f55ae6 | 2001-06-28 04:37:22 +0000 | [diff] [blame] | 1030 | /* raw messages always get removed */ |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1031 | if ((msg->type == MSG_HARDWARE_RAW) || (flags & GET_MSG_REMOVE)) |
Alexandre Julliard | 9f55ae6 | 2001-06-28 04:37:22 +0000 | [diff] [blame] | 1032 | { |
| 1033 | queue->last_msg = NULL; |
Eric Pouech | 0faceb0 | 2002-01-18 19:22:55 +0000 | [diff] [blame] | 1034 | 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 Julliard | 9f55ae6 | 2001-06-28 04:37:22 +0000 | [diff] [blame] | 1040 | remove_queue_message( queue, msg, kind ); |
| 1041 | } |
| 1042 | else /* remember it as the last returned message */ |
| 1043 | { |
Eric Pouech | 0faceb0 | 2002-01-18 19:22:55 +0000 | [diff] [blame] | 1044 | if (msg->data) set_reply_data( msg->data, msg->data_size ); |
Alexandre Julliard | 9f55ae6 | 2001-06-28 04:37:22 +0000 | [diff] [blame] | 1045 | queue->last_msg = msg; |
| 1046 | queue->last_msg_kind = kind; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 1051 | inline static struct message *find_matching_message( const struct message_list *list, |
| 1052 | user_handle_t win, |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 1053 | 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 Julliard | 9f55ae6 | 2001-06-28 04:37:22 +0000 | [diff] [blame] | 1060 | if (msg->msg == WM_QUIT) break; /* WM_QUIT is never filtered */ |
Alexandre Julliard | a09da0c | 2001-09-21 21:08:40 +0000 | [diff] [blame] | 1061 | if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue; |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 1062 | if (msg->msg < first) continue; |
| 1063 | if (msg->msg > last) continue; |
| 1064 | break; /* found one */ |
| 1065 | } |
| 1066 | return msg; |
| 1067 | } |
| 1068 | |
| 1069 | |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1070 | /* get a message from the current queue */ |
| 1071 | DECL_HANDLER(get_message) |
| 1072 | { |
| 1073 | struct timer *timer; |
| 1074 | struct message *msg; |
| 1075 | struct msg_queue *queue = get_current_queue(); |
Alexandre Julliard | bc878ef | 2001-09-12 17:09:24 +0000 | [diff] [blame] | 1076 | user_handle_t get_win = get_user_full_handle( req->get_win ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1077 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1078 | if (!queue) return; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1079 | |
| 1080 | /* first check for sent messages */ |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 1081 | if ((msg = queue->msg_list[SEND_MESSAGE].first)) |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1082 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1083 | receive_message( queue, msg, reply ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1084 | return; |
| 1085 | } |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 1086 | if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */ |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1087 | |
Alexandre Julliard | 9f55ae6 | 2001-06-28 04:37:22 +0000 | [diff] [blame] | 1088 | /* 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 Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 1096 | /* then check for posted messages */ |
Alexandre Julliard | bc878ef | 2001-09-12 17:09:24 +0000 | [diff] [blame] | 1097 | if ((msg = find_matching_message( &queue->msg_list[POST_MESSAGE], get_win, |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 1098 | req->get_first, req->get_last ))) |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1099 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1100 | return_message_to_app( queue, req->flags, reply, msg, POST_MESSAGE ); |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 1101 | return; |
| 1102 | } |
| 1103 | |
| 1104 | /* then check for cooked hardware messages */ |
Alexandre Julliard | bc878ef | 2001-09-12 17:09:24 +0000 | [diff] [blame] | 1105 | if ((msg = find_matching_message( &queue->msg_list[COOKED_HW_MESSAGE], get_win, |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 1106 | req->get_first, req->get_last ))) |
| 1107 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1108 | return_message_to_app( queue, req->flags, reply, msg, COOKED_HW_MESSAGE ); |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 1109 | return; |
| 1110 | } |
| 1111 | |
| 1112 | /* then check for any raw hardware message */ |
| 1113 | if ((msg = queue->msg_list[RAW_HW_MESSAGE].first)) |
| 1114 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1115 | return_message_to_app( queue, req->flags, reply, msg, RAW_HW_MESSAGE ); |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 1116 | return; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | /* now check for WM_PAINT */ |
Alexandre Julliard | 47f9821 | 2001-11-14 21:28:36 +0000 | [diff] [blame] | 1120 | if (queue->paint_count && |
| 1121 | (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) && |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1122 | (reply->win = find_window_to_repaint( get_win, current ))) |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1123 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1124 | 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 Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1132 | return; |
| 1133 | } |
| 1134 | |
| 1135 | /* now check for timer */ |
Alexandre Julliard | bc878ef | 2001-09-12 17:09:24 +0000 | [diff] [blame] | 1136 | if ((timer = find_expired_timer( queue, get_win, req->get_first, |
Alexandre Julliard | 838d65a | 2001-06-19 19:16:41 +0000 | [diff] [blame] | 1137 | req->get_last, (req->flags & GET_MSG_REMOVE) ))) |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1138 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1139 | 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 Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1148 | return; |
| 1149 | } |
| 1150 | |
| 1151 | done: |
| 1152 | set_error( STATUS_PENDING ); /* FIXME */ |
| 1153 | } |
| 1154 | |
| 1155 | |
| 1156 | /* reply to a sent message */ |
| 1157 | DECL_HANDLER(reply_message) |
| 1158 | { |
Eric Pouech | 476c2b4 | 2001-05-19 17:38:21 +0000 | [diff] [blame] | 1159 | if (current->queue && current->queue->recv_result) |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 1160 | reply_message( current->queue, req->result, 0, req->remove, |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1161 | get_req_data(), get_req_data_size() ); |
Eric Pouech | 476c2b4 | 2001-05-19 17:38:21 +0000 | [diff] [blame] | 1162 | else |
| 1163 | set_error( STATUS_ACCESS_DENIED ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | |
| 1167 | /* retrieve the reply for the last message sent */ |
| 1168 | DECL_HANDLER(get_message_reply) |
| 1169 | { |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 1170 | struct msg_queue *queue = current->queue; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1171 | |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 1172 | if (queue) |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1173 | { |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 1174 | struct message_result *result = queue->send_result; |
| 1175 | |
| 1176 | set_error( STATUS_PENDING ); |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1177 | reply->result = 0; |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 1178 | |
| 1179 | if (result && (result->replied || req->cancel)) |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1180 | { |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 1181 | if (result->replied) |
| 1182 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1183 | reply->result = result->result; |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 1184 | set_error( result->error ); |
| 1185 | if (result->data) |
| 1186 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1187 | size_t data_len = min( result->data_size, get_reply_max_size() ); |
| 1188 | set_reply_data_ptr( result->data, data_len ); |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 1189 | 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 Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1198 | } |
| 1199 | } |
Alexandre Julliard | d253c58 | 2001-08-07 19:19:08 +0000 | [diff] [blame] | 1200 | else set_error( STATUS_ACCESS_DENIED ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1204 | /* set a window timer */ |
| 1205 | DECL_HANDLER(set_win_timer) |
| 1206 | { |
| 1207 | struct timer *timer; |
| 1208 | struct msg_queue *queue = get_current_queue(); |
Alexandre Julliard | bc878ef | 2001-09-12 17:09:24 +0000 | [diff] [blame] | 1209 | user_handle_t win = get_user_full_handle( req->win ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1210 | |
| 1211 | if (!queue) return; |
| 1212 | |
| 1213 | /* remove it if it existed already */ |
Alexandre Julliard | bc878ef | 2001-09-12 17:09:24 +0000 | [diff] [blame] | 1214 | if (win) kill_timer( queue, win, req->msg, req->id ); |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1215 | |
| 1216 | if ((timer = set_timer( queue, req->rate ))) |
| 1217 | { |
Alexandre Julliard | bc878ef | 2001-09-12 17:09:24 +0000 | [diff] [blame] | 1218 | timer->win = win; |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1219 | timer->msg = req->msg; |
| 1220 | timer->id = req->id; |
| 1221 | timer->lparam = req->lparam; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | /* kill a window timer */ |
| 1226 | DECL_HANDLER(kill_win_timer) |
| 1227 | { |
| 1228 | struct msg_queue *queue = current->queue; |
| 1229 | |
Alexandre Julliard | bc878ef | 2001-09-12 17:09:24 +0000 | [diff] [blame] | 1230 | if (!queue || !kill_timer( queue, get_user_full_handle(req->win), req->msg, req->id )) |
Alexandre Julliard | 51ab43b | 2001-05-18 22:51:56 +0000 | [diff] [blame] | 1231 | set_error( STATUS_INVALID_PARAMETER ); |
| 1232 | } |
Alexandre Julliard | ab5063b | 2002-10-11 18:50:15 +0000 | [diff] [blame] | 1233 | |
| 1234 | |
| 1235 | /* attach (or detach) thread inputs */ |
| 1236 | DECL_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 */ |
| 1259 | DECL_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 | } |