home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d8xx / d828 / disktest.lha / DiskTest / Source / beginend.c next >
C/C++ Source or Header  |  2001-02-25  |  11KB  |  371 lines

  1. /*---------------------------------------------------------*
  2.  | File: BEGINEND.c - Initialisation, and Cleanup routines |
  3.  +---------------------------------------------------------+
  4.  | Author:  Maurizio Loreti, aka MLO or I3NOO.             |
  5.  | Address: University of Padova - Department of Physics   |
  6.  |          Via F. Marzolo, 8 - 35131 PADOVA - Italy       |
  7.  | Phone:   (39)(49) 844-313         FAX: (39)(49) 844-245 |
  8.  | E-Mail:  loreti@padova.infn.it (TCP/IP)                 |
  9.  | Home: Via G. Donizetti 6 - 35010 CADONEGHE (PD) - Italy |
  10.  *---------------------------------------------------------*/
  11.  
  12. /**
  13.  | #includes
  14. **/
  15.  
  16. #include <stddef.h>                       /* Standard library */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <exec/types.h>                   /* Amiga specific */
  21. #include <exec/memory.h>
  22. #include <intuition/gadgetclass.h>
  23. #include <dos/dos.h>
  24. #include <clib/exec_protos.h>
  25. #include <clib/graphics_protos.h>
  26. #include <clib/intuition_protos.h>
  27. #include <clib/gadtools_protos.h>
  28. #include <clib/alib_protos.h>
  29. #include <clib/wb_protos.h>
  30. #include <clib/dos_protos.h>
  31. #include "main.h"                         /* Local stuff */
  32. #include "beginend.h"
  33. #include "ext.h"
  34.  
  35. void Cleanup(void)
  36. {
  37. /**
  38.  | This routines releases all allocated resources before
  39.  | exiting to the operating system.
  40.  | Since the program detaches itself from the CLI, we don't
  41.  | need to return an error code if something strange has been
  42.  | detected: the return code is always "EXIT_OK".
  43.  |
  44.  | - frees all memory obtained from malloc() or AllocMem();
  45.  | - frees the IOrequest and deletes the port of the trackdisk.device;
  46.  | - closes the "break" window;
  47.  | - closes the AppWindow stuff;
  48.  | - closes the output window;
  49.  | - releases every resource needed for the gadtools gadgets, and
  50.  |   unlocks the Workbench screen;
  51.  | - closes all the libraries.
  52. **/
  53.  
  54.   if (pClearRP   != NULL)     free(pClearRP);
  55.   if (diskBuffer != NULL)     FreeMem(diskBuffer, diskBufferLength);
  56.   if (pFIB       != NULL)     FreeMem(pFIB, sizeof(struct FileInfoBlock));
  57.   ClearText(FALSE);
  58.  
  59.   if (diskReq != NULL) {
  60.     CloseDevice((struct IORequest *) diskReq);
  61.     DeleteExtIO((struct IORequest *) diskReq);
  62.   }
  63.   if (diskPort != NULL)       DeletePort(diskPort);
  64.  
  65.   if (pBWind != NULL) {
  66.     struct IntuiMessage *pIM;
  67.  
  68.     while ((pIM = GT_GetIMsg(pBWind->UserPort)) != NULL) {
  69.       GT_ReplyIMsg(pIM);
  70.     }
  71.     CloseWindow(pBWind);
  72.   }
  73.   if (pBGad != NULL) FreeGadgets(pBGad);
  74.  
  75.   if (pAWind != NULL) {
  76.     struct Message *pM;
  77.  
  78.     while ((pM = GetMsg(appWinPort)) != NULL) {
  79.       ReplyMsg(pM);
  80.     }
  81.     (void) RemoveAppWindow(pAWind);
  82.   }
  83.   if (appWinPort != NULL)     DeletePort(appWinPort);
  84.  
  85.   if (pWind != NULL) {
  86.     struct IntuiMessage *pIM;
  87.  
  88.     while ((pIM = GT_GetIMsg(pWind->UserPort)) != NULL) {
  89.       GT_ReplyIMsg(pIM);
  90.     }
  91.     CloseWindow(pWind);
  92.   }
  93.  
  94.   if (pGlist  != NULL)        FreeGadgets(pGlist);
  95.   if (pVI     != NULL)        FreeVisualInfo(pVI);
  96.   if (pScr    != NULL)        UnlockPubScreen(NULL, pScr);
  97.   if (GadFont != NULL)        CloseFont(GadFont);
  98.  
  99.   if (WorkbenchBase != NULL)  CloseLibrary(WorkbenchBase);
  100.   if (GadToolsBase  != NULL)  CloseLibrary(GadToolsBase);
  101.   if (LayersBase    != NULL)  CloseLibrary(LayersBase);
  102.   if (GfxBase       != NULL)  CloseLibrary(GfxBase);
  103.   if (IntuitionBase != NULL)  CloseLibrary(IntuitionBase);
  104.  
  105.   exit(EXIT_OK);
  106. }
  107.  
  108. void Init(void)
  109. {
  110. /**
  111.  | Main initialisation routine
  112. **/
  113.  
  114.   IntuiInit();
  115.  
  116.   if ((pClearRP = malloc(sizeof(struct RastPort))) == NULL   ||
  117.     (pFIB = (struct FileInfoBlock *)
  118.        AllocMem(sizeof(struct FileInfoBlock), MEMF_CLEAR)) == NULL) {
  119.     Error("Not enough memory");
  120.   }
  121.  
  122.   HelpScreen();
  123.   InitScroller();
  124. }
  125.  
  126. /*-------------------------- Local Procedures --------------------------*/
  127.  
  128. static void HelpScreen(void)
  129. {
  130. /**
  131.  | Fills the output scroller with a short help text,
  132.  | to be displayed at the program start.
  133. **/
  134.  
  135.   size_t i;
  136.   char *helpText[] = {
  137.     VersionTag + 7,
  138.     "",
  139.     "You can now click on a button gadget: the DF buttons start the",
  140.     "full disk test (defective tracks, and file integrity)  for the",
  141.     "selected drive,  while the QUIT button terminates the program.",
  142.     "",
  143.     "You can also drop an icon  into this window:  for file related",
  144.     "icons (ie tool or project), DT will check the integrity of the",
  145.     "corresponding file; otherwise (disk, drawer or trashcan icons)",
  146.     "DT will perform the integrity test on _all_ files found in the",
  147.     "related [root] directory, and _in every underlying directory_."
  148.   };
  149.  
  150.   for (i=0; i<(sizeof(helpText)/sizeof(char *)); i++) {
  151.     AddLine(helpText[i], strlen(helpText[i]), FALSE);
  152.   }
  153. }
  154.  
  155. static void IntuiInit(void)
  156. {
  157. /**
  158.  | This routine performs the Intuition part of the initialisation:
  159.  | - opens all the needed libraries;
  160.  | - finds how many and which floppy units are on the system;
  161.  | - creates all the gadtools gadgets to be put in the output window;
  162.  | - creates the gadtools gadgets to be put in the "break" window;
  163.  | - opens the output window itself.
  164.  |
  165.  | If an error is detected in this phase, IntuiInit() exits calling
  166.  | Error(); the only exception is when the v37 libraries were not
  167.  | found, because Error() uses an EasyRequester (available in v37
  168.  | only); in that case IntuiInit displays an AutoRequester on the
  169.  | Workbench window (we need intuition.library, that is opened at
  170.  | every available revision) and exits calling Cleanup().
  171. **/
  172.  
  173.   WORD            xOffset;
  174.   WORD            yOffset;
  175.   short           i;
  176.   struct Gadget  *pG;
  177.   struct DosList *pDL;
  178.   ULONG           flags;
  179.   char            drive[] = "DF0";
  180.  
  181.   static struct TextAttr t80 = {      /* Text used for button gadgets */
  182.     "topaz.font", TOPAZ_EIGHTY,       /*   (plain topaz-80) */
  183.     FS_NORMAL, FPF_ROMFONT
  184.   };
  185.  
  186.   struct NewGadget ng = {
  187.     GAD_LEFT, GAD_TOP,
  188.     GAD_WIDTH, GAD_HEIGHT,
  189.     NULL,                             /* Gadget text */
  190.     &t80,
  191.     0,                                /* Gadget ID */
  192.     PLACETEXT_IN,
  193.     NULL,                             /* GetVisualInfo() ret. val. */
  194.     NULL
  195.   };
  196.  
  197.   char *gadText[NUM_BUT] = {          /* Button gadgets text */
  198.     "DF0", "DF1", "DF2",
  199.     "DF3", "Quit"
  200.   };
  201.  
  202.   UWORD gadID[NUM_BUT] = {            /* Button gadgets ID */
  203.     BUT_1, BUT_2, BUT_3,
  204.     BUT_4, BUT_QUIT
  205.   };
  206.  
  207.   BOOL disabled[NUM_BUT] = {          /* Button gadgets state */
  208.     FALSE, FALSE, FALSE,
  209.     FALSE, FALSE
  210.   };
  211.  
  212.   WORD zoomBox[4] = {
  213.     ZB_LEFT, ZB_TOP, ZB_WIDTH, 0
  214.   };
  215.  
  216. /**
  217.  | Libraries
  218. **/
  219.  
  220.   IntuitionBase = OpenLibrary("intuition.library",  OS_MINREV);
  221.   GfxBase       = OpenLibrary("graphics.library",   OS_MINREV);
  222.   LayersBase    = OpenLibrary("layers.library",     OS_MINREV);
  223.   GadToolsBase  = OpenLibrary("gadtools.library",   OS_MINREV);
  224.   WorkbenchBase = OpenLibrary("workbench.library",  OS_MINREV);
  225.  
  226.   if (IntuitionBase == NULL || LayersBase    == NULL ||  GfxBase == NULL ||
  227.       GadToolsBase  == NULL || WorkbenchBase == NULL) {
  228.     struct IntuiText it = {
  229.       0, 1, JAM2, AR_ITX, AR_ITY, NULL,
  230.       "DiskTest requires OS 2.04 (v37)", NULL
  231.     };
  232.     struct IntuiText ok = {
  233.       0, 1, JAM2, AR_OKX, AR_OKY, NULL, "OK", NULL
  234.     };
  235.  
  236.     if (IntuitionBase != NULL   ||
  237.         (IntuitionBase = OpenLibrary("intuition.library",  0)) != NULL) {
  238.       (void) AutoRequest(NULL, &it, NULL, &ok, 0, 0, AR_WX, AR_WY);
  239.     }
  240.     Cleanup();
  241.   }
  242.  
  243. /**
  244.  | Connected floppies
  245. **/
  246.  
  247.   flags = LDF_DEVICES | LDF_READ;
  248.   pDL = LockDosList(flags);
  249.   for (i=0; i<4; i++, (drive[2])++) {
  250.     disabled[i] = (BOOL)(FindDosEntry(pDL, drive, flags) == NULL);
  251.   }
  252.   UnLockDosList(flags);
  253.  
  254. /**
  255.  | Gadtools preamble: font, screen, VisualInfo, offsets.
  256. **/
  257.  
  258.   if ((GadFont = OpenFont(&t80)) == NULL   ||
  259.       (pScr = LockPubScreen("Workbench")) == NULL)
  260.           Error("Couldn't lock Workbench screen");
  261.  
  262.   if ((pVI = GetVisualInfo(pScr, TAG_END)) == NULL ||
  263.       (pG = CreateContext(&pGlist)) == NULL)
  264.           Error("Error from CreateContext");
  265.  
  266.   xOffset = pScr->WBorLeft;
  267.   yOffset = pScr->WBorTop + (WORD) pScr->RastPort.TxHeight + 1;
  268.  
  269. /**
  270.  | Gadtools button gadgets
  271. **/
  272.  
  273.   ng.ng_LeftEdge += xOffset;
  274.   ng.ng_TopEdge  += yOffset;
  275.   ng.ng_VisualInfo = pVI;
  276.  
  277.   for (i=0; i<NUM_BUT; i++) {
  278.     ng.ng_GadgetText = gadText[i];
  279.     ng.ng_GadgetID   = gadID[i];
  280.     pG = CreateGadget(BUTTON_KIND, pG, &ng,
  281.                       GA_Disabled,  disabled[i],
  282.                       TAG_DONE);
  283.     ng.ng_LeftEdge += (i == 3 ? GAD_DX2 : GAD_DX1);
  284.   }
  285.  
  286. /**
  287.  | Gadtools checkbutton gadget
  288. **/
  289.  
  290.   ng.ng_LeftEdge   = xOffset + CBG_LEFT;
  291.   ng.ng_TopEdge    = yOffset + CBG_TOP;
  292.   ng.ng_Width      = CBG_WIDTH;
  293.   ng.ng_Height     = CBG_HEIGHT;
  294.   ng.ng_GadgetText = "List file names";
  295.   ng.ng_GadgetID   = BUT_FN;
  296.   ng.ng_Flags      = PLACETEXT_RIGHT;
  297.   pG = CreateGadget(CHECKBOX_KIND, pG, &ng,
  298.                     GTCB_Checked,   TRUE,
  299.                     TAG_DONE);
  300.  
  301. /**
  302.  | Gadtools scroller gadget
  303. **/
  304.  
  305.   ng.ng_LeftEdge   = xOffset + SCR_LEFT;
  306.   ng.ng_TopEdge    = yOffset + SCR_TOP;
  307.   ng.ng_Width      = SCR_WIDTH;
  308.   ng.ng_Height     = SCR_HEIGHT;
  309.   ng.ng_GadgetText = NULL;
  310.   ng.ng_GadgetID   = SCROLLER;
  311.   ng.ng_Flags      = 0;
  312.  
  313.   if ((pScroller = pG = CreateGadget(SCROLLER_KIND, pG, &ng,
  314.                         GTSC_Arrows,   ARROW_HEIGHT,
  315.                         PGA_Freedom,   LORIENT_VERT,
  316.                         GA_Immediate,  TRUE,
  317.                         GA_RelVerify,  TRUE,
  318.                         TAG_DONE)) == NULL) {
  319.     Error("Error from CreateGadget");
  320.   }
  321.  
  322. /**
  323.  | Break window gadgets
  324. **/
  325.  
  326.   if ((pG = CreateContext(&pBGad)) == NULL) {
  327.     Error("Error from CreateContext (2)");
  328.   }
  329.  
  330.   ng.ng_LeftEdge    = xOffset + BKG_LEFT;
  331.   ng.ng_TopEdge     = yOffset + BKG_TOP;
  332.   ng.ng_Width       = BKG_WIDTH;
  333.   ng.ng_Height      = BKG_HEIGHT;
  334.   ng.ng_GadgetText  = "Click here to Break";
  335.   ng.ng_GadgetID    = BUT_BREAK;
  336.   ng.ng_Flags       = PLACETEXT_IN;
  337.  
  338.   if ((pG = CreateGadget(BUTTON_KIND, pG, &ng,
  339.                          TAG_DONE)) == NULL) {
  340.     Error("Error from Creategadget (2)");
  341.   }
  342.  
  343. /**
  344.  | Output window
  345. **/
  346.  
  347.   zoomBox[0] += xOffset;
  348.   zoomBox[1] += yOffset;
  349.   zoomBox[3]  = yOffset;
  350.  
  351.   if ((pWind = OpenWindowTags(NULL,
  352.                               WA_Left,           WIN_LEFT,
  353.                               WA_Top,            WIN_TOP,
  354.                               WA_Width,          WIN_WIDTH + xOffset,
  355.                               WA_Height,         WIN_HEIGHT + yOffset,
  356.                               WA_Activate,       TRUE,
  357.                               WA_DragBar,        TRUE,
  358.                               WA_DepthGadget,    TRUE,
  359.                               WA_Zoom,           zoomBox,
  360.                               WA_SmartRefresh,   TRUE,
  361.                               WA_IDCMP,          WIN_IDCMP,
  362.                               WA_Title,          PROG_NAME " v" REVISION,
  363.                               WA_Gadgets,        pGlist,
  364.                               WA_PubScreen,      pScr,
  365.                               TAG_DONE)) == NULL) {
  366.     Error("Error from OpenWindow");
  367.   }
  368.  
  369.   GT_RefreshWindow(pWind, NULL);
  370. }
  371.