-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparcapt.c
325 lines (287 loc) · 9.86 KB
/
parcapt.c
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
/* Linux Canon LBP800 CAPT driver
* parallel capt backend
* Copyright (c) 2007 Massimo Del Fedele <[email protected]>
*
* Adapted from a printer driver for Samsung ML-85G laser printer
* (C) Copyleft, 2000 Rildo Pragana <[email protected]>
* and from Linux Canon CAPT LBP810 driver
* Copyright (C) 2004 Nicolas Boichat <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <cups/backend.h>
#include "machine.h"
#include "errcapt.h"
#include "pport.h"
#include "llcapt.h"
#include "hlcapt.h"
#include "pkcapt.h"
#include "parcapt.h"
////////////////////////////////////////////////////////////////
// CONTROLLO PRESENZA STAMPANTE NEL PORT SPECIFICATO
BOOL HasLBP800(char *DeviceName)
{
BYTE Status=0;
int Device=0;
// TENTA L' APERTURA DEL DEVICE
Device = Parport_Open(DeviceName); // pport
if (Device == -1){
fprintf(stderr,"parcapt:HasLBP No printer found at %s\n",DeviceName);
return FALSE;
}
// TENTA L' INIZIALIZZAZIONE DELLA STAMPANTE
Status = InitPrinter(Device) ; //hlcapt
// CHIUDE IL DEVICE
Parport_Close(Device);
fprintf(stderr,"parcapt:HasLBP Printer found at %s\n",DeviceName);
// RITORNA 'TRUE' SE HA TROVATO LA STAMPANTE
return (Status == CAPT_PRINTER_OK);
} // END HasLBP800()
////////////////////////////////////////////////////////////////
// STAMPA DELLA PAGINA CORRENTE
BYTE PrintPage(int Device, FILE *CBitmapf)
{
int nBands=0, iBand=0;
int nBandBytes=0;
BYTE **Bands=0;
BYTE *BandPtr=0;
BYTE Status=0;
int dTmp=0;
// LEGGE IL NUMERO DI BANDE
dTmp=fread(&nBands, sizeof(nBands), 1, CBitmapf);
if (dTmp<1){
fprintf(stderr,"parcapt:PrintPage is here. Bad input data\n");
return CAPT_PRINTER_BAD;
}
// ALLOCA IL DESCRITTORE IN MEMORIA
Bands = malloc(nBands * sizeof(BYTE *));
fprintf(stderr,"parcapt:PrintPage is here for %d bands\n",nBands);
// LEGGE LE BANDE DAL DISCO ALLOCANDO LO SPAZIO NECESSARIO
// QUESTO PER POTER SCRIVERE VELOCEMENTE SULLA STAMPANTE SENZA ATTESE
// DI LETTURA DA FILE
for(iBand = 0 ; iBand < nBands; iBand++)
{
int dTmpDummy=fread(&nBandBytes, sizeof(nBandBytes), 1, CBitmapf);
if (dTmpDummy<1){
fprintf(stderr,"parcapt:PrintPage is here. Bad input data. P 100\n");
return CAPT_PRINTER_BAD;
}
Bands[iBand] = malloc(nBandBytes * sizeof(BYTE *) + sizeof(nBandBytes));
BandPtr = Bands[iBand];
memmove(BandPtr, &nBandBytes, sizeof(nBandBytes));
BandPtr += sizeof(nBandBytes);
dTmpDummy=fread(BandPtr, nBandBytes, 1, CBitmapf);
if (dTmpDummy<1){
fprintf(stderr,"parcapt:PrintPage is here. Bad input data. P 200\n");
return CAPT_PRINTER_BAD;
}
}
//
// INIZIO FASE DI STAMPA
if( (Status = StartPrint(Device)) == CAPT_PRINTER_OK)
{
// STAMPA TUTTE LE BANDS
for(iBand = 0 ; iBand < nBands; iBand++)
{
BandPtr = Bands[iBand];
memmove(&nBandBytes, BandPtr, sizeof(nBandBytes));
// fprintf(stderr," parcapt:PrintPage StartData for band %d\n",iBand);
if((Status = StartData(Device, nBandBytes)) != CAPT_PRINTER_OK){
fprintf(stderr,"parcapt:PrintPage Error Can not StartData for band %d\n",iBand);
break;
}
// fprintf(stderr," parcapt:PrintPage SendData for band %d\n",iBand);
if((Status = SendData(Device, BandPtr+sizeof(nBandBytes), nBandBytes, 0x43, 1)) != CAPT_PRINTER_OK){
fprintf(stderr,"parcapt:PrintPage Error Can not SendData for band %d\n",iBand);
break;
}
fprintf(stderr," parcapt:PrintPage EndData for band %d\n",iBand);
if((Status = EndData(Device)) != CAPT_PRINTER_OK){
fprintf(stderr,"parcapt:PrintPage Error Can not EndData for band %d\n",iBand);
break;
}
} // for iBand
}
// LIBERA LA MEMORIA
for(iBand = 0 ; iBand < nBands; iBand++){
free(Bands[iBand]);
}
free(Bands);
// ATTENDE CHE LA STAMPA SIA COMPLETA
fprintf(stderr,"parcapt:PrintPage Page printed. WaitForPageReady \n");
// WaitForPageReady(Device);
Status=WaitCurrentPageIsPrintedBeforToSendNext(Device);
return Status;
} // END PrintPage()
////////////////////////////////////////////////////////////////
// STAMPA DI TUTTE LE PAGINE
BYTE PrintPages(int Device, FILE *CBitmapf)
{
int nPages=0 , iPage=0;
// int dIndex=0;
int Status=CAPT_PRINTER_OK;
int dPaperStatus=CAPT_PRINTER_OK;
// for (dIndex=0;dIndex<10;dIndex++){
// fprintf(stderr,"parcapt:PrintPages looking for status %d\n",dIndex);
// Send_InterBandPacket(Device);
// Send_PaperPacket(Device);
// PrintBuffer();
// sleep(1);
// }
// return Status;
// int dTmpDummy=0;
// LEGGE IL NUMERO DI PAGINE DAL BITMAP
int dTmpDummy=fread(&nPages, sizeof(nPages), 1, CBitmapf);
dTmpDummy=dTmpDummy;
// CICLO DI STAMPA PER TUTTE LE PAGINE
// Status = CAPT_PRINTER_OK;
fprintf(stderr,"parcapt:PrintPages We have to print %d pages \n",nPages+1);
// nPages=1;
for( iPage = 0; iPage < nPages; iPage++)
{
// fprintf(stderr,"parcapt:PrintPages page %d. Is paper Ok?\n",iPage);
dPaperStatus= CheckForPaperBeforeSendNewPageToPrinter(Device);
if (dPaperStatus!=CAPT_PRINTER_OK){
Status=dPaperStatus;
// fprintf(stderr, "NOTICE: No paper to print page %d \n", iPage+1);
// fprintf(stderr, "NOTICE: No paper to print page %d \n", iPage+1);
// fprintf(stderr,"parcapt:PrintPages No paper to print page %d\n",iPage);
break;
}
fprintf(stderr, "NOTICE: Printing page %d of %d \n", iPage+1,nPages);
// STAMPA LA PAGINA CORRENTE
fprintf(stderr,"parcapt:PrintPages Printing page %d\n",iPage);
if((Status = PrintPage(Device, CBitmapf)) != CAPT_PRINTER_OK){
fprintf(stderr,"parcapt:PrintPages Error to printing page %d\n",iPage);
break;
}
// fprintf(stderr, "INFO: Page %d printed\n", iPage+1);
// fprintf(stderr, "NOTICE: total %d-pages \n", iPage+1);
//NOTICE: message
//PAGE: total #-pages
// fprintf(stderr,"parcapt:PrintPages Sleep till next page %d\n",iPage+1);
// sleep(19);
}
// for (dIndex=0;dIndex<60;dIndex++){
// fprintf(stderr,"parcapt:PrintPages looking for status %d\n",dIndex);
// Send_InterBandPacket(Device);
// PrintBuffer();
// sleep(1);
// }
return Status;
} // END PrintBitmaps()
////////////////////////////////////////////////////////////////
// PROGRAMMA PRINCIPALE
int main(int argc, char** argv)
{
int Status=CUPS_BACKEND_FAILED;
int dTimeIndex=0;
int dPaperStatus=CAPT_PRINTER_BAD;
int dByteCiunterForstdin=0;
FILE * fdTmpInputFile=tmpfile(); // input buffer file
// int dPortStatus=CAPT_PRINTER_OK;
// HANDLE AL DEVICE parport(n)
int Device=0;
// NOME DEL FILE DEL DEVICE
char DeviceName[255] = "/dev/parport0";
// SE CHIAMATO SENZA PARAMETRI, STAMPA SU stdout L' ELENCO DELLE STAMPANTI TROVATE
if(argc == 1)
{
char i;
for(i = 0; i < 4; i++)
{
// DeviceName[strlen(DeviceName)-1] = i + '0';
sprintf (DeviceName,"/dev/parport%1d",i);
if(HasLBP800(DeviceName)) // parcapt
printf("direct parcapt:/dev/parport%1d \"CANON-LBP800\" \"CANON-LBP800 on parport%1d\"\n", i, i);
}
return CUPS_BACKEND_OK;
}
// LEGGE IL NOME DEL DEVICE DA argv[0], SE PRESENTE LA STRINGA "parcapt:",
// ALTRIMENTI SI TRATTA DI UNA CHIAMATA DI TEST QUINDI LEGGE IL DEVICE DA argv[1]
if(!strncmp(argv[0], "parcapt:", strlen("parcapt:"))){
strncpy(DeviceName, argv[0]+strlen("parcapt:"), 254);
}
else{
strncpy(DeviceName, argv[1], 254);
}
// APRE IL DEVICE
Device = Parport_Open(DeviceName);
if (Device == -1)
{
fprintf(stderr, "ERROR: ");
fprintf(stderr, ErrorMessages[CAPT_CantOpenDevice], DeviceName);
exit(CUPS_BACKEND_FAILED);
}
// INIZIALIZZA LA STAMPANTE
if((InitPrinter(Device) != CAPT_PRINTER_OK)&& InitPrinter(Device) != CAPT_PRINTER_OK)
{
fprintf(stderr, "ERROR: ");
char * pcErrorMessage=ErrorMessages[CAPT_PrinterNotResponding];
fprintf(stderr, pcErrorMessage,0);
Parport_Close(Device);
exit(CUPS_BACKEND_FAILED);
}
// debug
// WaitCurrentPageIsPrintedBeforToSendNext(Device);
// exit(CUPS_BACKEND_FAILED);
// wait for page before first page
for (dTimeIndex=0;dTimeIndex<120;dTimeIndex++){
// PrintBuffer();
// Send_Packet_a0_a0(Device);
dPaperStatus=CheckForPaperBeforeSendNewPageToPrinter(Device);
if(dPaperStatus==CAPT_PRINTER_OK){
break;
}
Send_PaperPacket(Device);
sleep(1);
}
// exit(CUPS_BACKEND_FAILED);
// Copy input file to tmp buffer
while(!feof(stdin)){
BYTE bTmpByte=0;
int dBytesRead=fread(&bTmpByte,sizeof(bTmpByte),1,stdin);
dBytesRead=fwrite(&bTmpByte,sizeof(bTmpByte),1,fdTmpInputFile);
dByteCiunterForstdin+=dBytesRead;
}
fprintf(stderr, "parcapt:main I had got %d bytes from stdin \n",dByteCiunterForstdin);
// exit(CUPS_BACKEND_FAILED);
// STAMPA LE PAGINE
if(dPaperStatus==CAPT_PRINTER_OK){
if (fdTmpInputFile!=stdin){
rewind(fdTmpInputFile);
}
Status = PrintPages(Device, fdTmpInputFile);
}
// CHIUDE IL DEVICE
Parport_Close(Device);
// METTE LO STATUS IN FORMATO CUPS
// E SEGNALA AL FILTRO CHE LA STAMPA E' FINITA
if(Status == CAPT_PRINTER_OK)
{
Status = CUPS_BACKEND_OK;
fprintf(stderr, "NOTICE: Printed.\n");
}
else
{
Status = CUPS_BACKEND_FAILED;
}
// RITORNA LO STATO
return Status;
}