Fixed a few prototypes in the USER driver.
[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 "wine/list.h"
31
32 #include "request.h"
33 #include "object.h"
34 #include "process.h"
35 #include "user.h"
36 #include "winuser.h"
37
38 struct window_class
39 {
40     struct list     entry;           /* entry in process list */
41     struct process *process;         /* process owning the class */
42     int             count;           /* reference count */
43     int             local;           /* local class? */
44     atom_t          atom;            /* class atom */
45     void           *instance;        /* module instance */
46     unsigned int    style;           /* class style */
47     int             win_extra;       /* number of window extra bytes */
48     void           *client_ptr;      /* pointer to class in client address space */
49     int             nb_extra_bytes;  /* number of extra bytes */
50     char            extra_bytes[1];  /* extra bytes storage */
51 };
52
53 #define DESKTOP_ATOM  ((atom_t)32769)
54
55 static struct window_class *desktop_class;
56
57 static struct window_class *create_class( struct process *process, int extra_bytes, int local )
58 {
59     struct window_class *class;
60
61     if (!(class = mem_alloc( sizeof(*class) + extra_bytes - 1 ))) return NULL;
62
63     class->process = (struct process *)grab_object( process );
64     class->count = 0;
65     class->local = local;
66     class->nb_extra_bytes = extra_bytes;
67     memset( class->extra_bytes, 0, extra_bytes );
68     /* other fields are initialized by caller */
69
70     /* local classes have priority so we put them first in the list */
71     if (local) list_add_head( &process->classes, &class->entry );
72     else list_add_tail( &process->classes, &class->entry );
73     return class;
74 }
75
76 static struct window_class *create_desktop_class( unsigned int style, int win_extra )
77 {
78     struct window_class *class;
79
80     if (!(class = mem_alloc( sizeof(*class) - 1 ))) return NULL;
81
82     class->process        = NULL;
83     class->count          = 0;
84     class->local          = 0;
85     class->nb_extra_bytes = 0;
86     class->atom           = DESKTOP_ATOM;
87     class->instance       = NULL;
88     class->style          = style;
89     class->win_extra      = win_extra;
90     class->client_ptr     = NULL;
91     desktop_class = class;
92     return class;
93 }
94
95 static void destroy_class( struct window_class *class )
96 {
97     list_remove( &class->entry );
98     release_object( class->process );
99     free( class );
100 }
101
102 void destroy_process_classes( struct process *process )
103 {
104     struct list *ptr;
105
106     while ((ptr = list_head( &process->classes )))
107     {
108         struct window_class *class = LIST_ENTRY( ptr, struct window_class, entry );
109         destroy_class( class );
110     }
111 }
112
113 static struct window_class *find_class( struct process *process, atom_t atom, void *instance )
114 {
115     struct list *ptr;
116
117     LIST_FOR_EACH( ptr, &process->classes )
118     {
119         struct window_class *class = LIST_ENTRY( ptr, struct window_class, entry );
120         if (class->atom != atom) continue;
121         if (!instance || !class->local || class->instance == instance) return class;
122     }
123     if (atom == DESKTOP_ATOM) return desktop_class;
124     return NULL;
125 }
126
127 struct window_class *grab_class( struct process *process, atom_t atom,
128                                  void *instance, int *extra_bytes )
129 {
130     struct window_class *class = find_class( process, atom, instance );
131     if (class)
132     {
133         class->count++;
134         *extra_bytes = class->win_extra;
135     }
136     else set_error( STATUS_INVALID_HANDLE );
137     return class;
138 }
139
140 void release_class( struct window_class *class )
141 {
142     assert( class->count > 0 );
143     class->count--;
144 }
145
146 atom_t get_class_atom( struct window_class *class )
147 {
148     return class->atom;
149 }
150
151 void *get_class_client_ptr( struct window_class *class )
152 {
153     return class->client_ptr;
154 }
155
156 /* create a window class */
157 DECL_HANDLER(create_class)
158 {
159     struct window_class *class;
160     struct winstation *winstation;
161
162     if (!req->local && req->atom == DESKTOP_ATOM)
163     {
164         if (!desktop_class) create_desktop_class( req->style, req->win_extra );
165         return;  /* silently ignore further attempts to create the desktop class */
166     }
167
168     class = find_class( current->process, req->atom, req->instance );
169     if (class && !class->local == !req->local)
170     {
171         set_win32_error( ERROR_CLASS_ALREADY_EXISTS );
172         return;
173     }
174     if (req->extra < 0 || req->extra > 4096 || req->win_extra < 0 || req->win_extra > 4096)
175     {
176         /* don't allow stupid values here */
177         set_error( STATUS_INVALID_PARAMETER );
178         return;
179     }
180
181     if (!(winstation = get_process_winstation( current->process, WINSTA_ACCESSGLOBALATOMS )))
182         return;
183
184     if (!grab_global_atom( winstation, req->atom ))
185     {
186         release_object( winstation );
187         return;
188     }
189     if (!(class = create_class( current->process, req->extra, req->local )))
190     {
191         release_global_atom( winstation, req->atom );
192         release_object( winstation );
193         return;
194     }
195     class->atom       = req->atom;
196     class->instance   = req->instance;
197     class->style      = req->style;
198     class->win_extra  = req->win_extra;
199     class->client_ptr = req->client_ptr;
200     release_object( winstation );
201 }
202
203 /* destroy a window class */
204 DECL_HANDLER(destroy_class)
205 {
206     struct window_class *class = find_class( current->process, req->atom, req->instance );
207
208     if (!class)
209         set_win32_error( ERROR_CLASS_DOES_NOT_EXIST );
210     else if (class->count)
211         set_win32_error( ERROR_CLASS_HAS_WINDOWS );
212     else
213     {
214         reply->client_ptr = class->client_ptr;
215         if (class != desktop_class) destroy_class( class );
216     }
217 }
218
219
220 /* set some information in a class */
221 DECL_HANDLER(set_class_info)
222 {
223     struct window_class *class = get_window_class( req->window );
224
225     if (!class) return;
226
227     if (req->flags && class->process != current->process)
228     {
229         set_error( STATUS_ACCESS_DENIED );
230         return;
231     }
232
233     if (req->extra_size > sizeof(req->extra_value) ||
234         req->extra_offset < -1 ||
235         req->extra_offset > class->nb_extra_bytes - (int)req->extra_size)
236     {
237         set_win32_error( ERROR_INVALID_INDEX );
238         return;
239     }
240     if ((req->flags & SET_CLASS_WINEXTRA) && (req->win_extra < 0 || req->win_extra > 4096))
241     {
242         set_error( STATUS_INVALID_PARAMETER );
243         return;
244     }
245     if (req->extra_offset != -1)
246     {
247         memcpy( &reply->old_extra_value, class->extra_bytes + req->extra_offset, req->extra_size );
248     }
249     else if (req->flags & SET_CLASS_EXTRA)
250     {
251         set_win32_error( ERROR_INVALID_INDEX );
252         return;
253     }
254
255     reply->old_atom      = class->atom;
256     reply->old_style     = class->style;
257     reply->old_extra     = class->nb_extra_bytes;
258     reply->old_win_extra = class->win_extra;
259     reply->old_instance  = class->instance;
260
261     if (req->flags & SET_CLASS_ATOM)
262     {
263         struct winstation *winstation = get_process_winstation( current->process,
264                                                                 WINSTA_ACCESSGLOBALATOMS );
265         if (!grab_global_atom( winstation, req->atom ))
266         {
267             release_object( winstation );
268             return;
269         }
270         release_global_atom( winstation, class->atom );
271         class->atom = req->atom;
272         release_object( winstation );
273     }
274     if (req->flags & SET_CLASS_STYLE) class->style = req->style;
275     if (req->flags & SET_CLASS_WINEXTRA) class->win_extra = req->win_extra;
276     if (req->flags & SET_CLASS_INSTANCE) class->instance = req->instance;
277     if (req->flags & SET_CLASS_EXTRA) memcpy( class->extra_bytes + req->extra_offset,
278                                               &req->extra_value, req->extra_size );
279 }