summaryrefslogtreecommitdiff
path: root/Xprint/pcl/PclInit.c
blob: b70af4fd7fb5c587028b567b609cef5923cdfcbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/* $Xorg: PclInit.c,v 1.3 2000/08/17 19:48:08 cpqbld Exp $ */
/*******************************************************************
**
**    *********************************************************
**    *
**    *  File:		PclInit.c
**    *
**    *  Contents:
**    *                 Initialization code of Pcl driver for the print server.
**    *
**    *  Created:	1/30/95
**    *
**    *********************************************************
** 
********************************************************************/
/*
(c) Copyright 1996 Hewlett-Packard Company
(c) Copyright 1996 International Business Machines Corp.
(c) Copyright 1996 Sun Microsystems, Inc.
(c) Copyright 1996 Novell, Inc.
(c) Copyright 1996 Digital Equipment Corp.
(c) Copyright 1996 Fujitsu Limited
(c) Copyright 1996 Hitachi, Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the names of the copyright holders shall
not be used in advertising or otherwise to promote the sale, use or other
dealings in this Software without prior written authorization from said
copyright holders.
*/

/*
(c) Copyright 1996 Hewlett-Packard Company
(c) Copyright 1996 International Business Machines Corp.
(c) Copyright 1996 Sun Microsystems, Inc.
(c) Copyright 1996 Novell, Inc.
(c) Copyright 1996 Digital Equipment Corp.
(c) Copyright 1996 Fujitsu Limited
(c) Copyright 1996 Hitachi, Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the names of the copyright holders shall
not be used in advertising or otherwise to promote the sale, use or other
dealings in this Software without prior written authorization from said
copyright holders.
*/
/* $XFree86$ */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

#include "Pcl.h"

#include "cfb.h"
#include "Xos.h"	/* for unlink() */

#include "attributes.h"
#include "DiPrint.h"

#define MODELDIRNAME "/models"

static void AllocatePclPrivates(ScreenPtr pScreen);
static int PclInitContext(XpContextPtr pCon);
static Bool PclDestroyContext(XpContextPtr pCon);

int PclScreenPrivateIndex;
int PclContextPrivateIndex;
int PclPixmapPrivateIndex;
int PclWindowPrivateIndex;
int PclGCPrivateIndex;

#ifdef XP_PCL_COLOR
/*
 * The supported visuals on this screen
 */
static VisualRec Visuals[] = 
{
    { 1, StaticGray, 1, 2, 1, 0, 0, 0, 0, 0, 0 },
    { 2, PseudoColor, 8, 256, 8, 0, 0, 0, 0, 0, 0 },
    { 3, TrueColor, 8, 256, 24, 0xFF0000, 0xFF00, 0xFF, 16, 8, 0 }
};

/*
 * The supported depths on this screen
 */
static DepthRec Depths[] = 
{
    { 1, 1, NULL },
    { 8, 1, NULL },
    { 24, 1, NULL }
};
#else
/*
 * The supported visuals on this screen
 */
static VisualRec Visuals[] = 
{
    { 1, StaticGray, 1, 2, 1, 0, 0, 0, 0, 0, 0}
};

/*
 * The supported depths on this screen
 */
static DepthRec Depths[] = 
{
    { 1, 1, NULL }
};
#endif /* XP_PCL_COLOR */


#define NUM_VISUALS(visuals) (sizeof(visuals) / sizeof(VisualRec))
#define NUM_DEPTHS(depths) (sizeof(depths) / sizeof(DepthRec))

Bool
PclCloseScreen(int index,
	       ScreenPtr pScreen)
{
    PclScreenPrivPtr pPriv = pScreen->devPrivates[PclScreenPrivateIndex].ptr;

    pScreen->CloseScreen = pPriv->CloseScreen;
    xfree( pPriv );
    
    return (*pScreen->CloseScreen)(index, pScreen);
}

