2001-08-28 18:44:52 +00:00
|
|
|
/*
|
|
|
|
* Server-side window handling
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001 Alexandre Julliard
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "object.h"
|
|
|
|
#include "request.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "process.h"
|
|
|
|
#include "user.h"
|
|
|
|
|
|
|
|
struct window
|
|
|
|
{
|
2001-09-21 21:08:40 +00:00
|
|
|
struct window *parent; /* parent window */
|
|
|
|
struct window *owner; /* owner of this window */
|
|
|
|
struct window *first_child; /* first child window */
|
|
|
|
struct window *last_child; /* last child window */
|
|
|
|
struct window *next; /* next window in Z-order */
|
|
|
|
struct window *prev; /* prev window in Z-order */
|
|
|
|
user_handle_t handle; /* full handle for this window */
|
|
|
|
struct thread *thread; /* thread owning the window */
|
2001-08-28 18:44:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct window *top_window; /* top-level (desktop) window */
|
|
|
|
|
2001-09-21 21:08:40 +00:00
|
|
|
|
|
|
|
/* retrieve a pointer to a window from its handle */
|
|
|
|
inline static struct window *get_window( user_handle_t handle )
|
|
|
|
{
|
|
|
|
struct window *ret = get_user_object( handle, USER_WINDOW );
|
|
|
|
if (!ret) set_error( STATUS_INVALID_HANDLE );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-08-28 18:44:52 +00:00
|
|
|
/* link a window into the tree (or unlink it if the new parent is NULL) */
|
|
|
|
static void link_window( struct window *win, struct window *parent, struct window *previous )
|
|
|
|
{
|
|
|
|
if (win->parent) /* unlink it from the previous location */
|
|
|
|
{
|
|
|
|
if (win->next) win->next->prev = win->prev;
|
2001-09-21 21:08:40 +00:00
|
|
|
else if (win->parent->last_child == win) win->parent->last_child = win->prev;
|
2001-08-28 18:44:52 +00:00
|
|
|
if (win->prev) win->prev->next = win->next;
|
2001-09-21 21:08:40 +00:00
|
|
|
else if (win->parent->first_child == win) win->parent->first_child = win->next;
|
2001-08-28 18:44:52 +00:00
|
|
|
}
|
2001-09-21 21:08:40 +00:00
|
|
|
if (parent)
|
2001-08-28 18:44:52 +00:00
|
|
|
{
|
2001-09-21 21:08:40 +00:00
|
|
|
win->parent = parent;
|
2001-08-28 18:44:52 +00:00
|
|
|
if ((win->prev = previous))
|
|
|
|
{
|
|
|
|
if ((win->next = previous->next)) win->next->prev = win;
|
2001-09-21 21:08:40 +00:00
|
|
|
else if (win->parent->last_child == previous) win->parent->last_child = win;
|
2001-08-28 18:44:52 +00:00
|
|
|
win->prev->next = win;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-09-21 21:08:40 +00:00
|
|
|
if ((win->next = parent->first_child)) win->next->prev = win;
|
|
|
|
else win->parent->last_child = win;
|
|
|
|
parent->first_child = win;
|
2001-08-28 18:44:52 +00:00
|
|
|
}
|
|
|
|
}
|
2001-09-21 21:08:40 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* don't touch parent; an unlinked window still has a valid parent pointer */
|
|
|
|
win->next = win->prev = NULL;
|
|
|
|
}
|
2001-08-28 18:44:52 +00:00
|
|
|
}
|
|
|
|
|
2001-09-21 21:08:40 +00:00
|
|
|
/* destroy a window */
|
|
|
|
static void destroy_window( struct window *win )
|
2001-08-28 18:44:52 +00:00
|
|
|
{
|
|
|
|
assert( win != top_window );
|
|
|
|
|
2001-09-21 21:08:40 +00:00
|
|
|
/* destroy all children */
|
|
|
|
while (win->first_child) destroy_window( win->first_child );
|
|
|
|
|
|
|
|
/* reset siblings owner */
|
|
|
|
if (win->parent)
|
|
|
|
{
|
|
|
|
struct window *ptr;
|
|
|
|
for (ptr = win->parent->first_child; ptr; ptr = ptr->next)
|
|
|
|
if (ptr->owner == win) ptr->owner = NULL;
|
|
|
|
}
|
|
|
|
|
2001-08-28 18:44:52 +00:00
|
|
|
if (win->thread->queue) queue_cleanup_window( win->thread, win->handle );
|
|
|
|
free_user_handle( win->handle );
|
2001-09-21 21:08:40 +00:00
|
|
|
link_window( win, NULL, NULL );
|
2001-08-28 18:44:52 +00:00
|
|
|
memset( win, 0x55, sizeof(*win) );
|
|
|
|
free( win );
|
|
|
|
}
|
|
|
|
|
2001-09-21 21:08:40 +00:00
|
|
|
/* create a new window structure (note: the window is not linked in the window tree) */
|
|
|
|
static struct window *create_window( struct window *parent, struct window *owner )
|
2001-08-28 18:44:52 +00:00
|
|
|
{
|
|
|
|
struct window *win = mem_alloc( sizeof(*win) );
|
|
|
|
if (!win) return NULL;
|
|
|
|
|
|
|
|
if (!(win->handle = alloc_user_handle( win, USER_WINDOW )))
|
|
|
|
{
|
|
|
|
free( win );
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-09-21 21:08:40 +00:00
|
|
|
win->parent = parent;
|
|
|
|
win->owner = owner;
|
|
|
|
win->first_child = NULL;
|
|
|
|
win->last_child = NULL;
|
|
|
|
win->next = NULL;
|
|
|
|
win->prev = NULL;
|
|
|
|
win->thread = current;
|
2001-08-28 18:44:52 +00:00
|
|
|
return win;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* destroy all windows belonging to a given thread */
|
|
|
|
void destroy_thread_windows( struct thread *thread )
|
|
|
|
{
|
|
|
|
user_handle_t handle = 0;
|
|
|
|
struct window *win;
|
|
|
|
|
|
|
|
while ((win = next_user_handle( &handle, USER_WINDOW )))
|
|
|
|
{
|
|
|
|
if (win->thread != thread) continue;
|
2001-09-21 21:08:40 +00:00
|
|
|
destroy_window( win );
|
2001-08-28 18:44:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-21 21:08:40 +00:00
|
|
|
/* check whether child is a descendant of parent */
|
|
|
|
int is_child_window( user_handle_t parent, user_handle_t child )
|
2001-08-28 18:44:52 +00:00
|
|
|
{
|
2001-09-21 21:08:40 +00:00
|
|
|
struct window *child_ptr = get_user_object( child, USER_WINDOW );
|
|
|
|
struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
|
2001-08-28 18:44:52 +00:00
|
|
|
|
2001-09-21 21:08:40 +00:00
|
|
|
if (!child_ptr || !parent_ptr) return 0;
|
|
|
|
while (child_ptr->parent)
|
|
|
|
{
|
|
|
|
if (child_ptr->parent == parent_ptr) return 1;
|
|
|
|
child_ptr = child_ptr->parent;
|
|
|
|
}
|
|
|
|
return 0;
|
2001-08-28 18:44:52 +00:00
|
|
|
}
|
|
|
|
|
2001-09-21 21:08:40 +00:00
|
|
|
/* create a window */
|
|
|
|
DECL_HANDLER(create_window)
|
2001-08-28 18:44:52 +00:00
|
|
|
{
|
|
|
|
req->handle = 0;
|
2001-09-21 21:08:40 +00:00
|
|
|
if (!req->parent) /* return desktop window */
|
2001-08-28 18:44:52 +00:00
|
|
|
{
|
2001-09-21 21:08:40 +00:00
|
|
|
if (!top_window)
|
|
|
|
{
|
|
|
|
if (!(top_window = create_window( NULL, NULL ))) return;
|
|
|
|
top_window->thread = NULL; /* no thread owns the desktop */
|
|
|
|
}
|
|
|
|
req->handle = top_window->handle;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct window *win, *parent, *owner = NULL;
|
|
|
|
|
|
|
|
if (!(parent = get_window( req->parent ))) return;
|
|
|
|
if (req->owner && !(owner = get_window( req->owner ))) return;
|
|
|
|
if (!(win = create_window( parent, owner ))) return;
|
|
|
|
req->handle = win->handle;
|
2001-08-28 18:44:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* link a window into the tree */
|
|
|
|
DECL_HANDLER(link_window)
|
|
|
|
{
|
|
|
|
struct window *win, *parent = NULL, *previous = NULL;
|
|
|
|
|
2001-09-21 21:08:40 +00:00
|
|
|
if (!(win = get_window( req->handle ))) return;
|
|
|
|
if (req->parent && !(parent = get_window( req->parent ))) return;
|
2001-08-28 18:44:52 +00:00
|
|
|
if (parent && req->previous)
|
|
|
|
{
|
|
|
|
if (req->previous == 1) /* special case: HWND_BOTTOM */
|
2001-09-21 21:08:40 +00:00
|
|
|
previous = parent->last_child;
|
|
|
|
else
|
|
|
|
if (!(previous = get_window( req->previous ))) return;
|
2001-08-28 18:44:52 +00:00
|
|
|
|
|
|
|
if (previous && previous->parent != parent) /* previous must be a child of parent */
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
link_window( win, parent, previous );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* destroy a window */
|
|
|
|
DECL_HANDLER(destroy_window)
|
|
|
|
{
|
2001-09-21 21:08:40 +00:00
|
|
|
struct window *win = get_window( req->handle );
|
2001-08-28 18:44:52 +00:00
|
|
|
if (win)
|
|
|
|
{
|
2001-09-21 21:08:40 +00:00
|
|
|
if (win != top_window) destroy_window( win );
|
2001-08-28 18:44:52 +00:00
|
|
|
else set_error( STATUS_ACCESS_DENIED );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-21 21:08:40 +00:00
|
|
|
/* get information from a window handle */
|
2001-08-28 18:44:52 +00:00
|
|
|
DECL_HANDLER(get_window_info)
|
|
|
|
{
|
2001-09-21 21:08:40 +00:00
|
|
|
struct window *win = get_window( req->handle );
|
2001-08-28 18:44:52 +00:00
|
|
|
|
|
|
|
req->full_handle = 0;
|
|
|
|
req->tid = req->pid = 0;
|
|
|
|
if (win)
|
|
|
|
{
|
2001-09-21 21:08:40 +00:00
|
|
|
req->full_handle = win->handle;
|
2001-08-28 18:44:52 +00:00
|
|
|
if (win->thread)
|
|
|
|
{
|
|
|
|
req->tid = get_thread_id( win->thread );
|
|
|
|
req->pid = get_process_id( win->thread->process );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-09-21 21:08:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* get a list of the window parents, up to the root of the tree */
|
|
|
|
DECL_HANDLER(get_window_parents)
|
|
|
|
{
|
|
|
|
struct window *ptr, *win = get_window( req->handle );
|
|
|
|
int total = 0;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
|
|
|
|
|
|
|
|
req->count = total;
|
|
|
|
len = min( get_req_data_size(req), total * sizeof(user_handle_t) );
|
|
|
|
set_req_data_size( req, len );
|
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
user_handle_t *data = get_req_data(req);
|
|
|
|
for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
|
|
|
|
*data++ = ptr->handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* get a list of the window children */
|
|
|
|
DECL_HANDLER(get_window_children)
|
|
|
|
{
|
|
|
|
struct window *ptr, *parent = get_window( req->parent );
|
|
|
|
int total = 0;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (parent)
|
|
|
|
for (ptr = parent->first_child, total = 0; ptr; ptr = ptr->next) total++;
|
|
|
|
|
|
|
|
req->count = total;
|
|
|
|
len = min( get_req_data_size(req), total * sizeof(user_handle_t) );
|
|
|
|
set_req_data_size( req, len );
|
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
user_handle_t *data = get_req_data(req);
|
|
|
|
for (ptr = parent->first_child; ptr && len; ptr = ptr->next, len -= sizeof(*data))
|
|
|
|
*data++ = ptr->handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* get window tree information from a window handle */
|
|
|
|
DECL_HANDLER(get_window_tree)
|
|
|
|
{
|
|
|
|
struct window *win = get_window( req->handle );
|
|
|
|
|
|
|
|
if (!win) return;
|
|
|
|
|
|
|
|
if (win->parent)
|
|
|
|
{
|
|
|
|
struct window *parent = win->parent;
|
|
|
|
req->parent = parent->handle;
|
|
|
|
req->owner = win->owner ? win->owner->handle : 0;
|
|
|
|
req->next_sibling = win->next ? win->next->handle : 0;
|
|
|
|
req->prev_sibling = win->prev ? win->prev->handle : 0;
|
|
|
|
req->first_sibling = parent->first_child ? parent->first_child->handle : 0;
|
|
|
|
req->last_sibling = parent->last_child ? parent->last_child->handle : 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
req->parent = 0;
|
|
|
|
req->owner = 0;
|
|
|
|
req->next_sibling = 0;
|
|
|
|
req->prev_sibling = 0;
|
|
|
|
req->first_sibling = 0;
|
|
|
|
req->last_sibling = 0;
|
|
|
|
}
|
|
|
|
req->first_child = win->first_child ? win->first_child->handle : 0;
|
|
|
|
req->last_child = win->last_child ? win->last_child->handle : 0;
|
|
|
|
}
|