winegstreamer: Implement IWMStreamConfig::GetStreamType().
[wine.git] / server / class.c
blobe1e180bd97c7f6da91ccb6c050241c923467db62
1 /*
2 * Server-side window class management
4 * Copyright (C) 2002 Mike McCormack
5 * Copyright (C) 2003 Alexandre Julliard
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
32 #include "wine/list.h"
34 #include "request.h"
35 #include "object.h"
36 #include "process.h"
37 #include "user.h"
38 #include "winuser.h"
39 #include "winternl.h"
41 struct window_class
43 struct list entry; /* entry in process list */
44 struct process *process; /* process owning the class */
45 int count; /* reference count */
46 int local; /* local class? */
47 atom_t atom; /* class atom */
48 atom_t base_atom; /* base class atom for versioned class */
49 mod_handle_t instance; /* module instance */
50 unsigned int style; /* class style */
51 int win_extra; /* number of window extra bytes */
52 client_ptr_t 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 */
57 static struct window_class *create_class( struct process *process, int extra_bytes, int local )
59 struct window_class *class;
61 if (!(class = mem_alloc( sizeof(*class) + extra_bytes - 1 ))) return NULL;
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 */
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;
76 static void destroy_class( struct window_class *class )
78 release_global_atom( NULL, class->atom );
79 release_global_atom( NULL, class->base_atom );
80 list_remove( &class->entry );
81 release_object( class->process );
82 free( class );
85 void destroy_process_classes( struct process *process )
87 struct list *ptr;
89 while ((ptr = list_head( &process->classes )))
91 struct window_class *class = LIST_ENTRY( ptr, struct window_class, entry );
92 destroy_class( class );
96 static struct window_class *find_class( struct process *process, atom_t atom, mod_handle_t instance )
98 struct list *ptr;
100 LIST_FOR_EACH( ptr, &process->classes )
102 int is_win16;
103 struct window_class *class = LIST_ENTRY( ptr, struct window_class, entry );
104 if (class->atom != atom) continue;
105 is_win16 = !(class->instance >> 16);
106 if (!instance || !class->local || class->instance == instance ||
107 (!is_win16 && ((class->instance & ~0xffff) == (instance & ~0xffff)))) return class;
109 return NULL;
112 struct window_class *grab_class( struct process *process, atom_t atom,
113 mod_handle_t instance, int *extra_bytes )
115 struct window_class *class = find_class( process, atom, instance );
116 if (class)
118 class->count++;
119 *extra_bytes = class->win_extra;
121 else set_error( STATUS_INVALID_HANDLE );
122 return class;
125 void release_class( struct window_class *class )
127 assert( class->count > 0 );
128 class->count--;
131 int is_desktop_class( struct window_class *class )
133 return (class->atom == DESKTOP_ATOM && !class->local);
136 int is_hwnd_message_class( struct window_class *class )
138 static const WCHAR messageW[] = {'M','e','s','s','a','g','e'};
139 static const struct unicode_str name = { messageW, sizeof(messageW) };
141 return (!class->local && class->atom == find_global_atom( NULL, &name ));
144 atom_t get_class_atom( struct window_class *class )
146 return class->base_atom;
149 client_ptr_t get_class_client_ptr( struct window_class *class )
151 return class->client_ptr;
154 /* create a window class */
155 DECL_HANDLER(create_class)
157 struct window_class *class;
158 struct unicode_str name = get_req_unicode_str();
159 atom_t atom, base_atom;
161 if (name.len)
163 atom = add_global_atom( NULL, &name );
164 if (!atom) return;
165 if (req->name_offset && req->name_offset < name.len / sizeof(WCHAR))
167 name.str += req->name_offset;
168 name.len -= req->name_offset * sizeof(WCHAR);
170 base_atom = add_global_atom( NULL, &name );
171 if (!base_atom)
173 release_global_atom( NULL, atom );
174 return;
177 else
179 base_atom = atom;
180 grab_global_atom( NULL, atom );
183 else
185 base_atom = atom = req->atom;
186 if (!grab_global_atom( NULL, atom )) return;
187 grab_global_atom( NULL, base_atom );
190 class = find_class( current->process, atom, req->instance );
191 if (class && !class->local == !req->local)
193 set_win32_error( ERROR_CLASS_ALREADY_EXISTS );
194 release_global_atom( NULL, atom );
195 release_global_atom( NULL, base_atom );
196 return;
198 if (req->extra < 0 || req->extra > 4096 || req->win_extra < 0 || req->win_extra > 4096)
200 /* don't allow stupid values here */
201 set_error( STATUS_INVALID_PARAMETER );
202 release_global_atom( NULL, atom );
203 release_global_atom( NULL, base_atom );
204 return;
207 if (!(class = create_class( current->process, req->extra, req->local )))
209 release_global_atom( NULL, atom );
210 release_global_atom( NULL, base_atom );
211 return;
213 class->atom = atom;
214 class->base_atom = base_atom;
215 class->instance = req->instance;
216 class->style = req->style;
217 class->win_extra = req->win_extra;
218 class->client_ptr = req->client_ptr;
219 reply->atom = atom;
222 /* destroy a window class */
223 DECL_HANDLER(destroy_class)
225 struct window_class *class;
226 struct unicode_str name = get_req_unicode_str();
227 atom_t atom = req->atom;
229 if (name.len) atom = find_global_atom( NULL, &name );
231 if (!(class = find_class( current->process, atom, req->instance )))
232 set_win32_error( ERROR_CLASS_DOES_NOT_EXIST );
233 else if (class->count)
234 set_win32_error( ERROR_CLASS_HAS_WINDOWS );
235 else
237 reply->client_ptr = class->client_ptr;
238 destroy_class( class );
243 /* set some information in a class */
244 DECL_HANDLER(set_class_info)
246 struct window_class *class = get_window_class( req->window );
248 if (!class) return;
250 if (req->flags && class->process != current->process)
252 set_error( STATUS_ACCESS_DENIED );
253 return;
256 if (req->extra_size > sizeof(req->extra_value) ||
257 req->extra_offset < -1 ||
258 req->extra_offset > class->nb_extra_bytes - (int)req->extra_size)
260 set_win32_error( ERROR_INVALID_INDEX );
261 return;
263 if ((req->flags & SET_CLASS_WINEXTRA) && (req->win_extra < 0 || req->win_extra > 4096))
265 set_error( STATUS_INVALID_PARAMETER );
266 return;
268 if (req->extra_offset != -1)
270 memcpy( &reply->old_extra_value, class->extra_bytes + req->extra_offset, req->extra_size );
272 else if (req->flags & SET_CLASS_EXTRA)
274 set_win32_error( ERROR_INVALID_INDEX );
275 return;
278 reply->old_atom = class->atom;
279 reply->old_style = class->style;
280 reply->old_extra = class->nb_extra_bytes;
281 reply->old_win_extra = class->win_extra;
282 reply->old_instance = class->instance;
283 reply->base_atom = class->base_atom;
285 if (req->flags & SET_CLASS_ATOM)
287 if (!grab_global_atom( NULL, req->atom )) return;
288 release_global_atom( NULL, class->atom );
289 class->atom = req->atom;
291 if (req->flags & SET_CLASS_STYLE) class->style = req->style;
292 if (req->flags & SET_CLASS_WINEXTRA) class->win_extra = req->win_extra;
293 if (req->flags & SET_CLASS_INSTANCE) class->instance = req->instance;
294 if (req->flags & SET_CLASS_EXTRA) memcpy( class->extra_bytes + req->extra_offset,
295 &req->extra_value, req->extra_size );