privates.c revision 05b261ec
1/*
2
3Copyright 1993, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included
12in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall
23not be used in advertising or otherwise to promote the sale, use or
24other dealings in this Software without prior written authorization
25from The Open Group.
26
27*/
28
29#ifdef HAVE_DIX_CONFIG_H
30#include <dix-config.h>
31#endif
32
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"
46
47/*
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.
51 */
52
53/*
54 *  extension private machinery
55 */
56
57static int  extensionPrivateCount;
58int extensionPrivateLen;
59unsigned *extensionPrivateSizes;
60unsigned totalExtensionSize;
61
62void
63ResetExtensionPrivates(void)
64{
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);
71}
72
73_X_EXPORT int
74AllocateExtensionPrivateIndex(void)
75{
76    return extensionPrivateCount++;
77}
78
79_X_EXPORT Bool
80AllocateExtensionPrivate(int index2, unsigned amount)
81{
82    unsigned oldamount;
83
84    /* Round up sizes for proper alignment */
85    amount = ((amount + (sizeof(long) - 1)) / sizeof(long)) * sizeof(long);
86
87    if (index2 >= extensionPrivateLen)
88    {
89	unsigned *nsizes;
90	nsizes = (unsigned *)xrealloc(extensionPrivateSizes,
91				      (index2 + 1) * sizeof(unsigned));
92	if (!nsizes)
93	    return FALSE;
94	while (extensionPrivateLen <= index2)
95	{
96	    nsizes[extensionPrivateLen++] = 0;
97	    totalExtensionSize += sizeof(DevUnion);
98	}
99	extensionPrivateSizes = nsizes;
100    }
101    oldamount = extensionPrivateSizes[index2];
102    if (amount > oldamount)
103    {
104	extensionPrivateSizes[index2] = amount;
105	totalExtensionSize += (amount - oldamount);
106    }
107    return TRUE;
108}
109
110/*
111 *  client private machinery
112 */
113
114static int  clientPrivateCount;
115int clientPrivateLen;
116unsigned *clientPrivateSizes;
117unsigned totalClientSize;
118
119void
120ResetClientPrivates(void)
121{
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);
128}
129
130_X_EXPORT int
131AllocateClientPrivateIndex(void)
132{
133    return clientPrivateCount++;
134}
135
136_X_EXPORT Bool
137AllocateClientPrivate(int index2, unsigned amount)
138{
139    unsigned oldamount;
140
141    /* Round up sizes for proper alignment */
142    amount = ((amount + (sizeof(long) - 1)) / sizeof(long)) * sizeof(long);
143
144    if (index2 >= clientPrivateLen)
145    {
146	unsigned *nsizes;
147	nsizes = (unsigned *)xrealloc(clientPrivateSizes,
148				      (index2 + 1) * sizeof(unsigned));
149	if (!nsizes)
150	    return FALSE;
151	while (clientPrivateLen <= index2)
152	{
153	    nsizes[clientPrivateLen++] = 0;
154	    totalClientSize += sizeof(DevUnion);
155	}
156	clientPrivateSizes = nsizes;
157    }
158    oldamount = clientPrivateSizes[index2];
159    if (amount > oldamount)
160    {
161	clientPrivateSizes[index2] = amount;
162	totalClientSize += (amount - oldamount);
163    }
164    return TRUE;
165}
166
167/*
168 *  screen private machinery
169 */
170
171int  screenPrivateCount;
172
173void
174ResetScreenPrivates(void)
175{
176    screenPrivateCount = 0;
177}
178
179/* this can be called after some screens have been created,
180 * so we have to worry about resizing existing devPrivates
181 */
182_X_EXPORT int
183AllocateScreenPrivateIndex(void)
184{
185    int		idx;
186    int		i;
187    ScreenPtr	pScreen;
188    DevUnion	*nprivs;
189
190    idx = screenPrivateCount++;
191    for (i = 0; i < screenInfo.numScreens; i++)
192    {
193	pScreen = screenInfo.screens[i];
194	nprivs = (DevUnion *)xrealloc(pScreen->devPrivates,
195				      screenPrivateCount * sizeof(DevUnion));
196	if (!nprivs)
197	{
198	    screenPrivateCount--;
199	    return -1;
200	}
201	/* Zero the new private */
202	bzero(&nprivs[idx], sizeof(DevUnion));
203	pScreen->devPrivates = nprivs;
204    }
205    return idx;
206}
207
208
209/*
210 *  window private machinery
211 */
212
213static int  windowPrivateCount;
214
215void
216ResetWindowPrivates(void)
217{
218    windowPrivateCount = 0;
219}
220
221_X_EXPORT int
222AllocateWindowPrivateIndex(void)
223{
224    return windowPrivateCount++;
225}
226
227_X_EXPORT Bool
228AllocateWindowPrivate(ScreenPtr pScreen, int index2, unsigned amount)
229{
230    unsigned oldamount;
231
232    /* Round up sizes for proper alignment */
233    amount = ((amount + (sizeof(long) - 1)) / sizeof(long)) * sizeof(long);
234
235    if (index2 >= pScreen->WindowPrivateLen)
236    {
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)
243	{
244	    nsizes[pScreen->WindowPrivateLen++] = 0;
245	    pScreen->totalWindowSize += sizeof(DevUnion);
246	}
247	pScreen->WindowPrivateSizes = nsizes;
248    }
249    oldamount = pScreen->WindowPrivateSizes[index2];
250    if (amount > oldamount)
251    {
252	pScreen->WindowPrivateSizes[index2] = amount;
253	pScreen->totalWindowSize += (amount - oldamount);
254    }
255    return TRUE;
256}
257
258
259/*
260 *  gc private machinery
261 */
262
263static int  gcPrivateCount;
264
265void
266ResetGCPrivates(void)
267{
268    gcPrivateCount = 0;
269}
270
271_X_EXPORT int
272AllocateGCPrivateIndex(void)
273{
274    return gcPrivateCount++;
275}
276
277_X_EXPORT Bool
278AllocateGCPrivate(ScreenPtr pScreen, int index2, unsigned amount)
279{
280    unsigned oldamount;
281
282    /* Round up sizes for proper alignment */
283    amount = ((amount + (sizeof(long) - 1)) / sizeof(long)) * sizeof(long);
284
285    if (index2 >= pScreen->GCPrivateLen)
286    {
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)
293	{
294	    nsizes[pScreen->GCPrivateLen++] = 0;
295	    pScreen->totalGCSize += sizeof(DevUnion);
296	}
297	pScreen->GCPrivateSizes = nsizes;
298    }
299    oldamount = pScreen->GCPrivateSizes[index2];
300    if (amount > oldamount)
301    {
302	pScreen->GCPrivateSizes[index2] = amount;
303	pScreen->totalGCSize += (amount - oldamount);
304    }
305    return TRUE;
306}
307
308
309/*
310 *  pixmap private machinery
311 */
312static int  pixmapPrivateCount;
313
314void
315ResetPixmapPrivates(void)
316{
317    pixmapPrivateCount = 0;
318}
319
320_X_EXPORT int
321AllocatePixmapPrivateIndex(void)
322{
323    return pixmapPrivateCount++;
324}
325
326_X_EXPORT Bool
327AllocatePixmapPrivate(ScreenPtr pScreen, int index2, unsigned amount)
328{
329    unsigned oldamount;
330
331    /* Round up sizes for proper alignment */
332    amount = ((amount + (sizeof(long) - 1)) / sizeof(long)) * sizeof(long);
333
334    if (index2 >= pScreen->PixmapPrivateLen)
335    {
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)
342	{
343	    nsizes[pScreen->PixmapPrivateLen++] = 0;
344	    pScreen->totalPixmapSize += sizeof(DevUnion);
345	}
346	pScreen->PixmapPrivateSizes = nsizes;
347    }
348    oldamount = pScreen->PixmapPrivateSizes[index2];
349    if (amount > oldamount)
350    {
351	pScreen->PixmapPrivateSizes[index2] = amount;
352	pScreen->totalPixmapSize += (amount - oldamount);
353    }
354    pScreen->totalPixmapSize = BitmapBytePad(pScreen->totalPixmapSize * 8);
355    return TRUE;
356}
357
358
359/*
360 *  colormap private machinery
361 */
362
363int  colormapPrivateCount;
364
365void
366ResetColormapPrivates(void)
367{
368    colormapPrivateCount = 0;
369}
370
371
372_X_EXPORT int
373AllocateColormapPrivateIndex (InitCmapPrivFunc initPrivFunc)
374{
375    int		index;
376    int		i;
377    ColormapPtr	pColormap;
378    DevUnion	*privs;
379
380    index = colormapPrivateCount++;
381
382    for (i = 0; i < screenInfo.numScreens; i++)
383    {
384	/*
385	 * AllocateColormapPrivateIndex may be called after the
386	 * default colormap has been created on each screen!
387	 *
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.
392	 */
393
394	ScreenPtr pScreen = screenInfo.screens[i];
395
396	pColormap = (ColormapPtr) LookupIDByType (
397	    pScreen->defColormap, RT_COLORMAP);
398
399	if (pColormap)
400	{
401	    privs = (DevUnion *) xrealloc (pColormap->devPrivates,
402		colormapPrivateCount * sizeof(DevUnion));
403	    if (!privs) {
404		colormapPrivateCount--;
405		return -1;
406	    }
407	    bzero(&privs[index], sizeof(DevUnion));
408	    pColormap->devPrivates = privs;
409	    if (!(*initPrivFunc)(pColormap,index))
410	    {
411		colormapPrivateCount--;
412		return -1;
413	    }
414	}
415    }
416
417    return index;
418}
419
420/*
421 *  device private machinery
422 */
423
424static int devicePrivateIndex = 0;
425
426_X_EXPORT int
427AllocateDevicePrivateIndex(void)
428{
429    return devicePrivateIndex++;
430}
431
432_X_EXPORT Bool
433AllocateDevicePrivate(DeviceIntPtr device, int index)
434{
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;
447    }
448}
449
450void
451ResetDevicePrivateIndex(void)
452{
453    devicePrivateIndex = 0;
454}
455