home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of A1200
/
World_Of_A1200.iso
/
datafiles
/
text
/
c_manual
/
devices
/
printerdevice
/
example3.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-02-27
|
16KB
|
438 lines
/***********************************************************/
/* */
/* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
/* ------------------------------- ------------------ */
/* */
/* Book: ACM Devices Amiga C Club */
/* Chapter: Printer Device Tulevagen 22 */
/* File: Example3.c 181 41 LIDINGO */
/* Author: Anders Bjerin SWEDEN */
/* Date: 92-04-27 */
/* Version: 1.00 */
/* */
/* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
/* */
/* Registered members may use this program freely in their */
/* own commercial/noncommercial programs/articles. */
/* */
/***********************************************************/
/* This program demonstrates how you can send printer commands to */
/* the Printer Device, which will translate these commands with */
/* help of Preferences, before they are sent to the printer. */
#include <exec/types.h> /* Data types. */
#include <exec/errors.h> /* Exec error messages. */
#include <devices/printer.h> /* Printer Device. */
#include <exec/io.h> /* Standard request block. */
/* Declare how the printer request block look like: */
union printerIO
{
struct IOStdReq ios;
struct IODRPReq iodrp;
struct IOPrtCmdReq iopc;
};
/* Declare a pointer to our reply port: */
struct MsgPort *replymp = NULL;
/* Declare a pointer our printer request block: */
union printerIO *printer_req = NULL;
/* Store the printer device error here: */
UWORD printer_dever = TRUE;
/* Declare our data buffer: (12 characters + NULL sign) */
BYTE buffer[] = "Amiga C Club";
/* Declare our functions: */
/* Our main function: */
void main();
/* Clears and removes everything nice and neatly: */
void clean_up( BYTE error, STRPTR text );
/* Prints some information about the error: */
void PrtError( BYTE error );
/* Sends characters (which are translated) to the printer: */
BYTE PrintText(
union printerIO *ioreq,
BYTE *data,
ULONG length
);
/* Sends raw (untranslated) characters to the printer: */
BYTE PrintRaw(
union printerIO *ioreq,
BYTE *data,
ULONG length
);
/* Sends printer commands with up to four optional parameters: */
BYTE PrinterCommand(
union printerIO *ioreq,
UWORD command,
UBYTE par0,
UBYTE par1,
UBYTE par2,
UBYTE par3
);
void main()
{
/* Error number: */
BYTE error;
/* Get a reply port: (No name, priority 0) */
replymp = (struct MsgPort *)
CreatePort( NULL, 0 );
if( !replymp )
clean_up( 0, "Could not create the reply port!" );
/* Create the printer request block: */
printer_req = (union printerIO *)
CreateExtIO( replymp, sizeof(union printerIO) );
if( !printer_req )
clean_up( 0, "Not enough memory for the printer request block!" );
/* Open the Printer Device: */
printer_dever = OpenDevice( "printer.device", 0, printer_req, 0 );
if( printer_dever )
clean_up( 0, "Could not open the Printer Device!" );
/* Set a line feed (LF) at the end of our buffer: */
buffer[12]=10; /* 10 = LF (ASCII) */
/* Send a printer command: [Underline On] */
PrinterCommand
(
printer_req, /* Pointer to the printer request block. */
aSGR4, /* The printer command: "underline On". */
0, /* Parameter 1, Not used. */
0, /* Parameter 2, Not used. */
0, /* Parameter 3, Not used. */
0 /* Parameter 4, Not used. */
);
/* Send some text to the printer: (Will be translated) */
error = PrintText( printer_req, buffer, 13 );
if( error )
printf( "Problems while printing...\n" );
/* Send a printer command: [Italics On] */
PrinterCommand
(
printer_req, /* Pointer to the printer request block. */
aSGR3, /* The printer command: "italics On". */
0, /* Parameter 1, Not used. */
0, /* Parameter 2, Not used. */
0, /* Parameter 3, Not used. */
0 /* Parameter 4, Not used. */
);
/* Send some text to the printer: (Will be translated) */
error = PrintText( printer_req, buffer, 13 );
if( error )
printf( "Problems while printing...\n" );
/* Send a printer command: [Underline Off] */
PrinterCommand
(
printer_req, /* Pointer to the printer request block. */
aSGR24, /* The printer command: "underline Off". */
0, /* Parameter 1, Not used. */
0, /* Parameter 2, Not used. */
0, /* Parameter 3, Not used. */
0 /* Parameter 4, Not used. */
);
/* Send a printer command: [Italics Off] */
PrinterCommand
(
printer_req, /* Pointer to the printer request block. */
aSGR23, /* The printer command: "italics Off". */
0, /* Parameter 1, Not used. */
0, /* Parameter 2, Not used. */
0, /* Parameter 3, Not used. */
0 /* Parameter 4, Not used. */
);
/* Send a printer command: */
/* [Left margin: 30] */
/* [Right margin: 70] */
PrinterCommand
(
printer_req, /* Pointer to the printer request block. */
aSLRM, /* The printer command: "Set Left Right Margins. */
30, /* Parameter 1, Set left margin to 30. */
70, /* Parameter 2, Set right margin to 70. */
0, /* Parameter 3, Not used. */
0 /* Parameter 4, Not used. */
);
/* Send some text to the printer: (Will be translated) */
error = PrintText( printer_req, buffer, 12 );
if( error )
printf( "Problems while printing...\n" );
/* Clean up and quit: */
clean_up( 0, "The End!" );
}
/* Close and return everything that has been */
/* opened and allocated before we quit: */
void clean_up( BYTE error, STRPTR text )
{
/* Print some information about the problem: */
if( error )
PrtError( error );
/* Close the Printer Device: */
if( !printer_dever )
CloseDevice( printer_req );
/* Deallocate the printer request block: */
if( printer_req )
DeleteExtIO( printer_req, sizeof(union printerIO) );
/* Remove the replyport: */
if( replymp )
DeletePort( replymp);
/* Print the message: */
printf( "\n%s\n", text );
/* Quit: */
exit( 0 );
}
/* PrtError() tells the user what went wrong. You give it the error code */
/* you received, and PrtError() will print a short description of the */
/* problem. Useful when debugging. (Printer errors) */
/* */
/* Synopsis: PrtError( error ); */
/* */
/* error: (BYTE) The error value you want to have explained. */
void PrtError( BYTE error )
{
switch( error )
{
/* EXEC error messages: (defined in "exec/errors.h") */
case IOERR_OPENFAIL:
printf( "Could not open the device!\n" );
break;
case IOERR_ABORTED:
printf( "The request was aborted!\n" );
break;
case IOERR_NOCMD:
printf( "Unknown Command!\n" );
break;
case IOERR_BADLENGTH:
printf( "Bad length of the command - data!\n" );
/* Printer Device errors: (defined in "devices/printer.h") */
case PDERR_CANCEL:
printf( "User cancelled the request!\n" );
break;
case PDERR_NOTGRAPHICS:
printf( "The printer does not support graphics!\n" );
break;
case PDERR_BADDIMENSION:
printf( "The printer dimension is not valid!\n" );
break;
case PDERR_INTERNALMEMORY:
printf( "Not enough memory for the internal printer functions!\n" );
break;
case PDERR_BUFFERMEMORY:
printf( "Not enough memory for the print buffer!\n" );
break;
default:
printf( "An unknown error was reported! Error nr: %d\n", error );
}
}
/* PrintText() sends characters (which will be translated) to the */
/* printer. Since the printer device will use the Preference's */
/* settings, it will know to which port (parallel or serial) the */
/* printer is connected to, what type of printer it is, and what */
/* special settings (margins, density, quality mode etc) the user */
/* have defined. */
/* */
/* Note! All characters which are sent with this function may be */
/* translated by Preferences before it is passed on to the */
/* printer. */
/* */
/* Synopsis: error = PrintText( io, data, length ); */
/* */
/* error: (BYTE) PrintWrite() returns 0 if everything was OK, */
/* else an error number is returned. */
/* */
/* io: (union printerIO *) Pointer to a printer request */
/* block. */
/* */
/* data: (BYTE *) Pointer to the first character that should */
/* be printed. */
/* */
/* length (ULONG) How many characters (bytes) you want to send */
/* to the printer. */
BYTE PrintText(
union printerIO *ioreq, /* Pointer to the printer request block. */
BYTE *data, /* Pointer to the data which should be printed. */
ULONG length /* How many characters (bytes) should be printed. */
)
{
/* We want to print some text: (send data to PRT:) */
ioreq->ios.io_Command = CMD_WRITE;
/* Give the start address of our data: */
ioreq->ios.io_Data = (APTR) data;
/* Set number of chracters that should be printed: */
ioreq->ios.io_Length = length;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: (This is a task sleep.) */
return( (BYTE) DoIO( ioreq ) );
}
/* PrintRaw() sends untranslated characters to the printer. Note */
/* that this is usually not a very good idea. Since the characters */
/* will not be translated by Preferences, you can not be sure that */
/* the characters you send will be the same when printed. */
/* */
/* Synopsis: error = PrintRaw( io, data, length ); */
/* */
/* error: (BYTE) PrintRaw() returns 0 if everything was OK, */
/* else an error number is returned. */
/* */
/* io: (union printerIO *) Pointer to a printer request */
/* block. */
/* */
/* data: (BYTE *) Pointer to the first character that should */
/* be printed. */
/* */
/* length (ULONG) How many characters (bytes) you want to send */
/* to the printer. */
BYTE PrintRaw(
union printerIO *ioreq, /* Pointer to the printer request block. */
BYTE *data, /* Pointer to the data which should be printed. */
ULONG length /* How many characters (bytes) should be printed. */
)
{
/* We want to print some raw (untranslated) text: */
ioreq->ios.io_Command = PRD_RAWWRITE;
/* Give the start address of our data: */
ioreq->ios.io_Data = (APTR) data;
/* Set number of chracters that should be printed: */
ioreq->ios.io_Length = length;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: (This is a task sleep.) */
return( (BYTE) DoIO( ioreq ) );
}
/* PrinterCommand() sends a printer command with up to four optional */
/* arguments. See this chapter docummentation for more information about */
/* printer commands. */
/* */
/* Synopsis: error = PrinterCommand( io, command, par1, par2, par3, par 4 ); */
/* */
/* error: (BYTE) If the command could not be sent to the printer an */
/* error number is returned. If everything was OK, NULL is */
/* returned. */
/* */
/* io: (union printerIO *) Pointer to a printer request block. */
/* */
/* command: (UWORD) The command that should be sent to the printer. If the */
/* command should be followed by one or more parameters, set these */
/* parameters in the following fields: */
/* */
/* par1: (UBYTE) The first parameter. */
/* */
/* par2: (UBYTE) The second parameter. */
/* */
/* par3: (UBYTE) The third parameter. */
/* */
/* par4: (UBYTE) The fourth parameter. */
BYTE PrinterCommand(
union printerIO *ioreq, /* Pointer to the printer request block. */
UWORD command, /* The printer command. */
UBYTE par0, /* The first parameter. */
UBYTE par1, /* The second parameter. */
UBYTE par2, /* The third parameter. */
UBYTE par3 /* The fourth parameter. */
)
{
/* We want to send a printer command to the printer: */
ioreq->iopc.io_Command = PRD_PRTCOMMAND;
/* Set the printer command: */
ioreq->iopc.io_PrtCommand = command;
/* Set the parameters: */
ioreq->iopc.io_Parm0 = par0;
ioreq->iopc.io_Parm1 = par1;
ioreq->iopc.io_Parm2 = par2;
ioreq->iopc.io_Parm3 = par3;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: (This is a task sleep.) */
return( (BYTE) DoIO( ioreq ) );
}