
/*
 *  RENDER.C
 *
 *  David Berezowski - March/88.
 *  Modified for DICE - May/91	Matthew Dillon
 *  Modified for ElCheapoFax April 1993 Olaf 'Rhialto' Seibert.
 *
 *  Copyright (c) 1988  Commodore-Amiga, Inc.
 *  (c)Copyright 1991 Matthew Dillon
 *  (c)Copyright 1993 Olaf Seibert
 */

#include <string.h>
#include "defs.h"

Prototype __geta4 long Render(long, long, long, long);
Prototype void Transfer(PrtInfo *, UWORD, UBYTE *);
Prototype void *OpenFaxFile(void);

int NoFormFeed;

/*__geta4*/ long
Render(ct, x, y, status)
long ct, x, y, status;
{
    static long LineBufSize, TotalBufSize;
    static void *FaxHandle;
    static int XSize;

    long err;

    err = PDERR_NOERR;

    /*debug(("Render ct %08lx x %d y %d status %d\n", ct, x, y, status));*/
    switch(status) {
    case 5: /* Pre-Master Initialization */
	/*
	 *	ct	- 0 or pointer to IODRPReq structure.
	 *	x	- io_Special flag from IODRPReq.
	 *	y	- 0.
	 */
	debug(("Pre-master init\n"));
	/* select density */
	SetDensity(x & SPECIAL_DENSITYMASK);
	PED->ped_NumRows = 16;
	break;
    case 0:	/* Master Initialization */
	/*
	 *	ct	- pointer to IODRPReq structure.
	 *	x	- width of printed picture in pixels.
	 *	y	- height of printed picture in pixels.
	 */
	debug(("Master init\n"));
	XSize = x;
	if (XSize != LINE_BITS) {
	    XSize = LINE_BITS;
	    debug(("XSize adjusted from %d to %d\n", x, XSize));
	}
	NoFormFeed = (((struct IODRPReq *)ct)->io_Special & SPECIAL_NOFORMFEED)
		     != 0;
	PD->pd_PrintBuf = NULL;
	if ((FaxHandle = OpenFaxFile()) == NULL) {
	    err = PDERR_BUFFERMEMORY;
	    break;
	}
	LineBufSize = (XSize + 7) / 8;
	TotalBufSize = PED->ped_NumRows * LineBufSize;
	PD->pd_PrintBuf = AllocMem(TotalBufSize, MEMF_PUBLIC);
	if (PD->pd_PrintBuf == NULL) {
	    faxout_close(FaxHandle);
	    FaxHandle = NULL;
	    err = PDERR_BUFFERMEMORY;
	    break;
	}
	faxout_begin_page(FaxHandle, PED->ped_YDotsInch > Y_DPI/2);
	break;
    case 1: /* Scale, Dither and Render */
	    /*
	     *	    ct	    - pointer to PrtInfo structure.
	     *	    x	    - 0.
	     *	    y	    - row # (0 to Height - 1).
	     */
#define pi ((struct PrtInfo *)ct)
	if (pi->pi_xpos >= XSize) {
	    debug(("Invalid pi_xpos %d (%x)\n", pi->pi_xpos, pi->pi_xpos));
	    pi->pi_xpos = 0;
	}
	Transfer((struct PrtInfo *)ct, y, PD->pd_PrintBuf + LineBufSize * (y % PED->ped_NumRows));
	break;
    case 2: /* Dump Buffer to Printer */
	/*
	 *	ct	- 0.
	 *	x	- 0.
	 *	y	- # of rows sent (1 to ped_NumRows).
	 */
	{
	    int 	    offset;

	    debug(("Call tofax %dx\n", y));
	    for (offset = 0; y > 0; offset += LineBufSize, y--) {
		tofax(FaxHandle, PD->pd_PrintBuf + offset, XSize);
	    }
	}
	break;
    case 3:	/* Clear and Init Buffer */
	/*
	 *	ct	- 0.
	 *	x	- 0.
	 *	y	- 0.
	 */
	memset(PD->pd_PrintBuf, 0, TotalBufSize);
	break;
    case 4: /* Close Down */
	/*
	 *	ct	- error code.
	 *	x	- io_Special flag from IODRPReq.
	 *	y	- 0.
	 */

	debug(("Close down\n"));
	if (FaxHandle) {
	    faxout_end_page(FaxHandle);
	    faxout_close(FaxHandle);
	    FaxHandle = NULL;
	}
	if (PD->pd_PrintBuf != NULL) {
	    FreeMem(PD->pd_PrintBuf, TotalBufSize);
	    PD->pd_PrintBuf = NULL;
	}
	break;
#ifdef DEBUG
    default:
	debug(("!!Render ct %08lx x %d y %d status %d\n", ct, x, y, status));
	break;
#endif
    }
    return(err);
}

#include <clib/asl_protos.h>

void *
OpenFaxFile(void)
{
    char	   *filename = NULL;

    if (AslBase && FileReq) {
	if (AslRequest(FileReq, NULL)) {
	    static char     fn[128];
	    int 	    len;

	    strcpy(fn, FileReq->rf_Dir);
	    len = strlen(fn);
	    if (len > 0 && fn[len-1] != ':')
		strcat(fn, "/");
	    strcat(fn, FileReq->rf_File);
	    filename = fn;
	}
    } else {
	filename = "FAX:faxrastportdump";
    }

    if (filename)
	return faxout_open(filename, 1 + NoFormFeed);
    return NULL;
}