Bool
InitializePclDriver(
     int ndx,
     ScreenPtr pScreen,
     int argc,
     char **argv)
{
    int maxRes, xRes, yRes, maxDim;
    unsigned i;
    PclScreenPrivPtr pPriv;
    
    /*
     * Register this driver's InitContext function with the print
     * extension.  This is a bit sleazy, as the extension hasn't yet
     * been initialized, but the extensionneeds to know this, and this
     * seems the best time to provide the information.
     */
#ifdef XP_PCL_COLOR
    XpRegisterInitFunc( pScreen, "XP-PCL-COLOR", PclInitContext );
#elif XP_PCL_MONO
    XpRegisterInitFunc( pScreen, "XP-PCL-MONO", PclInitContext );
#else
    XpRegisterInitFunc( pScreen, "XP-PCL-LJ3", PclInitContext );
#endif /* XP_PCL_MONO */
    
    /*
     * Create and fill in the devPrivate for the PCL driver.
     */
    AllocatePclPrivates(pScreen);
   
    pPriv =
      (PclScreenPrivPtr)pScreen->devPrivates[PclScreenPrivateIndex].ptr;

    maxDim = MAX( pScreen->height, pScreen->width );
    xRes = pScreen->width / ( pScreen->mmWidth / 25.4 );
    yRes = pScreen->height / ( pScreen->mmHeight / 25.4 );
    maxRes = MAX( xRes, yRes );

#ifdef XP_PCL_COLOR
    cfbScreenInit( pScreen, NULL, maxDim, maxDim, maxRes, maxRes,
		  maxRes );
    /*
     * Clean up the fields that we stomp (code taken from cfbCloseScreen)
     */
    for( i = 0; (int) i < pScreen->numDepths; i++ )
      xfree( pScreen->allowedDepths[i].vids );
    xfree( pScreen->allowedDepths );
    xfree( pScreen->visuals );
#else
    mfbScreenInit( pScreen, NULL, maxDim, maxDim, maxRes, maxRes,
		  maxRes );
#endif /* XP_PCL_COLOR */

    miInitializeBackingStore ( pScreen );

    pScreen->defColormap = FakeClientID(0);
    pScreen->blackPixel = 1;
    pScreen->whitePixel = 0;

    pPriv->CloseScreen = pScreen->CloseScreen;
    pScreen->CloseScreen = PclCloseScreen;
    
    pScreen->QueryBestSize = (QueryBestSizeProcPtr)PclQueryBestSize;
    pScreen->SaveScreen = (SaveScreenProcPtr)_XpBoolNoop;
    pScreen->GetImage = (GetImageProcPtr)_XpVoidNoop;
    pScreen->GetSpans = (GetSpansProcPtr)_XpVoidNoop;
    pScreen->CreateWindow = PclCreateWindow;
    pScreen->DestroyWindow = PclDestroyWindow;
/*
    pScreen->PositionWindow = PclPositionWindow;
*/
    pScreen->ChangeWindowAttributes = PclChangeWindowAttributes;
/*
    pScreen->RealizeWindow = PclMapWindow;
    pScreen->UnrealizeWindow = PclUnmapWindow;
*/
    pScreen->PaintWindowBackground = PclPaintWindow;
    pScreen->PaintWindowBorder = PclPaintWindow;
    pScreen->CopyWindow = PclCopyWindow; /* XXX Hard routine to write! */

    pScreen->CreatePixmap = PclCreatePixmap;
    pScreen->DestroyPixmap = PclDestroyPixmap;
    pScreen->RealizeFont = PclRealizeFont;
    pScreen->UnrealizeFont = PclUnrealizeFont;
    pScreen->CreateGC = PclCreateGC;

    pScreen->CreateColormap = PclCreateColormap;
    pScreen->DestroyColormap = PclDestroyColormap;
    pScreen->InstallColormap = (InstallColormapProcPtr)NoopDDA;
    pScreen->UninstallColormap = (UninstallColormapProcPtr)NoopDDA;
    pScreen->ListInstalledColormaps = PclListInstalledColormaps;
    pScreen->StoreColors = PclStoreColors;
/*
    pScreen->ResolveColor = PclResolveColor;
*/

    pScreen->BitmapToRegion = mfbPixmapToRegion;

    pScreen->ConstrainCursor = PclConstrainCursor;
    pScreen->CursorLimits = PclCursorLimits;
    pScreen->DisplayCursor = PclDisplayCursor;
    pScreen->RealizeCursor = PclRealizeCursor;
    pScreen->UnrealizeCursor = PclUnrealizeCursor;
    pScreen->RecolorCursor = PclRecolorCursor;
    pScreen->SetCursorPosition = PclSetCursorPosition;

    pScreen->visuals = Visuals;
    pScreen->numVisuals = NUM_VISUALS( Visuals );
    pScreen->allowedDepths = Depths;
    pScreen->numDepths = NUM_DEPTHS( Depths );

    for( i = 0; i < NUM_DEPTHS( Depths ); i++ )
      {
	  pScreen->allowedDepths[i].vids =
	    (VisualID *)xalloc( sizeof(VisualID ) );
	  pScreen->allowedDepths[i].vids[0] = i + 1;
      }
    
#ifdef XP_PCL_COLOR
    pScreen->rootVisual = 2;
    pScreen->rootDepth = 8;
#else
    pScreen->rootVisual = 1;
    pScreen->rootDepth = 1;
#endif /* XP_PCL_COLOR */

    pPriv->colormaps = NULL;
    PclCreateDefColormap( pScreen );
    
    return TRUE;
}

