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