1 /* $XConsortium: privates.c /main/5 1996/06/17 10:56:22 mor $ */
2 /* $XFree86: xc/programs/Xserver/dix/privates.c,v 3.2 1997/01/23 10:57:19 dawes Exp $ */
3 /*
4 
5 Copyright (c) 1993  X Consortium
6 
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14 
15 The above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 OTHER DEALINGS IN THE SOFTWARE.
25 
26 Except as contained in this notice, the name of the X Consortium shall
27 not be used in advertising or otherwise to promote the sale, use or
28 other dealings in this Software without prior written authorization
29 from the X Consortium.
30 
31 */
32 
33 #include "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 
45 /*
46  *  See the Wrappers and devPrivates section in "Definition of the
47  *  Porting Layer for the X v11 Sample Server" (doc/Server/ddx.tbl.ms)
48  *  for information on how to use devPrivates.
49  */
50 
51 /*
52  *  client private machinery
53  */
54 
55 static int  clientPrivateCount;
56 int clientPrivateLen;
57 unsigned *clientPrivateSizes;
58 unsigned totalClientSize;
59 
60 void
ResetClientPrivates()61 ResetClientPrivates()
62 {
63     clientPrivateCount = 0;
64     clientPrivateLen = 0;
65     xfree(clientPrivateSizes);
66     clientPrivateSizes = (unsigned *)NULL;
67     totalClientSize = sizeof(ClientRec);
68 
69 }
70 
71 int
AllocateClientPrivateIndex()72 AllocateClientPrivateIndex()
73 {
74     return clientPrivateCount++;
75 }
76 
77 Bool
AllocateClientPrivate(index2,amount)78 AllocateClientPrivate(index2, amount)
79     int index2;
80     unsigned amount;
81 {
82     unsigned oldamount;
83 
84     if (index2 >= clientPrivateLen)
85     {
86 	unsigned *nsizes;
87 	nsizes = (unsigned *)xrealloc(clientPrivateSizes,
88 				      (index2 + 1) * sizeof(unsigned));
89 	if (!nsizes)
90 	    return FALSE;
91 	while (clientPrivateLen <= index2)
92 	{
93 	    nsizes[clientPrivateLen++] = 0;
94 	    totalClientSize += sizeof(DevUnion);
95 	}
96 	clientPrivateSizes = nsizes;
97     }
98     oldamount = clientPrivateSizes[index2];
99     if (amount > oldamount)
100     {
101 	clientPrivateSizes[index2] = amount;
102 	totalClientSize += (amount - oldamount);
103     }
104     return TRUE;
105 }
106 
107 /*
108  *  screen private machinery
109  */
110 
111 int  screenPrivateCount;
112 
113 void
ResetScreenPrivates()114 ResetScreenPrivates()
115 {
116     screenPrivateCount = 0;
117 }
118 
119 /* this can be called after some screens have been created,
120  * so we have to worry about resizing existing devPrivates
121  */
122 int
AllocateScreenPrivateIndex()123 AllocateScreenPrivateIndex()
124 {
125     int		index2;
126     int		i;
127     ScreenPtr	pScreen;
128     DevUnion	*nprivs;
129 
130     index2 = screenPrivateCount++;
131     for (i = 0; i < screenInfo.numScreens; i++)
132     {
133 	pScreen = screenInfo.screens[i];
134 	nprivs = (DevUnion *)xrealloc(pScreen->devPrivates,
135 				      screenPrivateCount * sizeof(DevUnion));
136 	if (!nprivs)
137 	{
138 	    screenPrivateCount--;
139 	    return -1;
140 	}
141 	pScreen->devPrivates = nprivs;
142     }
143     return index2;
144 }
145 
146 
147 /*
148  *  window private machinery
149  */
150 
151 static int  windowPrivateCount;
152 
153 void
ResetWindowPrivates()154 ResetWindowPrivates()
155 {
156     windowPrivateCount = 0;
157 }
158 
159 int
AllocateWindowPrivateIndex()160 AllocateWindowPrivateIndex()
161 {
162     return windowPrivateCount++;
163 }
164 
165 Bool
AllocateWindowPrivate(pScreen,index2,amount)166 AllocateWindowPrivate(pScreen, index2, amount)
167     register ScreenPtr pScreen;
168     int index2;
169     unsigned amount;
170 {
171     unsigned oldamount;
172 
173     if (index2 >= pScreen->WindowPrivateLen)
174     {
175 	unsigned *nsizes;
176 	nsizes = (unsigned *)xrealloc(pScreen->WindowPrivateSizes,
177 				      (index2 + 1) * sizeof(unsigned));
178 	if (!nsizes)
179 	    return FALSE;
180 	while (pScreen->WindowPrivateLen <= index2)
181 	{
182 	    nsizes[pScreen->WindowPrivateLen++] = 0;
183 	    pScreen->totalWindowSize += sizeof(DevUnion);
184 	}
185 	pScreen->WindowPrivateSizes = nsizes;
186     }
187     oldamount = pScreen->WindowPrivateSizes[index2];
188     if (amount > oldamount)
189     {
190 	pScreen->WindowPrivateSizes[index2] = amount;
191 	pScreen->totalWindowSize += (amount - oldamount);
192     }
193     return TRUE;
194 }
195 
196 
197 /*
198  *  gc private machinery
199  */
200 
201 static int  gcPrivateCount;
202 
203 void
ResetGCPrivates()204 ResetGCPrivates()
205 {
206     gcPrivateCount = 0;
207 }
208 
209 int
AllocateGCPrivateIndex()210 AllocateGCPrivateIndex()
211 {
212     return gcPrivateCount++;
213 }
214 
215 Bool
AllocateGCPrivate(pScreen,index2,amount)216 AllocateGCPrivate(pScreen, index2, amount)
217     register ScreenPtr pScreen;
218     int index2;
219     unsigned amount;
220 {
221     unsigned oldamount;
222 
223     if (index2 >= pScreen->GCPrivateLen)
224     {
225 	unsigned *nsizes;
226 	nsizes = (unsigned *)xrealloc(pScreen->GCPrivateSizes,
227 				      (index2 + 1) * sizeof(unsigned));
228 	if (!nsizes)
229 	    return FALSE;
230 	while (pScreen->GCPrivateLen <= index2)
231 	{
232 	    nsizes[pScreen->GCPrivateLen++] = 0;
233 	    pScreen->totalGCSize += sizeof(DevUnion);
234 	}
235 	pScreen->GCPrivateSizes = nsizes;
236     }
237     oldamount = pScreen->GCPrivateSizes[index2];
238     if (amount > oldamount)
239     {
240 	pScreen->GCPrivateSizes[index2] = amount;
241 	pScreen->totalGCSize += (amount - oldamount);
242     }
243     return TRUE;
244 }
245 
246 
247 /*
248  *  pixmap private machinery
249  */
250 #ifdef PIXPRIV
251 static int  pixmapPrivateCount;
252 
253 void
ResetPixmapPrivates()254 ResetPixmapPrivates()
255 {
256     pixmapPrivateCount = 0;
257 }
258 
259 int
AllocatePixmapPrivateIndex()260 AllocatePixmapPrivateIndex()
261 {
262     return pixmapPrivateCount++;
263 }
264 
265 Bool
AllocatePixmapPrivate(pScreen,index2,amount)266 AllocatePixmapPrivate(pScreen, index2, amount)
267     register ScreenPtr pScreen;
268     int index2;
269     unsigned amount;
270 {
271     unsigned oldamount;
272 
273     if (index2 >= pScreen->PixmapPrivateLen)
274     {
275 	unsigned *nsizes;
276 	nsizes = (unsigned *)xrealloc(pScreen->PixmapPrivateSizes,
277 				      (index2 + 1) * sizeof(unsigned));
278 	if (!nsizes)
279 	    return FALSE;
280 	while (pScreen->PixmapPrivateLen <= index2)
281 	{
282 	    nsizes[pScreen->PixmapPrivateLen++] = 0;
283 	    pScreen->totalPixmapSize += sizeof(DevUnion);
284 	}
285 	pScreen->PixmapPrivateSizes = nsizes;
286     }
287     oldamount = pScreen->PixmapPrivateSizes[index2];
288     if (amount > oldamount)
289     {
290 	pScreen->PixmapPrivateSizes[index2] = amount;
291 	pScreen->totalPixmapSize += (amount - oldamount);
292     }
293     pScreen->totalPixmapSize = BitmapBytePad(pScreen->totalPixmapSize * 8);
294     return TRUE;
295 }
296 #endif
297 
298 
299 /*
300  *  colormap private machinery
301  */
302 
303 int  colormapPrivateCount;
304 
305 void
ResetColormapPrivates()306 ResetColormapPrivates()
307 {
308     colormapPrivateCount = 0;
309 }
310 
311 
312 int
AllocateColormapPrivateIndex(initPrivFunc)313 AllocateColormapPrivateIndex (initPrivFunc)
314 
315 InitCmapPrivFunc initPrivFunc;
316 
317 {
318     int		index;
319     int		i;
320     ColormapPtr	pColormap;
321     DevUnion	*privs;
322 
323     index = colormapPrivateCount++;
324 
325     for (i = 0; i < screenInfo.numScreens; i++)
326     {
327 	/*
328 	 * AllocateColormapPrivateIndex may be called after the
329 	 * default colormap has been created on each screen!
330 	 *
331 	 * We must resize the devPrivates array for the default
332 	 * colormap on each screen, making room for this new private.
333 	 * We also call the initialization function 'initPrivFunc' on
334 	 * the new private allocated for each default colormap.
335 	 */
336 
337 	ScreenPtr pScreen = screenInfo.screens[i];
338 
339 	pColormap = (ColormapPtr) LookupIDByType (
340 	    pScreen->defColormap, RT_COLORMAP);
341 
342 	privs = (DevUnion *) xrealloc (pColormap->devPrivates,
343 	    colormapPrivateCount * sizeof(DevUnion));
344 
345 	pColormap->devPrivates = privs;
346 
347 	if (!privs || !(*initPrivFunc)(pColormap))
348 	{
349 	    colormapPrivateCount--;
350 	    return -1;
351 	}
352     }
353 
354     return index;
355 }
356