Avoid ftruncate to work around broken UMSDOS file system.
[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 == top_window) owner = NULL;
341         else if (owner && owner->parent != parent)
342         {
343             /* owner must be a sibling of the new window */
344             set_error( STATUS_ACCESS_DENIED );
345             return;
346         }
347         if (!(win = create_window( parent, owner, req->atom ))) return;
348         req->handle = win->handle;
349     }
350 }
351
352
353 /* link a window into the tree */
354 DECL_HANDLER(link_window)
355 {
356     struct window *win, *parent = NULL, *previous = NULL;
357
358     if (!(win = get_window( req->handle ))) return;
359     if (req->parent && !(parent = get_window( req->parent ))) return;
360
361     if (win == top_window)
362     {
363         set_error( STATUS_INVALID_PARAMETER );
364         return;
365     }
366     req->full_parent = parent ? parent->handle : 0;
367     if (parent && req->previous)
368     {
369         if (req->previous == (user_handle_t)1)  /* special case: HWND_BOTTOM */
370         {
371             previous = parent->last_child;
372             if (previous == win) return;  /* nothing to do */
373         }
374         else
375         {
376             if (!(previous = get_window( req->previous ))) return;
377             /* previous must be a child of parent, and not win itself */
378             if (previous->parent != parent || previous == win)
379             {
380                 set_error( STATUS_INVALID_PARAMETER );
381                 return;
382             }
383         }
384     }
385     link_window( win, parent, previous );
386 }
387
388
389 /* destroy a window */
390 DECL_HANDLER(destroy_window)
391 {
392     struct window *win = get_window( req->handle );
393     if (win)
394     {
395         if (win != top_window) destroy_window( win );
396         else set_error( STATUS_ACCESS_DENIED );
397     }
398 }
399
400
401 /* set a window owner */
402 DECL_HANDLER(set_window_owner)
403 {
404     struct window *win = get_window( req->handle );
405     struct window *owner = get_window( req->owner );
406
407     if (!win || !owner) return;
408     if (owner->parent != win->parent)
409     {
410         /* owner has to be a sibling of window */
411         set_error( STATUS_ACCESS_DENIED );
412         return;
413     }
414     win->owner = owner;
415     req->full_owner = owner->handle;
416 }
417
418
419 /* get information from a window handle */
420 DECL_HANDLER(get_window_info)
421 {
422     struct window *win = get_window( req->handle );
423
424     req->full_handle = 0;
425     req->tid = req->pid = 0;
426     if (win)
427     {
428         req->full_handle = win->handle;
429         if (win->thread)
430         {
431             req->tid  = get_thread_id( win->thread );
432             req->pid  = get_process_id( win->thread->process );
433             req->atom = win->atom;
434         }
435     }
436 }
437
438
439 /* set some information in a window */
440 DECL_HANDLER(set_window_info)
441 {
442     struct window *win = get_window( req->handle );
443     if (!win) return;
444     req->old_style     = win->style;
445     req->old_ex_style  = win->ex_style;
446     req->old_id        = win->id;
447     req->old_instance  = win->instance;
448     req->old_user_data = win->user_data;
449     if (req->flags & SET_WIN_STYLE) win->style = req->style;
450     if (req->flags & SET_WIN_EXSTYLE) win->ex_style = req->ex_style;
451     if (req->flags & SET_WIN_ID) win->id = req->id;
452     if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
453     if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
454 }
455
456
457 /* get a list of the window parents, up to the root of the tree */
458 DECL_HANDLER(get_window_parents)
459 {
460     struct window *ptr, *win = get_window( req->handle );
461     int total = 0;
462     size_t len;
463
464     if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
465
466     req->count = total;
467     len = min( get_req_data_size(req), total * sizeof(user_handle_t) );
468     set_req_data_size( req, len );
469     if (len)
470     {
471         user_handle_t *data = get_req_data(req);
472         for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
473             *data++ = ptr->handle;
474     }
475 }
476
477
478 /* get a list of the window children */
479 DECL_HANDLER(get_window_children)
480 {
481     struct window *ptr, *parent = get_window( req->parent );
482     int total = 0;
483     size_t len;
484
485     if (parent)
486         for (ptr = parent->first_child, total = 0; ptr; ptr = ptr->next)
487         {
488             if (req->atom && ptr->atom != req->atom) continue;
489             if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
490             total++;
491         }
492
493     req->count = total;
494     len = min( get_req_data_size(req), total * sizeof(user_handle_t) );
495     set_req_data_size( req, len );
496     if (len)
497     {
498         user_handle_t *data = get_req_data(req);
499         for (ptr = parent->first_child; ptr && len; ptr = ptr->next, len -= sizeof(*data))
500         {
501             if (req->atom && ptr->atom != req->atom) continue;
502             if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
503             *data++ = ptr->handle;
504         }
505     }
506 }
507
508
509 /* get window tree information from a window handle */
510 DECL_HANDLER(get_window_tree)
511 {
512     struct window *win = get_window( req->handle );
513
514     if (!win) return;
515
516     if (win->parent)
517     {
518         struct window *parent = win->parent;
519         req->parent        = parent->handle;
520         req->owner         = win->owner ? win->owner->handle : 0;
521         req->next_sibling  = win->next ? win->next->handle : 0;
522         req->prev_sibling  = win->prev ? win->prev->handle : 0;
523         req->first_sibling = parent->first_child ? parent->first_child->handle : 0;
524         req->last_sibling  = parent->last_child ? parent->last_child->handle : 0;
525     }
526     else
527     {
528         req->parent        = 0;
529         req->owner         = 0;
530         req->next_sibling  = 0;
531         req->prev_sibling  = 0;
532         req->first_sibling = 0;
533         req->last_sibling  = 0;
534     }
535     req->first_child = win->first_child ? win->first_child->handle : 0;
536     req->last_child  = win->last_child ? win->last_child->handle : 0;
537 }
538
539
540 /* set the window and client rectangles of a window */
541 DECL_HANDLER(set_window_rectangles)
542 {
543     struct window *win = get_window( req->handle );
544
545     if (win)
546     {
547         win->window_rect = req->window;
548         win->client_rect = req->client;
549     }
550 }
551
552
553 /* get the window and client rectangles of a window */
554 DECL_HANDLER(get_window_rectangles)
555 {
556     struct window *win = get_window( req->handle );
557
558     if (win)
559     {
560         req->window = win->window_rect;
561         req->client = win->client_rect;
562     }
563 }
564
565
566 /* get the coordinates offset between two windows */
567 DECL_HANDLER(get_windows_offset)
568 {
569     struct window *win;
570
571     req->x = req->y = 0;
572     if (req->from)
573     {
574         if (!(win = get_window( req->from ))) return;
575         while (win)
576         {
577             req->x += win->client_rect.left;
578             req->y += win->client_rect.top;
579             win = win->parent;
580         }
581     }
582     if (req->to)
583     {
584         if (!(win = get_window( req->to ))) return;
585         while (win)
586         {
587             req->x -= win->client_rect.left;
588             req->y -= win->client_rect.top;
589             win = win->parent;
590         }
591     }
592 }
593
594
595 /* set a window property */
596 DECL_HANDLER(set_window_property)
597 {
598     struct window *win = get_window( req->window );
599
600     if (win) set_property( win, req->atom, req->handle,
601                            req->string ? PROP_TYPE_STRING : PROP_TYPE_ATOM );
602 }
603
604
605 /* remove a window property */
606 DECL_HANDLER(remove_window_property)
607 {
608     struct window *win = get_window( req->window );
609     req->handle = 0;
610     if (win) req->handle = remove_property( win, req->atom );
611 }
612
613
614 /* get a window property */
615 DECL_HANDLER(get_window_property)
616 {
617     struct window *win = get_window( req->window );
618     req->handle = 0;
619     if (win) req->handle = get_property( win, req->atom );
620 }
621
622
623 /* get the list of properties of a window */
624 DECL_HANDLER(get_window_properties)
625 {
626     int count = 0;
627     property_data_t *data = get_req_data(req);
628     struct window *win = get_window( req->window );
629
630     if (win) count = enum_properties( win, data, get_req_data_size(req) / sizeof(*data) );
631     set_req_data_size( req, count * sizeof(*data) );
632 }