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