First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xprint / pcl / PclMisc.c
blobe0b7dced9eaa4a3108a8a13e2e9edace600573d5
1 /*******************************************************************
2 **
3 ** *********************************************************
4 ** *
5 ** * File: PclMisc.c
6 ** *
7 ** * Contents:
8 ** * Miscellaneous code for Pcl driver.
9 ** *
10 ** * Created: 2/01/95
11 ** *
12 ** *********************************************************
13 **
14 ********************************************************************/
16 (c) Copyright 1996 Hewlett-Packard Company
17 (c) Copyright 1996 International Business Machines Corp.
18 (c) Copyright 1996 Sun Microsystems, Inc.
19 (c) Copyright 1996 Novell, Inc.
20 (c) Copyright 1996 Digital Equipment Corp.
21 (c) Copyright 1996 Fujitsu Limited
22 (c) Copyright 1996 Hitachi, Ltd.
24 Permission is hereby granted, free of charge, to any person obtaining a copy
25 of this software and associated documentation files (the "Software"), to deal
26 in the Software without restriction, including without limitation the rights
27 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28 copies of the Software, and to permit persons to whom the Software is
29 furnished to do so, subject to the following conditions:
31 The above copyright notice and this permission notice shall be included in
32 all copies or substantial portions of the Software.
34 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
38 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
39 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 Except as contained in this notice, the names of the copyright holders shall
42 not be used in advertising or otherwise to promote the sale, use or other
43 dealings in this Software without prior written authorization from said
44 copyright holders.
47 #ifdef HAVE_DIX_CONFIG_H
48 #include <dix-config.h>
49 #endif
51 #include <stdlib.h>
52 #include <signal.h>
53 #include <sys/types.h>
54 #include <sys/wait.h>
55 #include <X11/Xos.h> /* for SIGCLD on pre-POSIX systems */
56 #include "Pcl.h"
58 #include "cursor.h"
59 #include "resource.h"
61 #include "windowstr.h"
62 #include "propertyst.h"
63 #include "attributes.h"
66 /*ARGSUSED*/
67 void
68 PclQueryBestSize(
69 int type,
70 short *pwidth,
71 short *pheight,
72 ScreenPtr pScreen)
74 unsigned width, highBit;
76 switch(type)
78 case CursorShape:
79 *pwidth = 0;
80 *pheight = 0;
81 break;
82 case TileShape:
83 case StippleShape:
84 width = *pwidth;
85 if (!width) break;
86 /* Return the nearest power of two >= what they gave us */
87 highBit = 0x80000000;
88 /* Find the highest 1 bit in the given width */
89 while(!(highBit & width))
90 highBit >>= 1;
91 /* If greater than that then return the next power of two */
92 if((highBit - 1) & width)
93 highBit <<= 1;
94 *pwidth = highBit;
95 /* height is a don't-care */
96 break;
101 * GetPropString searches the window heirarchy from pWin up looking for
102 * a property by the name of propName. If found, returns the property's
103 * value. If not, it returns NULL.
105 char *
106 GetPropString(
107 WindowPtr pWin,
108 char *propName)
110 Atom atom;
111 PropertyPtr pProp = (PropertyPtr)NULL;
112 char *retVal;
114 atom = MakeAtom(propName, strlen(propName), FALSE);
115 if(atom != BAD_RESOURCE)
117 WindowPtr pPropWin;
118 int n;
121 * The atom has been defined, but it might only exist as a
122 * property on an unrelated window.
124 for(pPropWin = pWin; pPropWin != (WindowPtr)NULL;
125 pPropWin = pPropWin->parent)
127 for(pProp = (PropertyPtr)(wUserProps(pPropWin));
128 pProp != (PropertyPtr)NULL;
129 pProp = pProp->next)
131 if (pProp->propertyName == atom)
132 break;
134 if(pProp != (PropertyPtr)NULL)
135 break;
137 if(pProp == (PropertyPtr)NULL)
138 return (char *)NULL;
140 n = (pProp->format/8) * pProp->size; /* size (bytes) of prop */
141 retVal = (char *)xalloc(n + 1);
142 (void)memcpy((void *)retVal, (void *)pProp->data, n);
143 retVal[n] = '\0';
145 return retVal;
148 return (char *)NULL;
151 #include <signal.h>
152 #include <errno.h>
154 /* ARGSUSED */
155 static void SigchldHndlr (
156 int dummy)
158 int status;
159 int olderrno = errno;
160 struct sigaction act;
161 sigfillset(&act.sa_mask);
162 act.sa_flags = 0;
163 act.sa_handler = SigchldHndlr;
165 (void) wait (&status);
168 * Is this really necessary?
170 sigaction(SIGCHLD, &act, (struct sigaction *)NULL);
171 errno = olderrno;
175 * SystemCmd provides a wrapper for the 'system' library call. The call
176 * appears to be sensitive to the handling of SIGCHLD, so this wrapper
177 * sets the status to SIG_DFL, and then resets the established handler
178 * after system returns.
181 SystemCmd(char *cmdStr)
183 int status;
184 struct sigaction newAct, oldAct;
185 sigfillset(&newAct.sa_mask);
186 newAct.sa_flags = 0;
187 newAct.sa_handler = SIG_DFL;
188 sigfillset(&oldAct.sa_mask);
189 oldAct.sa_flags = 0;
190 oldAct.sa_handler = SigchldHndlr;
193 * get the old handler, and set the action to IGN
195 sigaction(SIGCHLD, &newAct, &oldAct);
197 status = system (cmdStr);
199 sigaction(SIGCHLD, &oldAct, (struct sigaction *)NULL);
200 return status;
205 * PclGetMediumDimensions is installed in the GetMediumDimensions field
206 * of each Pcl-initialized context.
209 PclGetMediumDimensions(XpContextPtr pCon,
210 CARD16 *width,
211 CARD16 *height)
213 XpGetMediumDimensions(pCon, width, height);
214 return Success;
218 * PclGetReproducibleArea is installed in the GetReproducibleArea field
219 * of each Pcl-initialized context.
222 PclGetReproducibleArea(XpContextPtr pCon,
223 xRectangle *pRect)
225 XpGetReproductionArea(pCon, pRect);
226 return Success;
229 #ifdef XP_PCL_LJ3
231 * PclSpoolFigs spooled the rendering PCL/HP-GL2 commands into the
232 * temporary buffer pointed by figures pointer in pcl private context.
233 * LaserJet IIIs printers don't support the macro function which
234 * includes some HP-GL/2 commands.
236 void
237 PclSpoolFigs(PclContextPrivPtr pConPriv, char *t, int n)
239 char *ptr;
241 ptr = pConPriv->figures;
242 while ( ( pConPriv->fcount + n) > pConPriv->fcount_max ) {
243 ptr = (char *)xrealloc(ptr, 1024 + pConPriv->fcount_max);
244 if ( !ptr )
245 return;
246 pConPriv->figures = ptr;
247 pConPriv->fcount_max += 1024;
249 ptr += pConPriv->fcount;
250 pConPriv->fcount += n;
251 memcpy(ptr, t, n);
253 #endif /* XP_PCL_LJ3 */
256 * PclSendData:
257 * For XP-PCL-COLOR/XP-PCL-MONO, it executes the macro stored before
258 * in the clipped area.
259 * For XP-PCL-LJ3, it draws the spooled figures in the clipped area.
261 void
262 PclSendData(
263 FILE *outFile,
264 PclContextPrivPtr pConPriv,
265 BoxPtr pbox,
266 int nbox,
267 double ratio
270 char *ptr;
271 int n;
272 char t[80];
274 #ifdef XP_PCL_LJ3
275 ptr = pConPriv->figures;
276 n = pConPriv->fcount;
277 #else
278 ptr = "\033&f3X";
279 n = 5;
280 #endif /* XP_PCL_LJ3 */
282 while( nbox )
285 * Set the HP-GL/2 input window to the current
286 * rectangle in the clip region, then send the code to
287 * execute the macro defined above.
289 if (ratio == 1.0)
290 sprintf( t, "\033%%0BIW%d,%d,%d,%d;\033%%0A",
291 pbox->x1, pbox->y1,
292 pbox->x2, pbox->y2 );
293 else
294 sprintf( t, "\033%%0BIW%g,%d,%g,%d;\033%%0A",
295 ratio * pbox->x1, pbox->y1,
296 ratio * pbox->x2, pbox->y2 );
298 SEND_PCL( outFile, t );
299 SEND_PCL_COUNT( outFile, ptr, n);
301 nbox--;
302 pbox++;