(*********************************************************************
 *                                                                   *
 *  :Program.    PrinterSupport.mod                                  *
 *  :Author.     Michael Frieß                                       *
 *  :Address.    Kernerstr. 22a                                      *
 *  :Address.    7000 Stuttgart 1                                    *
 *  :shortcut.   [mif]                                               *
 *  :Version.    2.01                                                *
 *  .Modified.   [gs] Anpassung an m2c 3.2d                          *
 *  :Date.       12.10.89                                            *
 *  :Copyright.  PD                                                  *
 *  :Language.   Modula-II                                           *
 *  :Translator. M2Amiga                                             *
 *  :Contents.   PrinterSupport                                      *
 *  :update.     update from V1.0 of Fridtjof Siebert [fbs]          *
 *                                                                   *
 *********************************************************************)

IMPLEMENTATION MODULE PrinterSupport;
(* (C) Copyright 1988 by Michael Frieß & Fridtjof Siebert *)

FROM Arts        IMPORT TermProcedure, Assert;
FROM SYSTEM      IMPORT ADR, LONGSET, CAST;
FROM Exec        IMPORT
        IOStdReq, MsgPortPtr, OpenDevice, CloseDevice,
        write, DoIO, UByte;
FROM ExecSupport IMPORT
        CreatePort, DeletePort,
        CreateExtIO, DeleteExtIO;
FROM Graphics    IMPORT RastPortPtr, ColorMapPtr, ViewModeSet;
FROM Printer     IMPORT
        IODRPReq, IOPrinter, ris, dumpRPort, Special, SpecialSet,
        printerName, rawWrite, prtCommand;

CONST copyright =
"MODULE PrinterSupport: (C) Copyright 1988 by M.Frieß & F.Siebert";

TYPE IOMode = (Standard, Dump, Command);
     printerIO = RECORD
                  CASE      : IOMode OF
                   Standard :
                     std    : IOStdReq |
                   Dump     :
                     dmp    : IODRPReq |
                   Command  :
                     cmd    : IOPrinter;
                  END;
                 END;

VAR PrtPort : MsgPortPtr;
    PrtMsg  : POINTER TO printerIO;
    PrtInit : BOOLEAN;


PROCEDURE ClosePrinter;
BEGIN
 IF PrtInit THEN          (* Bitte nur einmal schließen ! *)
  CloseDevice (PrtMsg);
  DeleteExtIO (PrtMsg);
  DeletePort (PrtPort);
  PrtInit := FALSE;
 END;
END ClosePrinter;


PROCEDURE OpenPrinter;
BEGIN
  PrtPort := CreatePort (ADR("PrinterSupport"),0);
  Assert (PrtPort # NIL, ADR("MODUL PrinterSupport: cannot create PrtPort"));
  PrtMsg  := CreateExtIO (PrtPort, SIZE(printerIO));
  IF PrtMsg = NIL THEN
    DeletePort (PrtPort);
    Assert (FALSE, ADR("MODUL PrinterSupport: cannot create ExtIO"));
  END;
  WITH PrtMsg^.cmd DO
   command    := prtCommand;
   prtCommand := ris;
   parm0      := 0;
   parm1      := 0;
   parm2      := 0;
   parm3      := 0
  END;
  OpenDevice (ADR (printerName), 0, PrtMsg, LONGSET{0});
  TermProcedure (ClosePrinter);
  PrtInit := TRUE
END OpenPrinter;


PROCEDURE PrintString (s : ARRAY OF CHAR);
BEGIN
  WITH PrtMsg^.std DO
   command := write;
   data    := ADR(s);
   length  := HIGH(s) + 1
  END;
  DoIO (PrtMsg)
END PrintString;


PROCEDURE PrintRaw (s : ARRAY OF CHAR);
BEGIN
  WITH PrtMsg^.std DO
   command := rawWrite;
   data    := ADR(s);
   length  := HIGH(s) + 1
  END;
  DoIO (PrtMsg)
END PrintRaw;


PROCEDURE PrintChar (c : CHAR);
BEGIN
  WITH PrtMsg^.std DO
   command := rawWrite;
   data    := ADR(c);
   length  := 1
  END;
  DoIO (PrtMsg)
END PrintChar;


PROCEDURE PrintCommand (Cmd : CARDINAL;
                        p0, p1, p2, p3 : UByte);
BEGIN
  PrtMsg^.cmd.command    := prtCommand;
  WITH PrtMsg^.cmd DO
   prtCommand := Cmd;
   parm0      := p0;
   parm1      := p1;
   parm2      := p2;
   parm3      := p3
  END;
  DoIO (PrtMsg)
END PrintCommand;


PROCEDURE DumpRPort (rp: RastPortPtr; cm: ColorMapPtr;
                    Modes: ViewModeSet; x,y,w,h: CARDINAL;
                    c,r: LONGINT; s: SpecialSet);
(* Procedure is still not tested! *)

BEGIN
  WITH PrtMsg^.dmp DO
    command := dumpRPort;
    rastPort := rp;
    colorMap := cm;
    (* next line looks a bit weird, but I found no other way to do this: *)
    modes     := Modes;
    srcX      := x;
    srcY      := y;
    srcWidth  := w;
    srcHeight := h;
    destCols  := c;
    destRows  := r;
    special   := s;
  END;
  DoIO (PrtMsg);
END DumpRPort;

BEGIN
 PrtInit := FALSE;
END PrinterSupport.
