/*
 * WBPrinter - Put a printer icon on the workbench screen
 * 
 *
 * Copyright (c) 1992, Mike Ruble
 *
 * Permission is hereby granted to distribute this program for any purposes
 * whatsoever, so long as this notice, including the above copyright, is
 * included with the distribution.
 *
 * 04/10/92 Written by Mike Ruble using SAS (Lattice) C 5.10b
 *
 */

#include <dos.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <exec/tasks.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <dos/dostags.h>
#include <libraries/gadtools.h>
#include <libraries/asl.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <intuition/gadgetclass.h>
#include <workbench/startup.h>
#include <workbench/workbench.h>
#include <proto/exec.h>
#include <proto/timer.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <proto/wb.h>
#include <proto/gadtools.h>
#include <proto/icon.h>
#include <proto/asl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#define GD_LPI_Gad                             0
#define GD_Quality_Gad                         1
#define GD_Pitch_Gad                           2
#define GD_L_Marg_Gad                          3
#define GD_R_Marg_Gad                          4
#define GD_Pref_Gad                            5
#define GD_Skip_Perf_Gad                       6
#define GD_Page_Eject_Gad                      7
#define GD_Spool_Gad                           8
#define GD_Next_Gad                            9
#define GD_Cancel_Gad                          10
#define GD_Job_List_Gad                        11
#define GD_Quit_Gad                            12

struct FileNode
{
   struct Node  nn_Node;
   char        *nn_SpooledName;
};

struct PrinterPrefs
{
   BYTE  pp_LinesPerInch;
   BOOL  pp_SkipPerf;
   BYTE  pp_PrintQuality;
   BYTE  pp_PrintPitch;
   UWORD pp_LeftMargin;
   UWORD pp_RightMargin;
   BOOL  pp_EjectPage;
};

struct PrtMsg
{
   struct Message  pm_Message;
   WORD            pm_Command;
};

#define PRINTS_READY    (0x0001)
#define CANCEL_PRINT    (0x0010)
#define CANCEL_AND_DIE  (0x1111)

#define MEMORY_TYPE     (MEMF_PUBLIC | MEMF_CLEAR)

#define PRINT_FILE      (0x01)
#define PRINTING_FILE   (0x10)


struct ProcMsg                          /* startup message sent to child */
{
   struct Message msg;
   void  *global_data;                  /* global data reg (A4)         */
};


/********** Libraries **********/
struct Library       *GadToolsBase  = NULL;
struct IntuitionBase *IntuitionBase = NULL;
struct Library       *IconBase      = NULL;
struct Library       *WorkbenchBase = NULL;
struct Library       *GfxBase       = NULL;
struct Library       *AslBase       = NULL;


/********** AppWindow globals **********/
struct Window        *Wnd        = 0l;
struct Screen        *Scr        = 0l;
APTR                  VisualInfo = 0l;
struct Gadget        *GList      = 0l;
struct Gadget        *Gadgets[13];

ULONG  window_top;
ULONG  window_left;

UBYTE         *LPI_GadLabels[] = {
    "6 Lines Per Inch",
    "8 Lines Per Inch",
    0l };

UBYTE         *Quality_GadLabels[] = {
    "Draft Quality",
    "Letter Quality",
    0l };

UBYTE         *Pitch_GadLabels[] = {
    "10-Pica  Pitch",
    "12-Elite Pitch",
    "15-Fine  Pitch",
    0l };

struct TagItem    WindowTags[] = {
    WA_Left,          119,
    WA_Top,           49,
    WA_Width,         375,
    WA_Height,        146,
    WA_IDCMP,         IDCMP_GADGETDOWN|IDCMP_GADGETUP|IDCMP_CLOSEWINDOW,
    WA_Flags,         WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_ACTIVATE,
    WA_Gadgets,       0l,
    WA_Title,         &"WBPrinter Preferences",
    WA_CustomScreen,  0l,
    TAG_DONE };


/********** AppIcon globals **********/
struct MsgPort       *AppPort;
struct AppIcon       *Appicon;
struct DiskObject    *icon;


/********** Printer globals **********/
struct List               *Print_Q;
struct SignalSemaphore    *print_q_semaphore;
struct PrinterPrefs        PrtPrefs;

char *PrinterSetup[9] =
{
   "\x1b[1z",    /* Six LPI     */
   "\x1b[0z",    /* Eight LPI   */
   "\x1b[0q",    /* Don't skip  */
   "\x1b[nq",    /* Skip Perf   */
   "\x1b[1\"z",  /* Draft Qual  */
   "\x1b[2\"z",  /* Letter Qual */
   "\x1b[0w",    /* Pica        */
   "\x1b[2w",    /* Elite       */
   "\x1b[4w"     /* Fine        */
};

#define LPI   0
#define SKIP  2
#define QUAL  4
#define PITCH 6


/********** Misc. **********/
char                       Name_Buffer[255];
char                       FilePrefix[] = "PrintFile.";
char                      *spooldir;
ULONG                      filecount = 0;
struct Preferences        *prefs;
struct ProcMsg            *start_msg;


/********** Declarations for CBACK **********/
/* extern BPTR _Backstdout; */       /* standard output when run in background */
/* long BackGroundIO = 0;   */       /* Flag to tell it we don't want to do I/O*/
/* long stack = 4000;       */       /* Amount of stack space our task needs   */
/* char *procname = "WBPrinter"; */  /* The name of the task to create         */
/*long priority = 20;       */      /* The priority to run us at              */


/********** Function definitions **********/
void Do_MainFunc(void);
void SetUp(void);
void Tell_Print_Task(WORD);
void Clear_Print_Q(void);
void done(int);
int  Do_AppWindow(void);
int  Make_AppWindow(void);
void Remove_AppWindow(void);
void StartPrintTask(void);
__saveds void PrintFiles(void);

