GCManager.c revision 444c061a
1444c061aSmrg/* $Xorg: GCManager.c,v 1.5 2001/02/09 02:03:54 xorgcvs Exp $ */
2444c061aSmrg
3444c061aSmrg/***********************************************************
4444c061aSmrgCopyright 1987, 1988, 1990 by Digital Equipment Corporation, Maynard, Massachusetts
5444c061aSmrgCopyright 1993 by Sun Microsystems, Inc. Mountain View, CA.
6444c061aSmrg
7444c061aSmrg                        All Rights Reserved
8444c061aSmrg
9444c061aSmrgPermission to use, copy, modify, and distribute this software and its
10444c061aSmrgdocumentation for any purpose and without fee is hereby granted,
11444c061aSmrgprovided that the above copyright notice appear in all copies and that
12444c061aSmrgboth that copyright notice and this permission notice appear in
13444c061aSmrgsupporting documentation, and that the names of Digital or Sun not be
14444c061aSmrgused in advertising or publicity pertaining to distribution of the
15444c061aSmrgsoftware without specific, written prior permission.
16444c061aSmrg
17444c061aSmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
18444c061aSmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
19444c061aSmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
20444c061aSmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21444c061aSmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22444c061aSmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23444c061aSmrgSOFTWARE.
24444c061aSmrg
25444c061aSmrgSUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
26444c061aSmrgINCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
27444c061aSmrgNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
28444c061aSmrgABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
29444c061aSmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
30444c061aSmrgPROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
31444c061aSmrgOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
32444c061aSmrgTHE USE OR PERFORMANCE OF THIS SOFTWARE.
33444c061aSmrg
34444c061aSmrg******************************************************************/
35444c061aSmrg
36444c061aSmrg/*
37444c061aSmrg
38444c061aSmrgCopyright 1987, 1988, 1990, 1994, 1998  The Open Group
39444c061aSmrg
40444c061aSmrgPermission to use, copy, modify, distribute, and sell this software and its
41444c061aSmrgdocumentation for any purpose is hereby granted without fee, provided that
42444c061aSmrgthe above copyright notice appear in all copies and that both that
43444c061aSmrgcopyright notice and this permission notice appear in supporting
44444c061aSmrgdocumentation.
45444c061aSmrg
46444c061aSmrgThe above copyright notice and this permission notice shall be included in
47444c061aSmrgall copies or substantial portions of the Software.
48444c061aSmrg
49444c061aSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50444c061aSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51444c061aSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
52444c061aSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
53444c061aSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
54444c061aSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
55444c061aSmrg
56444c061aSmrgExcept as contained in this notice, the name of The Open Group shall not be
57444c061aSmrgused in advertising or otherwise to promote the sale, use or other dealings
58444c061aSmrgin this Software without prior written authorization from The Open Group.
59444c061aSmrg
60444c061aSmrg*/
61444c061aSmrg/* $XFree86: xc/lib/Xt/GCManager.c,v 1.5 2001/08/22 22:52:18 dawes Exp $ */
62444c061aSmrg
63444c061aSmrg#ifdef HAVE_CONFIG_H
64444c061aSmrg#include <config.h>
65444c061aSmrg#endif
66444c061aSmrg#include "IntrinsicI.h"
67444c061aSmrg
68444c061aSmrg
69444c061aSmrgtypedef struct _GCrec {
70444c061aSmrg    unsigned char screen;	/* Screen for GC */
71444c061aSmrg    unsigned char depth;	/* Depth for GC */
72444c061aSmrg    char	  dashes;	/* Dashes value */
73444c061aSmrg    Pixmap	  clip_mask;	/* Clip_mask value */
74444c061aSmrg    Cardinal	  ref_count;    /* # of shareholders */
75444c061aSmrg    GC		  gc;		/* The GC itself. */
76444c061aSmrg    XtGCMask	  dynamic_mask;	/* Writable values */
77444c061aSmrg    XtGCMask	  unused_mask;	/* Unused values */
78444c061aSmrg    struct _GCrec *next;	/* Next GC for this widgetkind. */
79444c061aSmrg} GCrec, *GCptr;
80444c061aSmrg
81444c061aSmrg#define GCVAL(bit,mask,val,default) ((bit&mask) ? val : default)
82444c061aSmrg
83444c061aSmrg#define CHECK(bit,comp,default) \
84444c061aSmrg    if ((checkMask & bit) && \
85444c061aSmrg	(GCVAL(bit,valueMask,v->comp,default) != gcv.comp)) return False
86444c061aSmrg
87444c061aSmrg#define ALLGCVALS (GCFunction | GCPlaneMask | GCForeground | \
88444c061aSmrg		   GCBackground | GCLineWidth | GCLineStyle | \
89444c061aSmrg		   GCCapStyle | GCJoinStyle | GCFillStyle | \
90444c061aSmrg		   GCFillRule | GCTile | GCStipple | \
91444c061aSmrg		   GCTileStipXOrigin | GCTileStipYOrigin | \
92444c061aSmrg		   GCFont | GCSubwindowMode | GCGraphicsExposures | \
93444c061aSmrg		   GCClipXOrigin | GCClipYOrigin | GCDashOffset | \
94444c061aSmrg		   GCArcMode)
95444c061aSmrg
96444c061aSmrgstatic Bool Matches(
97444c061aSmrg    Display *dpy,
98444c061aSmrg    GCptr ptr,
99444c061aSmrg    register XtGCMask valueMask,
100444c061aSmrg    register XGCValues *v,
101444c061aSmrg    XtGCMask readOnlyMask,
102444c061aSmrg    XtGCMask dynamicMask)
103444c061aSmrg{
104444c061aSmrg    XGCValues gcv;
105444c061aSmrg    register XtGCMask checkMask;
106444c061aSmrg
107444c061aSmrg    if (readOnlyMask & ptr->dynamic_mask)
108444c061aSmrg	return False;
109444c061aSmrg    if (((ptr->dynamic_mask|ptr->unused_mask) & dynamicMask) != dynamicMask)
110444c061aSmrg	return False;
111444c061aSmrg    if (!XGetGCValues(dpy, ptr->gc, ALLGCVALS, &gcv))
112444c061aSmrg	return False;
113444c061aSmrg    checkMask = readOnlyMask & ~ptr->unused_mask;
114444c061aSmrg    CHECK(GCForeground, foreground, 0);
115444c061aSmrg    CHECK(GCBackground, background, 1);
116444c061aSmrg    CHECK(GCFont, font, ~0UL);
117444c061aSmrg    CHECK(GCFillStyle, fill_style, FillSolid);
118444c061aSmrg    CHECK(GCLineWidth, line_width, 0);
119444c061aSmrg    CHECK(GCFunction, function, GXcopy);
120444c061aSmrg    CHECK(GCGraphicsExposures, graphics_exposures, True);
121444c061aSmrg    CHECK(GCTile, tile, ~0UL);
122444c061aSmrg    CHECK(GCSubwindowMode, subwindow_mode, ClipByChildren);
123444c061aSmrg    CHECK(GCPlaneMask, plane_mask, AllPlanes);
124444c061aSmrg    CHECK(GCLineStyle, line_style, LineSolid);
125444c061aSmrg    CHECK(GCCapStyle, cap_style, CapButt);
126444c061aSmrg    CHECK(GCJoinStyle, join_style, JoinMiter);
127444c061aSmrg    CHECK(GCFillRule, fill_rule, EvenOddRule);
128444c061aSmrg    CHECK(GCArcMode, arc_mode, ArcPieSlice);
129444c061aSmrg    CHECK(GCStipple, stipple, ~0UL);
130444c061aSmrg    CHECK(GCTileStipXOrigin, ts_x_origin, 0);
131444c061aSmrg    CHECK(GCTileStipYOrigin, ts_y_origin, 0);
132444c061aSmrg    CHECK(GCClipXOrigin, clip_x_origin, 0);
133444c061aSmrg    CHECK(GCClipYOrigin, clip_y_origin, 0);
134444c061aSmrg    CHECK(GCDashOffset, dash_offset, 0);
135444c061aSmrg    gcv.clip_mask = ptr->clip_mask;
136444c061aSmrg    CHECK(GCClipMask, clip_mask, None);
137444c061aSmrg    gcv.dashes = ptr->dashes;
138444c061aSmrg    CHECK(GCDashList, dashes, 4);
139444c061aSmrg    valueMask &= ptr->unused_mask | dynamicMask;
140444c061aSmrg    if (valueMask) {
141444c061aSmrg	XChangeGC(dpy, ptr->gc, valueMask, v);
142444c061aSmrg	if (valueMask & GCDashList)
143444c061aSmrg	    ptr->dashes = v->dashes;
144444c061aSmrg	if (valueMask & GCClipMask)
145444c061aSmrg	    ptr->clip_mask = v->clip_mask;
146444c061aSmrg    }
147444c061aSmrg    ptr->unused_mask &= ~(dynamicMask | readOnlyMask);
148444c061aSmrg    ptr->dynamic_mask |= dynamicMask;
149444c061aSmrg    return True;
150444c061aSmrg} /* Matches */
151444c061aSmrg
152444c061aSmrg/* Called by CloseDisplay to free the per-display GC list */
153444c061aSmrgvoid _XtGClistFree(
154444c061aSmrg    Display *dpy,
155444c061aSmrg    register XtPerDisplay pd)
156444c061aSmrg{
157444c061aSmrg    register GCptr GClist, next;
158444c061aSmrg    register int i;
159444c061aSmrg
160444c061aSmrg    GClist = pd->GClist;
161444c061aSmrg    while (GClist) {
162444c061aSmrg	next = GClist->next;
163444c061aSmrg	XtFree((char*)GClist);
164444c061aSmrg	GClist = next;
165444c061aSmrg    }
166444c061aSmrg    if (pd->pixmap_tab) {
167444c061aSmrg	for (i = ScreenCount(dpy); --i >= 0; ) {
168444c061aSmrg	    if (pd->pixmap_tab[i])
169444c061aSmrg		XtFree((char *)pd->pixmap_tab[i]);
170444c061aSmrg	}
171444c061aSmrg	XtFree((char *)pd->pixmap_tab);
172444c061aSmrg    }
173444c061aSmrg}
174444c061aSmrg
175444c061aSmrg
176444c061aSmrg/*
177444c061aSmrg * Return a GC with the given values and characteristics.
178444c061aSmrg */
179444c061aSmrg
180444c061aSmrgGC XtAllocateGC(
181444c061aSmrg    register Widget widget,
182444c061aSmrg    Cardinal	    depth,
183444c061aSmrg    XtGCMask        valueMask,
184444c061aSmrg    XGCValues       *values,
185444c061aSmrg    XtGCMask        dynamicMask,
186444c061aSmrg    XtGCMask        unusedMask)
187444c061aSmrg{
188444c061aSmrg    register GCptr *prev;
189444c061aSmrg    register GCptr cur;
190444c061aSmrg    Screen *screen;
191444c061aSmrg    register Display *dpy;
192444c061aSmrg    register XtPerDisplay pd;
193444c061aSmrg    Drawable drawable;
194444c061aSmrg    Drawable *pixmaps;
195444c061aSmrg    XtGCMask readOnlyMask;
196444c061aSmrg    GC retval;
197444c061aSmrg    WIDGET_TO_APPCON(widget);
198444c061aSmrg
199444c061aSmrg    LOCK_APP(app);
200444c061aSmrg    LOCK_PROCESS;
201444c061aSmrg    if (!XtIsWidget(widget))
202444c061aSmrg	widget = _XtWindowedAncestor(widget);
203444c061aSmrg    if (!depth)
204444c061aSmrg	depth = widget->core.depth;
205444c061aSmrg    screen = XtScreen(widget);
206444c061aSmrg    dpy = DisplayOfScreen(screen);
207444c061aSmrg    pd = _XtGetPerDisplay(dpy);
208444c061aSmrg    unusedMask &= ~valueMask;
209444c061aSmrg    readOnlyMask = ~(dynamicMask | unusedMask);
210444c061aSmrg
211444c061aSmrg    /* Search for existing GC that matches exactly */
212444c061aSmrg    for (prev = &pd->GClist; (cur = *prev); prev = &cur->next) {
213444c061aSmrg	if (cur->depth == depth &&
214444c061aSmrg	    ScreenOfDisplay(dpy, cur->screen) == screen &&
215444c061aSmrg	    Matches(dpy, cur, valueMask, values, readOnlyMask, dynamicMask)) {
216444c061aSmrg            cur->ref_count++;
217444c061aSmrg	    /* Move this GC to front of list */
218444c061aSmrg	    *prev = cur->next;
219444c061aSmrg	    cur->next = pd->GClist;
220444c061aSmrg	    pd->GClist = cur;
221444c061aSmrg	    retval = cur->gc;
222444c061aSmrg	    UNLOCK_PROCESS;
223444c061aSmrg	    UNLOCK_APP(app);
224444c061aSmrg	    return retval;
225444c061aSmrg	}
226444c061aSmrg    }
227444c061aSmrg
228444c061aSmrg    /* No matches, have to create a new one */
229444c061aSmrg    cur = XtNew(GCrec);
230444c061aSmrg    cur->screen = XScreenNumberOfScreen(screen);
231444c061aSmrg    cur->depth = depth;
232444c061aSmrg    cur->ref_count = 1;
233444c061aSmrg    cur->dynamic_mask = dynamicMask;
234444c061aSmrg    cur->unused_mask = (unusedMask & ~dynamicMask);
235444c061aSmrg    cur->dashes = GCVAL(GCDashList, valueMask, values->dashes, 4);
236444c061aSmrg    cur->clip_mask = GCVAL(GCClipMask, valueMask, values->clip_mask, None);
237444c061aSmrg    drawable = 0;
238444c061aSmrg    if (depth == widget->core.depth)
239444c061aSmrg	drawable = XtWindow(widget);
240444c061aSmrg    if (!drawable && depth == (Cardinal) DefaultDepthOfScreen(screen))
241444c061aSmrg	drawable = RootWindowOfScreen(screen);
242444c061aSmrg    if (!drawable) {
243444c061aSmrg	if (!pd->pixmap_tab) {
244444c061aSmrg	    int n;
245444c061aSmrg	    pd->pixmap_tab = (Drawable **)__XtMalloc((unsigned)ScreenCount(dpy) *
246444c061aSmrg						   sizeof(Drawable *));
247444c061aSmrg	    for (n = 0; n < ScreenCount(dpy); n++)
248444c061aSmrg		pd->pixmap_tab[n] = NULL;
249444c061aSmrg	}
250444c061aSmrg	pixmaps = pd->pixmap_tab[cur->screen];
251444c061aSmrg	if (!pixmaps) {
252444c061aSmrg	    int max, n, *depths;
253444c061aSmrg	    depths = XListDepths(dpy, cur->screen, &n);
254444c061aSmrg	    n--;
255444c061aSmrg	    max = depths[n];
256444c061aSmrg	    while (n--) {
257444c061aSmrg		if (depths[n] > max)
258444c061aSmrg		    max = depths[n];
259444c061aSmrg	    }
260444c061aSmrg	    XFree((char *)depths);
261444c061aSmrg	    pixmaps = (Drawable *)__XtCalloc((unsigned)max, sizeof(Drawable));
262444c061aSmrg	    pd->pixmap_tab[cur->screen] = pixmaps;
263444c061aSmrg	}
264444c061aSmrg	drawable = pixmaps[cur->depth - 1];
265444c061aSmrg	if (!drawable) {
266444c061aSmrg	    drawable = XCreatePixmap(dpy, RootWindowOfScreen(screen), 1, 1,
267444c061aSmrg				     cur->depth);
268444c061aSmrg	    pixmaps[cur->depth - 1] = drawable;
269444c061aSmrg	}
270444c061aSmrg    }
271444c061aSmrg    cur->gc = XCreateGC(dpy, drawable, valueMask, values);
272444c061aSmrg    cur->next = pd->GClist;
273444c061aSmrg    pd->GClist = cur;
274444c061aSmrg    retval = cur->gc;
275444c061aSmrg    UNLOCK_PROCESS;
276444c061aSmrg    UNLOCK_APP(app);
277444c061aSmrg    return retval;
278444c061aSmrg} /* XtAllocateGC */
279444c061aSmrg
280444c061aSmrg/*
281444c061aSmrg * Return a read-only GC with the given values.
282444c061aSmrg */
283444c061aSmrg
284444c061aSmrgGC XtGetGC(
285444c061aSmrg    register Widget widget,
286444c061aSmrg    XtGCMask        valueMask,
287444c061aSmrg    XGCValues       *values)
288444c061aSmrg{
289444c061aSmrg    return XtAllocateGC(widget, 0, valueMask, values, 0, 0);
290444c061aSmrg} /* XtGetGC */
291444c061aSmrg
292444c061aSmrgvoid  XtReleaseGC(
293444c061aSmrg    Widget      widget,
294444c061aSmrg    register GC gc)
295444c061aSmrg{
296444c061aSmrg    register GCptr cur, *prev;
297444c061aSmrg    Display* dpy;
298444c061aSmrg    XtPerDisplay pd;
299444c061aSmrg    WIDGET_TO_APPCON(widget);
300444c061aSmrg
301444c061aSmrg    LOCK_APP(app);
302444c061aSmrg    LOCK_PROCESS;
303444c061aSmrg    dpy = XtDisplayOfObject(widget);
304444c061aSmrg    pd = _XtGetPerDisplay(dpy);
305444c061aSmrg
306444c061aSmrg    for (prev = &pd->GClist; (cur = *prev); prev = &cur->next) {
307444c061aSmrg	if (cur->gc == gc) {
308444c061aSmrg	    if (--(cur->ref_count) == 0) {
309444c061aSmrg		*prev = cur->next;
310444c061aSmrg		XFreeGC(dpy, gc);
311444c061aSmrg		XtFree((char *) cur);
312444c061aSmrg	    }
313444c061aSmrg	    break;
314444c061aSmrg	}
315444c061aSmrg    }
316444c061aSmrg    UNLOCK_PROCESS;
317444c061aSmrg    UNLOCK_APP(app);
318444c061aSmrg} /* XtReleaseGC */
319444c061aSmrg
320444c061aSmrg/*  The following interface is broken and supplied only for backwards
321444c061aSmrg *  compatibility.  It will work properly in all cases only if there
322444c061aSmrg *  is exactly 1 Display created by the application.
323444c061aSmrg */
324444c061aSmrg
325444c061aSmrgvoid XtDestroyGC(register GC gc)
326444c061aSmrg{
327444c061aSmrg    GCptr cur, *prev;
328444c061aSmrg    XtAppContext app;
329444c061aSmrg
330444c061aSmrg    LOCK_PROCESS;
331444c061aSmrg    app = _XtGetProcessContext()->appContextList;
332444c061aSmrg    /* This is awful; we have to search through all the lists
333444c061aSmrg       to find the GC. */
334444c061aSmrg    for (; app; app = app->next) {
335444c061aSmrg	int i;
336444c061aSmrg	for (i = app->count; i ;) {
337444c061aSmrg	    Display *dpy = app->list[--i];
338444c061aSmrg	    XtPerDisplay pd = _XtGetPerDisplay(dpy);
339444c061aSmrg	    for (prev = &pd->GClist; (cur = *prev); prev = &cur->next) {
340444c061aSmrg		if (cur->gc == gc) {
341444c061aSmrg		    if (--(cur->ref_count) == 0) {
342444c061aSmrg			*prev = cur->next;
343444c061aSmrg			XFreeGC(dpy, gc);
344444c061aSmrg			XtFree((char *) cur);
345444c061aSmrg		    }
346444c061aSmrg		    UNLOCK_PROCESS;
347444c061aSmrg		    return;
348444c061aSmrg		}
349444c061aSmrg	    }
350444c061aSmrg	}
351444c061aSmrg    }
352444c061aSmrg    UNLOCK_PROCESS;
353444c061aSmrg} /* XtDestroyGC */
354