static void
AllocatePclPrivates(ScreenPtr pScreen)
{
    static unsigned long PclGeneration = 0;

    if((unsigned long) PclGeneration != serverGeneration)
    {
        PclScreenPrivateIndex = AllocateScreenPrivateIndex();

	PclWindowPrivateIndex = AllocateWindowPrivateIndex();
	AllocateWindowPrivate( pScreen, PclWindowPrivateIndex,
			       sizeof( PclWindowPrivRec ) );

	PclContextPrivateIndex = XpAllocateContextPrivateIndex();
	XpAllocateContextPrivate( PclContextPrivateIndex, 
				  sizeof( PclContextPrivRec ) );

	PclGCPrivateIndex = AllocateGCPrivateIndex();
	AllocateGCPrivate( pScreen, PclGCPrivateIndex, 
			   sizeof( PclGCPrivRec ) );

	PclPixmapPrivateIndex = AllocatePixmapPrivateIndex();
	AllocatePixmapPrivate( pScreen, PclPixmapPrivateIndex, 
			       sizeof( PclPixmapPrivRec ) );
	
        PclGeneration = serverGeneration;
    }

    pScreen->devPrivates[PclScreenPrivateIndex].ptr = (pointer)xalloc(
                sizeof(PclScreenPrivRec));
}

/*
 * PclInitContext
 *
 * Establish the appropriate values for a PrintContext used with the PCL
 * driver.
 */

static char DOC_ATT_SUPP[]="document-attributes-supported";
static char DOC_ATT_VAL[]="document-format";
static char JOB_ATT_SUPP[]="job-attributes-supported";
static char JOB_ATT_VAL[]="";
static char PAGE_ATT_SUPP[]="xp-page-attributes-supported";
static char PAGE_ATT_VAL[]="content-orientation default-printer-resolution \
default-input-tray default-medium plex";

