server: Track created and removed directories in the tree of inodes for inotify.
[wine] / server / class.c
1 /*
2  * Server-side window class management
3  *
4  * Copyright (C) 2002 Mike McCormack
5  * Copyright (C) 2003 Alexandre Julliard
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32
33 #include "wine/list.h"
34
35 #include "request.h"
36 #include "object.h"
37 #include "process.h"
38 #include "user.h"
39 #include "winuser.h"
40 #include "winternl.h"
41
42 struct window_class
43 {
44     struct list     entry;           /* entry in process list */
45     struct process *process;         /* process owning the class */
46     int             count;           /* reference count */
47     int             local;           /* local class? */
48     atom_t          atom;            /* class atom */
49     void           *instance;        /* module instance */
50     unsigned int    style;           /* class style */
51     int             win_extra;       /* number of window extra bytes */
52     void           *client_ptr;      /* pointer to class in client address space */
53     int             nb_extra_bytes;  /* number of extra bytes */
54     char            extra_bytes[1];  /* extra bytes storage */
55 };
56
57 static struct window_class *desktop_class;
58
59 static struct window_class *create_class( struct process *process, int extra_bytes, int local )
60 {
61     struct window_class *class;
62
63     if (!(class = mem_alloc( sizeof(*class) + extra_bytes - 1 ))) return NULL;
64
65     class->process = (struct process *)grab_object( process );
66     class->count = 0;
67     class->local = local;
68     class->nb_extra_bytes = extra_bytes;
69     memset( class->extra_bytes, 0, extra_bytes );
70     /* other fields are initialized by caller */
71
72     /* local classes have priority so we put them first in the list */
73     if (local) list_add_head( &process->classes, &class->entry );
74     else list_add_tail( &process->classes, &class->entry );
75     return class;
76 }
77
78 static struct window_class *get_desktop_class(void)
79 {
80     if (!desktop_class)
81     {
82         if (!(desktop_class = mem_alloc( sizeof(*desktop_class) - 1 ))) return NULL;
83         desktop_class->process        = NULL;
84         desktop_class->count          = 0;
85         desktop_class->local          = 0;
86         desktop_class->nb_extra_bytes = 0;
87         desktop_class->atom           = DESKTOP_ATOM;
88         desktop_class->instance       = NULL;
89         desktop_class->style          = CS_DBLCLKS;
90         desktop_class->win_extra      = 0;
91         desktop_class->client_ptr     = NULL;
92     }
93     return desktop_class;
94 }
95
96 static void destroy_class( struct window_class *class )
97 {
98     list_remove( &class->entry );
99     release_object( class->process );
100     free( class );
101 }
102
103 void destroy_process_classes( struct process *process )
104 {
105     struct list *ptr;
106
107     while ((ptr = list_head( &process->classes )))
108     {
109         struct window_class *class = LIST_ENTRY( ptr, struct window_class, entry );
110         destroy_class( class );
111     }
112 }
113
114 static struct window_class *find_class( struct process *process, atom_t atom, void *instance )
115 {
116     struct list *ptr;
117
118     LIST_FOR_EACH( ptr, &process->classes )
119     {
120         struct window_class *class = LIST_ENTRY( ptr, struct window_class, entry );
121         if (class->atom != atom) continue;
122         if (!instance || !class->local || class->instance == instance) return class;
123     }
124     if (atom == DESKTOP_ATOM) return get_desktop_class();
125     return NULL;
126 }
127
128 struct window_class *grab_class( struct process *process, atom_t atom,
129                                  void *instance, int *extra_bytes )
130 {
131     struct window_class *class = find_class( process, atom, instance );
132     if (class)
133     {
134         class->count++;
135         *extra_bytes = class->win_extra;
136     }
137     else set_error( STATUS_INVALID_HANDLE );
138     return class;
139 }
140
141 void release_class( struct window_class *class )
142 {
143     assert( class->count > 0 );
144     class->count--;
145 }
146
147 atom_t get_class_atom( struct window_class *class )
148 {
149     return class->atom;
150 }
151
152 void *get_class_client_ptr( struct window_class *class )
153 {
154     return class->client_ptr;
155 }
156
157 /* create a window class */
158 DECL_HANDLER(create_class)
159 {
160     struct window_class *class;
161     struct winstation *winstation;
162
163     if (!req->local && req->atom == DESKTOP_ATOM)
164         return;  /* silently ignore attempts to create the desktop class */
165
166     class = find_class( current->process, req->atom, req->instance );
167     if (class && !class->local == !req->local)
168     {
169         set_win32_error( ERROR_CLASS_ALREADY_EXISTS );
170         return;
171     }
172     if (req->extra < 0 || req->extra > 4096 || req->win_extra < 0 || req->win_extra > 4096)
173     {
174         /* don't allow stupid values here */
175         set_error( STATUS_INVALID_PARAMETER );
176         return;
177     }
178
179     if (!(winstation = get_process_winstation( current->process, WINSTA_ACCESSGLOBALATOMS )))
180         return;
181
182     if (!grab_global_atom( winstation, req->atom ))
183     {
184         release_object( winstation );
185         return;
186     }
187     if (!(class = create_class( current->process, req->extra, req->local )))
188     {
189         release_global_atom( winstation, req->atom );
190         release_object( winstation );
191         return;
192     }
193     class->atom       = req->atom;
194     class->instance   = req->instance;
195     class->style      = req->style;
196     class->win_extra  = req->win_extra;
197     class->client_ptr = req->client_ptr;
198     release_object( winstation );
199 }
200
201 /* destroy a window class */
202 DECL_HANDLER(destroy_class)
203 {
204     struct window_class *class = find_class( current->process, req->atom, req->instance );
205
206     if (!class)
207         set_win32_error( ERROR_CLASS_DOES_NOT_EXIST );
208     else if (class->count)
209         set_win32_error( ERROR_CLASS_HAS_WINDOWS );
210     else
211     {
212         reply->client_ptr = class->client_ptr;
213         if (class != desktop_class) destroy_class( class );
214     }
215 }
216
217
218 /* set some information in a class */
219 DECL_HANDLER(set_class_info)
220 {
221     struct window_class *class = get_window_class( req->window );
222
223     if (!class) return;
224
225     if (req->flags && class->process != current->process)
226     {
227         set_error( STATUS_ACCESS_DENIED );
228         return;
229     }
230
231     if (req->extra_size > sizeof(req->extra_value) ||
232         req->extra_offset < -1 ||
233         req->extra_offset > class->nb_extra_bytes - (int)req->extra_size)
234     {
235         set_win32_error( ERROR_INVALID_INDEX );
236         return;
237     }
238     if ((req->flags & SET_CLASS_WINEXTRA) && (req->win_extra < 0 || req->win_extra > 4096))
239     {
240         set_error( STATUS_INVALID_PARAMETER );
241         return;
242     }
243     if (req->extra_offset != -1)
244     {
245         memcpy( &reply->old_extra_value, class->extra_bytes + req->extra_offset, req->extra_size );
246     }
247     else if (req->flags & SET_CLASS_EXTRA)
248     {
249         set_win32_error( ERROR_INVALID_INDEX );
250         return;
251     }
252
253     reply->old_atom      = class->atom;
254     reply->old_style     = class->style;
255     reply->old_extra     = class->nb_extra_bytes;
256     reply->old_win_extra = class->win_extra;
257     reply->old_instance  = class->instance;
258
259     if (req->flags & SET_CLASS_ATOM)
260     {
261         struct winstation *winstation = get_process_winstation( current->process,
262                                                                 WINSTA_ACCESSGLOBALATOMS );
263         if (!grab_global_atom( winstation, req->atom ))
264         {
265             release_object( winstation );
266             return;
267         }
268         release_global_atom( winstation, class->atom );
269         class->atom = req->atom;
270         release_object( winstation );
271     }
272     if (req->flags & SET_CLASS_STYLE) class->style = req->style;
273     if (req->flags & SET_CLASS_WINEXTRA) class->win_extra = req->win_extra;
274     if (req->flags & SET_CLASS_INSTANCE) class->instance = req->instance;
275     if (req->flags & SET_CLASS_EXTRA) memcpy( class->extra_bytes + req->extra_offset,
276                                               &req->extra_value, req->extra_size );
277 }