Added support for multiple object namespaces, and a bunch functions
[wine] / server / window.c
1 /*
2  * Server-side window handling
3  *
4  * Copyright (C) 2001 Alexandre Julliard
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29
30 #include "object.h"
31 #include "request.h"
32 #include "thread.h"
33 #include "process.h"
34 #include "user.h"
35 #include "unicode.h"
36
37 /* a window property */
38 struct property
39 {
40     unsigned short type;     /* property type (see below) */
41     atom_t         atom;     /* property atom */
42     obj_handle_t   handle;   /* property handle (user-defined storage) */
43 };
44
45 enum property_type
46 {
47     PROP_TYPE_FREE,   /* free entry */
48     PROP_TYPE_STRING, /* atom that was originally a string */
49     PROP_TYPE_ATOM    /* plain atom */
50 };
51
52
53 struct window
54 {
55     struct window   *parent;          /* parent window */
56     user_handle_t    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 */
77 };
78
79 static struct window *top_window;  /* top-level (desktop) window */
80
81
82 /* retrieve a pointer to a window from its handle */
83 inline static struct window *get_window( user_handle_t handle )
84 {
85     struct window *ret = get_user_object( handle, USER_WINDOW );
86     if (!ret) set_error( STATUS_INVALID_HANDLE );
87     return ret;
88 }
89
90 /* unlink a window from the tree */
91 static void unlink_window( struct window *win )
92 {
93     struct window *parent = win->parent;
94
95     assert( parent );
96
97     if (win->next) win->next->prev = win->prev;
98     else if (parent->last_child == win) parent->last_child = win->prev;
99
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;
103 }
104
105
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 )
108 {
109     unlink_window( win );  /* unlink it from the previous location */
110
111     if (parent)
112     {
113         win->parent = parent;
114         if ((win->prev = previous))
115         {
116             if ((win->next = previous->next)) win->next->prev = win;
117             else if (win->parent->last_child == previous) win->parent->last_child = win;
118             win->prev->next = win;
119         }
120         else
121         {
122             if ((win->next = parent->first_child)) win->next->prev = win;
123             else win->parent->last_child = win;
124             parent->first_child = win;
125         }
126     }
127     else  /* move it to parent unlinked list */
128     {
129         parent = win->parent;
130         if ((win->next = parent->first_unlinked)) win->next->prev = win;
131         win->prev = NULL;
132         parent->first_unlinked = win;
133     }
134 }
135
136 /* set a window property */
137 static void set_property( struct window *win, atom_t atom, obj_handle_t handle,
138                           enum property_type type )
139 {
140     int i, free = -1;
141     struct property *new_props;
142
143     /* check if it exists already */
144     for (i = 0; i < win->prop_inuse; i++)
145     {
146         if (win->properties[i].type == PROP_TYPE_FREE)
147         {
148             free = i;
149             continue;
150         }
151         if (win->properties[i].atom == atom)
152         {
153             win->properties[i].type = type;
154             win->properties[i].handle = handle;
155             return;
156         }
157     }
158
159     /* need to add an entry */
160     if (!grab_global_atom( atom )) return;
161     if (free == -1)
162     {
163         /* no free entry */
164         if (win->prop_inuse >= win->prop_alloc)
165         {
166             /* need to grow the array */
167             if (!(new_props = realloc( win->properties,
168                                        sizeof(*new_props) * (win->prop_alloc + 16) )))
169             {
170                 set_error( STATUS_NO_MEMORY );
171                 release_global_atom( atom );
172                 return;
173             }
174             win->prop_alloc += 16;
175             win->properties = new_props;
176         }
177         free = win->prop_inuse++;
178     }
179     win->properties[free].atom   = atom;
180     win->properties[free].type   = type;
181     win->properties[free].handle = handle;
182 }
183
184 /* remove a window property */
185 static obj_handle_t remove_property( struct window *win, atom_t atom )
186 {
187     int i;
188
189     for (i = 0; i < win->prop_inuse; i++)
190     {
191         if (win->properties[i].type == PROP_TYPE_FREE) continue;
192         if (win->properties[i].atom == atom)
193         {
194             release_global_atom( atom );
195             win->properties[i].type = PROP_TYPE_FREE;
196             return win->properties[i].handle;
197         }
198     }
199     /* FIXME: last error? */
200     return 0;
201 }
202
203 /* find a window property */
204 static obj_handle_t get_property( struct window *win, atom_t atom )
205 {
206     int i;
207
208     for (i = 0; i < win->prop_inuse; i++)
209     {
210         if (win->properties[i].type == PROP_TYPE_FREE) continue;
211         if (win->properties[i].atom == atom) return win->properties[i].handle;
212     }
213     /* FIXME: last error? */
214     return 0;
215 }
216
217 /* destroy all properties of a window */
218 inline static void destroy_properties( struct window *win )
219 {
220     int i;
221
222     if (!win->properties) return;
223     for (i = 0; i < win->prop_inuse; i++)
224     {
225         if (win->properties[i].type == PROP_TYPE_FREE) continue;
226         release_global_atom( win->properties[i].atom );
227     }
228     free( win->properties );
229 }
230
231 /* destroy a window */
232 static void destroy_window( struct window *win )
233 {
234     assert( win != top_window );
235
236     /* destroy all children */
237     while (win->first_child) destroy_window( win->first_child );
238     while (win->first_unlinked) destroy_window( win->first_unlinked );
239
240     if (win->thread->queue)
241     {
242         if (win->paint_count) inc_queue_paint_count( win->thread, -win->paint_count );
243         queue_cleanup_window( win->thread, win->handle );
244     }
245     free_user_handle( win->handle );
246     destroy_properties( win );
247     unlink_window( win );
248     if (win->text) free( win->text );
249     memset( win, 0x55, sizeof(*win) );
250     free( win );
251 }
252
253 /* create a new window structure (note: the window is not linked in the window tree) */
254 static struct window *create_window( struct window *parent, struct window *owner, atom_t atom )
255 {
256     struct window *win = mem_alloc( sizeof(*win) );
257     if (!win) return NULL;
258
259     if (!(win->handle = alloc_user_handle( win, USER_WINDOW )))
260     {
261         free( win );
262         return NULL;
263     }
264     win->parent         = parent;
265     win->owner          = owner ? owner->handle : 0;
266     win->first_child    = NULL;
267     win->last_child     = NULL;
268     win->first_unlinked = NULL;
269     win->thread         = current;
270     win->atom           = atom;
271     win->style          = 0;
272     win->ex_style       = 0;
273     win->id             = 0;
274     win->instance       = NULL;
275     win->user_data      = NULL;
276     win->text           = NULL;
277     win->paint_count    = 0;
278     win->prop_inuse     = 0;
279     win->prop_alloc     = 0;
280     win->properties     = NULL;
281
282     if (parent)  /* put it on parent unlinked list */
283     {
284         if ((win->next = parent->first_unlinked)) win->next->prev = win;
285         win->prev = NULL;
286         parent->first_unlinked = win;
287     }
288     else win->next = win->prev = NULL;
289
290     return win;
291 }
292
293 /* destroy all windows belonging to a given thread */
294 void destroy_thread_windows( struct thread *thread )
295 {
296     user_handle_t handle = 0;
297     struct window *win;
298
299     while ((win = next_user_handle( &handle, USER_WINDOW )))
300     {
301         if (win->thread != thread) continue;
302         destroy_window( win );
303     }
304 }
305
306 /* check whether child is a descendant of parent */
307 int is_child_window( user_handle_t parent, user_handle_t child )
308 {
309     struct window *child_ptr = get_user_object( child, USER_WINDOW );
310     struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
311
312     if (!child_ptr || !parent_ptr) return 0;
313     while (child_ptr->parent)
314     {
315         if (child_ptr->parent == parent_ptr) return 1;
316         child_ptr = child_ptr->parent;
317     }
318     return 0;
319 }
320
321 /* return the thread owning a window */
322 struct thread *get_window_thread( user_handle_t handle )
323 {
324     struct window *win = get_user_object( handle, USER_WINDOW );
325     if (!win || !win->thread) return NULL;
326     return (struct thread *)grab_object( win->thread );
327 }
328
329 /* find a child of the specified window that needs repainting */
330 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
331 {
332     struct window *ptr, *ret = NULL;
333
334     for (ptr = parent->first_child; ptr && !ret; ptr = ptr->next)
335     {
336         if (!(ptr->style & WS_VISIBLE)) continue;
337         if (ptr->paint_count && ptr->thread == thread)
338             ret = ptr;
339         else /* explore its children */
340             ret = find_child_to_repaint( ptr, thread );
341     }
342
343     if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
344     {
345         /* transparent window, check for non-transparent sibling to paint first */
346         for (ptr = ret->next; ptr; ptr = ptr->next)
347         {
348             if (!(ptr->style & WS_VISIBLE)) continue;
349             if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
350             if (ptr->paint_count && ptr->thread == thread) return ptr;
351         }
352     }
353     return ret;
354 }
355
356
357 /* find a window that needs repainting */
358 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
359 {
360     struct window *win = parent ? get_window( parent ) : top_window;
361
362     if (!win || !(win->style & WS_VISIBLE)) return 0;
363     if (!win->paint_count || win->thread != thread)
364         win = find_child_to_repaint( win, thread );
365     return win ? win->handle : 0;
366 }
367
368
369 /* create a window */
370 DECL_HANDLER(create_window)
371 {
372     reply->handle = 0;
373     if (!req->parent)  /* return desktop window */
374     {
375         if (!top_window)
376         {
377             if (!(top_window = create_window( NULL, NULL, req->atom ))) return;
378             top_window->thread = NULL;  /* no thread owns the desktop */
379             top_window->style  = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
380         }
381         reply->handle = top_window->handle;
382     }
383     else
384     {
385         struct window *win, *parent, *owner = NULL;
386
387         if (!(parent = get_window( req->parent ))) return;
388         if (req->owner && !(owner = get_window( req->owner ))) return;
389         if (owner == top_window) owner = NULL;
390         else if (owner && parent != top_window)
391         {
392             /* an owned window must be created as top-level */
393             set_error( STATUS_ACCESS_DENIED );
394             return;
395         }
396         if (!(win = create_window( parent, owner, req->atom ))) return;
397         reply->handle = win->handle;
398     }
399 }
400
401
402 /* link a window into the tree */
403 DECL_HANDLER(link_window)
404 {
405     struct window *win, *parent = NULL, *previous = NULL;
406
407     if (!(win = get_window( req->handle ))) return;
408     if (req->parent && !(parent = get_window( req->parent ))) return;
409
410     if (win == top_window)
411     {
412         set_error( STATUS_INVALID_PARAMETER );
413         return;
414     }
415     reply->full_parent = parent ? parent->handle : 0;
416     if (parent && req->previous)
417     {
418         if (req->previous == (user_handle_t)1)  /* special case: HWND_BOTTOM */
419         {
420             previous = parent->last_child;
421             if (previous == win) return;  /* nothing to do */
422         }
423         else
424         {
425             if (!(previous = get_window( req->previous ))) return;
426             /* previous must be a child of parent, and not win itself */
427             if (previous->parent != parent || previous == win)
428             {
429                 set_error( STATUS_INVALID_PARAMETER );
430                 return;
431             }
432         }
433     }
434     link_window( win, parent, previous );
435 }
436
437
438 /* destroy a window */
439 DECL_HANDLER(destroy_window)
440 {
441     struct window *win = get_window( req->handle );
442     if (win)
443     {
444         if (win != top_window) destroy_window( win );
445         else set_error( STATUS_ACCESS_DENIED );
446     }
447 }
448
449
450 /* set a window owner */
451 DECL_HANDLER(set_window_owner)
452 {
453     struct window *win = get_window( req->handle );
454     struct window *owner = NULL;
455
456     if (!win) return;
457     if (req->owner && !(owner = get_window( req->owner ))) return;
458     if (win == top_window)
459     {
460         set_error( STATUS_ACCESS_DENIED );
461         return;
462     }
463     reply->prev_owner = win->owner;
464     reply->full_owner = win->owner = owner ? owner->handle : 0;
465 }
466
467
468 /* get information from a window handle */
469 DECL_HANDLER(get_window_info)
470 {
471     struct window *win = get_window( req->handle );
472
473     reply->full_handle = 0;
474     reply->tid = reply->pid = 0;
475     if (win)
476     {
477         reply->full_handle = win->handle;
478         if (win->thread)
479         {
480             reply->tid  = get_thread_id( win->thread );
481             reply->pid  = get_process_id( win->thread->process );
482             reply->atom = win->atom;
483         }
484     }
485 }
486
487
488 /* set some information in a window */
489 DECL_HANDLER(set_window_info)
490 {
491     struct window *win = get_window( req->handle );
492
493     if (!win) return;
494     if (req->flags && win == top_window)
495     {
496         set_error( STATUS_ACCESS_DENIED );
497         return;
498     }
499     reply->old_style     = win->style;
500     reply->old_ex_style  = win->ex_style;
501     reply->old_id        = win->id;
502     reply->old_instance  = win->instance;
503     reply->old_user_data = win->user_data;
504     if (req->flags & SET_WIN_STYLE) win->style = req->style;
505     if (req->flags & SET_WIN_EXSTYLE) win->ex_style = req->ex_style;
506     if (req->flags & SET_WIN_ID) win->id = req->id;
507     if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
508     if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
509 }
510
511
512 /* get a list of the window parents, up to the root of the tree */
513 DECL_HANDLER(get_window_parents)
514 {
515     struct window *ptr, *win = get_window( req->handle );
516     int total = 0;
517     user_handle_t *data;
518     size_t len;
519
520     if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
521
522     reply->count = total;
523     len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
524     if (len && ((data = set_reply_data_size( len ))))
525     {
526         for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
527             *data++ = ptr->handle;
528     }
529 }
530
531
532 /* get a list of the window children */
533 DECL_HANDLER(get_window_children)
534 {
535     struct window *ptr, *parent = get_window( req->parent );
536     int total = 0;
537     user_handle_t *data;
538     size_t len;
539
540     if (parent)
541         for (ptr = parent->first_child, total = 0; ptr; ptr = ptr->next)
542         {
543             if (req->atom && ptr->atom != req->atom) continue;
544             if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
545             total++;
546         }
547
548     reply->count = total;
549     len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
550     if (len && ((data = set_reply_data_size( len ))))
551     {
552         for (ptr = parent->first_child; ptr && len; ptr = ptr->next)
553         {
554             if (req->atom && ptr->atom != req->atom) continue;
555             if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
556             *data++ = ptr->handle;
557             len -= sizeof(*data);
558         }
559     }
560 }
561
562
563 /* get window tree information from a window handle */
564 DECL_HANDLER(get_window_tree)
565 {
566     struct window *win = get_window( req->handle );
567
568     if (!win) return;
569
570     if (win->parent)
571     {
572         struct window *parent = win->parent;
573         reply->parent        = parent->handle;
574         reply->owner         = win->owner;
575         reply->next_sibling  = win->next ? win->next->handle : 0;
576         reply->prev_sibling  = win->prev ? win->prev->handle : 0;
577         reply->first_sibling = parent->first_child ? parent->first_child->handle : 0;
578         reply->last_sibling  = parent->last_child ? parent->last_child->handle : 0;
579     }
580     else
581     {
582         reply->parent        = 0;
583         reply->owner         = 0;
584         reply->next_sibling  = 0;
585         reply->prev_sibling  = 0;
586         reply->first_sibling = 0;
587         reply->last_sibling  = 0;
588     }
589     reply->first_child = win->first_child ? win->first_child->handle : 0;
590     reply->last_child  = win->last_child ? win->last_child->handle : 0;
591 }
592
593
594 /* set the window and client rectangles of a window */
595 DECL_HANDLER(set_window_rectangles)
596 {
597     struct window *win = get_window( req->handle );
598
599     if (win)
600     {
601         win->window_rect = req->window;
602         win->client_rect = req->client;
603     }
604 }
605
606
607 /* get the window and client rectangles of a window */
608 DECL_HANDLER(get_window_rectangles)
609 {
610     struct window *win = get_window( req->handle );
611
612     if (win)
613     {
614         reply->window = win->window_rect;
615         reply->client = win->client_rect;
616     }
617 }
618
619
620 /* get the window text */
621 DECL_HANDLER(get_window_text)
622 {
623     struct window *win = get_window( req->handle );
624
625     if (win && win->text)
626     {
627         size_t len = strlenW( win->text ) * sizeof(WCHAR);
628         if (len > get_reply_max_size()) len = get_reply_max_size();
629         set_reply_data( win->text, len );
630     }
631 }
632
633
634 /* set the window text */
635 DECL_HANDLER(set_window_text)
636 {
637     struct window *win = get_window( req->handle );
638
639     if (win)
640     {
641         WCHAR *text = NULL;
642         size_t len = get_req_data_size() / sizeof(WCHAR);
643         if (len)
644         {
645             if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
646             memcpy( text, get_req_data(), len * sizeof(WCHAR) );
647             text[len] = 0;
648         }
649         if (win->text) free( win->text );
650         win->text = text;
651     }
652 }
653
654
655 /* increment the window paint count */
656 DECL_HANDLER(inc_window_paint_count)
657 {
658     struct window *win = get_window( req->handle );
659
660     if (win && win->thread)
661     {
662         int old = win->paint_count;
663         if ((win->paint_count += req->incr) < 0) win->paint_count = 0;
664         inc_queue_paint_count( win->thread, win->paint_count - old );
665     }
666 }
667
668
669 /* get the coordinates offset between two windows */
670 DECL_HANDLER(get_windows_offset)
671 {
672     struct window *win;
673
674     reply->x = reply->y = 0;
675     if (req->from)
676     {
677         if (!(win = get_window( req->from ))) return;
678         while (win)
679         {
680             reply->x += win->client_rect.left;
681             reply->y += win->client_rect.top;
682             win = win->parent;
683         }
684     }
685     if (req->to)
686     {
687         if (!(win = get_window( req->to ))) return;
688         while (win)
689         {
690             reply->x -= win->client_rect.left;
691             reply->y -= win->client_rect.top;
692             win = win->parent;
693         }
694     }
695 }
696
697
698 /* set a window property */
699 DECL_HANDLER(set_window_property)
700 {
701     struct window *win = get_window( req->window );
702
703     if (win) set_property( win, req->atom, req->handle,
704                            req->string ? PROP_TYPE_STRING : PROP_TYPE_ATOM );
705 }
706
707
708 /* remove a window property */
709 DECL_HANDLER(remove_window_property)
710 {
711     struct window *win = get_window( req->window );
712     reply->handle = 0;
713     if (win) reply->handle = remove_property( win, req->atom );
714 }
715
716
717 /* get a window property */
718 DECL_HANDLER(get_window_property)
719 {
720     struct window *win = get_window( req->window );
721     reply->handle = 0;
722     if (win) reply->handle = get_property( win, req->atom );
723 }
724
725
726 /* get the list of properties of a window */
727 DECL_HANDLER(get_window_properties)
728 {
729     property_data_t *data;
730     int i, count, max = get_reply_max_size() / sizeof(*data);
731     struct window *win = get_window( req->window );
732
733     reply->total = 0;
734     if (!win) return;
735
736     for (i = count = 0; i < win->prop_inuse; i++)
737         if (win->properties[i].type != PROP_TYPE_FREE) count++;
738     reply->total = count;
739
740     if (count > max) count = max;
741     if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
742
743     for (i = 0; i < win->prop_inuse && count; i++)
744     {
745         if (win->properties[i].type == PROP_TYPE_FREE) continue;
746         data->atom   = win->properties[i].atom;
747         data->string = (win->properties[i].type == PROP_TYPE_STRING);
748         data->handle = win->properties[i].handle;
749         data++;
750         count--;
751     }
752 }