static int
PclInitContext(XpContextPtr pCon)
{
    XpDriverFuncsPtr pFuncs;
    PclContextPrivPtr pConPriv;
    char *server, *attrStr;
    char *modelID;
    char *configDir;
    char *pathName;
    int i, j;
    float width, height;
    XpOidMediumDiscreteSizeList* ds_list;
    XpOidArea* repro;
    XpOid page_size;
    XpOidMediumSS* m;
    
    /*
     * Initialize the attribute store for this printer.
     */
    XpInitAttributes( pCon );

    /*
     * Initialize the function pointers
     */
    pFuncs = &( pCon->funcs );
    pFuncs->StartJob = PclStartJob;
    pFuncs->EndJob = PclEndJob;
    pFuncs->StartDoc = PclStartDoc;
    pFuncs->EndDoc = PclEndDoc;
    pFuncs->StartPage = PclStartPage;
    pFuncs->EndPage = PclEndPage;
    pFuncs->PutDocumentData = PclDocumentData;
    pFuncs->GetDocumentData = PclGetDocumentData;
    pFuncs->GetAttributes = PclGetAttributes;
    pFuncs->SetAttributes = PclSetAttributes;
    pFuncs->AugmentAttributes = PclAugmentAttributes;
    pFuncs->GetOneAttribute = PclGetOneAttribute;
    pFuncs->DestroyContext = PclDestroyContext;
    pFuncs->GetMediumDimensions = PclGetMediumDimensions;
    pFuncs->GetReproducibleArea = PclGetReproducibleArea;
    

    /*
     * Set up the context privates
     */
    pConPriv =
      (PclContextPrivPtr)pCon->devPrivates[PclContextPrivateIndex].ptr;
    
    pConPriv->jobFileName = (char *)NULL;
    pConPriv->pageFileName = (char *)NULL;
    pConPriv->pJobFile = (FILE *)NULL;
    pConPriv->pPageFile = (FILE *)NULL;
    pConPriv->dash = NULL;
    pConPriv->validGC = 0;

    pConPriv->getDocClient = (ClientPtr)NULL;
    pConPriv->getDocBufSize = 0;
    modelID = XpGetOneAttribute(pCon, XPPrinterAttr, "xp-model-identifier");
    if ( (configDir = XpGetConfigDir(False)) != (char *) NULL ) {
	pathName = (char *)xalloc(strlen(configDir) + strlen(MODELDIRNAME) +
				strlen(modelID) + strlen("color.map") + 4);
	if (pathName) {
	    sprintf(pathName, "%s/%s/%s/%s", configDir, MODELDIRNAME, modelID,
				"color.map");
	    pConPriv->ctbl = PclReadMap(pathName, &pConPriv->ctbldim);
	    xfree(pathName);

	} else
	    pConPriv->ctbl = NULL;
    } else
	pConPriv->ctbl = NULL;

#ifdef XP_PCL_LJ3
    /*
     * Initialize the spooling buffer for saving the figures temporary
     * (LaserJet IIIs printers don't support the macro function which
     *  includes some HP-GL/2 commands.)
     */
    pConPriv->fcount = 0;
    if ( !(pConPriv->figures = (char *)xalloc(1024)) )
	pConPriv->fcount_max = 0;
    else
	pConPriv->fcount_max = 1024;
#endif /* XP_PCL_LJ3 */
    
    /*
     * document-attributes-supported
     */
    server = XpGetOneAttribute( pCon, XPServerAttr, DOC_ATT_SUPP );
    if( ( attrStr = (char *)xalloc(strlen(server) + strlen(DOC_ATT_SUPP)
				   + strlen(DOC_ATT_VAL) +
				   strlen(PAGE_ATT_VAL) + 6 ) ) 
       == (char *)NULL )
      return BadAlloc;
    sprintf( attrStr, "*%s:\t%s %s %s", DOC_ATT_SUPP, server,
	    DOC_ATT_VAL, PAGE_ATT_VAL );
    XpAugmentAttributes( pCon, XPPrinterAttr, attrStr );
    xfree( attrStr );
    
    /*
     * job-attributes-supported
     */
    server = XpGetOneAttribute( pCon, XPServerAttr, JOB_ATT_SUPP );
    if( ( attrStr = (char *)xalloc(strlen(server) + strlen(JOB_ATT_SUPP)
				   + strlen(JOB_ATT_VAL) + 4 ) ) 
       == (char *)NULL )
      return BadAlloc;
    sprintf( attrStr, "*%s:\t%s %s", JOB_ATT_SUPP, server, JOB_ATT_VAL );
    XpAugmentAttributes( pCon, XPPrinterAttr, attrStr );
    xfree( attrStr );
    
    /*
     * xp-page-attributes-supported
     */
    server = XpGetOneAttribute( pCon, XPServerAttr, PAGE_ATT_SUPP );
    if( ( attrStr = (char *)xalloc(strlen(server) + strlen(PAGE_ATT_SUPP)
				   + strlen(PAGE_ATT_VAL) + 4 ) ) 
       == (char *)NULL )
      return BadAlloc;
    sprintf( attrStr, "*%s:\t%s %s", PAGE_ATT_SUPP, server, PAGE_ATT_VAL );
    XpAugmentAttributes( pCon, XPPrinterAttr, attrStr );
    xfree( attrStr );

    /*
     * Validate the attribute pools
     */
    XpValidateAttributePool( pCon, XPPrinterAttr, &PclValidatePoolsRec );

    /*
     * Munge the reproducible areas to reflect the fact that PCL will not let
     * me move the right or left margins closer than .25" to the edge of the
     * paper.
     */
    m = XpGetMediumSSAttr( pCon, XPPrinterAttr,
			  xpoid_att_medium_source_sizes_supported, 
			  (const XpOidList*) NULL,
			  (const XpOidList*) NULL );
    for( i = 0; i < XpOidMediumSSCount( m ); i++ )
      {
	  if( XpOidMediumSS_DISCRETE == (m->mss)[i].mstag )
	    {
		ds_list = (m->mss)[i].ms.discrete;
		for( j = 0; j < ds_list->count; j++ )
		  {
		      repro = &(ds_list->list)[j].assured_reproduction_area;
		      page_size = (ds_list->list)[j].page_size;
		      XpGetMediumMillimeters( page_size, &width, &height );
		      
		      if( repro->minimum_x < 6.35 )
			repro->minimum_x = 6.35;
		      if( width - repro->maximum_x < 6.35 )
			repro->maximum_x = width - 6.35;
		  }
	    }
      }
    XpPutMediumSSAttr( pCon, XPPrinterAttr,
		      xpoid_att_medium_source_sizes_supported, m );
    XpOidMediumSSDelete( m );

    /*
     * Finish validating the attribute pools
     */

    XpValidateAttributePool( pCon, XPDocAttr, &PclValidatePoolsRec );
    XpValidateAttributePool( pCon, XPJobAttr, &PclValidatePoolsRec );
    XpValidateAttributePool( pCon, XPPageAttr, &PclValidatePoolsRec );

    /*
     * Clear out the colormap storage
     */
    pConPriv->palettes = NULL;
    
    return Success;
}

