Don't keep main exe and dlls handles open when the file is 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 "object.h"
10 #include "request.h"
11 #include "thread.h"
12 #include "process.h"
13 #include "user.h"
14
15 /* a window property */
16 struct property
17 {
18     unsigned short type;     /* property type (see below) */
19     atom_t         atom;     /* property atom */
20     handle_t       handle;   /* property handle (user-defined storage) */
21 };
22
23 enum property_type
24 {
25     PROP_TYPE_FREE,   /* free entry */
26     PROP_TYPE_STRING, /* atom that was originally a string */
27     PROP_TYPE_ATOM    /* plain atom */
28 };
29
30
31 struct window
32 {
33     struct window   *parent;          /* parent window */
34     struct window   *owner;           /* owner of this window */
35     struct window   *first_child;     /* first child in Z-order */
36     struct window   *last_child;      /* last child in Z-order */
37     struct window   *first_unlinked;  /* first child not linked in the Z-order list */
38     struct window   *next;            /* next window in Z-order */
39     struct window   *prev;            /* prev window in Z-order */
40     user_handle_t    handle;          /* full handle for this window */
41     struct thread   *thread;          /* thread owning the window */
42     atom_t           atom;            /* class atom */
43     rectangle_t      window_rect;     /* window rectangle */
44     rectangle_t      client_rect;     /* client rectangle */
45     unsigned int     style;           /* window style */
46     unsigned int     ex_style;        /* window extended style */
47     unsigned int     id;              /* window id */
48     void*            instance;        /* creator instance */
49     void*            user_data;       /* user-specific data */
50     int              prop_inuse;      /* number of in-use window properties */
51     int              prop_alloc;      /* number of allocated window properties */
52     struct property *properties;      /* window properties array */
53 };
54
55 static struct window *top_window;  /* top-level (desktop) window */
56
57
58 /* retrieve a pointer to a window from its handle */
59 inline static struct window *get_window( user_handle_t handle )
60 {
61     struct window *ret = get_user_object( handle, USER_WINDOW );
62     if (!ret) set_error( STATUS_INVALID_HANDLE );
63     return ret;
64 }
65
66 /* unlink a window from the tree */
67 static void unlink_window( struct window *win )
68 {
69     struct window *parent = win->parent;
70
71     assert( parent );
72
73     if (win->next) win->next->prev = win->prev;
74     else if (parent->last_child == win) parent->last_child = win->prev;
75
76     if (win->prev) win->prev->next = win->next;
77     else if (parent->first_child == win) parent->first_child = win->next;
78     else if (parent->first_unlinked == win) parent->first_unlinked = win->next;
79 }
80
81
82 /* link a window into the tree (or unlink it if the new parent is NULL)  */
83 static void link_window( struct window *win, struct window *parent, struct window *previous )
84 {
85     unlink_window( win );  /* unlink it from the previous location */
86
87     if (parent)
88     {
89         if (win->parent != parent)
90         {
91             win->owner = NULL;  /* reset owner if changing parent */
92             win->parent = parent;
93         }
94         if ((win->prev = previous))
95         {
96             if ((win->next = previous->next)) win->next->prev = win;
97             else if (win->parent->last_child == previous) win->parent->last_child = win;
98             win->prev->next = win;
99         }
100         else
101         {
102             if ((win->next = parent->first_child)) win->next->prev = win;
103             else win->parent->last_child = win;
104             parent->first_child = win;
105         }
106     }
107     else  /* move it to parent unlinked list */
108     {
109         parent = win->parent;
110         if ((win->next = parent->first_unlinked)) win->next->prev = win;
111         win->prev = NULL;
112         parent->first_unlinked = win;
113     }
114 }
115
116 /* set a window property */
117 static void set_property( struct window *win, atom_t atom, handle_t handle,
118                           enum property_type type )
119 {
120     int i, free = -1;
121     struct property *new_props;
122
123     /* check if it exists already */
124     for (i = 0; i < win->prop_inuse; i++)
125     {
126         if (win->properties[i].type == PROP_TYPE_FREE)
127         {
128             free = i;
129             continue;
130         }
131         if (win->properties[i].atom == atom)
132         {
133             win->properties[i].type = type;
134             win->properties[i].handle = handle;
135             return;
136         }
137     }
138
139     /* need to add an entry */
140     if (!grab_global_atom( atom )) return;
141     if (free == -1)
142     {
143         /* no free entry */
144         if (win->prop_inuse >= win->prop_alloc)
145         {
146             /* need to grow the array */
147             if (!(new_props = realloc( win->properties,
148                                        sizeof(*new_props) * (win->prop_alloc + 16) )))
149             {
150                 set_error( STATUS_NO_MEMORY );
151                 release_global_atom( atom );
152                 return;
153             }
154             win->prop_alloc += 16;
155             win->properties = new_props;
156         }
157         free = win->prop_inuse++;
158     }
159     win->properties[free].atom   = atom;
160     win->properties[free].type   = type;
161     win->properties[free].handle = handle;
162 }
163
164 /* remove a window property */
165 static handle_t remove_property( struct window *win, atom_t atom )
166 {
167     int i;
168
169     for (i = 0; i < win->prop_inuse; i++)
170     {
171         if (win->properties[i].type == PROP_TYPE_FREE) continue;
172         if (win->properties[i].atom == atom)
173         {
174             release_global_atom( atom );
175             win->properties[i].type = PROP_TYPE_FREE;
176             return win->properties[i].handle;
177         }
178     }
179     /* FIXME: last error? */
180     return 0;
181 }
182
183 /* find a window property */
184 static handle_t get_property( struct window *win, atom_t atom )
185 {
186     int i;
187
188     for (i = 0; i < win->prop_inuse; i++)
189     {
190         if (win->properties[i].type == PROP_TYPE_FREE) continue;
191         if (win->properties[i].atom == atom) return win->properties[i].handle;
192     }
193     /* FIXME: last error? */
194     return 0;
195 }
196
197 /* destroy all properties of a window */
198 inline static void destroy_properties( struct window *win )
199 {
200     int i;
201
202     if (!win->properties) return;
203     for (i = 0; i < win->prop_inuse; i++)
204     {
205         if (win->properties[i].type == PROP_TYPE_FREE) continue;
206         release_global_atom( win->properties[i].atom );
207     }
208     free( win->properties );
209 }
210
211 /* enum all properties into the data array */
212 static int enum_properties( struct window *win, property_data_t *data, int max )
213 {
214     int i, count;
215
216     for (i = count = 0; i < win->prop_inuse && count < max; i++)
217     {
218         if (win->properties[i].type == PROP_TYPE_FREE) continue;
219         data->atom   = win->properties[i].atom;
220         data->string = (win->properties[i].type == PROP_TYPE_STRING);
221         data->handle = win->properties[i].handle;
222         data++;
223         count++;
224     }
225     return count;
226 }
227
228 /* destroy a window */
229 static void destroy_window( struct window *win )
230 {
231     assert( win != top_window );
232
233     /* destroy all children */
234     while (win->first_child) destroy_window( win->first_child );
235     while (win->first_unlinked) destroy_window( win->first_unlinked );
236
237     /* reset siblings owner */
238     if (win->parent)
239     {
240         struct window *ptr;
241         for (ptr = win->parent->first_child; ptr; ptr = ptr->next)
242             if (ptr->owner == win) ptr->owner = NULL;
243         for (ptr = win->parent->first_unlinked; ptr; ptr = ptr->next)
244             if (ptr->owner == win) ptr->owner = NULL;
245     }
246
247     if (win->thread->queue) queue_cleanup_window( win->thread, win->handle );
248     free_user_handle( win->handle );
249     destroy_properties( win );
250     unlink_window( win );
251     memset( win, 0x55, sizeof(*win) );
252     free( win );
253 }
254
255 /* create a new window structure (note: the window is not linked in the window tree) */
256 static struct window *create_window( struct window *parent, struct window *owner, atom_t atom )
257 {
258     struct window *win = mem_alloc( sizeof(*win) );
259     if (!win) return NULL;
260
261     if (!(win->handle = alloc_user_handle( win, USER_WINDOW )))
262     {
263         free( win );
264         return NULL;
265     }
266     win->parent         = parent;
267     win->owner          = owner;
268     win->first_child    = NULL;
269     win->last_child     = NULL;
270     win->first_unlinked = NULL;
271     win->thread         = current;
272     win->atom           = atom;
273     win->style          = 0;
274     win->ex_style       = 0;
275     win->id             = 0;
276     win->instance       = NULL;
277     win->user_data      = NULL;
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 /* create a window */
322 DECL_HANDLER(create_window)
323 {
324     req->handle = 0;
325     if (!req->parent)  /* return desktop window */
326     {
327         if (!top_window)
328         {
329             if (!(top_window = create_window( NULL, NULL, req->atom ))) return;
330             top_window->thread = NULL;  /* no thread owns the desktop */
331         }
332         req->handle = top_window->handle;
333     }
334     else
335     {
336         struct window *win, *parent, *owner = NULL;
337
338         if (!(parent = get_window( req->parent ))) return;
339         if (req->owner && !(owner = get_window( req->owner ))) return;
340         if (owner && owner->parent != parent)
341         {
342             /* owner must be a sibling of the new window */
343             set_error( STATUS_ACCESS_DENIED );
344             return;
345         }
346         if (!(win = create_window( parent, owner, req->atom ))) return;
347         req->handle = win->handle;
348     }
349 }
350
351
352 /* link a window into the tree */
353 DECL_HANDLER(link_window)
354 {
355     struct window *win, *parent = NULL, *previous = NULL;
356
357     if (!(win = get_window( req->handle ))) return;
358     if (req->parent && !(parent = get_window( req->parent ))) return;
359
360     if (win == top_window)
361     {
362         set_error( STATUS_INVALID_PARAMETER );
363         return;
364     }
365     req->full_parent = parent ? parent->handle : 0;
366     if (parent && req->previous)
367     {
368         if (req->previous == (user_handle_t)1)  /* special case: HWND_BOTTOM */
369         {
370             previous = parent->last_child;
371             if (previous == win) return;  /* nothing to do */
372         }
373         else
374         {
375             if (!(previous = get_window( req->previous ))) return;
376             /* previous must be a child of parent, and not win itself */
377             if (previous->parent != parent || previous == win)
378             {
379                 set_error( STATUS_INVALID_PARAMETER );
380                 return;
381             }
382         }
383     }
384     link_window( win, parent, previous );
385 }
386
387
388 /* destroy a window */
389 DECL_HANDLER(destroy_window)
390 {
391     struct window *win = get_window( req->handle );
392     if (win)
393     {
394         if (win != top_window) destroy_window( win );
395         else set_error( STATUS_ACCESS_DENIED );
396     }
397 }
398
399
400 /* set a window owner */
401 DECL_HANDLER(set_window_owner)
402 {
403     struct window *win = get_window( req->handle );
404     struct window *owner = get_window( req->owner );
405
406     if (!win || !owner) return;
407     if (owner->parent != win->parent)
408     {
409         /* owner has to be a sibling of window */
410         set_error( STATUS_ACCESS_DENIED );
411         return;
412     }
413     win->owner = owner;
414     req->full_owner = owner->handle;
415 }
416
417
418 /* get information from a window handle */
419 DECL_HANDLER(get_window_info)
420 {
421     struct window *win = get_window( req->handle );
422
423     req->full_handle = 0;
424     req->tid = req->pid = 0;
425     if (win)
426     {
427         req->full_handle = win->handle;
428         if (win->thread)
429         {
430             req->tid  = get_thread_id( win->thread );
431             req->pid  = get_process_id( win->thread->process );
432             req->atom = win->atom;
433         }
434     }
435 }
436
437
438 /* set some information in a window */
439 DECL_HANDLER(set_window_info)
440 {
441     struct window *win = get_window( req->handle );
442     if (!win) return;
443     req->old_style     = win->style;
444     req->old_ex_style  = win->ex_style;
445     req->old_id        = win->id;
446     req->old_instance  = win->instance;
447     req->old_user_data = win->user_data;
448     if (req->flags & SET_WIN_STYLE) win->style = req->style;
449     if (req->flags & SET_WIN_EXSTYLE) win->ex_style = req->ex_style;
450     if (req->flags & SET_WIN_ID) win->id = req->id;
451     if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
452     if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
453 }
454
455
456 /* get a list of the window parents, up to the root of the tree */
457 DECL_HANDLER(get_window_parents)
458 {
459     struct window *ptr, *win = get_window( req->handle );
460     int total = 0;
461     size_t len;
462
463     if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
464
465     req->count = total;
466     len = min( get_req_data_size(req), total * sizeof(user_handle_t) );
467     set_req_data_size( req, len );
468     if (len)
469     {
470         user_handle_t *data = get_req_data(req);
471         for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
472             *data++ = ptr->handle;
473     }
474 }
475
476
477 /* get a list of the window children */
478 DECL_HANDLER(get_window_children)
479 {
480     struct window *ptr, *parent = get_window( req->parent );
481     int total = 0;
482     size_t len;
483
484     if (parent)
485         for (ptr = parent->first_child, total = 0; ptr; ptr = ptr->next)
486         {
487             if (req->atom && ptr->atom != req->atom) continue;
488             if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
489             total++;
490         }
491
492     req->count = total;
493     len = min( get_req_data_size(req), total * sizeof(user_handle_t) );
494     set_req_data_size( req, len );
495     if (len)
496     {
497         user_handle_t *data = get_req_data(req);
498         for (ptr = parent->first_child; ptr && len; ptr = ptr->next, len -= sizeof(*data))
499         {
500             if (req->atom && ptr->atom != req->atom) continue;
501             if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
502             *data++ = ptr->handle;
503         }
504     }
505 }
506
507
508 /* get window tree information from a window handle */
509 DECL_HANDLER(get_window_tree)
510 {
511     struct window *win = get_window( req->handle );
512
513     if (!win) return;
514
515     if (win->parent)
516     {
517         struct window *parent = win->parent;
518         req->parent        = parent->handle;
519         req->owner         = win->owner ? win->owner->handle : 0;
520         req->next_sibling  = win->next ? win->next->handle : 0;
521         req->prev_sibling  = win->prev ? win->prev->handle : 0;
522         req->first_sibling = parent->first_child ? parent->first_child->handle : 0;
523         req->last_sibling  = parent->last_child ? parent->last_child->handle : 0;
524     }
525     else
526     {
527         req->parent        = 0;
528         req->owner         = 0;
529         req->next_sibling  = 0;
530         req->prev_sibling  = 0;
531         req->first_sibling = 0;
532         req->last_sibling  = 0;
533     }
534     req->first_child = win->first_child ? win->first_child->handle : 0;
535     req->last_child  = win->last_child ? win->last_child->handle : 0;
536 }
537
538
539 /* set the window and client rectangles of a window */
540 DECL_HANDLER(set_window_rectangles)
541 {
542     struct window *win = get_window( req->handle );
543
544     if (win)
545     {
546         win->window_rect = req->window;
547         win->client_rect = req->client;
548     }
549 }
550
551
552 /* get the window and client rectangles of a window */
553 DECL_HANDLER(get_window_rectangles)
554 {
555     struct window *win = get_window( req->handle );
556
557     if (win)
558     {
559         req->window = win->window_rect;
560         req->client = win->client_rect;
561     }
562 }
563
564
565 /* get the coordinates offset between two windows */
566 DECL_HANDLER(get_windows_offset)
567 {
568     struct window *win;
569
570     req->x = req->y = 0;
571     if (req->from)
572     {
573         if (!(win = get_window( req->from ))) return;
574         while (win)
575         {
576             req->x += win->client_rect.left;
577             req->y += win->client_rect.top;
578             win = win->parent;
579         }
580     }
581     if (req->to)
582     {
583         if (!(win = get_window( req->to ))) return;
584         while (win)
585         {
586             req->x -= win->client_rect.left;
587             req->y -= win->client_rect.top;
588             win = win->parent;
589         }
590     }
591 }
592
593
594 /* set a window property */
595 DECL_HANDLER(set_window_property)
596 {
597     struct window *win = get_window( req->window );
598
599     if (win) set_property( win, req->atom, req->handle,
600                            req->string ? PROP_TYPE_STRING : PROP_TYPE_ATOM );
601 }
602
603
604 /* remove a window property */
605 DECL_HANDLER(remove_window_property)
606 {
607     struct window *win = get_window( req->window );
608     req->handle = 0;
609     if (win) req->handle = remove_property( win, req->atom );
610 }
611
612
613 /* get a window property */
614 DECL_HANDLER(get_window_property)
615 {
616     struct window *win = get_window( req->window );
617     req->handle = 0;
618     if (win) req->handle = get_property( win, req->atom );
619 }
620
621
622 /* get the list of properties of a window */
623 DECL_HANDLER(get_window_properties)
624 {
625     int count = 0;
626     property_data_t *data = get_req_data(req);
627     struct window *win = get_window( req->window );
628
629     if (win) count = enum_properties( win, data, get_req_data_size(req) / sizeof(*data) );
630     set_req_data_size( req, count * sizeof(*data) );
631 }