Add support for environment variables in the printer settings.
[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     int              prop_inuse;      /* number of in-use window properties */
46     int              prop_alloc;      /* number of allocated window properties */
47     struct property *properties;      /* window properties array */
48 };
49
50 static struct window *top_window;  /* top-level (desktop) window */
51
52
53 /* retrieve a pointer to a window from its handle */
54 inline static struct window *get_window( user_handle_t handle )
55 {
56     struct window *ret = get_user_object( handle, USER_WINDOW );
57     if (!ret) set_error( STATUS_INVALID_HANDLE );
58     return ret;
59 }
60
61 /* unlink a window from the tree */
62 static void unlink_window( struct window *win )
63 {
64     struct window *parent = win->parent;
65
66     assert( parent );
67
68     if (win->next) win->next->prev = win->prev;
69     else if (parent->last_child == win) parent->last_child = win->prev;
70
71     if (win->prev) win->prev->next = win->next;
72     else if (parent->first_child == win) parent->first_child = win->next;
73     else if (parent->first_unlinked == win) parent->first_unlinked = win->next;
74 }
75
76
77 /* link a window into the tree (or unlink it if the new parent is NULL)  */
78 static void link_window( struct window *win, struct window *parent, struct window *previous )
79 {
80     unlink_window( win );  /* unlink it from the previous location */
81
82     if (parent)
83     {
84         win->parent = parent;
85         if ((win->prev = previous))
86         {
87             if ((win->next = previous->next)) win->next->prev = win;
88             else if (win->parent->last_child == previous) win->parent->last_child = win;
89             win->prev->next = win;
90         }
91         else
92         {
93             if ((win->next = parent->first_child)) win->next->prev = win;
94             else win->parent->last_child = win;
95             parent->first_child = win;
96         }
97     }
98     else  /* move it to parent unlinked list */
99     {
100         parent = win->parent;
101         if ((win->next = parent->first_unlinked)) win->next->prev = win;
102         win->prev = NULL;
103         parent->first_unlinked = win;
104     }
105 }
106
107 /* set a window property */
108 static void set_property( struct window *win, atom_t atom, handle_t handle,
109                           enum property_type type )
110 {
111     int i, free = -1;
112     struct property *new_props;
113
114     /* check if it exists already */
115     for (i = 0; i < win->prop_inuse; i++)
116     {
117         if (win->properties[i].type == PROP_TYPE_FREE)
118         {
119             free = i;
120             continue;
121         }
122         if (win->properties[i].atom == atom)
123         {
124             win->properties[i].type = type;
125             win->properties[i].handle = handle;
126             return;
127         }
128     }
129
130     /* need to add an entry */
131     if (!grab_global_atom( atom )) return;
132     if (free == -1)
133     {
134         /* no free entry */
135         if (win->prop_inuse >= win->prop_alloc)
136         {
137             /* need to grow the array */
138             if (!(new_props = realloc( win->properties,
139                                        sizeof(*new_props) * (win->prop_alloc + 16) )))
140             {
141                 set_error( STATUS_NO_MEMORY );
142                 release_global_atom( atom );
143                 return;
144             }
145             win->prop_alloc += 16;
146             win->properties = new_props;
147         }
148         free = win->prop_inuse++;
149     }
150     win->properties[free].atom   = atom;
151     win->properties[free].type   = type;
152     win->properties[free].handle = handle;
153 }
154
155 /* remove a window property */
156 static handle_t remove_property( struct window *win, atom_t atom )
157 {
158     int i;
159
160     for (i = 0; i < win->prop_inuse; i++)
161     {
162         if (win->properties[i].type == PROP_TYPE_FREE) continue;
163         if (win->properties[i].atom == atom)
164         {
165             release_global_atom( atom );
166             win->properties[i].type = PROP_TYPE_FREE;
167             return win->properties[i].handle;
168         }
169     }
170     /* FIXME: last error? */
171     return 0;
172 }
173
174 /* find a window property */
175 static handle_t get_property( struct window *win, atom_t atom )
176 {
177     int i;
178
179     for (i = 0; i < win->prop_inuse; i++)
180     {
181         if (win->properties[i].type == PROP_TYPE_FREE) continue;
182         if (win->properties[i].atom == atom) return win->properties[i].handle;
183     }
184     /* FIXME: last error? */
185     return 0;
186 }
187
188 /* destroy all properties of a window */
189 inline static void destroy_properties( struct window *win )
190 {
191     int i;
192
193     if (!win->properties) return;
194     for (i = 0; i < win->prop_inuse; i++)
195     {
196         if (win->properties[i].type == PROP_TYPE_FREE) continue;
197         release_global_atom( win->properties[i].atom );
198     }
199     free( win->properties );
200 }
201
202 /* enum all properties into the data array */
203 static int enum_properties( struct window *win, property_data_t *data, int max )
204 {
205     int i, count;
206
207     for (i = count = 0; i < win->prop_inuse && count < max; i++)
208     {
209         if (win->properties[i].type == PROP_TYPE_FREE) continue;
210         data->atom   = win->properties[i].atom;
211         data->string = (win->properties[i].type == PROP_TYPE_STRING);
212         data->handle = win->properties[i].handle;
213         data++;
214         count++;
215     }
216     return count;
217 }
218
219 /* destroy a window */
220 static void destroy_window( struct window *win )
221 {
222     assert( win != top_window );
223
224     /* destroy all children */
225     while (win->first_child) destroy_window( win->first_child );
226     while (win->first_unlinked) destroy_window( win->first_unlinked );
227
228     /* reset siblings owner */
229     if (win->parent)
230     {
231         struct window *ptr;
232         for (ptr = win->parent->first_child; ptr; ptr = ptr->next)
233             if (ptr->owner == win) ptr->owner = NULL;
234         for (ptr = win->parent->first_unlinked; ptr; ptr = ptr->next)
235             if (ptr->owner == win) ptr->owner = NULL;
236     }
237
238     if (win->thread->queue) queue_cleanup_window( win->thread, win->handle );
239     free_user_handle( win->handle );
240     destroy_properties( win );
241     unlink_window( win );
242     memset( win, 0x55, sizeof(*win) );
243     free( win );
244 }
245
246 /* create a new window structure (note: the window is not linked in the window tree) */
247 static struct window *create_window( struct window *parent, struct window *owner, atom_t atom )
248 {
249     struct window *win = mem_alloc( sizeof(*win) );
250     if (!win) return NULL;
251
252     if (!(win->handle = alloc_user_handle( win, USER_WINDOW )))
253     {
254         free( win );
255         return NULL;
256     }
257     win->parent         = parent;
258     win->owner          = owner;
259     win->first_child    = NULL;
260     win->last_child     = NULL;
261     win->first_unlinked = NULL;
262     win->thread         = current;
263     win->atom           = atom;
264     win->prop_inuse     = 0;
265     win->prop_alloc     = 0;
266     win->properties     = NULL;
267
268     if (parent)  /* put it on parent unlinked list */
269     {
270         if ((win->next = parent->first_unlinked)) win->next->prev = win;
271         win->prev = NULL;
272         parent->first_unlinked = win;
273     }
274     else win->next = win->prev = NULL;
275
276     return win;
277 }
278
279 /* destroy all windows belonging to a given thread */
280 void destroy_thread_windows( struct thread *thread )
281 {
282     user_handle_t handle = 0;
283     struct window *win;
284
285     while ((win = next_user_handle( &handle, USER_WINDOW )))
286     {
287         if (win->thread != thread) continue;
288         destroy_window( win );
289     }
290 }
291
292 /* check whether child is a descendant of parent */
293 int is_child_window( user_handle_t parent, user_handle_t child )
294 {
295     struct window *child_ptr = get_user_object( child, USER_WINDOW );
296     struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
297
298     if (!child_ptr || !parent_ptr) return 0;
299     while (child_ptr->parent)
300     {
301         if (child_ptr->parent == parent_ptr) return 1;
302         child_ptr = child_ptr->parent;
303     }
304     return 0;
305 }
306
307 /* create a window */
308 DECL_HANDLER(create_window)
309 {
310     req->handle = 0;
311     if (!req->parent)  /* return desktop window */
312     {
313         if (!top_window)
314         {
315             if (!(top_window = create_window( NULL, NULL, req->atom ))) return;
316             top_window->thread = NULL;  /* no thread owns the desktop */
317         }
318         req->handle = top_window->handle;
319     }
320     else
321     {
322         struct window *win, *parent, *owner = NULL;
323
324         if (!(parent = get_window( req->parent ))) return;
325         if (req->owner && !(owner = get_window( req->owner ))) return;
326         if (!(win = create_window( parent, owner, req->atom ))) return;
327         req->handle = win->handle;
328     }
329 }
330
331
332 /* link a window into the tree */
333 DECL_HANDLER(link_window)
334 {
335     struct window *win, *parent = NULL, *previous = NULL;
336
337     if (!(win = get_window( req->handle ))) return;
338     if (req->parent && !(parent = get_window( req->parent ))) return;
339
340     if (win == top_window)
341     {
342         set_error( STATUS_INVALID_PARAMETER );
343         return;
344     }
345     if (parent && req->previous)
346     {
347         if (req->previous == (user_handle_t)1)  /* special case: HWND_BOTTOM */
348         {
349             previous = parent->last_child;
350             if (previous == win) return;  /* nothing to do */
351         }
352         else
353         {
354             if (!(previous = get_window( req->previous ))) return;
355             /* previous must be a child of parent, and not win itself */
356             if (previous->parent != parent || previous == win)
357             {
358                 set_error( STATUS_INVALID_PARAMETER );
359                 return;
360             }
361         }
362     }
363     link_window( win, parent, previous );
364 }
365
366
367 /* destroy a window */
368 DECL_HANDLER(destroy_window)
369 {
370     struct window *win = get_window( req->handle );
371     if (win)
372     {
373         if (win != top_window) destroy_window( win );
374         else set_error( STATUS_ACCESS_DENIED );
375     }
376 }
377
378
379 /* get information from a window handle */
380 DECL_HANDLER(get_window_info)
381 {
382     struct window *win = get_window( req->handle );
383
384     req->full_handle = 0;
385     req->tid = req->pid = 0;
386     if (win)
387     {
388         req->full_handle = win->handle;
389         if (win->thread)
390         {
391             req->tid = get_thread_id( win->thread );
392             req->pid = get_process_id( win->thread->process );
393         }
394     }
395 }
396
397
398 /* get a list of the window parents, up to the root of the tree */
399 DECL_HANDLER(get_window_parents)
400 {
401     struct window *ptr, *win = get_window( req->handle );
402     int total = 0;
403     size_t len;
404
405     if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
406
407     req->count = total;
408     len = min( get_req_data_size(req), total * sizeof(user_handle_t) );
409     set_req_data_size( req, len );
410     if (len)
411     {
412         user_handle_t *data = get_req_data(req);
413         for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
414             *data++ = ptr->handle;
415     }
416 }
417
418
419 /* get a list of the window children */
420 DECL_HANDLER(get_window_children)
421 {
422     struct window *ptr, *parent = get_window( req->parent );
423     int total = 0;
424     size_t len;
425
426     if (parent)
427         for (ptr = parent->first_child, total = 0; ptr; ptr = ptr->next)
428         {
429             if (req->atom && ptr->atom != req->atom) continue;
430             if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
431             total++;
432         }
433
434     req->count = total;
435     len = min( get_req_data_size(req), total * sizeof(user_handle_t) );
436     set_req_data_size( req, len );
437     if (len)
438     {
439         user_handle_t *data = get_req_data(req);
440         for (ptr = parent->first_child; ptr && len; ptr = ptr->next, len -= sizeof(*data))
441         {
442             if (req->atom && ptr->atom != req->atom) continue;
443             if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
444             *data++ = ptr->handle;
445         }
446     }
447 }
448
449
450 /* get window tree information from a window handle */
451 DECL_HANDLER(get_window_tree)
452 {
453     struct window *win = get_window( req->handle );
454
455     if (!win) return;
456
457     if (win->parent)
458     {
459         struct window *parent = win->parent;
460         req->parent        = parent->handle;
461         req->owner         = win->owner ? win->owner->handle : 0;
462         req->next_sibling  = win->next ? win->next->handle : 0;
463         req->prev_sibling  = win->prev ? win->prev->handle : 0;
464         req->first_sibling = parent->first_child ? parent->first_child->handle : 0;
465         req->last_sibling  = parent->last_child ? parent->last_child->handle : 0;
466     }
467     else
468     {
469         req->parent        = 0;
470         req->owner         = 0;
471         req->next_sibling  = 0;
472         req->prev_sibling  = 0;
473         req->first_sibling = 0;
474         req->last_sibling  = 0;
475     }
476     req->first_child = win->first_child ? win->first_child->handle : 0;
477     req->last_child  = win->last_child ? win->last_child->handle : 0;
478 }
479
480
481 /* set the window and client rectangles of a window */
482 DECL_HANDLER(set_window_rectangles)
483 {
484     struct window *win = get_window( req->handle );
485
486     if (win)
487     {
488         win->window_rect = req->window;
489         win->client_rect = req->client;
490     }
491 }
492
493
494 /* get the window and client rectangles of a window */
495 DECL_HANDLER(get_window_rectangles)
496 {
497     struct window *win = get_window( req->handle );
498
499     if (win)
500     {
501         req->window = win->window_rect;
502         req->client = win->client_rect;
503     }
504 }
505
506
507 /* get the coordinates offset between two windows */
508 DECL_HANDLER(get_windows_offset)
509 {
510     struct window *win;
511
512     req->x = req->y = 0;
513     if (req->from)
514     {
515         if (!(win = get_window( req->from ))) return;
516         while (win)
517         {
518             req->x += win->client_rect.left;
519             req->y += win->client_rect.top;
520             win = win->parent;
521         }
522     }
523     if (req->to)
524     {
525         if (!(win = get_window( req->to ))) return;
526         while (win)
527         {
528             req->x -= win->client_rect.left;
529             req->y -= win->client_rect.top;
530             win = win->parent;
531         }
532     }
533 }
534
535
536 /* set a window property */
537 DECL_HANDLER(set_window_property)
538 {
539     struct window *win = get_window( req->window );
540
541     if (win) set_property( win, req->atom, req->handle,
542                            req->string ? PROP_TYPE_STRING : PROP_TYPE_ATOM );
543 }
544
545
546 /* remove a window property */
547 DECL_HANDLER(remove_window_property)
548 {
549     struct window *win = get_window( req->window );
550     req->handle = 0;
551     if (win) req->handle = remove_property( win, req->atom );
552 }
553
554
555 /* get a window property */
556 DECL_HANDLER(get_window_property)
557 {
558     struct window *win = get_window( req->window );
559     req->handle = 0;
560     if (win) req->handle = get_property( win, req->atom );
561 }
562
563
564 /* get the list of properties of a window */
565 DECL_HANDLER(get_window_properties)
566 {
567     int count = 0;
568     property_data_t *data = get_req_data(req);
569     struct window *win = get_window( req->window );
570
571     if (win) count = enum_properties( win, data, get_req_data_size(req) / sizeof(*data) );
572     set_req_data_size( req, count * sizeof(*data) );
573 }