static Bool
PclDestroyContext(XpContextPtr pCon)
{
    PclContextPrivPtr pConPriv = (PclContextPrivPtr)
      pCon->devPrivates[PclContextPrivateIndex].ptr;
    PclPaletteMapPtr p, t;
    PclCmapToContexts *pCmap;
    ScreenPtr screen;
    PclScreenPrivPtr sPriv;
    PclContextListPtr con, prevCon, temp;
    
    
    /*
     * Clean up the temporary files
     */
    if( pConPriv->pPageFile != (FILE *)NULL )
      {
	  fclose( pConPriv->pPageFile );
	  pConPriv->pPageFile = (FILE *)NULL;
      }
    if( pConPriv->pageFileName != (char *)NULL )
      {
	  unlink( pConPriv->pageFileName );
	  xfree( pConPriv->pageFileName );
	  pConPriv->pageFileName = (char *)NULL;
      }
    
    if( pConPriv->pJobFile != (FILE *)NULL )
      {
	  fclose( pConPriv->pJobFile );
	  pConPriv->pJobFile = NULL;
      }
    if( pConPriv->jobFileName != (char *)NULL )
      {
	  unlink( pConPriv->jobFileName );
	  xfree( pConPriv->jobFileName );
	  pConPriv->jobFileName = (char *)NULL;
      }

    xfree( pConPriv->dash );
    xfree(pConPriv->ctbl);
    pConPriv->ctbl = NULL;
#ifdef XP_PCL_LJ3
    xfree( pConPriv->figures );
#endif /* XP_PCL_LJ3 */

    /*
     * Destroy the colormap<->palette mappings
     */
    p = pConPriv->palettes;
    while( p )
      {
	  t = p;
	  p = p->next;
	  xfree( t );
      }
    pConPriv->palettes = NULL;

    /*
     * Remove the context from the screen-level colormap<->contexts mappings
     */
    screen = screenInfo.screens[pCon->screenNum];
    sPriv = (PclScreenPrivPtr)screen->devPrivates[PclScreenPrivateIndex].ptr;
    pCmap = sPriv->colormaps;
    while( pCmap )
      {
	  con = pCmap->contexts;
	  prevCon = NULL;
	  
	  while( con )
	    {
		if( con->context->contextID == pCon->contextID )
		  {
		      if( prevCon )
			{
			    temp = con;
			    prevCon->next = con = con->next;
			}
		      else
			{
			    temp = pCmap->contexts;
			    pCmap->contexts = con = con->next;
			}
		      xfree( temp );
		  }
		else
		  con = con->next;
	    }

	  pCmap = pCmap->next;
      }
    
    XpDestroyAttributes(pCon);

    return Success;
}

XpContextPtr
PclGetContextFromWindow(WindowPtr win)
{
    PclWindowPrivPtr pPriv;
    
    while( win )
      {
	  pPriv =
	    (PclWindowPrivPtr)win->devPrivates[PclWindowPrivateIndex].ptr;
	  if( pPriv->validContext )
	    return pPriv->context;
      
	  win = win->parent;
      }
    
    return NULL;
}