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