Only include 'sys/user.h' for Linux. Fixes a compilation error on
[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 /* enum all properties into the data array */
219 static int enum_properties( struct window *win, property_data_t *data, int max )
220 {
221     int i, count;
222
223     for (i = count = 0; i < win->prop_inuse && count < max; i++)
224     {
225         if (win->properties[i].type == PROP_TYPE_FREE) continue;
226         data->atom   = win->properties[i].atom;
227         data->string = (win->properties[i].type == PROP_TYPE_STRING);
228         data->handle = win->properties[i].handle;
229         data++;
230         count++;
231     }
232     return count;
233 }
234
235 /* destroy a window */
236 static void destroy_window( struct window *win )
237 {
238     assert( win != top_window );
239
240     /* destroy all children */
241     while (win->first_child) destroy_window( win->first_child );
242     while (win->first_unlinked) destroy_window( win->first_unlinked );
243
244     /* reset siblings owner */
245     if (win->parent)
246     {
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;
252     }
253
254     if (win->paint_count) inc_queue_paint_count( win->thread, -win->paint_count );
255     queue_cleanup_window( win->thread, win->handle );
256     free_user_handle( win->handle );
257     destroy_properties( win );
258     unlink_window( win );
259     if (win->text) free( win->text );
260     memset( win, 0x55, sizeof(*win) );
261     free( win );
262 }
263
264 /* create a new window structure (note: the window is not linked in the window tree) */
265 static struct window *create_window( struct window *parent, struct window *owner, atom_t atom )
266 {
267     struct window *win = mem_alloc( sizeof(*win) );
268     if (!win) return NULL;
269
270     if (!(win->handle = alloc_user_handle( win, USER_WINDOW )))
271     {
272         free( win );
273         return NULL;
274     }
275     win->parent         = parent;
276     win->owner          = owner;
277     win->first_child    = NULL;
278     win->last_child     = NULL;
279     win->first_unlinked = NULL;
280     win->thread         = current;
281     win->atom           = atom;
282     win->style          = 0;
283     win->ex_style       = 0;
284     win->id             = 0;
285     win->instance       = NULL;
286     win->user_data      = NULL;
287     win->text           = NULL;
288     win->paint_count    = 0;
289     win->prop_inuse     = 0;
290     win->prop_alloc     = 0;
291     win->properties     = NULL;
292
293     if (parent)  /* put it on parent unlinked list */
294     {
295         if ((win->next = parent->first_unlinked)) win->next->prev = win;
296         win->prev = NULL;
297         parent->first_unlinked = win;
298     }
299     else win->next = win->prev = NULL;
300
301     return win;
302 }
303
304 /* destroy all windows belonging to a given thread */
305 void destroy_thread_windows( struct thread *thread )
306 {
307     user_handle_t handle = 0;
308     struct window *win;
309
310     while ((win = next_user_handle( &handle, USER_WINDOW )))
311     {
312         if (win->thread != thread) continue;
313         destroy_window( win );
314     }
315 }
316
317 /* check whether child is a descendant of parent */
318 int is_child_window( user_handle_t parent, user_handle_t child )
319 {
320     struct window *child_ptr = get_user_object( child, USER_WINDOW );
321     struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
322
323     if (!child_ptr || !parent_ptr) return 0;
324     while (child_ptr->parent)
325     {
326         if (child_ptr->parent == parent_ptr) return 1;
327         child_ptr = child_ptr->parent;
328     }
329     return 0;
330 }
331
332
333 /* find a child of the specified window that needs repainting */
334 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
335 {
336     struct window *ptr, *ret = NULL;
337
338     for (ptr = parent->first_child; ptr && !ret; ptr = ptr->next)
339     {
340         if (!(ptr->style & WS_VISIBLE)) continue;
341         if (ptr->paint_count && ptr->thread == thread)
342             ret = ptr;
343         else /* explore its children */
344             ret = find_child_to_repaint( ptr, thread );
345     }
346
347     if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
348     {
349         /* transparent window, check for non-transparent sibling to paint first */
350         for (ptr = ret->next; ptr; ptr = ptr->next)
351         {
352             if (!(ptr->style & WS_VISIBLE)) continue;
353             if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
354             if (ptr->paint_count && ptr->thread == thread) return ptr;
355         }
356     }
357     return ret;
358 }
359
360
361 /* find a window that needs repainting */
362 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
363 {
364     struct window *win = parent ? get_window( parent ) : top_window;
365
366     if (!win || !(win->style & WS_VISIBLE)) return 0;
367     if (!win->paint_count || win->thread != thread)
368         win = find_child_to_repaint( win, thread );
369     return win ? win->handle : 0;
370 }
371
372
373 /* create a window */
374 DECL_HANDLER(create_window)
375 {
376     req->handle = 0;
377     if (!req->parent)  /* return desktop window */
378     {
379         if (!top_window)
380         {
381             if (!(top_window = create_window( NULL, NULL, req->atom ))) return;
382             top_window->thread = NULL;  /* no thread owns the desktop */
383         }
384         req->handle = top_window->handle;
385     }
386     else
387     {
388         struct window *win, *parent, *owner = NULL;
389
390         if (!(parent = get_window( req->parent ))) return;
391         if (req->owner && !(owner = get_window( req->owner ))) return;
392         if (owner == top_window) owner = NULL;
393         else if (owner && owner->parent != parent)
394         {
395             /* owner must be a sibling of the new window */
396             set_error( STATUS_ACCESS_DENIED );
397             return;
398         }
399         if (!(win = create_window( parent, owner, req->atom ))) return;
400         req->handle = win->handle;
401     }
402 }
403
404
405 /* link a window into the tree */
406 DECL_HANDLER(link_window)
407 {
408     struct window *win, *parent = NULL, *previous = NULL;
409
410     if (!(win = get_window( req->handle ))) return;
411     if (req->parent && !(parent = get_window( req->parent ))) return;
412
413     if (win == top_window)
414     {
415         set_error( STATUS_INVALID_PARAMETER );
416         return;
417     }
418     req->full_parent = parent ? parent->handle : 0;
419     if (parent && req->previous)
420     {
421         if (req->previous == (user_handle_t)1)  /* special case: HWND_BOTTOM */
422         {
423             previous = parent->last_child;
424             if (previous == win) return;  /* nothing to do */
425         }
426         else
427         {
428             if (!(previous = get_window( req->previous ))) return;
429             /* previous must be a child of parent, and not win itself */
430             if (previous->parent != parent || previous == win)
431             {
432                 set_error( STATUS_INVALID_PARAMETER );
433                 return;
434             }
435         }
436     }
437     link_window( win, parent, previous );
438 }
439
440
441 /* destroy a window */
442 DECL_HANDLER(destroy_window)
443 {
444     struct window *win = get_window( req->handle );
445     if (win)
446     {
447         if (win != top_window) destroy_window( win );
448         else set_error( STATUS_ACCESS_DENIED );
449     }
450 }
451
452
453 /* set a window owner */
454 DECL_HANDLER(set_window_owner)
455 {
456     struct window *win = get_window( req->handle );
457     struct window *owner = get_window( req->owner );
458
459     if (!win || !owner) return;
460     if (owner->parent != win->parent)
461     {
462         /* owner has to be a sibling of window */
463         set_error( STATUS_ACCESS_DENIED );
464         return;
465     }
466     win->owner = owner;
467     req->full_owner = owner->handle;
468 }
469
470
471 /* get information from a window handle */
472 DECL_HANDLER(get_window_info)
473 {
474     struct window *win = get_window( req->handle );
475
476     req->full_handle = 0;
477     req->tid = req->pid = 0;
478     if (win)
479     {
480         req->full_handle = win->handle;
481         if (win->thread)
482         {
483             req->tid  = get_thread_id( win->thread );
484             req->pid  = get_process_id( win->thread->process );
485             req->atom = win->atom;
486         }
487     }
488 }
489
490
491 /* set some information in a window */
492 DECL_HANDLER(set_window_info)
493 {
494     struct window *win = get_window( req->handle );
495     if (!win) return;
496     req->old_style     = win->style;
497     req->old_ex_style  = win->ex_style;
498     req->old_id        = win->id;
499     req->old_instance  = win->instance;
500     req->old_user_data = win->user_data;
501     if (req->flags & SET_WIN_STYLE) win->style = req->style;
502     if (req->flags & SET_WIN_EXSTYLE) win->ex_style = req->ex_style;
503     if (req->flags & SET_WIN_ID) win->id = req->id;
504     if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
505     if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
506 }
507
508
509 /* get a list of the window parents, up to the root of the tree */
510 DECL_HANDLER(get_window_parents)
511 {
512     struct window *ptr, *win = get_window( req->handle );
513     int total = 0;
514     size_t len;
515
516     if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
517
518     req->count = total;
519     len = min( get_req_data_size(req), total * sizeof(user_handle_t) );
520     set_req_data_size( req, len );
521     if (len)
522     {
523         user_handle_t *data = get_req_data(req);
524         for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
525             *data++ = ptr->handle;
526     }
527 }
528
529
530 /* get a list of the window children */
531 DECL_HANDLER(get_window_children)
532 {
533     struct window *ptr, *parent = get_window( req->parent );
534     int total = 0;
535     size_t len;
536
537     if (parent)
538         for (ptr = parent->first_child, total = 0; ptr; ptr = ptr->next)
539         {
540             if (req->atom && ptr->atom != req->atom) continue;
541             if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
542             total++;
543         }
544
545     req->count = total;
546     len = min( get_req_data_size(req), total * sizeof(user_handle_t) );
547     set_req_data_size( req, len );
548     if (len)
549     {
550         user_handle_t *data = get_req_data(req);
551         for (ptr = parent->first_child; ptr && len; ptr = ptr->next, len -= sizeof(*data))
552         {
553             if (req->atom && ptr->atom != req->atom) continue;
554             if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
555             *data++ = ptr->handle;
556         }
557     }
558 }
559
560
561 /* get window tree information from a window handle */
562 DECL_HANDLER(get_window_tree)
563 {
564     struct window *win = get_window( req->handle );
565
566     if (!win) return;
567
568     if (win->parent)
569     {
570         struct window *parent = win->parent;
571         req->parent        = parent->handle;
572         req->owner         = win->owner ? win->owner->handle : 0;
573         req->next_sibling  = win->next ? win->next->handle : 0;
574         req->prev_sibling  = win->prev ? win->prev->handle : 0;
575         req->first_sibling = parent->first_child ? parent->first_child->handle : 0;
576         req->last_sibling  = parent->last_child ? parent->last_child->handle : 0;
577     }
578     else
579     {
580         req->parent        = 0;
581         req->owner         = 0;
582         req->next_sibling  = 0;
583         req->prev_sibling  = 0;
584         req->first_sibling = 0;
585         req->last_sibling  = 0;
586     }
587     req->first_child = win->first_child ? win->first_child->handle : 0;
588     req->last_child  = win->last_child ? win->last_child->handle : 0;
589 }
590
591
592 /* set the window and client rectangles of a window */
593 DECL_HANDLER(set_window_rectangles)
594 {
595     struct window *win = get_window( req->handle );
596
597     if (win)
598     {
599         win->window_rect = req->window;
600         win->client_rect = req->client;
601     }
602 }
603
604
605 /* get the window and client rectangles of a window */
606 DECL_HANDLER(get_window_rectangles)
607 {
608     struct window *win = get_window( req->handle );
609
610     if (win)
611     {
612         req->window = win->window_rect;
613         req->client = win->client_rect;
614     }
615 }
616
617
618 /* get the window text */
619 DECL_HANDLER(get_window_text)
620 {
621     struct window *win = get_window( req->handle );
622     size_t len = 0;
623
624     if (win && win->text)
625     {
626         len = strlenW( win->text ) * sizeof(WCHAR);
627         if (len > get_req_data_size(req)) len = get_req_data_size(req);
628         memcpy( get_req_data(req), win->text, len );
629     }
630     set_req_data_size( req, len );
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(req) / sizeof(WCHAR);
643         if (len)
644         {
645             if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
646             memcpy( text, get_req_data(req), 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     req->x = req->y = 0;
675     if (req->from)
676     {
677         if (!(win = get_window( req->from ))) return;
678         while (win)
679         {
680             req->x += win->client_rect.left;
681             req->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             req->x -= win->client_rect.left;
691             req->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     req->handle = 0;
713     if (win) req->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     req->handle = 0;
722     if (win) req->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     int count = 0;
730     property_data_t *data = get_req_data(req);
731     struct window *win = get_window( req->window );
732
733     if (win) count = enum_properties( win, data, get_req_data_size(req) / sizeof(*data) );
734     set_req_data_size( req, count * sizeof(*data) );
735 }