2 * Server-side window handling
4 * Copyright (C) 2001 Alexandre Julliard
15 /* a window property */
18 unsigned short type; /* property type (see below) */
19 atom_t atom; /* property atom */
20 handle_t handle; /* property handle (user-defined storage) */
25 PROP_TYPE_FREE, /* free entry */
26 PROP_TYPE_STRING, /* atom that was originally a string */
27 PROP_TYPE_ATOM /* plain atom */
33 struct window *parent; /* parent window */
34 struct window *owner; /* owner of this window */
35 struct window *first_child; /* first child in Z-order */
36 struct window *last_child; /* last child in Z-order */
37 struct window *first_unlinked; /* first child not linked in the Z-order list */
38 struct window *next; /* next window in Z-order */
39 struct window *prev; /* prev window in Z-order */
40 user_handle_t handle; /* full handle for this window */
41 struct thread *thread; /* thread owning the window */
42 atom_t atom; /* class atom */
43 rectangle_t window_rect; /* window rectangle */
44 rectangle_t client_rect; /* client rectangle */
45 int prop_inuse; /* number of in-use window properties */
46 int prop_alloc; /* number of allocated window properties */
47 struct property *properties; /* window properties array */
50 static struct window *top_window; /* top-level (desktop) window */
53 /* retrieve a pointer to a window from its handle */
54 inline static struct window *get_window( user_handle_t handle )
56 struct window *ret = get_user_object( handle, USER_WINDOW );
57 if (!ret) set_error( STATUS_INVALID_HANDLE );
61 /* unlink a window from the tree */
62 static void unlink_window( struct window *win )
64 struct window *parent = win->parent;
68 if (win->next) win->next->prev = win->prev;
69 else if (parent->last_child == win) parent->last_child = win->prev;
71 if (win->prev) win->prev->next = win->next;
72 else if (parent->first_child == win) parent->first_child = win->next;
73 else if (parent->first_unlinked == win) parent->first_unlinked = win->next;
77 /* link a window into the tree (or unlink it if the new parent is NULL) */
78 static void link_window( struct window *win, struct window *parent, struct window *previous )
80 unlink_window( win ); /* unlink it from the previous location */
85 if ((win->prev = previous))
87 if ((win->next = previous->next)) win->next->prev = win;
88 else if (win->parent->last_child == previous) win->parent->last_child = win;
89 win->prev->next = win;
93 if ((win->next = parent->first_child)) win->next->prev = win;
94 else win->parent->last_child = win;
95 parent->first_child = win;
98 else /* move it to parent unlinked list */
100 parent = win->parent;
101 if ((win->next = parent->first_unlinked)) win->next->prev = win;
103 parent->first_unlinked = win;
107 /* set a window property */
108 static void set_property( struct window *win, atom_t atom, handle_t handle,
109 enum property_type type )
112 struct property *new_props;
114 /* check if it exists already */
115 for (i = 0; i < win->prop_inuse; i++)
117 if (win->properties[i].type == PROP_TYPE_FREE)
122 if (win->properties[i].atom == atom)
124 win->properties[i].type = type;
125 win->properties[i].handle = handle;
130 /* need to add an entry */
131 if (!grab_global_atom( atom )) return;
135 if (win->prop_inuse >= win->prop_alloc)
137 /* need to grow the array */
138 if (!(new_props = realloc( win->properties,
139 sizeof(*new_props) * (win->prop_alloc + 16) )))
141 set_error( STATUS_NO_MEMORY );
142 release_global_atom( atom );
145 win->prop_alloc += 16;
146 win->properties = new_props;
148 free = win->prop_inuse++;
150 win->properties[free].atom = atom;
151 win->properties[free].type = type;
152 win->properties[free].handle = handle;
155 /* remove a window property */
156 static handle_t remove_property( struct window *win, atom_t atom )
160 for (i = 0; i < win->prop_inuse; i++)
162 if (win->properties[i].type == PROP_TYPE_FREE) continue;
163 if (win->properties[i].atom == atom)
165 release_global_atom( atom );
166 win->properties[i].type = PROP_TYPE_FREE;
167 return win->properties[i].handle;
170 /* FIXME: last error? */
174 /* find a window property */
175 static handle_t get_property( struct window *win, atom_t atom )
179 for (i = 0; i < win->prop_inuse; i++)
181 if (win->properties[i].type == PROP_TYPE_FREE) continue;
182 if (win->properties[i].atom == atom) return win->properties[i].handle;
184 /* FIXME: last error? */
188 /* destroy all properties of a window */
189 inline static void destroy_properties( struct window *win )
193 if (!win->properties) return;
194 for (i = 0; i < win->prop_inuse; i++)
196 if (win->properties[i].type == PROP_TYPE_FREE) continue;
197 release_global_atom( win->properties[i].atom );
199 free( win->properties );
202 /* enum all properties into the data array */
203 static int enum_properties( struct window *win, property_data_t *data, int max )
207 for (i = count = 0; i < win->prop_inuse && count < max; i++)
209 if (win->properties[i].type == PROP_TYPE_FREE) continue;
210 data->atom = win->properties[i].atom;
211 data->string = (win->properties[i].type == PROP_TYPE_STRING);
212 data->handle = win->properties[i].handle;
219 /* destroy a window */
220 static void destroy_window( struct window *win )
222 assert( win != top_window );
224 /* destroy all children */
225 while (win->first_child) destroy_window( win->first_child );
226 while (win->first_unlinked) destroy_window( win->first_unlinked );
228 /* reset siblings owner */
232 for (ptr = win->parent->first_child; ptr; ptr = ptr->next)
233 if (ptr->owner == win) ptr->owner = NULL;
234 for (ptr = win->parent->first_unlinked; ptr; ptr = ptr->next)
235 if (ptr->owner == win) ptr->owner = NULL;
238 if (win->thread->queue) queue_cleanup_window( win->thread, win->handle );
239 free_user_handle( win->handle );
240 destroy_properties( win );
241 unlink_window( win );
242 memset( win, 0x55, sizeof(*win) );
246 /* create a new window structure (note: the window is not linked in the window tree) */
247 static struct window *create_window( struct window *parent, struct window *owner, atom_t atom )
249 struct window *win = mem_alloc( sizeof(*win) );
250 if (!win) return NULL;
252 if (!(win->handle = alloc_user_handle( win, USER_WINDOW )))
257 win->parent = parent;
259 win->first_child = NULL;
260 win->last_child = NULL;
261 win->first_unlinked = NULL;
262 win->thread = current;
266 win->properties = NULL;
268 if (parent) /* put it on parent unlinked list */
270 if ((win->next = parent->first_unlinked)) win->next->prev = win;
272 parent->first_unlinked = win;
274 else win->next = win->prev = NULL;
279 /* destroy all windows belonging to a given thread */
280 void destroy_thread_windows( struct thread *thread )
282 user_handle_t handle = 0;
285 while ((win = next_user_handle( &handle, USER_WINDOW )))
287 if (win->thread != thread) continue;
288 destroy_window( win );
292 /* check whether child is a descendant of parent */
293 int is_child_window( user_handle_t parent, user_handle_t child )
295 struct window *child_ptr = get_user_object( child, USER_WINDOW );
296 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
298 if (!child_ptr || !parent_ptr) return 0;
299 while (child_ptr->parent)
301 if (child_ptr->parent == parent_ptr) return 1;
302 child_ptr = child_ptr->parent;
307 /* create a window */
308 DECL_HANDLER(create_window)
311 if (!req->parent) /* return desktop window */
315 if (!(top_window = create_window( NULL, NULL, req->atom ))) return;
316 top_window->thread = NULL; /* no thread owns the desktop */
318 req->handle = top_window->handle;
322 struct window *win, *parent, *owner = NULL;
324 if (!(parent = get_window( req->parent ))) return;
325 if (req->owner && !(owner = get_window( req->owner ))) return;
326 if (!(win = create_window( parent, owner, req->atom ))) return;
327 req->handle = win->handle;
332 /* link a window into the tree */
333 DECL_HANDLER(link_window)
335 struct window *win, *parent = NULL, *previous = NULL;
337 if (!(win = get_window( req->handle ))) return;
338 if (req->parent && !(parent = get_window( req->parent ))) return;
340 if (win == top_window)
342 set_error( STATUS_INVALID_PARAMETER );
345 if (parent && req->previous)
347 if (req->previous == (user_handle_t)1) /* special case: HWND_BOTTOM */
349 previous = parent->last_child;
350 if (previous == win) return; /* nothing to do */
354 if (!(previous = get_window( req->previous ))) return;
355 /* previous must be a child of parent, and not win itself */
356 if (previous->parent != parent || previous == win)
358 set_error( STATUS_INVALID_PARAMETER );
363 link_window( win, parent, previous );
367 /* destroy a window */
368 DECL_HANDLER(destroy_window)
370 struct window *win = get_window( req->handle );
373 if (win != top_window) destroy_window( win );
374 else set_error( STATUS_ACCESS_DENIED );
379 /* get information from a window handle */
380 DECL_HANDLER(get_window_info)
382 struct window *win = get_window( req->handle );
384 req->full_handle = 0;
385 req->tid = req->pid = 0;
388 req->full_handle = win->handle;
391 req->tid = get_thread_id( win->thread );
392 req->pid = get_process_id( win->thread->process );
398 /* get a list of the window parents, up to the root of the tree */
399 DECL_HANDLER(get_window_parents)
401 struct window *ptr, *win = get_window( req->handle );
405 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
408 len = min( get_req_data_size(req), total * sizeof(user_handle_t) );
409 set_req_data_size( req, len );
412 user_handle_t *data = get_req_data(req);
413 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
414 *data++ = ptr->handle;
419 /* get a list of the window children */
420 DECL_HANDLER(get_window_children)
422 struct window *ptr, *parent = get_window( req->parent );
427 for (ptr = parent->first_child, total = 0; ptr; ptr = ptr->next)
429 if (req->atom && ptr->atom != req->atom) continue;
430 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
435 len = min( get_req_data_size(req), total * sizeof(user_handle_t) );
436 set_req_data_size( req, len );
439 user_handle_t *data = get_req_data(req);
440 for (ptr = parent->first_child; ptr && len; ptr = ptr->next, len -= sizeof(*data))
442 if (req->atom && ptr->atom != req->atom) continue;
443 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
444 *data++ = ptr->handle;
450 /* get window tree information from a window handle */
451 DECL_HANDLER(get_window_tree)
453 struct window *win = get_window( req->handle );
459 struct window *parent = win->parent;
460 req->parent = parent->handle;
461 req->owner = win->owner ? win->owner->handle : 0;
462 req->next_sibling = win->next ? win->next->handle : 0;
463 req->prev_sibling = win->prev ? win->prev->handle : 0;
464 req->first_sibling = parent->first_child ? parent->first_child->handle : 0;
465 req->last_sibling = parent->last_child ? parent->last_child->handle : 0;
471 req->next_sibling = 0;
472 req->prev_sibling = 0;
473 req->first_sibling = 0;
474 req->last_sibling = 0;
476 req->first_child = win->first_child ? win->first_child->handle : 0;
477 req->last_child = win->last_child ? win->last_child->handle : 0;
481 /* set the window and client rectangles of a window */
482 DECL_HANDLER(set_window_rectangles)
484 struct window *win = get_window( req->handle );
488 win->window_rect = req->window;
489 win->client_rect = req->client;
494 /* get the window and client rectangles of a window */
495 DECL_HANDLER(get_window_rectangles)
497 struct window *win = get_window( req->handle );
501 req->window = win->window_rect;
502 req->client = win->client_rect;
507 /* get the coordinates offset between two windows */
508 DECL_HANDLER(get_windows_offset)
515 if (!(win = get_window( req->from ))) return;
518 req->x += win->client_rect.left;
519 req->y += win->client_rect.top;
525 if (!(win = get_window( req->to ))) return;
528 req->x -= win->client_rect.left;
529 req->y -= win->client_rect.top;
536 /* set a window property */
537 DECL_HANDLER(set_window_property)
539 struct window *win = get_window( req->window );
541 if (win) set_property( win, req->atom, req->handle,
542 req->string ? PROP_TYPE_STRING : PROP_TYPE_ATOM );
546 /* remove a window property */
547 DECL_HANDLER(remove_window_property)
549 struct window *win = get_window( req->window );
551 if (win) req->handle = remove_property( win, req->atom );
555 /* get a window property */
556 DECL_HANDLER(get_window_property)
558 struct window *win = get_window( req->window );
560 if (win) req->handle = get_property( win, req->atom );
564 /* get the list of properties of a window */
565 DECL_HANDLER(get_window_properties)
568 property_data_t *data = get_req_data(req);
569 struct window *win = get_window( req->window );
571 if (win) count = enum_properties( win, data, get_req_data_size(req) / sizeof(*data) );
572 set_req_data_size( req, count * sizeof(*data) );