/////////////////////////////////////////////////////////////////////////////
// CLAYCO INTERNAL COMMUNICATION                                           //
// PROJECT:	Federal Employment Form SF-171 Formatter/Printer           //
// MODULE:	Printer-driver                                             //
// PURPOSE:	Provide interface from main application to Epson-compatible//
//		and HP compatible printers.                                //
// REVISION:	1.0A 	DATE: 03/07/1994	PROGRAMMER: Sean Clay      //
/////////////////////////////////////////////////////////////////////////////

#define PRNOK	0    /* printed ok */	
#define NOPRN	1    /* no printer configured */
#define HPLJ	10   /* HP Laser Jet printer */
#define EPSON	20   /* Epson dot matrix printer */
#define STATUS  2    /* printer status command */
#define PORTNUM 0    /* port number for LPT1 */

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <bios.h>

int printer_exist(void);
int printer_ready(int);
void box(int,int,int,int);
void blanks(int,int,int,int);
void not_ready_message(void);


int main(void)
{
  clrscr();
  if (printer_exist) {
    while (!printer_ready(PORTNUM)) not_ready_message();
    printf("The printer is ready\n");
  }
}


// Check system to see if printer is configured.
int printer_exist(void)
{
  unsigned equipment,printers = 0;
  equipment = biosequip();
  printers = (equipment & 0xc000) >> 14;
  return printers;
}

// Check printer status. Returns 0 if printer is off line.
int printer_ready(int port)
{
   int status, abyte=0, rstat=1;
   status = biosprint(STATUS, abyte, port);
   if (status & 0x01) rstat=0; //Device time out
   if (status & 0x08) rstat=0; //I/O error
   if (status & 0x20) rstat=0; //Out of paper
   return rstat;
}

void not_ready_message(void)
{
  box(10,10,40,16);
  gotoxy(13,13);
  printf("The printer is offline!");
  while(!printer_ready(PORTNUM)){}
  blanks(10,10,45,20);
}
/*********************** [ MISC. ROUTINES ] *****************************/
void blanks(int horz1, int vert1, int horz2, int vert2)
{
  int counter1, counter2;
  for (counter1=horz1; counter1<horz2; counter1++)  {
    for (counter2=vert1; counter2<vert2; counter2++)  {
      gotoxy(counter1,counter2);
      printf(" ");
    }
  }
}

/************************************************************************/
/* box.c - Draws a double-walled box of user defined size and location. */
/* Proprietary Software of CLAYCO - Copyright (c) 1991 - Revision 1.0A  */
/************************************************************************/
void box(int horz1, int vert1, int horz2, int vert2)
{
  int counter;
  gotoxy(horz1,vert1); /* Upper left-hand corner of box */
  printf("\xC9");
  gotoxy(horz1,vert2); /* Lower left-hand corner of box */
  printf("\xC8");
  for (counter=horz1+1; counter<horz2; counter++)
    {
    gotoxy(counter,vert1);    /* Draw upper and lower sides   */
    printf("\xCD");           /* of the box at the same time. */
    gotoxy(counter,vert2);
    printf("\xCD");
    }
  gotoxy(horz2,vert1);   /* Upper right-hand corner of box */
  printf("\xBB");
  gotoxy(horz2,vert2);   /* Lower right-hand corner of box */
  printf("\xBC");
  for (counter=vert1+1; counter<vert2; counter++)
    {
    gotoxy(horz1,counter);    /* Draw left and right sides of */
    printf("\xBA");           /* the box at the same time.    */
    gotoxy(horz2,counter);
    printf("\xBA");
    }
} /* end box */
