First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xprint / ps / PsPrint.c
blob4571c07d7c8c474779379efb5de5da92b4cb5e47
1 /*
3 Copyright 1996, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
27 * (c) Copyright 1996 Hewlett-Packard Company
28 * (c) Copyright 1996 International Business Machines Corp.
29 * (c) Copyright 1996, 2000 Sun Microsystems, Inc. All rights reserved.
30 * (c) Copyright 1996 Novell, Inc.
31 * (c) Copyright 1996 Digital Equipment Corp.
32 * (c) Copyright 1996 Fujitsu Limited
33 * (c) Copyright 1996 Hitachi, Ltd.
35 * Permission is hereby granted, free of charge, to any person obtaining
36 * a copy of this software and associated documentation files (the
37 * "Software"), to deal in the Software without restriction, including
38 * without limitation the rights to use, copy, modify, merge, publish,
39 * distribute, sublicense, and/or sell copies of the Software, and to
40 * permit persons to whom the Software is furnished to do so, subject
41 * to the following conditions:
43 * The above copyright notice and this permission notice shall be included
44 * in all copies or substantial portions of the Software.
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
49 * THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
50 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
51 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
52 * SOFTWARE.
54 * Except as contained in this notice, the names of the copyright holders
55 * shall not be used in advertising or otherwise to promote the sale, use
56 * or other dealings in this Software without prior written authorization
57 * from said copyright holders.
60 /*******************************************************************
62 ** *********************************************************
63 ** *
64 ** * File: PsPrint.c
65 ** *
66 ** * Contents: Print extension code of Ps driver
67 ** *
68 ** * Created By: Roger Helmendach (Liberty Systems)
69 ** *
70 ** * Copyright: Copyright 1996 The Open Group, Inc.
71 ** *
72 ** *********************************************************
73 **
74 ********************************************************************/
76 #ifdef HAVE_DIX_CONFIG_H
77 #include <dix-config.h>
78 #endif
80 #include <stdio.h>
81 #include <string.h>
82 #include <sys/types.h>
83 #include <sys/stat.h>
84 #include <sys/wait.h>
85 #include <unistd.h>
86 #include <errno.h>
87 #include <X11/Xprotostr.h>
89 #define NEED_EVENTS
90 #include <X11/Xproto.h>
91 #undef NEED_EVENTS
93 #include "Ps.h"
95 #include "windowstr.h"
96 #include "attributes.h"
97 #include "Oid.h"
99 /* static utility function to get document/page attributes */
100 static void
101 S_GetPageAttributes(XpContextPtr pCon,int *iorient,int *icount, int *iplex,
102 int *ires, unsigned short *iwd, unsigned short *iht)
104 char *count;
105 XpOid orient, plex;
107 * Get the orientation
109 orient = XpGetContentOrientation(pCon);
110 switch (orient) {
111 case xpoid_val_content_orientation_landscape:
112 *iorient = 1;
113 break;
114 case xpoid_val_content_orientation_reverse_portrait:
115 *iorient = 2;
116 break;
117 case xpoid_val_content_orientation_reverse_landscape:
118 *iorient = 3;
119 break;
120 case xpoid_val_content_orientation_portrait:
121 default:
122 *iorient = 0;
123 break;
127 * Get the count
129 count = XpGetOneAttribute(pCon, XPDocAttr, "copy-count");
130 if( count )
132 int ii = sscanf(count, "%d", icount);
133 if( ii!=1 ) *icount = 1;
135 else *icount = 1;
138 * Get the plex
140 plex = XpGetPlex(pCon);
141 switch(plex)
143 case xpoid_val_plex_duplex:
144 *iplex = 1;
145 break;
146 case xpoid_val_plex_tumble:
147 *iplex = 2;
148 break;
149 default:
150 *iplex = 0;
151 break;
155 * Get the resolution and media size
157 *ires = XpGetResolution(pCon);
158 XpGetMediumDimensions(pCon, iwd, iht);
163 PsStartJob(
164 XpContextPtr pCon,
165 Bool sendClientData,
166 ClientPtr client)
168 PsContextPrivPtr pConPriv =
169 (PsContextPrivPtr)pCon->devPrivates[PsContextPrivateIndex].ptr;
172 * Create a temporary file to store the printer output.
174 if (!XpOpenTmpFile("w", &pConPriv->jobFileName, &pConPriv->pJobFile))
175 return BadAlloc;
177 return Success;
182 /* I thought about making this following code into a set of routines
183 or using a goto, or something, but in the end decided not to,
184 because the plain old listing here makes the logic clearer. */
186 PsEndJob(
187 XpContextPtr pCon,
188 Bool cancel)
190 int r;
191 struct stat buffer;
192 int error;
194 PsContextPrivPtr priv =
195 (PsContextPrivPtr)pCon->devPrivates[PsContextPrivateIndex].ptr;
197 if (cancel == True) {
198 if (priv->getDocClient != (ClientPtr) NULL) {
199 (void) XpFinishDocData( priv->getDocClient );
201 priv->getDocClient = NULL;
202 priv->getDocBufSize = 0;
205 /* job is cancelled - do we really care if we're out of space? */
206 (void) fclose(priv->pJobFile);
207 priv->pJobFile = NULL;
209 unlink(priv->jobFileName);
210 xfree(priv->jobFileName);
211 priv->jobFileName = (char *)NULL;
213 PsFreeFontInfoRecords(priv);
215 return Success;
219 * Append any trailing information here
221 PsOut_EndFile(priv->pPsOut, 0);
222 priv->pPsOut = NULL;
224 /* this is where we find out if we're out of space */
225 error = (fclose(priv->pJobFile) == EOF);
226 priv->pJobFile = NULL;
228 /* status to the client if we have ran out of space on the disk or
229 some other resource problem with the temporary file... */
230 if (error) {
231 if (priv->getDocClient != (ClientPtr) NULL) {
232 (void) XpFinishDocData( priv->getDocClient );
234 priv->getDocClient = NULL;
235 priv->getDocBufSize = 0;
238 unlink(priv->jobFileName);
239 xfree(priv->jobFileName);
240 priv->jobFileName = (char *)NULL;
242 PsFreeFontInfoRecords(priv);
244 return BadAlloc;
247 /* we have finished without incident & no cancel */
249 if (priv->getDocClient != NULL && priv->getDocBufSize > 0) {
250 FILE *file;
252 file = fopen(priv->jobFileName, "r");
253 if (!file || (fstat(fileno(file), &buffer) < 0))
254 r = BadAlloc;
255 else
256 r = XpSendDocumentData(priv->getDocClient, file, buffer.st_size,
257 priv->getDocBufSize);
258 if (file)
259 fclose(file);
261 (void) XpFinishDocData(priv->getDocClient);
263 priv->getDocClient = NULL;
264 priv->getDocBufSize = 0;
266 else {
267 XpSubmitJob(priv->jobFileName, pCon);
269 r = Success;
272 unlink(priv->jobFileName);
273 xfree(priv->jobFileName);
274 priv->jobFileName = (char *)NULL;
276 PsFreeFontInfoRecords(priv);
278 #ifdef BM_CACHE
279 PsBmClearImageCache();
280 #endif
282 return r;
285 /* StartPage
288 PsStartPage(
289 XpContextPtr pCon,
290 WindowPtr pWin)
292 int iorient, iplex, icount, ires;
293 unsigned short iwd, iht;
294 PsContextPrivPtr pConPriv =
295 (PsContextPrivPtr)pCon->devPrivates[PsContextPrivateIndex].ptr;
296 PsWindowPrivPtr pWinPriv =
297 (PsWindowPrivPtr)pWin->devPrivates[PsWindowPrivateIndex].ptr;
300 * Put a pointer to the context in the window private structure
302 pWinPriv->validContext = 1;
303 pWinPriv->context = pCon;
305 /* get page level attributes */
306 S_GetPageAttributes(pCon,&iorient,&icount,&iplex,&ires,&iwd,&iht);
308 * Start the page
310 if (pConPriv->pPsOut == NULL) {
311 char *title;
313 /* get job level attributes */
314 title = XpGetOneAttribute(pCon, XPJobAttr, "job-name");
316 pConPriv->pPsOut = PsOut_BeginFile(pConPriv->pJobFile,
317 title, iorient, icount, iplex, ires,
318 (int)iwd, (int)iht, False);
319 pConPriv->fontInfoRecords = NULL;
320 pConPriv->fontTypeInfoRecords = NULL;
322 PsOut_BeginPage(pConPriv->pPsOut, iorient, icount, iplex, ires,
323 (int)iwd, (int)iht);
325 return Success;
330 * EndPage:
332 * Write page trailer to page file
333 * Write page file to job file
336 PsEndPage(
337 XpContextPtr pCon,
338 WindowPtr pWin)
340 PsWindowPrivPtr pWinPriv =
341 (PsWindowPrivPtr)pWin->devPrivates[PsWindowPrivateIndex].ptr;
342 PsContextPrivPtr pConPriv =
343 (PsContextPrivPtr)pCon->devPrivates[PsContextPrivateIndex].ptr;
345 PsOut_EndPage(pConPriv->pPsOut);
347 pWinPriv->validContext = 0;
348 pWinPriv->context = NULL;
350 /* status to the client if we have ran out of space on the disk or
351 some other resource problem with the temporary file... */
352 /* if (ferror(pConPriv->pJobFile)) return BadAlloc; */
354 return Success;
358 * The PsStartDoc() and PsEndDoc() functions serve basically as NOOP
359 * placeholders. This driver doesn't deal with the notion of multiple
360 * documents per page.
364 PsStartDoc(XpContextPtr pCon, XPDocumentType type)
366 int iorient, iplex, icount, ires;
367 unsigned short iwd, iht;
368 char *title;
369 PsContextPrivPtr pConPriv =
370 (PsContextPrivPtr)pCon->devPrivates[PsContextPrivateIndex].ptr;
372 /* get job level attributes */
373 title = XpGetOneAttribute(pCon, XPJobAttr, "job-name");
375 /* get document level attributes */
376 S_GetPageAttributes(pCon,&iorient,&icount,&iplex,&ires,&iwd,&iht);
378 pConPriv->pPsOut = PsOut_BeginFile(pConPriv->pJobFile,
379 title, iorient, icount, iplex, ires,
380 (int)iwd, (int)iht, (Bool)(type == XPDocRaw));
382 pConPriv->fontInfoRecords = NULL;
383 pConPriv->fontTypeInfoRecords = NULL;
385 return Success;
389 PsEndDoc(
390 XpContextPtr pCon,
391 Bool cancel)
393 return Success;
397 * PsDocumentData()
399 * Hand any pre-generated PDL down to the spool files, formatting it
400 * as necessary to fit the given window.
404 PsDocumentData(
405 XpContextPtr pCon,
406 DrawablePtr pDraw,
407 char *pData,
408 int len_data,
409 char *pFmt,
410 int len_fmt,
411 char *pOpt,
412 int len_opt,
413 ClientPtr client)
415 PsContextPrivPtr cPriv;
416 PsOutPtr psOut;
418 if (len_fmt != 12 ||
419 strncasecmp(pFmt, "PostScript 2", len_fmt) != 0 ||
420 len_opt)
421 return BadValue;
423 cPriv = pCon->devPrivates[PsContextPrivateIndex].ptr;
424 psOut = cPriv->pPsOut;
426 if (pDraw)
427 PsOut_BeginFrame(psOut, 0, 0, pDraw->x, pDraw->y,
428 pDraw->width, pDraw->height);
429 PsOut_RawData(psOut, pData, len_data);
430 if (pDraw)
431 PsOut_EndFrame(psOut);
433 return Success;
438 * PsGetDocumentData()
440 * This function allows the driver to send the generated PS back to
441 * the client.
445 PsGetDocumentData(
446 XpContextPtr pCon,
447 ClientPtr client,
448 int maxBufferSize)
450 PsContextPrivPtr pPriv = (PsContextPrivPtr)
451 pCon->devPrivates[PsContextPrivateIndex].ptr;
453 pPriv->getDocClient = client;
454 pPriv->getDocBufSize = maxBufferSize;
456 return Success;