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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/port.h"
30 #include "wine/list.h"
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 */
53 #define DESKTOP_ATOM ((atom_t)32769)
55 static struct window_class
*desktop_class
;
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
);
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
);
76 static struct window_class
*create_desktop_class( unsigned int style
, int win_extra
)
78 struct window_class
*class;
80 if (!(class = mem_alloc( sizeof(*class) - 1 ))) return NULL
;
82 class->process
= NULL
;
85 class->nb_extra_bytes
= 0;
86 class->atom
= DESKTOP_ATOM
;
87 class->instance
= NULL
;
89 class->win_extra
= win_extra
;
90 class->client_ptr
= NULL
;
91 desktop_class
= class;
95 static void destroy_class( struct window_class
*class )
97 list_remove( &class->entry
);
98 release_object( class->process
);
102 void destroy_process_classes( struct process
*process
)
106 while ((ptr
= list_head( &process
->classes
)))
108 struct window_class
*class = LIST_ENTRY( ptr
, struct window_class
, entry
);
109 destroy_class( class );
113 static struct window_class
*find_class( struct process
*process
, atom_t atom
, void *instance
)
117 LIST_FOR_EACH( ptr
, &process
->classes
)
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;
123 if (atom
== DESKTOP_ATOM
) return desktop_class
;
127 struct window_class
*grab_class( struct process
*process
, atom_t atom
,
128 void *instance
, int *extra_bytes
)
130 struct window_class
*class = find_class( process
, atom
, instance
);
134 *extra_bytes
= class->win_extra
;
136 else set_error( STATUS_INVALID_HANDLE
);
140 void release_class( struct window_class
*class )
142 assert( class->count
> 0 );
146 atom_t
get_class_atom( struct window_class
*class )
151 void *get_class_client_ptr( struct window_class
*class )
153 return class->client_ptr
;
156 /* create a window class */
157 DECL_HANDLER(create_class
)
159 struct window_class
*class;
160 struct winstation
*winstation
;
162 if (!req
->local
&& req
->atom
== DESKTOP_ATOM
)
164 if (!desktop_class
) create_desktop_class( req
->style
, req
->win_extra
);
165 return; /* silently ignore further attempts to create the desktop class */
168 class = find_class( current
->process
, req
->atom
, req
->instance
);
169 if (class && !class->local
== !req
->local
)
171 set_win32_error( ERROR_CLASS_ALREADY_EXISTS
);
174 if (req
->extra
< 0 || req
->extra
> 4096 || req
->win_extra
< 0 || req
->win_extra
> 4096)
176 /* don't allow stupid values here */
177 set_error( STATUS_INVALID_PARAMETER
);
181 if (!(winstation
= get_process_winstation( current
->process
, WINSTA_ACCESSGLOBALATOMS
)))
184 if (!grab_global_atom( winstation
, req
->atom
))
186 release_object( winstation
);
189 if (!(class = create_class( current
->process
, req
->extra
, req
->local
)))
191 release_global_atom( winstation
, req
->atom
);
192 release_object( winstation
);
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
);
203 /* destroy a window class */
204 DECL_HANDLER(destroy_class
)
206 struct window_class
*class = find_class( current
->process
, req
->atom
, req
->instance
);
209 set_win32_error( ERROR_CLASS_DOES_NOT_EXIST
);
210 else if (class->count
)
211 set_win32_error( ERROR_CLASS_HAS_WINDOWS
);
214 reply
->client_ptr
= class->client_ptr
;
215 if (class != desktop_class
) destroy_class( class );
220 /* set some information in a class */
221 DECL_HANDLER(set_class_info
)
223 struct window_class
*class = get_window_class( req
->window
);
227 if (req
->flags
&& class->process
!= current
->process
)
229 set_error( STATUS_ACCESS_DENIED
);
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
)
237 set_win32_error( ERROR_INVALID_INDEX
);
240 if ((req
->flags
& SET_CLASS_WINEXTRA
) && (req
->win_extra
< 0 || req
->win_extra
> 4096))
242 set_error( STATUS_INVALID_PARAMETER
);
245 if (req
->extra_offset
!= -1)
247 memcpy( &reply
->old_extra_value
, class->extra_bytes
+ req
->extra_offset
, req
->extra_size
);
249 else if (req
->flags
& SET_CLASS_EXTRA
)
251 set_win32_error( ERROR_INVALID_INDEX
);
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
;
261 if (req
->flags
& SET_CLASS_ATOM
)
263 struct winstation
*winstation
= get_process_winstation( current
->process
,
264 WINSTA_ACCESSGLOBALATOMS
);
265 if (!grab_global_atom( winstation
, req
->atom
))
267 release_object( winstation
);
270 release_global_atom( winstation
, class->atom
);
271 class->atom
= req
->atom
;
272 release_object( winstation
);
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
);