summaryrefslogtreecommitdiff
path: root/Xprint/ps/PsInit.c
blob: a11a61ae47be3db2baa1f669ca85e4964668e5de (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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/* $Xorg: PsInit.c,v 1.4 2001/02/09 02:04:36 xorgcvs Exp $ */
/*

Copyright 1996, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

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
OPEN GROUP 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 name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.

*/
/*
 * (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.
 */

/*******************************************************************
**
**    *********************************************************
**    *
**    *  File:		PsInit.c
**    *
**    *  Contents:      Initialization code of Ps driver for the print server.
**    *
**    *  Created By:	Roger Helmendach (Liberty Systems)
**    *
**    *  Copyright:	Copyright 1996 The Open Group, Inc.
**    *
**    *********************************************************
** 
********************************************************************/

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

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

#include "Ps.h"
#include "mi.h"
#include "micmap.h"
#include "AttrValid.h"
#include "mfb.h"

#include "windowstr.h"
#include "DiPrint.h"

static void AllocatePsPrivates(ScreenPtr pScreen);
static int PsInitContext(XpContextPtr pCon);
static int PsDestroyContext(XpContextPtr pCon);

int PsScreenPrivateIndex;
int PsContextPrivateIndex;
int PsPixmapPrivateIndex;
int PsWindowPrivateIndex;

#ifdef GLXEXT
extern void GlxWrapInitVisuals(miInitVisualsProcPtr *);
#endif /* GLXEXT */

Bool
InitializePsDriver(ndx, pScreen, argc, argv)
  int         ndx;
  ScreenPtr   pScreen;
  int         argc;
  char      **argv;
{
#if 0
  int               maxXres, maxYres, maxWidth, maxHeight;
  int               maxRes, maxDim, numBytes;
  PsScreenPrivPtr   pPriv;
#endif
  char            **printerNames;
  int               numPrinters;
  int               nv,       /* total number of visuals */
                    nv_1bit,  /* number of 8bit visuals */
                    nv_8bit,  /* number of 8bit visuals */
                    nv_12bit, /* number of 12bit visuals */
                    nv_14bit, /* number of 14bit visuals */
                    nv_16bit, /* number of 16bit visuals */
                    nv_24bit, /* number of 24bit visuals*/
                    nv_30bit; /* number of 30bit visuals*/
  int               nd;       /* number of depths */
  int               defaultVisualIndex = -1;
  VisualID         *vids_1bit,
                   *vids_8bit,
                   *vids_12bit,
                   *vids_14bit,
                   *vids_16bit,
                   *vids_24bit,
                   *vids_30bit;
  VisualPtr         visuals;
  DepthPtr          depths;
  VisualID          defaultVisual;
  int               rootDepth;

/*
 * Register this driver's InitContext function with the print
 * extension.
 */
  XpRegisterInitFunc(pScreen, "XP-POSTSCRIPT", PsInitContext);

/*
 * Create and fill in the devPrivate for the PS driver.
 */
  AllocatePsPrivates(pScreen);

#if 0
  pPriv = (PsScreenPrivPtr)pScreen->devPrivates[PsScreenPrivateIndex].ptr;
  pPriv->resDB = rmdb;
#endif

  pScreen->defColormap            = (Colormap) FakeClientID(0);
  pScreen->blackPixel             = 1;
  pScreen->whitePixel             = 0;
  pScreen->QueryBestSize          = (QueryBestSizeProcPtr)PsQueryBestSize;
  pScreen->SaveScreen             = (SaveScreenProcPtr)_XpBoolNoop;
  pScreen->GetImage               = (GetImageProcPtr)_XpVoidNoop;
  pScreen->GetSpans               = (GetSpansProcPtr)_XpVoidNoop;
  pScreen->CreateWindow           = PsCreateWindow;
  pScreen->DestroyWindow          = PsDestroyWindow;
  pScreen->PositionWindow         = PsPositionWindow;
  pScreen->ChangeWindowAttributes = PsChangeWindowAttributes;
  pScreen->RealizeWindow          = PsMapWindow;
  pScreen->UnrealizeWindow        = PsUnmapWindow;
  pScreen->PaintWindowBackground  = PsPaintWindow;
  pScreen->PaintWindowBorder      = PsPaintWindow;
  pScreen->CloseScreen            = PsCloseScreen;
  pScreen->CopyWindow             = PsCopyWindow;
       /* XXX Hard routine to write! */

/*
 * These two are going to be VERY different...
 */
  pScreen->CreatePixmap           = PsCreatePixmap;
  pScreen->DestroyPixmap          = PsDestroyPixmap;
  pScreen->RealizeFont            = PsRealizeFont;
  pScreen->UnrealizeFont          = PsUnrealizeFont;
  pScreen->CreateGC               = PsCreateGC;
  pScreen->CreateColormap         = PsCreateColormap;
  pScreen->DestroyColormap        = PsDestroyColormap;
  pScreen->InstallColormap        = PsInstallColormap;
  pScreen->UninstallColormap      = PsUninstallColormap;
  pScreen->ListInstalledColormaps = PsListInstalledColormaps;
  pScreen->StoreColors            = PsStoreColors;
  pScreen->ResolveColor           = PsResolveColor;
    /* Will BitmapToRegion make any difference at all? */
  pScreen->BitmapToRegion         = mfbPixmapToRegion;

  visuals    = (VisualPtr) xalloc(16*sizeof(VisualRec));
  depths     = (DepthPtr)  xalloc(16*sizeof(DepthRec));
  vids_1bit  = (VisualID *)xalloc(16*sizeof(VisualID));
  vids_8bit  = (VisualID *)xalloc(16*sizeof(VisualID));
  vids_12bit = (VisualID *)xalloc(16*sizeof(VisualID));
  vids_14bit = (VisualID *)xalloc(16*sizeof(VisualID));
  vids_16bit = (VisualID *)xalloc(16*sizeof(VisualID));
  vids_24bit = (VisualID *)xalloc(16*sizeof(VisualID));
  vids_30bit = (VisualID *)xalloc(16*sizeof(VisualID));

  nv = nv_1bit = nv_8bit = nv_12bit = nv_14bit = nv_16bit = nv_24bit = nv_30bit = nd = 0;

#ifdef PSOUT_USE_DEEPCOLOR
/* gisburn: 30bit TrueColor has been disabled for now since it causes problems
 * with GLX - see https://bugs.freedesktop.org/show_bug.cgi?id=2868 ("Mesa
 * seems to be unable to handle 30bit TrueColor visuals") for details... 
 */
#ifdef DISABLED_FOR_NOW
  /* TrueColor, 30bit, 10bit per R-,G-,B-gun */
  visuals[nv].vid             = FakeClientID(0);
  visuals[nv].class           = TrueColor;
  visuals[nv].bitsPerRGBValue = 10;
  visuals[nv].ColormapEntries = 1024;
  visuals[nv].nplanes         = 30;
  visuals[nv].redMask         = 0X3FF00000;
  visuals[nv].greenMask       = 0X000FFC00;
  visuals[nv].blueMask        = 0X000003FF;
  visuals[nv].offsetRed       = 20;
  visuals[nv].offsetGreen     = 10;
  visuals[nv].offsetBlue      = 0;
  vids_30bit[nv_30bit] = visuals[nv].vid;
  nv++; nv_30bit++;
#endif /* DISABLED_FOR_NOW */
#endif /* PSOUT_USE_DEEPCOLOR */

  /* TrueColor, 24bit */
  visuals[nv].vid             = FakeClientID(0);
  visuals[nv].class           = TrueColor;
  visuals[nv].bitsPerRGBValue = 8;
  visuals[nv].ColormapEntries = 256;
  visuals[nv].nplanes         = 24;
  visuals[nv].redMask         = 0X00FF0000;
  visuals[nv].greenMask       = 0X0000FF00;
  visuals[nv].blueMask        = 0X000000FF;
  visuals[nv].offsetRed       = 16;
  visuals[nv].offsetGreen     = 8;
  visuals[nv].offsetBlue      = 0;
  vids_24bit[nv_24bit] = visuals[nv].vid;
  nv++; nv_24bit++;

  /* TrueColor, 16bit */
  visuals[nv].vid             = FakeClientID(0);
  visuals[nv].class           = TrueColor;
  visuals[nv].bitsPerRGBValue = 6;
  visuals[nv].ColormapEntries = 64;
  visuals[nv].nplanes         = 16;
  visuals[nv].redMask         = 0x0000f800;
  visuals[nv].greenMask       = 0x000007e0;
  visuals[nv].blueMask        = 0x0000001f;
  visuals[nv].offsetRed       = 11;
  visuals[nv].offsetGreen     = 5;
  visuals[nv].offsetBlue      = 0;
  vids_16bit[nv_16bit] = visuals[nv].vid;
  nv++; nv_16bit++;
  
#ifdef PSOUT_USE_DEEPCOLOR
  /* PostScript Level 2 and above, colors can have 12 bits per component
   * (36 bit for RGB) */

  /* PseudoColor, 14bit (15bit won't work as |ColormapEntries==32768|
   * is too large for a |signed short|... xx@@!!!... ;-( ) */
  visuals[nv].vid             = FakeClientID(0);
  visuals[nv].class           = PseudoColor;
  visuals[nv].bitsPerRGBValue = 12;
  visuals[nv].ColormapEntries = 16384;
  visuals[nv].nplanes         = 14;
  visuals[nv].redMask         = 0x0;
  visuals[nv].greenMask       = 0x0;
  visuals[nv].blueMask        = 0x0;
  visuals[nv].offsetRed       = 0x0;
  visuals[nv].offsetGreen     = 0x0;
  visuals[nv].offsetBlue      = 0x0;
  vids_14bit[nv_14bit] = visuals[nv].vid;
  nv++; nv_14bit++;

  /* PseudoColor, 12bit */
  visuals[nv].vid             = FakeClientID(0);
  visuals[nv].class           = PseudoColor;
  visuals[nv].bitsPerRGBValue = 12;
  visuals[nv].ColormapEntries = 4096;
  visuals[nv].nplanes         = 12;
  visuals[nv].redMask         = 0x0;
  visuals[nv].greenMask       = 0x0;
  visuals[nv].blueMask        = 0x0;
  visuals[nv].offsetRed       = 0x0;
  visuals[nv].offsetGreen     = 0x0;
  visuals[nv].offsetBlue      = 0x0;
  vids_12bit[nv_12bit] = visuals[nv].vid;
  defaultVisualIndex = nv;
  nv++; nv_12bit++;

  /* GrayScale, 12bit, 12bit per R-,G-,B-gun */
  visuals[nv].vid             = FakeClientID(0);
  visuals[nv].class           = GrayScale;
  visuals[nv].bitsPerRGBValue = 12;
  visuals[nv].ColormapEntries = 4096;
  visuals[nv].nplanes         = 12;
  visuals[nv].redMask         = 0x0;
  visuals[nv].greenMask       = 0x0;
  visuals[nv].blueMask        = 0x0;
  visuals[nv].offsetRed       = 0x0;
  visuals[nv].offsetGreen     = 0x0;
  visuals[nv].offsetBlue      = 0x0;
  vids_12bit[nv_12bit] = visuals[nv].vid;
  nv++; nv_12bit++;

  /* StaticGray, 12bit, 12bit per R-,G-,B-gun */
  visuals[nv].vid             = FakeClientID(0);
  visuals[nv].class           = StaticGray;
  visuals[nv].bitsPerRGBValue = 12;
  visuals[nv].ColormapEntries = 4096;
  visuals[nv].nplanes         = 12;
  visuals[nv].redMask         = 0x0;
  visuals[nv].greenMask       = 0x0;
  visuals[nv].blueMask        = 0x0;
  visuals[nv].offsetRed       = 0x0;
  visuals[nv].offsetGreen     = 0x0;
  visuals[nv].offsetBlue      = 0x0;
  vids_12bit[nv_12bit] = visuals[nv].vid;
  nv++; nv_12bit++;
#endif /* PSOUT_USE_DEEPCOLOR */

  /* PseudoColor, 8bit */
  visuals[nv].vid             = FakeClientID(0);
  visuals[nv].class           = PseudoColor;
  visuals[nv].bitsPerRGBValue = 8;
  visuals[nv].ColormapEntries = 256;
  visuals[nv].nplanes         = 8;
  visuals[nv].redMask         = 0x0;
  visuals[nv].greenMask       = 0x0;
  visuals[nv].blueMask        = 0x0;
  visuals[nv].offsetRed       = 0x0;
  visuals[nv].offsetGreen     = 0x0;
  visuals[nv].offsetBlue      = 0x0;
  vids_8bit[nv_8bit] = visuals[nv].vid;
#ifndef PSOUT_USE_DEEPCOLOR
  defaultVisualIndex = nv;
#endif /* !PSOUT_USE_DEEPCOLOR */
  nv++; nv_8bit++;

  /* GrayScale, 8bit */
  visuals[nv].vid             = FakeClientID(0);
  visuals[nv].class           = GrayScale;
  visuals[nv].bitsPerRGBValue = 8;
  visuals[nv].ColormapEntries = 256;
  visuals[nv].nplanes         = 8;
  visuals[nv].redMask         = 0x0;
  visuals[nv].greenMask       = 0x0;
  visuals[nv].blueMask        = 0x0;
  visuals[nv].offsetRed       = 0x0;
  visuals[nv].offsetGreen     = 0x0;
  visuals[nv].offsetBlue      = 0x0;
  vids_8bit[nv_8bit] = visuals[nv].vid;
  nv++; nv_8bit++;

  /* StaticGray, 8bit */
  visuals[nv].vid             = FakeClientID(0);
  visuals[nv].class           = StaticGray;
  visuals[nv].bitsPerRGBValue = 8;
  visuals[nv].ColormapEntries = 256;
  visuals[nv].nplanes         = 8;
  visuals[nv].redMask         = 0x0;
  visuals[nv].greenMask       = 0x0;
  visuals[nv].blueMask        = 0x0;
  visuals[nv].offsetRed       = 0x0;
  visuals[nv].offsetGreen     = 0x0;
  visuals[nv].offsetBlue      = 0x0;
  vids_8bit[nv_8bit] = visuals[nv].vid;
  nv++; nv_8bit++;

  /* StaticGray, 1bit */
  visuals[nv].vid             = FakeClientID(0);
  visuals[nv].class           = StaticGray;
  visuals[nv].bitsPerRGBValue = 1;
  visuals[nv].ColormapEntries = 2;
  visuals[nv].nplanes         = 1;
  visuals[nv].redMask         = 0x0;
  visuals[nv].greenMask       = 0x0;
  visuals[nv].blueMask        = 0x0;
  visuals[nv].offsetRed       = 0x0;
  visuals[nv].offsetGreen     = 0x0;
  visuals[nv].offsetBlue      = 0x0;
  vids_1bit[nv_1bit] = visuals[nv].vid;
  nv++; nv_1bit++;

  if( nv_30bit > 0 )
  {
    depths[nd].depth   = 30;
    depths[nd].numVids = nv_30bit;
    depths[nd].vids    = vids_30bit;
    nd++;
  }

  if( nv_24bit > 0 )
  {
    depths[nd].depth   = 24;
    depths[nd].numVids = nv_24bit;
    depths[nd].vids    = vids_24bit;
    nd++;
  }

  if( nv_16bit > 0 )
  {
    depths[nd].depth   = 16;
    depths[nd].numVids = nv_16bit;
    depths[nd].vids    = vids_16bit;
    nd++;
  }

  if( nv_14bit > 0 )
  {
    depths[nd].depth   = 14;
    depths[nd].numVids = nv_14bit;
    depths[nd].vids    = vids_14bit;
    nd++;
  }
  
  if( nv_12bit > 0 )
  {
    depths[nd].depth   = 12;
    depths[nd].numVids = nv_12bit;
    depths[nd].vids    = vids_12bit;
    nd++;
  }
  
  if( nv_8bit > 0 )
  {
    depths[nd].depth   = 8;
    depths[nd].numVids = nv_8bit;
    depths[nd].vids    = vids_8bit;
    nd++;
  }

  if( nv_1bit > 0 )
  {
    depths[nd].depth   = 1;
    depths[nd].numVids = nv_1bit;
    depths[nd].vids    = vids_1bit;
    nd++;
  }

  /* Defaul visual is 12bit PseudoColor */
  defaultVisual = visuals[defaultVisualIndex].vid;
  rootDepth = visuals[defaultVisualIndex].nplanes;

#ifdef GLXEXT
  {
    miInitVisualsProcPtr proc = NULL;

    GlxWrapInitVisuals(&proc);
    /* GlxInitVisuals ignores the last three arguments. */
    proc(&visuals, &depths, &nv, &nd,
         &rootDepth, &defaultVisual, 0, 0, 0);
  }
#endif /* GLXEXT */

  miScreenInit(pScreen, (pointer)0,
               pScreen->width, pScreen->height,
               (int) (pScreen->width / (pScreen->mmWidth / 25.40)), 
               (int) (pScreen->height / (pScreen->mmHeight / 25.40)),
               0, rootDepth, nd,
               depths, defaultVisual, nv, visuals);

  if( miCreateDefColormap(pScreen)==FALSE ) return FALSE;

/*scalingScreenInit(pScreen);*/

  return TRUE;
}

static void
AllocatePsPrivates(ScreenPtr pScreen)
{
  static unsigned long PsGeneration = 0;

  if((unsigned long)PsGeneration != serverGeneration)
  {
    PsScreenPrivateIndex = AllocateScreenPrivateIndex();

    PsWindowPrivateIndex = AllocateWindowPrivateIndex();
    AllocateWindowPrivate(pScreen, PsWindowPrivateIndex,
                          sizeof(PsWindowPrivRec));

    PsContextPrivateIndex = XpAllocateContextPrivateIndex();
    XpAllocateContextPrivate(PsContextPrivateIndex, 
                             sizeof(PsContextPrivRec));

    PsPixmapPrivateIndex = AllocatePixmapPrivateIndex();
    AllocatePixmapPrivate(pScreen, PsPixmapPrivateIndex,
                          sizeof(PsPixmapPrivRec));

    PsGeneration = serverGeneration;
  }
  pScreen->devPrivates[PsScreenPrivateIndex].ptr =
           (pointer)xalloc(sizeof(PsScreenPrivRec));
}

/*
 * PsInitContext
 *
 * Establish the appropriate values for a PrintContext used with the PS
 * driver.
 */

static char DOC_ATT_SUPP[]="document-attributes-supported";
static char DOC_ATT_VAL[]="document-format xp-listfonts-modes";
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 xp-listfonts-modes";

static int
PsInitContext(pCon)
  XpContextPtr pCon;
{
  XpDriverFuncsPtr pFuncs;
  PsContextPrivPtr pConPriv;
  char *server, *attrStr;
    
  /*
   * Initialize the attribute store for this printer.
   */
  XpInitAttributes(pCon);

  /*
   * Initialize the function pointers
   */
  pFuncs = &(pCon->funcs);
  pFuncs->StartJob          = PsStartJob;
  pFuncs->EndJob            = PsEndJob;
  pFuncs->StartDoc          = PsStartDoc;
  pFuncs->EndDoc            = PsEndDoc;
  pFuncs->StartPage         = PsStartPage;
  pFuncs->EndPage           = PsEndPage;
  pFuncs->PutDocumentData   = PsDocumentData;
  pFuncs->GetDocumentData   = PsGetDocumentData;
  pFuncs->GetAttributes     = PsGetAttributes;
  pFuncs->SetAttributes     = PsSetAttributes;
  pFuncs->AugmentAttributes = PsAugmentAttributes;
  pFuncs->GetOneAttribute   = PsGetOneAttribute;
  pFuncs->DestroyContext    = PsDestroyContext;
  pFuncs->GetMediumDimensions = PsGetMediumDimensions;
  pFuncs->GetReproducibleArea = PsGetReproducibleArea;
  pFuncs->SetImageResolution = PsSetImageResolution;
    
  /*
   * Set up the context privates
   */
  pConPriv =
      (PsContextPrivPtr)pCon->devPrivates[PsContextPrivateIndex].ptr;

  memset(pConPriv, 0, sizeof(PsContextPrivRec));
  pConPriv->jobFileName         = (char *)NULL;
  pConPriv->pJobFile            = (FILE *)NULL;
  pConPriv->dash                = (unsigned char *)NULL;
  pConPriv->validGC             = 0;
  pConPriv->getDocClient        = (ClientPtr)NULL;
  pConPriv->getDocBufSize       = 0;
  pConPriv->pPsOut              = NULL;
  pConPriv->fontInfoRecords     = NULL;
  pConPriv->fontTypeInfoRecords = NULL;

  /*
   * 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) + 8)) == 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) + 8)) == 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) + 8)) == 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, &PsValidatePoolsRec);
  XpValidateAttributePool(pCon, XPDocAttr, &PsValidatePoolsRec);
  XpValidateAttributePool(pCon, XPJobAttr, &PsValidatePoolsRec);
  XpValidateAttributePool(pCon, XPPageAttr, &PsValidatePoolsRec);

  return Success;
}

static Bool
PsDestroyContext(pCon)
  XpContextPtr pCon;
{
  PsContextPrivPtr pConPriv =
      (PsContextPrivPtr)pCon->devPrivates[PsContextPrivateIndex].ptr;
    
  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;
  }

  PsFreeFontInfoRecords(pConPriv);

  /* Reset context to make sure we do not use any stale/invalid/obsolete data */
  memset(pConPriv, 0, sizeof(PsContextPrivRec));

/*### free up visuals/depths ###*/

  return Success;
}

XpContextPtr
PsGetContextFromWindow(win)
  WindowPtr win;
{
  PsWindowPrivPtr pPriv;

  while( win )
  {
    pPriv = (PsWindowPrivPtr)win->devPrivates[PsWindowPrivateIndex].ptr;
    if( pPriv->validContext ) return pPriv->context;
    win = win->parent;
  }

  return NULL;
}