]>
Commit | Line | Data |
---|---|---|
74456259 | 1 | /* |
c67094f2 | 2 | * "$Id: rastertodymo.c,v 1.4.2.4 2003/01/14 18:10:40 mike Exp $" |
74456259 | 3 | * |
4 | * DYMO label printer filter for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 2001 by Easy Software Products. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636-3111 USA | |
19 | * | |
20 | * Voice: (301) 373-9603 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
dab1a4d8 | 24 | * This file is subject to the Apple OS-Developed Software exception. |
25 | * | |
74456259 | 26 | * Contents: |
27 | * | |
28 | * Setup() - Prepare the printer for printing. | |
29 | * StartPage() - Start a page of graphics. | |
30 | * EndPage() - Finish a page of graphics. | |
31 | * Shutdown() - Shutdown the printer. | |
32 | * CancelJob() - Cancel the current job... | |
33 | * CompressData() - Compress a line of graphics. | |
34 | * OutputLine() - Output a line of graphics. | |
35 | * main() - Main entry and processing of driver. | |
36 | */ | |
37 | ||
38 | /* | |
39 | * Include necessary headers... | |
40 | */ | |
41 | ||
42 | #include <cups/cups.h> | |
43 | #include <cups/string.h> | |
44 | #include "raster.h" | |
45 | #include <stdlib.h> | |
46 | #include <unistd.h> | |
47 | #include <fcntl.h> | |
48 | #include <signal.h> | |
49 | ||
50 | ||
51 | /* | |
52 | * Globals... | |
53 | */ | |
54 | ||
55 | unsigned char *Buffer; /* Output buffer */ | |
56 | int Page, /* Current page */ | |
57 | Feed; /* Number of lines to skip */ | |
58 | ||
59 | ||
60 | /* | |
61 | * Prototypes... | |
62 | */ | |
63 | ||
64 | void Setup(void); | |
65 | void StartPage(cups_page_header_t *header); | |
66 | void EndPage(void); | |
67 | ||
68 | void CancelJob(int sig); | |
69 | ||
70 | /**** MRS - supported resolutions = 136, 203, 300 ****/ | |
71 | ||
72 | ||
73 | /* | |
74 | * 'Setup()' - Prepare the printer for printing. | |
75 | */ | |
76 | ||
77 | void | |
78 | Setup(void) | |
79 | { | |
80 | int i; /* Looping var */ | |
81 | ||
82 | ||
83 | /* | |
84 | * Clear any remaining data... | |
85 | */ | |
86 | ||
87 | for (i = 0; i < 100; i ++) | |
88 | putchar(0x1b); | |
c67094f2 | 89 | |
90 | /* | |
91 | * Reset the printer... | |
92 | */ | |
93 | ||
94 | printf("\033@"); | |
74456259 | 95 | } |
96 | ||
97 | ||
98 | /* | |
99 | * 'StartPage()' - Start a page of graphics. | |
100 | */ | |
101 | ||
102 | void | |
103 | StartPage(cups_page_header_t *header) /* I - Page header */ | |
104 | { | |
105 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) | |
106 | struct sigaction action; /* Actions for POSIX signals */ | |
107 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
108 | ||
109 | ||
110 | /* | |
111 | * Register a signal handler to eject the current page if the | |
112 | * job is cancelled. | |
113 | */ | |
114 | ||
115 | #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ | |
116 | sigset(SIGTERM, CancelJob); | |
117 | #elif defined(HAVE_SIGACTION) | |
118 | memset(&action, 0, sizeof(action)); | |
119 | ||
120 | sigemptyset(&action.sa_mask); | |
121 | action.sa_handler = CancelJob; | |
122 | sigaction(SIGTERM, &action, NULL); | |
123 | #else | |
124 | signal(SIGTERM, CancelJob); | |
125 | #endif /* HAVE_SIGSET */ | |
126 | ||
127 | /* | |
128 | * Setup printer/job attributes... | |
129 | */ | |
130 | ||
131 | printf("\033L%c%c", header->cupsHeight, header->cupsHeight >> 8); | |
132 | printf("\033D%c", header->cupsBytesPerLine); | |
133 | ||
134 | printf("\033%c", header->cupsCompression + 'c'); /* Darkness */ | |
135 | ||
136 | /* | |
137 | * Allocate memory for a line of graphics... | |
138 | */ | |
139 | ||
140 | Buffer = malloc(header->cupsBytesPerLine); | |
141 | Feed = 0; | |
142 | } | |
143 | ||
144 | ||
145 | /* | |
146 | * 'EndPage()' - Finish a page of graphics. | |
147 | */ | |
148 | ||
149 | void | |
150 | EndPage(void) | |
151 | { | |
152 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) | |
153 | struct sigaction action; /* Actions for POSIX signals */ | |
154 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
155 | ||
156 | ||
157 | /* | |
158 | * Eject the current page... | |
159 | */ | |
160 | ||
161 | printf("\033E"); | |
162 | ||
163 | fflush(stdout); | |
164 | ||
165 | /* | |
166 | * Unregister the signal handler... | |
167 | */ | |
168 | ||
169 | #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ | |
170 | sigset(SIGTERM, SIG_IGN); | |
171 | #elif defined(HAVE_SIGACTION) | |
172 | memset(&action, 0, sizeof(action)); | |
173 | ||
174 | sigemptyset(&action.sa_mask); | |
175 | action.sa_handler = SIG_IGN; | |
176 | sigaction(SIGTERM, &action, NULL); | |
177 | #else | |
178 | signal(SIGTERM, SIG_IGN); | |
179 | #endif /* HAVE_SIGSET */ | |
180 | ||
181 | /* | |
182 | * Free memory... | |
183 | */ | |
184 | ||
185 | free(Buffer); | |
186 | } | |
187 | ||
188 | ||
189 | /* | |
190 | * 'CancelJob()' - Cancel the current job... | |
191 | */ | |
192 | ||
193 | void | |
194 | CancelJob(int sig) /* I - Signal */ | |
195 | { | |
196 | int i; /* Looping var */ | |
197 | ||
198 | ||
199 | (void)sig; | |
200 | ||
201 | /* | |
202 | * Send out lots of ESC bytes to clear out any pending raster data... | |
203 | */ | |
204 | ||
205 | for (i = 0; i < 100; i ++) | |
206 | putchar(0x1b); | |
207 | ||
208 | /* | |
209 | * End the current page and exit... | |
210 | */ | |
211 | ||
212 | EndPage(); | |
213 | ||
214 | exit(0); | |
215 | } | |
216 | ||
217 | ||
218 | /* | |
219 | * 'main()' - Main entry and processing of driver. | |
220 | */ | |
221 | ||
222 | int /* O - Exit status */ | |
223 | main(int argc, /* I - Number of command-line arguments */ | |
224 | char *argv[]) /* I - Command-line arguments */ | |
225 | { | |
1594457e | 226 | FILE *fp; /* Raster data file */ |
74456259 | 227 | cups_raster_t *ras; /* Raster stream for printing */ |
228 | cups_page_header_t header; /* Page header from file */ | |
229 | int y; /* Current line */ | |
230 | ||
231 | ||
232 | /* | |
233 | * Make sure status messages are not buffered... | |
234 | */ | |
235 | ||
236 | setbuf(stderr, NULL); | |
237 | ||
238 | /* | |
239 | * Check command-line... | |
240 | */ | |
241 | ||
242 | if (argc < 6 || argc > 7) | |
243 | { | |
244 | /* | |
245 | * We don't have the correct number of arguments; write an error message | |
246 | * and return. | |
247 | */ | |
248 | ||
249 | fputs("ERROR: rastertodymo job-id user title copies options [file]\n", stderr); | |
250 | return (1); | |
251 | } | |
252 | ||
253 | /* | |
254 | * Open the page stream... | |
255 | */ | |
256 | ||
257 | if (argc == 7) | |
258 | { | |
1594457e | 259 | if ((fp = fopen(argv[6], "rb")) == NULL) |
74456259 | 260 | { |
261 | perror("ERROR: Unable to open raster file - "); | |
262 | sleep(1); | |
263 | return (1); | |
264 | } | |
265 | } | |
266 | else | |
1594457e | 267 | fp = stdin; |
74456259 | 268 | |
1594457e | 269 | ras = cupsRasterOpen(fp, CUPS_RASTER_READ); |
74456259 | 270 | |
271 | /* | |
272 | * Initialize the print device... | |
273 | */ | |
274 | ||
275 | Setup(); | |
276 | ||
277 | /* | |
278 | * Process pages as needed... | |
279 | */ | |
280 | ||
281 | Page = 0; | |
282 | ||
283 | while (cupsRasterReadHeader(ras, &header)) | |
284 | { | |
285 | /* | |
286 | * Write a status message with the page number and number of copies. | |
287 | */ | |
288 | ||
289 | Page ++; | |
290 | ||
291 | fprintf(stderr, "PAGE: %d 1\n", Page); | |
292 | ||
293 | /* | |
294 | * Start the page... | |
295 | */ | |
296 | ||
297 | StartPage(&header); | |
298 | ||
299 | /* | |
300 | * Loop for each line on the page... | |
301 | */ | |
302 | ||
303 | for (y = 0; y < header.cupsHeight; y ++) | |
304 | { | |
305 | /* | |
306 | * Let the user know how far we have progressed... | |
307 | */ | |
308 | ||
309 | if ((y & 15) == 0) | |
310 | fprintf(stderr, "INFO: Printing page %d, %d%% complete...\n", Page, | |
311 | 100 * y / header.cupsHeight); | |
312 | ||
313 | /* | |
314 | * Read a line of graphics... | |
315 | */ | |
316 | ||
317 | if (cupsRasterReadPixels(ras, Buffer, header.cupsBytesPerLine) < 1) | |
318 | break; | |
319 | ||
320 | /* | |
321 | * See if the line is blank; if not, write it to the printer... | |
322 | */ | |
323 | ||
324 | if (Buffer[0] || | |
325 | memcmp(Buffer, Buffer + 1, header.cupsBytesPerLine - 1)) | |
326 | { | |
327 | if (Feed) | |
328 | { | |
c67094f2 | 329 | while (Feed > 255) |
330 | { | |
331 | printf("\033f\001%c", 255); | |
332 | Feed -= 255; | |
333 | } | |
334 | ||
74456259 | 335 | printf("\033f\001%c", Feed); |
336 | Feed = 0; | |
337 | } | |
c67094f2 | 338 | |
74456259 | 339 | putchar(0x16); |
340 | fwrite(Buffer, header.cupsBytesPerLine, 1, stdout); | |
341 | } | |
342 | else | |
343 | Feed ++; | |
344 | } | |
345 | ||
346 | /* | |
347 | * Eject the page... | |
348 | */ | |
349 | ||
350 | EndPage(); | |
351 | } | |
352 | ||
353 | /* | |
354 | * Close the raster stream... | |
355 | */ | |
356 | ||
357 | cupsRasterClose(ras); | |
1594457e | 358 | if (fp != stdin) |
359 | fclose(fp); | |
74456259 | 360 | |
361 | /* | |
362 | * If no pages were printed, send an error message... | |
363 | */ | |
364 | ||
365 | if (Page == 0) | |
366 | fputs("ERROR: No pages found!\n", stderr); | |
367 | else | |
368 | fputs("INFO: " CUPS_SVERSION " is ready to print.\n", stderr); | |
369 | ||
370 | return (Page == 0); | |
371 | } | |
372 | ||
373 | ||
374 | /* | |
c67094f2 | 375 | * End of "$Id: rastertodymo.c,v 1.4.2.4 2003/01/14 18:10:40 mike Exp $". |
74456259 | 376 | */ |