Redirect all reads and writes to VGA controller to VGA emulation.
[wine/multimedia.git] / server / window.c
blobc5f836ebc414ebb3d79728247e9f003a68d259ea
1 /*
2 * Server-side window handling
4 * Copyright (C) 2001 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
30 #include "object.h"
31 #include "request.h"
32 #include "thread.h"
33 #include "process.h"
34 #include "user.h"
35 #include "unicode.h"
37 /* a window property */
38 struct property
40 unsigned short type; /* property type (see below) */
41 atom_t atom; /* property atom */
42 obj_handle_t handle; /* property handle (user-defined storage) */
45 enum property_type
47 PROP_TYPE_FREE, /* free entry */
48 PROP_TYPE_STRING, /* atom that was originally a string */
49 PROP_TYPE_ATOM /* plain atom */
53 struct window
55 struct window *parent; /* parent window */
56 struct window *owner; /* owner of this window */
57 struct window *first_child; /* first child in Z-order */
58 struct window *last_child; /* last child in Z-order */
59 struct window *first_unlinked; /* first child not linked in the Z-order list */
60 struct window *next; /* next window in Z-order */
61 struct window *prev; /* prev window in Z-order */
62 user_handle_t handle; /* full handle for this window */
63 struct thread *thread; /* thread owning the window */
64 atom_t atom; /* class atom */
65 rectangle_t window_rect; /* window rectangle */
66 rectangle_t client_rect; /* client rectangle */
67 unsigned int style; /* window style */
68 unsigned int ex_style; /* window extended style */
69 unsigned int id; /* window id */
70 void* instance; /* creator instance */
71 void* user_data; /* user-specific data */
72 WCHAR *text; /* window caption text */
73 int paint_count; /* count of pending paints for this window */
74 int prop_inuse; /* number of in-use window properties */
75 int prop_alloc; /* number of allocated window properties */
76 struct property *properties; /* window properties array */
79 static struct window *top_window; /* top-level (desktop) window */
82 /* retrieve a pointer to a window from its handle */
83 inline static struct window *get_window( user_handle_t handle )
85 struct window *ret = get_user_object( handle, USER_WINDOW );
86 if (!ret) set_error( STATUS_INVALID_HANDLE );
87 return ret;
90 /* unlink a window from the tree */
91 static void unlink_window( struct window *win )
93 struct window *parent = win->parent;
95 assert( parent );
97 if (win->next) win->next->prev = win->prev;
98 else if (parent->last_child == win) parent->last_child = win->prev;
100 if (win->prev) win->prev->next = win->next;
101 else if (parent->first_child == win) parent->first_child = win->next;
102 else if (parent->first_unlinked == win) parent->first_unlinked = win->next;
106 /* link a window into the tree (or unlink it if the new parent is NULL) */
107 static void link_window( struct window *win, struct window *parent, struct window *previous )
109 unlink_window( win ); /* unlink it from the previous location */
111 if (parent)
113 if (win->parent != parent)
115 win->owner = NULL; /* reset owner if changing parent */
116 win->parent = parent;
118 if ((win->prev = previous))
120 if ((win->next = previous->next)) win->next->prev = win;
121 else if (win->parent->last_child == previous) win->parent->last_child = win;
122 win->prev->next = win;
124 else
126 if ((win->next = parent->first_child)) win->next->prev = win;
127 else win->parent->last_child = win;
128 parent->first_child = win;
131 else /* move it to parent unlinked list */
133 parent = win->parent;
134 if ((win->next = parent->first_unlinked)) win->next->prev = win;
135 win->prev = NULL;
136 parent->first_unlinked = win;
140 /* set a window property */
141 static void set_property( struct window *win, atom_t atom, obj_handle_t handle,
142 enum property_type type )
144 int i, free = -1;
145 struct property *new_props;
147 /* check if it exists already */
148 for (i = 0; i < win->prop_inuse; i++)
150 if (win->properties[i].type == PROP_TYPE_FREE)
152 free = i;
153 continue;
155 if (win->properties[i].atom == atom)
157 win->properties[i].type = type;
158 win->properties[i].handle = handle;
159 return;
163 /* need to add an entry */
164 if (!grab_global_atom( atom )) return;
165 if (free == -1)
167 /* no free entry */
168 if (win->prop_inuse >= win->prop_alloc)
170 /* need to grow the array */
171 if (!(new_props = realloc( win->properties,
172 sizeof(*new_props) * (win->prop_alloc + 16) )))
174 set_error( STATUS_NO_MEMORY );
175 release_global_atom( atom );
176 return;
178 win->prop_alloc += 16;
179 win->properties = new_props;
181 free = win->prop_inuse++;
183 win->properties[free].atom = atom;
184 win->properties[free].type = type;
185 win->properties[free].handle = handle;
188 /* remove a window property */
189 static obj_handle_t remove_property( struct window *win, atom_t atom )
191 int i;
193 for (i = 0; i < win->prop_inuse; i++)
195 if (win->properties[i].type == PROP_TYPE_FREE) continue;
196 if (win->properties[i].atom == atom)
198 release_global_atom( atom );
199 win->properties[i].type = PROP_TYPE_FREE;
200 return win->properties[i].handle;
203 /* FIXME: last error? */
204 return 0;
207 /* find a window property */
208 static obj_handle_t get_property( struct window *win, atom_t atom )
210 int i;
212 for (i = 0; i < win->prop_inuse; i++)
214 if (win->properties[i].type == PROP_TYPE_FREE) continue;
215 if (win->properties[i].atom == atom) return win->properties[i].handle;
217 /* FIXME: last error? */
218 return 0;
221 /* destroy all properties of a window */
222 inline static void destroy_properties( struct window *win )
224 int i;
226 if (!win->properties) return;
227 for (i = 0; i < win->prop_inuse; i++)
229 if (win->properties[i].type == PROP_TYPE_FREE) continue;
230 release_global_atom( win->properties[i].atom );
232 free( win->properties );
235 /* destroy a window */
236 static void destroy_window( struct window *win )
238 assert( win != top_window );
240 /* destroy all children */
241 while (win->first_child) destroy_window( win->first_child );
242 while (win->first_unlinked) destroy_window( win->first_unlinked );
244 /* reset siblings owner */
245 if (win->parent)
247 struct window *ptr;
248 for (ptr = win->parent->first_child; ptr; ptr = ptr->next)
249 if (ptr->owner == win) ptr->owner = NULL;
250 for (ptr = win->parent->first_unlinked; ptr; ptr = ptr->next)
251 if (ptr->owner == win) ptr->owner = NULL;
254 if (win->thread->queue)
256 if (win->paint_count) inc_queue_paint_count( win->thread, -win->paint_count );
257 queue_cleanup_window( win->thread, win->handle );
259 free_user_handle( win->handle );
260 destroy_properties( win );
261 unlink_window( win );
262 if (win->text) free( win->text );
263 memset( win, 0x55, sizeof(*win) );
264 free( win );
267 /* create a new window structure (note: the window is not linked in the window tree) */
268 static struct window *create_window( struct window *parent, struct window *owner, atom_t atom )
270 struct window *win = mem_alloc( sizeof(*win) );
271 if (!win) return NULL;
273 if (!(win->handle = alloc_user_handle( win, USER_WINDOW )))
275 free( win );
276 return NULL;
278 win->parent = parent;
279 win->owner = owner;
280 win->first_child = NULL;
281 win->last_child = NULL;
282 win->first_unlinked = NULL;
283 win->thread = current;
284 win->atom = atom;
285 win->style = 0;
286 win->ex_style = 0;
287 win->id = 0;
288 win->instance = NULL;
289 win->user_data = NULL;
290 win->text = NULL;
291 win->paint_count = 0;
292 win->prop_inuse = 0;
293 win->prop_alloc = 0;
294 win->properties = NULL;
296 if (parent) /* put it on parent unlinked list */
298 if ((win->next = parent->first_unlinked)) win->next->prev = win;
299 win->prev = NULL;
300 parent->first_unlinked = win;
302 else win->next = win->prev = NULL;
304 return win;
307 /* destroy all windows belonging to a given thread */
308 void destroy_thread_windows( struct thread *thread )
310 user_handle_t handle = 0;
311 struct window *win;
313 while ((win = next_user_handle( &handle, USER_WINDOW )))
315 if (win->thread != thread) continue;
316 destroy_window( win );
320 /* check whether child is a descendant of parent */
321 int is_child_window( user_handle_t parent, user_handle_t child )
323 struct window *child_ptr = get_user_object( child, USER_WINDOW );
324 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
326 if (!child_ptr || !parent_ptr) return 0;
327 while (child_ptr->parent)
329 if (child_ptr->parent == parent_ptr) return 1;
330 child_ptr = child_ptr->parent;
332 return 0;
335 /* return the thread owning a window */
336 struct thread *get_window_thread( user_handle_t handle )
338 struct window *win = get_user_object( handle, USER_WINDOW );
339 if (!win || !win->thread) return NULL;
340 return (struct thread *)grab_object( win->thread );
343 /* find a child of the specified window that needs repainting */
344 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
346 struct window *ptr, *ret = NULL;
348 for (ptr = parent->first_child; ptr && !ret; ptr = ptr->next)
350 if (!(ptr->style & WS_VISIBLE)) continue;
351 if (ptr->paint_count && ptr->thread == thread)
352 ret = ptr;
353 else /* explore its children */
354 ret = find_child_to_repaint( ptr, thread );
357 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
359 /* transparent window, check for non-transparent sibling to paint first */
360 for (ptr = ret->next; ptr; ptr = ptr->next)
362 if (!(ptr->style & WS_VISIBLE)) continue;
363 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
364 if (ptr->paint_count && ptr->thread == thread) return ptr;
367 return ret;
371 /* find a window that needs repainting */
372 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
374 struct window *win = parent ? get_window( parent ) : top_window;
376 if (!win || !(win->style & WS_VISIBLE)) return 0;
377 if (!win->paint_count || win->thread != thread)
378 win = find_child_to_repaint( win, thread );
379 return win ? win->handle : 0;
383 /* create a window */
384 DECL_HANDLER(create_window)
386 reply->handle = 0;
387 if (!req->parent) /* return desktop window */
389 if (!top_window)
391 if (!(top_window = create_window( NULL, NULL, req->atom ))) return;
392 top_window->thread = NULL; /* no thread owns the desktop */
394 reply->handle = top_window->handle;
396 else
398 struct window *win, *parent, *owner = NULL;
400 if (!(parent = get_window( req->parent ))) return;
401 if (req->owner && !(owner = get_window( req->owner ))) return;
402 if (owner == top_window) owner = NULL;
403 else if (owner && owner->parent != parent)
405 /* owner must be a sibling of the new window */
406 set_error( STATUS_ACCESS_DENIED );
407 return;
409 if (!(win = create_window( parent, owner, req->atom ))) return;
410 reply->handle = win->handle;
415 /* link a window into the tree */
416 DECL_HANDLER(link_window)
418 struct window *win, *parent = NULL, *previous = NULL;
420 if (!(win = get_window( req->handle ))) return;
421 if (req->parent && !(parent = get_window( req->parent ))) return;
423 if (win == top_window)
425 set_error( STATUS_INVALID_PARAMETER );
426 return;
428 reply->full_parent = parent ? parent->handle : 0;
429 if (parent && req->previous)
431 if (req->previous == (user_handle_t)1) /* special case: HWND_BOTTOM */
433 previous = parent->last_child;
434 if (previous == win) return; /* nothing to do */
436 else
438 if (!(previous = get_window( req->previous ))) return;
439 /* previous must be a child of parent, and not win itself */
440 if (previous->parent != parent || previous == win)
442 set_error( STATUS_INVALID_PARAMETER );
443 return;
447 link_window( win, parent, previous );
451 /* destroy a window */
452 DECL_HANDLER(destroy_window)
454 struct window *win = get_window( req->handle );
455 if (win)
457 if (win != top_window) destroy_window( win );
458 else set_error( STATUS_ACCESS_DENIED );
463 /* set a window owner */
464 DECL_HANDLER(set_window_owner)
466 struct window *win = get_window( req->handle );
467 struct window *owner = get_window( req->owner );
469 if (!win || !owner) return;
470 if (owner->parent != win->parent)
472 /* owner has to be a sibling of window */
473 set_error( STATUS_ACCESS_DENIED );
474 return;
476 win->owner = owner;
477 reply->full_owner = owner->handle;
481 /* get information from a window handle */
482 DECL_HANDLER(get_window_info)
484 struct window *win = get_window( req->handle );
486 reply->full_handle = 0;
487 reply->tid = reply->pid = 0;
488 if (win)
490 reply->full_handle = win->handle;
491 if (win->thread)
493 reply->tid = get_thread_id( win->thread );
494 reply->pid = get_process_id( win->thread->process );
495 reply->atom = win->atom;
501 /* set some information in a window */
502 DECL_HANDLER(set_window_info)
504 struct window *win = get_window( req->handle );
505 if (!win) return;
506 reply->old_style = win->style;
507 reply->old_ex_style = win->ex_style;
508 reply->old_id = win->id;
509 reply->old_instance = win->instance;
510 reply->old_user_data = win->user_data;
511 if (req->flags & SET_WIN_STYLE) win->style = req->style;
512 if (req->flags & SET_WIN_EXSTYLE) win->ex_style = req->ex_style;
513 if (req->flags & SET_WIN_ID) win->id = req->id;
514 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
515 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
519 /* get a list of the window parents, up to the root of the tree */
520 DECL_HANDLER(get_window_parents)
522 struct window *ptr, *win = get_window( req->handle );
523 int total = 0;
524 user_handle_t *data;
525 size_t len;
527 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
529 reply->count = total;
530 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
531 if (len && ((data = set_reply_data_size( len ))))
533 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
534 *data++ = ptr->handle;
539 /* get a list of the window children */
540 DECL_HANDLER(get_window_children)
542 struct window *ptr, *parent = get_window( req->parent );
543 int total = 0;
544 user_handle_t *data;
545 size_t len;
547 if (parent)
548 for (ptr = parent->first_child, total = 0; ptr; ptr = ptr->next)
550 if (req->atom && ptr->atom != req->atom) continue;
551 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
552 total++;
555 reply->count = total;
556 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
557 if (len && ((data = set_reply_data_size( len ))))
559 for (ptr = parent->first_child; ptr && len; ptr = ptr->next)
561 if (req->atom && ptr->atom != req->atom) continue;
562 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
563 *data++ = ptr->handle;
564 len -= sizeof(*data);
570 /* get window tree information from a window handle */
571 DECL_HANDLER(get_window_tree)
573 struct window *win = get_window( req->handle );
575 if (!win) return;
577 if (win->parent)
579 struct window *parent = win->parent;
580 reply->parent = parent->handle;
581 reply->owner = win->owner ? win->owner->handle : 0;
582 reply->next_sibling = win->next ? win->next->handle : 0;
583 reply->prev_sibling = win->prev ? win->prev->handle : 0;
584 reply->first_sibling = parent->first_child ? parent->first_child->handle : 0;
585 reply->last_sibling = parent->last_child ? parent->last_child->handle : 0;
587 else
589 reply->parent = 0;
590 reply->owner = 0;
591 reply->next_sibling = 0;
592 reply->prev_sibling = 0;
593 reply->first_sibling = 0;
594 reply->last_sibling = 0;
596 reply->first_child = win->first_child ? win->first_child->handle : 0;
597 reply->last_child = win->last_child ? win->last_child->handle : 0;
601 /* set the window and client rectangles of a window */
602 DECL_HANDLER(set_window_rectangles)
604 struct window *win = get_window( req->handle );
606 if (win)
608 win->window_rect = req->window;
609 win->client_rect = req->client;
614 /* get the window and client rectangles of a window */
615 DECL_HANDLER(get_window_rectangles)
617 struct window *win = get_window( req->handle );
619 if (win)
621 reply->window = win->window_rect;
622 reply->client = win->client_rect;
627 /* get the window text */
628 DECL_HANDLER(get_window_text)
630 struct window *win = get_window( req->handle );
632 if (win && win->text)
634 size_t len = strlenW( win->text ) * sizeof(WCHAR);
635 if (len > get_reply_max_size()) len = get_reply_max_size();
636 set_reply_data( win->text, len );
641 /* set the window text */
642 DECL_HANDLER(set_window_text)
644 struct window *win = get_window( req->handle );
646 if (win)
648 WCHAR *text = NULL;
649 size_t len = get_req_data_size() / sizeof(WCHAR);
650 if (len)
652 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
653 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
654 text[len] = 0;
656 if (win->text) free( win->text );
657 win->text = text;
662 /* increment the window paint count */
663 DECL_HANDLER(inc_window_paint_count)
665 struct window *win = get_window( req->handle );
667 if (win && win->thread)
669 int old = win->paint_count;
670 if ((win->paint_count += req->incr) < 0) win->paint_count = 0;
671 inc_queue_paint_count( win->thread, win->paint_count - old );
676 /* get the coordinates offset between two windows */
677 DECL_HANDLER(get_windows_offset)
679 struct window *win;
681 reply->x = reply->y = 0;
682 if (req->from)
684 if (!(win = get_window( req->from ))) return;
685 while (win)
687 reply->x += win->client_rect.left;
688 reply->y += win->client_rect.top;
689 win = win->parent;
692 if (req->to)
694 if (!(win = get_window( req->to ))) return;
695 while (win)
697 reply->x -= win->client_rect.left;
698 reply->y -= win->client_rect.top;
699 win = win->parent;
705 /* set a window property */
706 DECL_HANDLER(set_window_property)
708 struct window *win = get_window( req->window );
710 if (win) set_property( win, req->atom, req->handle,
711 req->string ? PROP_TYPE_STRING : PROP_TYPE_ATOM );
715 /* remove a window property */
716 DECL_HANDLER(remove_window_property)
718 struct window *win = get_window( req->window );
719 reply->handle = 0;
720 if (win) reply->handle = remove_property( win, req->atom );
724 /* get a window property */
725 DECL_HANDLER(get_window_property)
727 struct window *win = get_window( req->window );
728 reply->handle = 0;
729 if (win) reply->handle = get_property( win, req->atom );
733 /* get the list of properties of a window */
734 DECL_HANDLER(get_window_properties)
736 property_data_t *data;
737 int i, count, max = get_reply_max_size() / sizeof(*data);
738 struct window *win = get_window( req->window );
740 reply->total = 0;
741 if (!win) return;
743 for (i = count = 0; i < win->prop_inuse; i++)
744 if (win->properties[i].type != PROP_TYPE_FREE) count++;
745 reply->total = count;
747 if (count > max) count = max;
748 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
750 for (i = 0; i < win->prop_inuse && count; i++)
752 if (win->properties[i].type == PROP_TYPE_FREE) continue;
753 data->atom = win->properties[i].atom;
754 data->string = (win->properties[i].type == PROP_TYPE_STRING);
755 data->handle = win->properties[i].handle;
756 data++;
757 count--;