First import
[xorg_rtime.git] / xorg-server-1.4 / dix / privates.c
blob2465971175c023efcaa78eb6def65c69dd2f0c72
1 /*
3 Copyright 1993, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
11 The above copyright notice and this permission notice shall be included
12 in all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 OTHER DEALINGS IN THE SOFTWARE.
22 Except as contained in this notice, the name of The Open Group shall
23 not be used in advertising or otherwise to promote the sale, use or
24 other dealings in this Software without prior written authorization
25 from The Open Group.
29 #ifdef HAVE_DIX_CONFIG_H
30 #include <dix-config.h>
31 #endif
33 #include <X11/X.h>
34 #include "scrnintstr.h"
35 #include "misc.h"
36 #include "os.h"
37 #include "windowstr.h"
38 #include "resource.h"
39 #include "dixstruct.h"
40 #include "gcstruct.h"
41 #include "colormapst.h"
42 #include "servermd.h"
43 #include "site.h"
44 #include "inputstr.h"
45 #include "extnsionst.h"
48 * See the Wrappers and devPrivates section in "Definition of the
49 * Porting Layer for the X v11 Sample Server" (doc/Server/ddx.tbl.ms)
50 * for information on how to use devPrivates.
54 * extension private machinery
57 static int extensionPrivateCount;
58 int extensionPrivateLen;
59 unsigned *extensionPrivateSizes;
60 unsigned totalExtensionSize;
62 void
63 ResetExtensionPrivates(void)
65 extensionPrivateCount = 0;
66 extensionPrivateLen = 0;
67 xfree(extensionPrivateSizes);
68 extensionPrivateSizes = (unsigned *)NULL;
69 totalExtensionSize =
70 ((sizeof(ExtensionEntry) + sizeof(long) - 1) / sizeof(long)) * sizeof(long);
73 _X_EXPORT int
74 AllocateExtensionPrivateIndex(void)
76 return extensionPrivateCount++;
79 _X_EXPORT Bool
80 AllocateExtensionPrivate(int index2, unsigned amount)
82 unsigned oldamount;
84 /* Round up sizes for proper alignment */
85 amount = ((amount + (sizeof(long) - 1)) / sizeof(long)) * sizeof(long);
87 if (index2 >= extensionPrivateLen)
89 unsigned *nsizes;
90 nsizes = (unsigned *)xrealloc(extensionPrivateSizes,
91 (index2 + 1) * sizeof(unsigned));
92 if (!nsizes)
93 return FALSE;
94 while (extensionPrivateLen <= index2)
96 nsizes[extensionPrivateLen++] = 0;
97 totalExtensionSize += sizeof(DevUnion);
99 extensionPrivateSizes = nsizes;
101 oldamount = extensionPrivateSizes[index2];
102 if (amount > oldamount)
104 extensionPrivateSizes[index2] = amount;
105 totalExtensionSize += (amount - oldamount);
107 return TRUE;
111 * client private machinery
114 static int clientPrivateCount;
115 int clientPrivateLen;
116 unsigned *clientPrivateSizes;
117 unsigned totalClientSize;
119 void
120 ResetClientPrivates(void)
122 clientPrivateCount = 0;
123 clientPrivateLen = 0;
124 xfree(clientPrivateSizes);
125 clientPrivateSizes = (unsigned *)NULL;
126 totalClientSize =
127 ((sizeof(ClientRec) + sizeof(long) - 1) / sizeof(long)) * sizeof(long);
130 _X_EXPORT int
131 AllocateClientPrivateIndex(void)
133 return clientPrivateCount++;
136 _X_EXPORT Bool
137 AllocateClientPrivate(int index2, unsigned amount)
139 unsigned oldamount;
141 /* Round up sizes for proper alignment */
142 amount = ((amount + (sizeof(long) - 1)) / sizeof(long)) * sizeof(long);
144 if (index2 >= clientPrivateLen)
146 unsigned *nsizes;
147 nsizes = (unsigned *)xrealloc(clientPrivateSizes,
148 (index2 + 1) * sizeof(unsigned));
149 if (!nsizes)
150 return FALSE;
151 while (clientPrivateLen <= index2)
153 nsizes[clientPrivateLen++] = 0;
154 totalClientSize += sizeof(DevUnion);
156 clientPrivateSizes = nsizes;
158 oldamount = clientPrivateSizes[index2];
159 if (amount > oldamount)
161 clientPrivateSizes[index2] = amount;
162 totalClientSize += (amount - oldamount);
164 return TRUE;
168 * screen private machinery
171 int screenPrivateCount;
173 void
174 ResetScreenPrivates(void)
176 screenPrivateCount = 0;
179 /* this can be called after some screens have been created,
180 * so we have to worry about resizing existing devPrivates
182 _X_EXPORT int
183 AllocateScreenPrivateIndex(void)
185 int idx;
186 int i;
187 ScreenPtr pScreen;
188 DevUnion *nprivs;
190 idx = screenPrivateCount++;
191 for (i = 0; i < screenInfo.numScreens; i++)
193 pScreen = screenInfo.screens[i];
194 nprivs = (DevUnion *)xrealloc(pScreen->devPrivates,
195 screenPrivateCount * sizeof(DevUnion));
196 if (!nprivs)
198 screenPrivateCount--;
199 return -1;
201 /* Zero the new private */
202 bzero(&nprivs[idx], sizeof(DevUnion));
203 pScreen->devPrivates = nprivs;
205 return idx;
210 * window private machinery
213 static int windowPrivateCount;
215 void
216 ResetWindowPrivates(void)
218 windowPrivateCount = 0;
221 _X_EXPORT int
222 AllocateWindowPrivateIndex(void)
224 return windowPrivateCount++;
227 _X_EXPORT Bool
228 AllocateWindowPrivate(ScreenPtr pScreen, int index2, unsigned amount)
230 unsigned oldamount;
232 /* Round up sizes for proper alignment */
233 amount = ((amount + (sizeof(long) - 1)) / sizeof(long)) * sizeof(long);
235 if (index2 >= pScreen->WindowPrivateLen)
237 unsigned *nsizes;
238 nsizes = (unsigned *)xrealloc(pScreen->WindowPrivateSizes,
239 (index2 + 1) * sizeof(unsigned));
240 if (!nsizes)
241 return FALSE;
242 while (pScreen->WindowPrivateLen <= index2)
244 nsizes[pScreen->WindowPrivateLen++] = 0;
245 pScreen->totalWindowSize += sizeof(DevUnion);
247 pScreen->WindowPrivateSizes = nsizes;
249 oldamount = pScreen->WindowPrivateSizes[index2];
250 if (amount > oldamount)
252 pScreen->WindowPrivateSizes[index2] = amount;
253 pScreen->totalWindowSize += (amount - oldamount);
255 return TRUE;
260 * gc private machinery
263 static int gcPrivateCount;
265 void
266 ResetGCPrivates(void)
268 gcPrivateCount = 0;
271 _X_EXPORT int
272 AllocateGCPrivateIndex(void)
274 return gcPrivateCount++;
277 _X_EXPORT Bool
278 AllocateGCPrivate(ScreenPtr pScreen, int index2, unsigned amount)
280 unsigned oldamount;
282 /* Round up sizes for proper alignment */
283 amount = ((amount + (sizeof(long) - 1)) / sizeof(long)) * sizeof(long);
285 if (index2 >= pScreen->GCPrivateLen)
287 unsigned *nsizes;
288 nsizes = (unsigned *)xrealloc(pScreen->GCPrivateSizes,
289 (index2 + 1) * sizeof(unsigned));
290 if (!nsizes)
291 return FALSE;
292 while (pScreen->GCPrivateLen <= index2)
294 nsizes[pScreen->GCPrivateLen++] = 0;
295 pScreen->totalGCSize += sizeof(DevUnion);
297 pScreen->GCPrivateSizes = nsizes;
299 oldamount = pScreen->GCPrivateSizes[index2];
300 if (amount > oldamount)
302 pScreen->GCPrivateSizes[index2] = amount;
303 pScreen->totalGCSize += (amount - oldamount);
305 return TRUE;
310 * pixmap private machinery
312 static int pixmapPrivateCount;
314 void
315 ResetPixmapPrivates(void)
317 pixmapPrivateCount = 0;
320 _X_EXPORT int
321 AllocatePixmapPrivateIndex(void)
323 return pixmapPrivateCount++;
326 _X_EXPORT Bool
327 AllocatePixmapPrivate(ScreenPtr pScreen, int index2, unsigned amount)
329 unsigned oldamount;
331 /* Round up sizes for proper alignment */
332 amount = ((amount + (sizeof(long) - 1)) / sizeof(long)) * sizeof(long);
334 if (index2 >= pScreen->PixmapPrivateLen)
336 unsigned *nsizes;
337 nsizes = (unsigned *)xrealloc(pScreen->PixmapPrivateSizes,
338 (index2 + 1) * sizeof(unsigned));
339 if (!nsizes)
340 return FALSE;
341 while (pScreen->PixmapPrivateLen <= index2)
343 nsizes[pScreen->PixmapPrivateLen++] = 0;
344 pScreen->totalPixmapSize += sizeof(DevUnion);
346 pScreen->PixmapPrivateSizes = nsizes;
348 oldamount = pScreen->PixmapPrivateSizes[index2];
349 if (amount > oldamount)
351 pScreen->PixmapPrivateSizes[index2] = amount;
352 pScreen->totalPixmapSize += (amount - oldamount);
354 pScreen->totalPixmapSize = BitmapBytePad(pScreen->totalPixmapSize * 8);
355 return TRUE;
360 * colormap private machinery
363 int colormapPrivateCount;
365 void
366 ResetColormapPrivates(void)
368 colormapPrivateCount = 0;
372 _X_EXPORT int
373 AllocateColormapPrivateIndex (InitCmapPrivFunc initPrivFunc)
375 int index;
376 int i;
377 ColormapPtr pColormap;
378 DevUnion *privs;
380 index = colormapPrivateCount++;
382 for (i = 0; i < screenInfo.numScreens; i++)
385 * AllocateColormapPrivateIndex may be called after the
386 * default colormap has been created on each screen!
388 * We must resize the devPrivates array for the default
389 * colormap on each screen, making room for this new private.
390 * We also call the initialization function 'initPrivFunc' on
391 * the new private allocated for each default colormap.
394 ScreenPtr pScreen = screenInfo.screens[i];
396 pColormap = (ColormapPtr) LookupIDByType (
397 pScreen->defColormap, RT_COLORMAP);
399 if (pColormap)
401 privs = (DevUnion *) xrealloc (pColormap->devPrivates,
402 colormapPrivateCount * sizeof(DevUnion));
403 if (!privs) {
404 colormapPrivateCount--;
405 return -1;
407 bzero(&privs[index], sizeof(DevUnion));
408 pColormap->devPrivates = privs;
409 if (!(*initPrivFunc)(pColormap,index))
411 colormapPrivateCount--;
412 return -1;
417 return index;
421 * device private machinery
424 static int devicePrivateIndex = 0;
426 _X_EXPORT int
427 AllocateDevicePrivateIndex(void)
429 return devicePrivateIndex++;
432 _X_EXPORT Bool
433 AllocateDevicePrivate(DeviceIntPtr device, int index)
435 if (device->nPrivates < ++index) {
436 DevUnion *nprivs = (DevUnion *) xrealloc(device->devPrivates,
437 index * sizeof(DevUnion));
438 if (!nprivs)
439 return FALSE;
440 device->devPrivates = nprivs;
441 bzero(&nprivs[device->nPrivates], sizeof(DevUnion)
442 * (index - device->nPrivates));
443 device->nPrivates = index;
444 return TRUE;
445 } else {
446 return TRUE;
450 void
451 ResetDevicePrivateIndex(void)
453 devicePrivateIndex = 0;