386BSD 0.1 development
[unix-history] / usr / othersrc / public / ghostscript-2.4.1 / gdevn533.c
CommitLineData
ff6e2e51
WJ
1/* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises. All rights reserved.
2 Distributed by Free Software Foundation, Inc.
3
4This file is part of Ghostscript.
5
6Ghostscript is distributed in the hope that it will be useful, but
7WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
8to anyone for the consequences of using it or for whether it serves any
9particular purpose or works at all, unless he says so in writing. Refer
10to the Ghostscript General Public License for full details.
11
12Everyone is granted permission to copy, modify and redistribute
13Ghostscript, but only under the conditions described in the Ghostscript
14General Public License. A copy of this license is supposed to have been
15given to you along with Ghostscript so you can know your rights and
16responsibilities. It should be in a file named COPYING. Among other
17things, the copyright notice and this notice must be preserved on all
18copies. */
19
20/* gdevn533.c */
21/* Sony NWP-533 driver for GhostScript */
22#include "gdevprn.h"
23#include <sys/file.h>
24#include <sys/ioctl.h>
25#include <newsiop/lbp.h>
26
27/* The device descriptor */
28private dev_proc_open_device(nwp533_open);
29private dev_proc_output_page(nwp533_output_page);
30private dev_proc_close_device(nwp533_close);
31private gx_device_procs nwp533_procs =
32 prn_procs(nwp533_open, nwp533_output_page, nwp533_close);
33gx_device_printer gs_nwp533_device =
34 prn_device(nwp533_procs, "nwp533",
35 78.4, /* width_10ths */
36 112.9, /* height_10ths */
37 400, /* x_dpi */
38 400, /* y_dpi */
39 0,0,0,0, /* margins */
40 1, 0);
41
42private int printer_file = -1;
43
44/* return True if should retry - False if should quit */
45private int
46analyze_error()
47{
48 struct lbp_stat status;
49 char message[80];
50 char *detail, *old_detail;
51 int waiting;
52 int retry_after_return;
53
54 if(ioctl(printer_file, LBIOCRESET, 0) < 0)
55 return (0 == 1);
56 if(ioctl(printer_file, LBIOCSTATUS, &status) < 0)
57 return (0 == 1);
58 sprintf(message, "printer status: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x",
59 status.stat[0], status.stat[1], status.stat[2],
60 status.stat[3], status.stat[4], status.stat[5]);
61 perror(message);
62
63 old_detail = detail = NULL;
64 waiting = retry_after_return = (1 == 1); /* True */
65 do
66 {
67 if(status.stat[0] & (ST0_CALL | ST0_REPRINT_REQ | ST0_WAIT | ST0_PAUSE))
68 {
69 if(status.stat[1] & ST1_NO_CARTRIGE)/* mispelled? */
70 detail = "No cartridge - waiting";
71 else if(status.stat[1] & ST1_NO_PAPER)
72 detail = "Out of paper - waiting";
73 else if(status.stat[1] & ST1_JAM)
74 detail = "Paper jam - waiting";
75 else if(status.stat[1] & ST1_OPEN)
76 detail = "Door open - waiting";
77 else if(status.stat[1] & ST1_TEST)
78 detail = "Test printing - waiting";
79 else
80 {
81 retry_after_return = (1 == 0);
82 waiting = (1 == 0);
83 detail = "Please analyze status bytes";
84 }
85 }
86 else
87 waiting = (0 == 1);
88 if(detail != NULL && detail != old_detail)
89 {
90 perror(detail);
91 old_detail = detail;
92 }
93 if(waiting)
94 {
95 ioctl(1, LBIOCRESET, 0);
96 sleep(5);
97 ioctl(1, LBIOCSTATUS, &status);
98 }
99 }
100 while(waiting);
101 return retry_after_return;
102}
103
104private int
105nwp533_open(gx_device *dev)
106{
107 fprintf(stderr, "in nwp533 open\n");
108
109 if(printer_file < 0)
110 if((printer_file = open("/dev/lbp", O_WRONLY)) < 0)
111 return printer_file;
112
113 return gdev_prn_open(dev);
114}
115
116private int
117nwp533_close(gx_device *dev)
118{
119 fprintf(stderr, "in nwp533 close\n");
120
121 if(printer_file >= 0)
122 {
123 close(printer_file);
124 printer_file = -1;
125 }
126
127 return gdev_prn_close(dev);
128}
129
130/* Send the page to the printer. */
131private int
132nwp533_output_page(gx_device *dev, int num_copies, int flush)
133{
134 fprintf(stderr, "in nwp533 output page [%d, %d]\n",
135 dev->width, dev->height);
136
137 restart:
138 if(ioctl(printer_file, LBIOCSTOP, 0) < 0)
139 {
140 if(analyze_error())
141 goto restart;
142 perror("Waiting for device");
143 return -1;
144 }
145 lseek(printer_file, 0, 0);
146
147 if(write(printer_file, prn_dev->mem.base,
148 (dev->width * dev->height) / 8) !=
149 (dev->width * dev->height) / 8)
150 {
151 perror("Writting to output");
152 return -1;
153 }
154 retry:
155 if(ioctl(printer_file, LBIOCSTART, 0) < 0)
156 {
157 if(analyze_error())
158 goto retry;
159 perror("Starting print");
160 return -1;
161 }
162
163 return 0;
164}