|
|
|
@ -3,9 +3,9 @@
|
|
|
|
|
#include <cups/cups.h>
|
|
|
|
|
#include <cups/ppd.h>
|
|
|
|
|
#include <cups/raster.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
// uncomment next line in order to have verbose dump in DEBUGFILE
|
|
|
|
|
// after print
|
|
|
|
@ -14,8 +14,7 @@
|
|
|
|
|
|
|
|
|
|
#define DEBUGFILE "/tmp/debugraster.txt"
|
|
|
|
|
|
|
|
|
|
struct settings_
|
|
|
|
|
{
|
|
|
|
|
struct settings_ {
|
|
|
|
|
int cashDrawer1;
|
|
|
|
|
int cashDrawer2;
|
|
|
|
|
int blankSpace;
|
|
|
|
@ -23,32 +22,30 @@ struct settings_
|
|
|
|
|
};
|
|
|
|
|
struct settings_ settings;
|
|
|
|
|
|
|
|
|
|
struct command
|
|
|
|
|
{
|
|
|
|
|
struct command {
|
|
|
|
|
int length;
|
|
|
|
|
char *command;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// define printer initialize command
|
|
|
|
|
static const struct command printerInitializeCommand =
|
|
|
|
|
{2,(char[2]){0x1b,0x40}};
|
|
|
|
|
static const struct command printerInitializeCommand = {2,
|
|
|
|
|
(char[2]){0x1b, 0x40}};
|
|
|
|
|
|
|
|
|
|
// define cashDrawerEjector command
|
|
|
|
|
static const struct command cashDrawerEject [2] =
|
|
|
|
|
{{5,(char[5]){0x1b,0x70,0,0x40,0x50}},
|
|
|
|
|
static const struct command cashDrawerEject[2] = {
|
|
|
|
|
{5, (char[5]){0x1b, 0x70, 0, 0x40, 0x50}},
|
|
|
|
|
{5, (char[5]){0x1b, 0x70, 1, 0x40, 0x50}}};
|
|
|
|
|
|
|
|
|
|
// define raster mode start command
|
|
|
|
|
static const struct command rasterModeStartCommand =
|
|
|
|
|
{4,(char[4]){0x1d,0x76,0x30,0}};
|
|
|
|
|
static const struct command rasterModeStartCommand = {
|
|
|
|
|
4, (char[4]){0x1d, 0x76, 0x30, 0}};
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUGP
|
|
|
|
|
FILE *lfd = 0;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// putchar with debug wrapper
|
|
|
|
|
inline void mputchar(char c)
|
|
|
|
|
{
|
|
|
|
|
inline void mputchar(char c) {
|
|
|
|
|
unsigned char m = c;
|
|
|
|
|
#ifdef DEBUGP
|
|
|
|
|
if (lfd)
|
|
|
|
@ -58,32 +55,23 @@ inline void mputchar(char c)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// procedure to output an array
|
|
|
|
|
inline void outputarray(const char * array, int length)
|
|
|
|
|
{
|
|
|
|
|
inline void outputarray(const char *array, int length) {
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (; i < length; ++i)
|
|
|
|
|
mputchar(array[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// output a command
|
|
|
|
|
inline void outputCommand(struct command output)
|
|
|
|
|
{
|
|
|
|
|
inline void outputCommand(struct command output) {
|
|
|
|
|
outputarray(output.command, output.length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline int lo(int val)
|
|
|
|
|
{
|
|
|
|
|
return val & 0xFF;
|
|
|
|
|
}
|
|
|
|
|
inline int lo(int val) { return val & 0xFF; }
|
|
|
|
|
|
|
|
|
|
inline int hi (int val)
|
|
|
|
|
{
|
|
|
|
|
return lo (val>>8);
|
|
|
|
|
}
|
|
|
|
|
inline int hi(int val) { return lo(val >> 8); }
|
|
|
|
|
|
|
|
|
|
// enter raster mode and set up x and y dimensions
|
|
|
|
|
inline void rasterheader(int xsize, int ysize)
|
|
|
|
|
{
|
|
|
|
|
inline void rasterheader(int xsize, int ysize) {
|
|
|
|
|
outputCommand(rasterModeStartCommand);
|
|
|
|
|
mputchar(lo(xsize));
|
|
|
|
|
mputchar(hi(xsize));
|
|
|
|
@ -93,33 +81,30 @@ inline void rasterheader(int xsize, int ysize)
|
|
|
|
|
|
|
|
|
|
// print all unprinted (i.e. flush the buffer)
|
|
|
|
|
// then feed given number of lines
|
|
|
|
|
inline void skiplines(int size)
|
|
|
|
|
{
|
|
|
|
|
inline void skiplines(int size) {
|
|
|
|
|
mputchar(0x1b);
|
|
|
|
|
mputchar(0x4a);
|
|
|
|
|
mputchar(size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get an option
|
|
|
|
|
inline int getOptionChoiceIndex(const char * choiceName, ppd_file_t * ppd)
|
|
|
|
|
{
|
|
|
|
|
inline int getOptionChoiceIndex(const char *choiceName, ppd_file_t *ppd) {
|
|
|
|
|
ppd_choice_t *choice;
|
|
|
|
|
ppd_option_t *option;
|
|
|
|
|
|
|
|
|
|
choice = ppdFindMarkedChoice(ppd, choiceName);
|
|
|
|
|
|
|
|
|
|
if (choice == NULL)
|
|
|
|
|
{
|
|
|
|
|
if ((option = ppdFindOption(ppd, choiceName)) == NULL) return -1;
|
|
|
|
|
if ((choice = ppdFindChoice(option,option->defchoice)) == NULL) return -1;
|
|
|
|
|
if (choice == NULL) {
|
|
|
|
|
if ((option = ppdFindOption(ppd, choiceName)) == NULL)
|
|
|
|
|
return -1;
|
|
|
|
|
if ((choice = ppdFindChoice(option, option->defchoice)) == NULL)
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return atoi(choice->choice);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void initializeSettings(char * commandLineOptionSettings)
|
|
|
|
|
{
|
|
|
|
|
void initializeSettings(char *commandLineOptionSettings) {
|
|
|
|
|
ppd_file_t *ppd = NULL;
|
|
|
|
|
cups_option_t *options = NULL;
|
|
|
|
|
int numOptions = 0;
|
|
|
|
@ -129,8 +114,7 @@ void initializeSettings(char * commandLineOptionSettings)
|
|
|
|
|
ppdMarkDefaults(ppd);
|
|
|
|
|
|
|
|
|
|
numOptions = cupsParseOptions(commandLineOptionSettings, 0, &options);
|
|
|
|
|
if ((numOptions != 0) && (options != NULL))
|
|
|
|
|
{
|
|
|
|
|
if ((numOptions != 0) && (options != NULL)) {
|
|
|
|
|
cupsMarkOptions(ppd, numOptions, options);
|
|
|
|
|
cupsFreeOptions(numOptions, options);
|
|
|
|
|
}
|
|
|
|
@ -146,8 +130,7 @@ void initializeSettings(char * commandLineOptionSettings)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sent on the beginning of print job
|
|
|
|
|
void jobSetup()
|
|
|
|
|
{
|
|
|
|
|
void jobSetup() {
|
|
|
|
|
if (settings.cashDrawer1 == 1)
|
|
|
|
|
outputCommand(cashDrawerEject[0]);
|
|
|
|
|
if (settings.cashDrawer2 == 1)
|
|
|
|
@ -156,8 +139,7 @@ void jobSetup()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sent at the very end of print job
|
|
|
|
|
void ShutDown()
|
|
|
|
|
{
|
|
|
|
|
void ShutDown() {
|
|
|
|
|
if (settings.cashDrawer1 == 2)
|
|
|
|
|
outputCommand(cashDrawerEject[0]);
|
|
|
|
|
if (settings.cashDrawer2 == 2)
|
|
|
|
@ -171,8 +153,7 @@ typedef void (*__sighandler_t) (int);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
__sighandler_t old_signal;
|
|
|
|
|
void EndPage()
|
|
|
|
|
{
|
|
|
|
|
void EndPage() {
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < settings.feedDist; ++i)
|
|
|
|
|
skiplines(0x18);
|
|
|
|
@ -180,8 +161,7 @@ void EndPage()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sent on job canceling
|
|
|
|
|
void cancelJob(int foo)
|
|
|
|
|
{
|
|
|
|
|
void cancelJob(int foo) {
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (; i < 0x258; ++i)
|
|
|
|
|
mputchar(0);
|
|
|
|
@ -190,15 +170,11 @@ void cancelJob(int foo)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// invoked before starting to print a page
|
|
|
|
|
void pageSetup()
|
|
|
|
|
{
|
|
|
|
|
old_signal = signal(15,cancelJob);
|
|
|
|
|
}
|
|
|
|
|
void pageSetup() { old_signal = signal(15, cancelJob); }
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
int fd = 0; // File descriptor providing CUPS raster data
|
|
|
|
|
cups_raster_t *ras = NULL; // Raster stream for printing
|
|
|
|
|
cups_page_header2_t header; // CUPS Page header
|
|
|
|
@ -206,18 +182,17 @@ int main(int argc, char *argv[])
|
|
|
|
|
int y = 0; // Vertical position in page 0 <= y <= header.cupsHeight
|
|
|
|
|
|
|
|
|
|
unsigned char *rasterData = NULL; // Pointer to raster data from CUPS
|
|
|
|
|
unsigned char * originalRasterDataPtr = NULL; // Copy of original pointer for freeing buffer
|
|
|
|
|
unsigned char *originalRasterDataPtr =
|
|
|
|
|
NULL; // Copy of original pointer for freeing buffer
|
|
|
|
|
|
|
|
|
|
if (argc < 6 || argc > 7)
|
|
|
|
|
{
|
|
|
|
|
fputs("ERROR: rastertozj job-id user title copies options [file]\n", stderr);
|
|
|
|
|
if (argc < 6 || argc > 7) {
|
|
|
|
|
fputs("ERROR: rastertozj job-id user title copies options [file]\n",
|
|
|
|
|
stderr);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (argc == 7)
|
|
|
|
|
{
|
|
|
|
|
if ((fd = open(argv[6], O_RDONLY)) == -1)
|
|
|
|
|
{
|
|
|
|
|
if (argc == 7) {
|
|
|
|
|
if ((fd = open(argv[6], O_RDONLY)) == -1) {
|
|
|
|
|
perror("ERROR: Unable to open raster file - ");
|
|
|
|
|
sleep(1);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
@ -234,26 +209,25 @@ int main(int argc, char *argv[])
|
|
|
|
|
ras = cupsRasterOpen(fd, CUPS_RASTER_READ);
|
|
|
|
|
page = 0;
|
|
|
|
|
|
|
|
|
|
while (cupsRasterReadHeader2(ras, &header))
|
|
|
|
|
{
|
|
|
|
|
while (cupsRasterReadHeader2(ras, &header)) {
|
|
|
|
|
if ((header.cupsHeight == 0) || (header.cupsBytesPerLine == 0))
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUGP
|
|
|
|
|
if (lfd) fprintf(lfd,"\nheader.cupsHeight=%d, header.cupsBytesPerLine=%d\n",header.cupsHeight,header.cupsBytesPerLine);
|
|
|
|
|
if (lfd)
|
|
|
|
|
fprintf(lfd, "\nheader.cupsHeight=%d, header.cupsBytesPerLine=%d\n",
|
|
|
|
|
header.cupsHeight, header.cupsBytesPerLine);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (rasterData == NULL)
|
|
|
|
|
{
|
|
|
|
|
if (rasterData == NULL) {
|
|
|
|
|
rasterData = malloc(header.cupsBytesPerLine * 24);
|
|
|
|
|
if (rasterData == NULL)
|
|
|
|
|
{
|
|
|
|
|
if (originalRasterDataPtr != NULL) free(originalRasterDataPtr);
|
|
|
|
|
if (rasterData == NULL) {
|
|
|
|
|
if (originalRasterDataPtr != NULL)
|
|
|
|
|
free(originalRasterDataPtr);
|
|
|
|
|
cupsRasterClose(ras);
|
|
|
|
|
if (fd != 0)
|
|
|
|
|
close(fd);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
originalRasterDataPtr = rasterData; // used to later free the memory
|
|
|
|
|
}
|
|
|
|
@ -266,37 +240,40 @@ int main(int argc, char *argv[])
|
|
|
|
|
int width_size = foo >> 3;
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUGP
|
|
|
|
|
if (lfd) fprintf(lfd,"\nheader.cupsWidth=%d, foo=%d, width_size=%d\n",header.cupsWidth,foo,width_size);
|
|
|
|
|
if (lfd)
|
|
|
|
|
fprintf(lfd, "\nheader.cupsWidth=%d, foo=%d, width_size=%d\n",
|
|
|
|
|
header.cupsWidth, foo, width_size);
|
|
|
|
|
#endif
|
|
|
|
|
y = 0;
|
|
|
|
|
int zeroy = 0;
|
|
|
|
|
|
|
|
|
|
while ( y < header.cupsHeight )
|
|
|
|
|
{
|
|
|
|
|
while (y < header.cupsHeight) {
|
|
|
|
|
if ((y & 127) != 0)
|
|
|
|
|
fprintf(stderr, "INFO: Printing page %d, %d%% complete...\n", page, (100 * y / header.cupsHeight));
|
|
|
|
|
fprintf(stderr, "INFO: Printing page %d, %d%% complete...\n", page,
|
|
|
|
|
(100 * y / header.cupsHeight));
|
|
|
|
|
|
|
|
|
|
int rest = header.cupsHeight - y;
|
|
|
|
|
if (rest > 24)
|
|
|
|
|
rest = 24;
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUGP
|
|
|
|
|
if (lfd) fprintf(lfd,"\nProcessing block of %d, starting from %d lines\n",rest,y);
|
|
|
|
|
if (lfd)
|
|
|
|
|
fprintf(lfd, "\nProcessing block of %d, starting from %d lines\n", rest,
|
|
|
|
|
y);
|
|
|
|
|
#endif
|
|
|
|
|
y += rest;
|
|
|
|
|
|
|
|
|
|
if (y)
|
|
|
|
|
{
|
|
|
|
|
if (y) {
|
|
|
|
|
unsigned char *buf = rasterData;
|
|
|
|
|
int j;
|
|
|
|
|
for ( j=0; j<rest; ++j)
|
|
|
|
|
{
|
|
|
|
|
for (j = 0; j < rest; ++j) {
|
|
|
|
|
if (!cupsRasterReadPixels(ras, buf, header.cupsBytesPerLine))
|
|
|
|
|
break;
|
|
|
|
|
buf += width_size;
|
|
|
|
|
}
|
|
|
|
|
#ifdef DEBUGP
|
|
|
|
|
if (lfd) fprintf(lfd,"\nReaded %d lines\n",j);
|
|
|
|
|
if (lfd)
|
|
|
|
|
fprintf(lfd, "\nReaded %d lines\n", j);
|
|
|
|
|
#endif
|
|
|
|
|
if (j < rest)
|
|
|
|
|
continue;
|
|
|
|
@ -308,11 +285,11 @@ int main(int argc, char *argv[])
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUGP
|
|
|
|
|
if (lfd) fprintf(lfd,"\nChecked %d bytes of %d for zero\n",j,rest_bytes);
|
|
|
|
|
if (lfd)
|
|
|
|
|
fprintf(lfd, "\nChecked %d bytes of %d for zero\n", j, rest_bytes);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (j>=rest_bytes)
|
|
|
|
|
{
|
|
|
|
|
if (j >= rest_bytes) {
|
|
|
|
|
++zeroy;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
@ -334,7 +311,8 @@ int main(int argc, char *argv[])
|
|
|
|
|
|
|
|
|
|
ShutDown();
|
|
|
|
|
|
|
|
|
|
if (originalRasterDataPtr != NULL) free(originalRasterDataPtr);
|
|
|
|
|
if (originalRasterDataPtr != NULL)
|
|
|
|
|
free(originalRasterDataPtr);
|
|
|
|
|
cupsRasterClose(ras);
|
|
|
|
|
if (fd != 0)
|
|
|
|
|
close(fd);
|
|
|
|
|