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