home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / pufferfish / source / pufferfish.c < prev    next >
C/C++ Source or Header  |  1994-03-29  |  12KB  |  488 lines

  1. /*
  2.  * PufferFish.c
  3.  *
  4.  * Generate Fish floppies from CD-ROM distribution
  5.  *
  6.  */
  7. #include <exec/types.h>
  8.  
  9. #include <utility/tagitem.h>
  10. #include <intuition/intuition.h>
  11. #include <intuition/gadgetclass.h>
  12. #include <intuition/imageclass.h>
  13. #include <intuition/icclass.h>
  14. #include <libraries/amigaguide.h>
  15. #include <libraries/dos.h>
  16. #include <dos/dosextens.h>
  17. #include <dos/dostags.h>
  18. #include <resources/disk.h>
  19.  
  20. #include <clib/exec_protos.h>
  21. #include <clib/amigaguide_protos.h>
  22. #include <clib/alib_protos.h>
  23. #include <clib/dos_protos.h>
  24. #include <clib/graphics_protos.h>
  25. #include <clib/icon_protos.h>
  26. #include <clib/intuition_protos.h>
  27.  
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31.  
  32. #include "WB2CLI/WB2CLI.h"
  33. #include "buttonclass/headers.h"
  34. #include "buttonclass/buttonclass.h"
  35. #include "PufferFish.h"
  36.  
  37. long __stack = 20000;
  38.  
  39. extern struct Library *SysBase;
  40. extern struct Library *IntuitionBase;
  41. extern struct Library *UtilityBase;
  42. extern struct Library *GfxBase;
  43. extern struct DosLibrary *DOSBase;
  44. struct Library *DiskBase = NULL;
  45. struct Library *AmigaGuideBase = NULL;
  46.  
  47. static const char versionstring[] = "\0$VER: PufferFish 1.0 (29.03.94)";
  48.  
  49. struct Screen *screen;
  50. struct DrawInfo *drawinfo;
  51. struct Window *win;
  52.  
  53. BPTR dirlock;
  54.  
  55. struct InfoData MyInfoData;
  56.  
  57. int start_disknum, end_disknum;
  58.  
  59. int dig[8] =
  60. {
  61.     0, 0, 0, 1,
  62.     0, 0, 0, 1
  63. };
  64.  
  65. char *dig_text[8] =
  66. {
  67.     "0", "0", "0", "1",
  68.     "0", "0", "0", "1",
  69. };
  70.  
  71. unsigned char avail_disks, drives_selected;
  72. struct MsgPort *myPort;
  73. struct Process *myProc[5];
  74.  
  75. int firstdisk, lastdisk;
  76. char *contentspath, *formatpath, *lhapath, *brikpath, *additional_device;
  77. BOOL verbose;
  78.  
  79. AMIGAGUIDECONTEXT agcontext = NULL;
  80. ULONG winbit;
  81.  
  82. Class *buttonclass;
  83.  
  84. void MakeBoxedStringClass(void);
  85. void FreeBoxedStringClass(void);
  86.  
  87. void handle_input(void);
  88.  
  89. // Change SAS/C's Ctrl-C checking (eventually abort accidental generation of wrong floppies?)
  90. void __regargs _CXBRK(void)
  91. {
  92. }
  93.  
  94. /*
  95.  * main()
  96.  *
  97.  * Handles library, window, image, gadget, help setup
  98.  * Handles input (should be separate)
  99.  *
  100.  */
  101. int main( int argc, char **argv )
  102. {
  103.     ULONG arrowwidth, arrowheight, checkwidth, winwidth, winheight;
  104.     struct NewAmigaGuide nag = {NULL};
  105.     STRPTR context[] =
  106.     {
  107.         "MAIN",
  108.         "DIGITS",
  109.         "DRIVES",
  110.         "START",
  111.         NULL
  112.     };
  113.     UBYTE **ttypes;
  114.     int i;
  115.     
  116.     WB2CLI((struct WBStartup *)argv,20000,DOSBase);
  117.     AmigaGuideBase = OpenLibrary( "amigaguide.library", 34 );
  118.     ttypes = ArgArrayInit( argc, argv );
  119.     firstdisk = ArgInt( ttypes, "FIRSTDISK", MIN_DISK_NUM );
  120.     lastdisk = ArgInt( ttypes, "LASTDISK", MAX_DISK_NUM );
  121.     contentspath = ArgString( ttypes, "CONTENTSPATH", CONTENTS_PATH );
  122.     formatpath = ArgString( ttypes, "FORMATPATH", FORMAT_PATH );
  123.     lhapath = ArgString( ttypes, "LHAPATH", LHA_PATH );
  124.     brikpath = ArgString( ttypes, "BRIKPATH", BRIK_PATH );
  125.     additional_device = ArgString( ttypes, "DEVICE", "" );
  126.     verbose = FindToolType( ttypes, "VERBOSE" ) ? TRUE : FALSE;
  127.     
  128.     if( firstdisk < MIN_DISK_NUM )
  129.     {
  130.         firstdisk = MIN_DISK_NUM;
  131.     }
  132.     if( lastdisk < firstdisk )
  133.     {
  134.         lastdisk = MAX_DISK_NUM;
  135.     }
  136.     start_disknum = end_disknum = firstdisk;
  137.     dig[0] = start_disknum / 1000;
  138.     dig[1] = (start_disknum - dig[0] * 1000) / 100;
  139.     dig[2] = (start_disknum - dig[0] * 1000 - dig[1] * 100) / 10;
  140.     dig[3] = (start_disknum - dig[0] * 1000 - dig[1] * 100 - dig[2] * 10);
  141.     dig[4] = end_disknum / 1000;
  142.     dig[5] = (end_disknum - dig[4] * 1000) / 100;
  143.     dig[6] = (end_disknum - dig[4] * 1000 - dig[5] * 100) / 10;
  144.     dig[7] = (end_disknum - dig[4] * 1000 - dig[5] * 100 - dig[6] * 10);
  145.     for( i = 0; i < 8; i += 1 )
  146.     {
  147.         dig_text[i][0] = '0' + dig[i];
  148.     }
  149.     
  150.     if( DiskBase = (struct Library *)OpenResource(DISKNAME) )
  151.     {
  152.         screen = LockPubScreen( NULL );
  153.         if( screen )
  154.         {
  155.             drawinfo = GetScreenDrawInfo( screen );
  156.             if( drawinfo )
  157.             {
  158.                 MakeBoxedStringClass();
  159.                 if( buttonclass = initButtonGadgetClass( IntuitionBase, UtilityBase, GfxBase ) )
  160.                 {
  161.                     if( make_systemimages() )
  162.                     {
  163.                         GetAttr( IA_Width, upimage, &arrowwidth );
  164.                         GetAttr( IA_Height, upimage, &arrowheight );
  165.                         GetAttr( IA_Width, checkimage, &checkwidth );
  166.                         
  167.                         // width of digits frame
  168.                         winwidth = ( arrowwidth * 11 / 2 + drawinfo->dri_Font->tf_XSize * 5 + 8);
  169.                         // width of drive frame
  170.                         drivenames_text[0].ITextFont = screen->Font;
  171.                         winwidth += drawinfo->dri_Font->tf_XSize
  172.                             + IntuiTextLength( &drivenames_text[0] )
  173.                             + checkwidth + 4
  174.                             + 2 * drawinfo->dri_Font->tf_XSize;
  175.                         
  176.                         // height of button frames
  177.                         winheight = 2 * ((arrowheight + 2)
  178.                             + (drawinfo->dri_Font->tf_YSize + 2 + 4)
  179.                             + (arrowheight + 2)
  180.                             + drawinfo->dri_Font->tf_YSize
  181.                                 + GF_TOPBORDER * 3 / 2) + drawinfo->dri_Font->tf_YSize;
  182.                         
  183.                         if( win = OpenWindowTags( NULL,
  184.                             WA_PubScreen,    screen,
  185.                             WA_Flags,    WFLG_DEPTHGADGET | WFLG_DRAGBAR | WFLG_CLOSEGADGET | WFLG_NEWLOOKMENUS,
  186.                                 WA_IDCMP,    GF_IDCMPFLAGS,
  187.                             WA_Width,    screen->WBorLeft + screen->WBorRight    // window borders
  188.                                 + GF_LEFTBORDER + GF_RIGHTBORDER                // margins from borders
  189.                                 + winwidth,
  190.                             WA_Height,    screen->WBorTop + screen->WBorBottom
  191.                                 + drawinfo->dri_Font->tf_YSize + 1
  192.                                 + GF_TOPBORDER + GF_BOTTOMBORDER
  193.                                 + winheight,
  194.                             WA_Activate,TRUE,
  195.                             WA_Title,    "PufferFish",
  196.                             TAG_END ))
  197.                         {
  198.                             if( GfxBase->lib_Version >= 39 )
  199.                             {
  200.                                 HelpControl( win, HC_GADGETHELP );
  201.                             }
  202.                             winbit = 1L << ((struct MsgPort *)win->UserPort)->mp_SigBit;
  203.                             avail_disks = which_disks();
  204.                             if( make_images() && make_digits() && make_drives() && make_gadgets() )
  205.                             {
  206.                                 draw_images();
  207.                                 
  208.                                 AddGList( win, uparrows[0], -1, -1, NULL );
  209.                                 RefreshGList( uparrows[0], win, NULL, -1 );
  210.                                 
  211.                                 if( AmigaGuideBase )
  212.                                 {
  213.                                     nag.nag_BaseName = "PUFFERFISH";
  214.                                     nag.nag_Name = "PufferFish.guide";
  215.                                     nag.nag_ClientPort = "PUFFERFISH_HELP";
  216.                                     nag.nag_Context = context;
  217.                                     nag.nag_Screen = screen;
  218.                                     if( agcontext = OpenAmigaGuideAsync( &nag, NULL ) )
  219.                                     {
  220.                                         SetAmigaGuideContext( agcontext, 0L, NULL );
  221.                                     }
  222.                                 }
  223.                                 handle_input();
  224.                                 if( AmigaGuideBase && agcontext )
  225.                                 {
  226.                                     CloseAmigaGuide( agcontext );
  227.                                 }
  228.                                 RemoveGList( win, uparrows[0], -1 );
  229.                                 free_gadgets();
  230.                                 free_drives();
  231.                                 free_digits();
  232.                                 free_images();
  233.                             }
  234.                             CloseWindow( win );
  235.                         }
  236.                         free_systemimages();
  237.                     }
  238.                     freeButtonGadgetClass( buttonclass );
  239.                 }
  240.                 FreeBoxedStringClass();
  241.                 FreeScreenDrawInfo( screen, drawinfo );
  242.             }
  243.             UnlockPubScreen( NULL, screen );
  244.         }
  245.         // NOTE: Don't close DiskBase...
  246.     }
  247.     ArgArrayDone();
  248.     if( AmigaGuideBase )
  249.     {
  250.         CloseLibrary( AmigaGuideBase );
  251.     }
  252. }
  253.  
  254. void handle_input(void)
  255. {
  256.     BOOL done = FALSE;
  257.     ULONG signal;
  258.     ULONG id, class, code;
  259.     void *ghelpid;
  260.     struct IntuiMessage *imsg;
  261.     struct EasyStruct easyStruct =
  262.     {
  263.         sizeof( struct EasyStruct ),
  264.         0,
  265.         NULL,
  266.         NULL,
  267.         NULL
  268.     };
  269.     struct Requester req;
  270.     LONG region, help_region;
  271.     
  272.     while( !done )
  273.     {
  274.         signal = Wait( winbit );
  275.         if( signal & winbit )
  276.         {
  277.             while( imsg = (struct IntuiMessage *)
  278.                 GetMsg( (struct MsgPort *)win->UserPort ) )
  279.             {
  280.                 class = imsg->Class;
  281.                 code = imsg->Code;
  282.                 if( class == IDCMP_GADGETUP )
  283.                 {
  284.                     id = ((struct Gadget *)imsg->IAddress)->GadgetID;
  285.                 }
  286.                 ghelpid = imsg->IAddress;
  287.                 ReplyMsg( (struct Message *)imsg );
  288.                 switch( class )
  289.                 {
  290.                     case IDCMP_CLOSEWINDOW:
  291.                         done = TRUE;
  292.                         break;
  293.                     case IDCMP_GADGETUP:
  294.                         if( id < GAD_START )
  295.                         {
  296.                             if( code == 0x5F )
  297.                             {
  298.                                 // Help for string/integer gadget requested
  299.                                 if( agcontext )
  300.                                 {
  301.                                     SetAmigaGuideContext( agcontext, GF_HELP_DIGITS, NULL );
  302.                                     SendAmigaGuideContext( agcontext, NULL );
  303.                                 }
  304.                                 else
  305.                                 {
  306.                                     /* amigaguide.library wasn't found, so help not available */
  307.                                     easyStruct.es_TextFormat = "Help not available\nYou need amigaguide.library V34";
  308.                                     easyStruct.es_GadgetFormat = "OK";
  309.                                     beginWait( win, &req );
  310.                                     EasyRequest( win, &easyStruct, NULL, NULL );
  311.                                     endWait( win, &req );
  312.                                 }
  313.                             }
  314.                             else
  315.                             {
  316.                                 update_gadgets(id);
  317.                             }
  318.                         }
  319.                         else if( id == GAD_START )
  320.                         {
  321.                             /* Select path */
  322.                             if( drives_selected )
  323.                             {
  324.                                 start_disk_generate();
  325.                             }
  326.                             else
  327.                             {
  328.                                 /* No destination disk chosen */
  329.                                 DisplayBeep( screen );
  330.                                 easyStruct.es_TextFormat = "No destination drives chosen";
  331.                                 easyStruct.es_GadgetFormat = "OK";
  332.                                 beginWait( win, &req );
  333.                                 EasyRequest( win, &easyStruct, NULL, NULL );
  334.                                 endWait( win, &req );
  335.                             }
  336.                         }
  337.                         else
  338.                         {
  339.                             DisplayBeep( screen );
  340.                             easyStruct.es_TextFormat = "Unknown gadget";
  341.                             easyStruct.es_GadgetFormat = "OK";
  342.                             beginWait( win, &req );
  343.                             EasyRequest( win, &easyStruct, NULL, NULL );
  344.                             endWait( win, &req );
  345.                         }
  346.                         break;
  347.                     case IDCMP_VANILLAKEY:
  348.                         switch( code )
  349.                         {
  350.                             case 'g':
  351.                             case 'G':
  352.                                 if( drives_selected )
  353.                                 {
  354.                                     start_disk_generate();
  355.                                 }
  356.                                 else
  357.                                 {
  358.                                     /* No destination disk chosen */
  359.                                     DisplayBeep( screen );
  360.                                     easyStruct.es_TextFormat = "No destination drives chosen";
  361.                                     easyStruct.es_GadgetFormat = "OK";
  362.                                     beginWait( win, &req );
  363.                                     EasyRequest( win, &easyStruct, NULL, NULL );
  364.                                     endWait( win, &req );
  365.                                 }
  366.                                 break;
  367.                             case '0':
  368.                             case '1':
  369.                             case '2':
  370.                             case '3':
  371.                             case '4':
  372.                                 if( avail_disks & (1<<(code - '0')) )
  373.                                 {
  374.                                     handle_drive( GAD_DRIVE_0 + code - '0' );
  375.                                     refresh_gadgets();
  376.                                 }
  377.                                 RefreshGList( uparrows[0], win, NULL, -1 );
  378.                                 break;
  379.                             case '=':
  380.                                 start_disknum += 1;
  381.                                 refresh_gadgets();
  382.                                 RefreshGList( uparrows[0], win, NULL, -1 );
  383.                                 break;
  384.                             case '-':
  385.                                 start_disknum -= 1;
  386.                                 refresh_gadgets();
  387.                                 RefreshGList( uparrows[0], win, NULL, -1 );
  388.                                 break;
  389.                             case '+':
  390.                                 end_disknum += 1;
  391.                                 refresh_gadgets();
  392.                                 RefreshGList( uparrows[0], win, NULL, -1 );
  393.                                 break;
  394.                             case '_':
  395.                                 end_disknum -= 1;
  396.                                 refresh_gadgets();
  397.                                 RefreshGList( uparrows[0], win, NULL, -1 );
  398.                                 break;
  399.                             case '\t':
  400.                                 ActivateGadget( integer[0], win, NULL );
  401.                                 RefreshGList( uparrows[0], win, NULL, -1 );
  402.                                 break;
  403.                         }
  404.                         break;
  405.                     case IDCMP_GADGETHELP:
  406.                         region = -1;
  407.                         if( ghelpid == NULL )
  408.                         {
  409.                             ;
  410.                         }
  411.                         else if( ghelpid == (APTR)win )
  412.                         {
  413.                             // Over window
  414.                             region = 0;
  415.                         }
  416.                         else
  417.                         {
  418.                             LONG sysgtype = ((struct Gadget *)ghelpid)->GadgetType & 0xF0;
  419.                             
  420.                             switch( sysgtype )
  421.                             {
  422.                                 case GTYP_WDEPTH:
  423.                                     // Depth gadget
  424.                                     region = GF_NUM_GADS + 1;
  425.                                     break;
  426.                                 case GTYP_CLOSE:
  427.                                     // Close gadget
  428.                                     region = GF_NUM_GADS + 2;
  429.                                     break;
  430.                                 case GTYP_WDRAGGING:
  431.                                     // Window dragbar
  432.                                     region = GF_NUM_GADS + 3;
  433.                                     break;
  434.                                 case 0:
  435.                                     // + 1 since region 0 is reserved for 'window'
  436.                                     region = ((struct Gadget *)ghelpid)->GadgetID + 1;
  437.                                     break;
  438.                                 default:
  439.                                     // Other gadget
  440.                                     region = 0;
  441.                                     break;
  442.                             }
  443.                         }
  444.                         break;
  445.                     case IDCMP_RAWKEY:
  446.                         switch( code )
  447.                         {
  448.                             case 0x5F:
  449.                                 if( agcontext )
  450.                                 {
  451.                                     help_region = GF_HELP_MAIN;
  452.                                     if( region <= 0 )
  453.                                     {
  454.                                         help_region = GF_HELP_MAIN;
  455.                                     }
  456.                                     else if( region <= GAD_PROP_2 + 1 )
  457.                                     {
  458.                                         help_region = GF_HELP_DIGITS;
  459.                                     }
  460.                                     else if( region <= GAD_DRIVE_3 + 1 )
  461.                                     {
  462.                                         help_region = GF_HELP_DRIVES;
  463.                                     }
  464.                                     else if( region <= GF_NUM_GADS )
  465.                                     {
  466.                                         help_region = GF_HELP_START;
  467.                                     }
  468.                                     SetAmigaGuideContext( agcontext, help_region, NULL );
  469.                                     SendAmigaGuideContext( agcontext, NULL );
  470.                                 }
  471.                                 break;
  472.                             case 0x42:
  473.                                 // Tab key
  474.                                 ActivateGadget( integer[0], win, NULL );
  475.                                 RefreshGList( integer[0], win, NULL, -1 );
  476.                                 break;
  477.                             default:
  478.                                 break;
  479.                         }
  480.                         break;
  481.                     default:
  482.                         break;
  483.                 }
  484.             }
  485.         }
  486.     }
  487. }
  488.