msvcr120: Add [_]strtoimax[_l] and [_]strtoumax[_l].
[wine.git] / server / class.c
blob92cbfa2d7e3250196e5e40c1c9947929443a631c
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"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
33 #include "wine/list.h"
35 #include "request.h"
36 #include "object.h"
37 #include "process.h"
38 #include "user.h"
39 #include "winuser.h"
40 #include "winternl.h"
42 struct window_class
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 atom_t base_atom; /* base class atom for versioned class */
50 mod_handle_t instance; /* module instance */
51 unsigned int style; /* class style */
52 int win_extra; /* number of window extra bytes */
53 client_ptr_t client_ptr; /* pointer to class in client address space */
54 int nb_extra_bytes; /* number of extra bytes */
55 char extra_bytes[1]; /* extra bytes storage */
58 static struct window_class *create_class( struct process *process, int extra_bytes, int local )
60 struct window_class *class;
62 if (!(class = mem_alloc( sizeof(*class) + extra_bytes - 1 ))) return NULL;
64 class->process = (struct process *)grab_object( process );
65 class->count = 0;
66 class->local = local;
67 class->nb_extra_bytes = extra_bytes;
68 memset( class->extra_bytes, 0, extra_bytes );
69 /* other fields are initialized by caller */
71 /* local classes have priority so we put them first in the list */
72 if (local) list_add_head( &process->classes, &class->entry );
73 else list_add_tail( &process->classes, &class->entry );
74 return class;
77 static void destroy_class( struct window_class *class )
79 release_global_atom( NULL, class->atom );
80 release_global_atom( NULL, class->base_atom );
81 list_remove( &class->entry );
82 release_object( class->process );
83 free( class );
86 void destroy_process_classes( struct process *process )
88 struct list *ptr;
90 while ((ptr = list_head( &process->classes )))
92 struct window_class *class = LIST_ENTRY( ptr, struct window_class, entry );
93 destroy_class( class );
97 static struct window_class *find_class( struct process *process, atom_t atom, mod_handle_t instance )
99 struct list *ptr;
101 LIST_FOR_EACH( ptr, &process->classes )
103 int is_win16;
104 struct window_class *class = LIST_ENTRY( ptr, struct window_class, entry );
105 if (class->atom != atom) continue;
106 is_win16 = !(class->instance >> 16);
107 if (!instance || !class->local || class->instance == instance ||
108 (!is_win16 && ((class->instance & ~0xffff) == (instance & ~0xffff)))) return class;
110 return NULL;
113 struct window_class *grab_class( struct process *process, atom_t atom,
114 mod_handle_t instance, int *extra_bytes )
116 struct window_class *class = find_class( process, atom, instance );
117 if (class)
119 class->count++;
120 *extra_bytes = class->win_extra;
122 else set_error( STATUS_INVALID_HANDLE );
123 return class;
126 void release_class( struct window_class *class )
128 assert( class->count > 0 );
129 class->count--;
132 int is_desktop_class( struct window_class *class )
134 return (class->atom == DESKTOP_ATOM && !class->local);
137 int is_hwnd_message_class( struct window_class *class )
139 static const WCHAR messageW[] = {'M','e','s','s','a','g','e'};
140 static const struct unicode_str name = { messageW, sizeof(messageW) };
142 return (!class->local && class->atom == find_global_atom( NULL, &name ));
145 atom_t get_class_atom( struct window_class *class )
147 return class->base_atom;
150 client_ptr_t get_class_client_ptr( struct window_class *class )
152 return class->client_ptr;
155 /* create a window class */
156 DECL_HANDLER(create_class)
158 struct window_class *class;
159 struct unicode_str name = get_req_unicode_str();
160 atom_t atom, base_atom;
162 if (name.len)
164 atom = add_global_atom( NULL, &name );
165 if (!atom) return;
166 if (req->name_offset && req->name_offset < name.len / sizeof(WCHAR))
168 name.str += req->name_offset;
169 name.len -= req->name_offset * sizeof(WCHAR);
171 base_atom = add_global_atom( NULL, &name );
172 if (!base_atom)
174 release_global_atom( NULL, atom );
175 return;
178 else
180 base_atom = atom;
181 grab_global_atom( NULL, atom );
184 else
186 base_atom = atom = req->atom;
187 if (!grab_global_atom( NULL, atom )) return;
188 grab_global_atom( NULL, base_atom );
191 class = find_class( current->process, atom, req->instance );
192 if (class && !class->local == !req->local)
194 set_win32_error( ERROR_CLASS_ALREADY_EXISTS );
195 release_global_atom( NULL, atom );
196 release_global_atom( NULL, base_atom );
197 return;
199 if (req->extra < 0 || req->extra > 4096 || req->win_extra < 0 || req->win_extra > 4096)
201 /* don't allow stupid values here */
202 set_error( STATUS_INVALID_PARAMETER );
203 release_global_atom( NULL, atom );
204 release_global_atom( NULL, base_atom );
205 return;
208 if (!(class = create_class( current->process, req->extra, req->local )))
210 release_global_atom( NULL, atom );
211 release_global_atom( NULL, base_atom );
212 return;
214 class->atom = atom;
215 class->base_atom = base_atom;
216 class->instance = req->instance;
217 class->style = req->style;
218 class->win_extra = req->win_extra;
219 class->client_ptr = req->client_ptr;
220 reply->atom = atom;
223 /* destroy a window class */
224 DECL_HANDLER(destroy_class)
226 struct window_class *class;
227 struct unicode_str name = get_req_unicode_str();
228 atom_t atom = req->atom;
230 if (name.len) atom = find_global_atom( NULL, &name );
232 if (!(class = find_class( current->process, atom, req->instance )))
233 set_win32_error( ERROR_CLASS_DOES_NOT_EXIST );
234 else if (class->count)
235 set_win32_error( ERROR_CLASS_HAS_WINDOWS );
236 else
238 reply->client_ptr = class->client_ptr;
239 destroy_class( class );
244 /* set some information in a class */
245 DECL_HANDLER(set_class_info)
247 struct window_class *class = get_window_class( req->window );
249 if (!class) return;
251 if (req->flags && class->process != current->process)
253 set_error( STATUS_ACCESS_DENIED );
254 return;
257 if (req->extra_size > sizeof(req->extra_value) ||
258 req->extra_offset < -1 ||
259 req->extra_offset > class->nb_extra_bytes - (int)req->extra_size)
261 set_win32_error( ERROR_INVALID_INDEX );
262 return;
264 if ((req->flags & SET_CLASS_WINEXTRA) && (req->win_extra < 0 || req->win_extra > 4096))
266 set_error( STATUS_INVALID_PARAMETER );
267 return;
269 if (req->extra_offset != -1)
271 memcpy( &reply->old_extra_value, class->extra_bytes + req->extra_offset, req->extra_size );
273 else if (req->flags & SET_CLASS_EXTRA)
275 set_win32_error( ERROR_INVALID_INDEX );
276 return;
279 reply->old_atom = class->atom;
280 reply->old_style = class->style;
281 reply->old_extra = class->nb_extra_bytes;
282 reply->old_win_extra = class->win_extra;
283 reply->old_instance = class->instance;
284 reply->base_atom = class->base_atom;
286 if (req->flags & SET_CLASS_ATOM)
288 if (!grab_global_atom( NULL, req->atom )) return;
289 release_global_atom( NULL, class->atom );
290 class->atom = req->atom;
292 if (req->flags & SET_CLASS_STYLE) class->style = req->style;
293 if (req->flags & SET_CLASS_WINEXTRA) class->win_extra = req->win_extra;
294 if (req->flags & SET_CLASS_INSTANCE) class->instance = req->instance;
295 if (req->flags & SET_CLASS_EXTRA) memcpy( class->extra_bytes + req->extra_offset,
296 &req->extra_value, req->extra_size );