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