home *** CD-ROM | disk | FTP | other *** search
- /*---------------------------------------------------------*
- | File: BEGINEND.c - Initialisation, and Cleanup routines |
- +---------------------------------------------------------+
- | Author: Maurizio Loreti, aka MLO or I3NOO. |
- | Address: University of Padova - Department of Physics |
- | Via F. Marzolo, 8 - 35131 PADOVA - Italy |
- | Phone: (39)(49) 844-313 FAX: (39)(49) 844-245 |
- | E-Mail: loreti@padova.infn.it (TCP/IP) |
- | Home: Via G. Donizetti 6 - 35010 CADONEGHE (PD) - Italy |
- *---------------------------------------------------------*/
-
- /**
- | #includes
- **/
-
- #include <stddef.h> /* Standard library */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <exec/types.h> /* Amiga specific */
- #include <exec/memory.h>
- #include <intuition/gadgetclass.h>
- #include <dos/dos.h>
- #include <clib/exec_protos.h>
- #include <clib/graphics_protos.h>
- #include <clib/intuition_protos.h>
- #include <clib/gadtools_protos.h>
- #include <clib/alib_protos.h>
- #include <clib/wb_protos.h>
- #include <clib/dos_protos.h>
- #include "main.h" /* Local stuff */
- #include "beginend.h"
- #include "ext.h"
-
- void Cleanup(void)
- {
- /**
- | This routines releases all allocated resources before
- | exiting to the operating system.
- | Since the program detaches itself from the CLI, we don't
- | need to return an error code if something strange has been
- | detected: the return code is always "EXIT_OK".
- |
- | - frees all memory obtained from malloc() or AllocMem();
- | - frees the IOrequest and deletes the port of the trackdisk.device;
- | - closes the "break" window;
- | - closes the AppWindow stuff;
- | - closes the output window;
- | - releases every resource needed for the gadtools gadgets, and
- | unlocks the Workbench screen;
- | - closes all the libraries.
- **/
-
- if (pClearRP != NULL) free(pClearRP);
- if (diskBuffer != NULL) FreeMem(diskBuffer, diskBufferLength);
- if (pFIB != NULL) FreeMem(pFIB, sizeof(struct FileInfoBlock));
- ClearText(FALSE);
-
- if (diskReq != NULL) {
- CloseDevice((struct IORequest *) diskReq);
- DeleteExtIO((struct IORequest *) diskReq);
- }
- if (diskPort != NULL) DeletePort(diskPort);
-
- if (pBWind != NULL) {
- struct IntuiMessage *pIM;
-
- while ((pIM = GT_GetIMsg(pBWind->UserPort)) != NULL) {
- GT_ReplyIMsg(pIM);
- }
- CloseWindow(pBWind);
- }
- if (pBGad != NULL) FreeGadgets(pBGad);
-
- if (pAWind != NULL) {
- struct Message *pM;
-
- while ((pM = GetMsg(appWinPort)) != NULL) {
- ReplyMsg(pM);
- }
- (void) RemoveAppWindow(pAWind);
- }
- if (appWinPort != NULL) DeletePort(appWinPort);
-
- if (pWind != NULL) {
- struct IntuiMessage *pIM;
-
- while ((pIM = GT_GetIMsg(pWind->UserPort)) != NULL) {
- GT_ReplyIMsg(pIM);
- }
- CloseWindow(pWind);
- }
-
- if (pGlist != NULL) FreeGadgets(pGlist);
- if (pVI != NULL) FreeVisualInfo(pVI);
- if (pScr != NULL) UnlockPubScreen(NULL, pScr);
- if (GadFont != NULL) CloseFont(GadFont);
-
- if (WorkbenchBase != NULL) CloseLibrary(WorkbenchBase);
- if (GadToolsBase != NULL) CloseLibrary(GadToolsBase);
- if (LayersBase != NULL) CloseLibrary(LayersBase);
- if (GfxBase != NULL) CloseLibrary(GfxBase);
- if (IntuitionBase != NULL) CloseLibrary(IntuitionBase);
-
- exit(EXIT_OK);
- }
-
- void Init(void)
- {
- /**
- | Main initialisation routine
- **/
-
- IntuiInit();
-
- if ((pClearRP = malloc(sizeof(struct RastPort))) == NULL ||
- (pFIB = (struct FileInfoBlock *)
- AllocMem(sizeof(struct FileInfoBlock), MEMF_CLEAR)) == NULL) {
- Error("Not enough memory");
- }
-
- HelpScreen();
- InitScroller();
- }
-
- /*-------------------------- Local Procedures --------------------------*/
-
- static void HelpScreen(void)
- {
- /**
- | Fills the output scroller with a short help text,
- | to be displayed at the program start.
- **/
-
- size_t i;
- char *helpText[] = {
- VersionTag + 7,
- "",
- "You can now click on a button gadget: the DF buttons start the",
- "full disk test (defective tracks, and file integrity) for the",
- "selected drive, while the QUIT button terminates the program.",
- "",
- "You can also drop an icon into this window: for file related",
- "icons (ie tool or project), DT will check the integrity of the",
- "corresponding file; otherwise (disk, drawer or trashcan icons)",
- "DT will perform the integrity test on _all_ files found in the",
- "related [root] directory, and _in every underlying directory_."
- };
-
- for (i=0; i<(sizeof(helpText)/sizeof(char *)); i++) {
- AddLine(helpText[i], strlen(helpText[i]), FALSE);
- }
- }
-
- static void IntuiInit(void)
- {
- /**
- | This routine performs the Intuition part of the initialisation:
- | - opens all the needed libraries;
- | - finds how many and which floppy units are on the system;
- | - creates all the gadtools gadgets to be put in the output window;
- | - creates the gadtools gadgets to be put in the "break" window;
- | - opens the output window itself.
- |
- | If an error is detected in this phase, IntuiInit() exits calling
- | Error(); the only exception is when the v37 libraries were not
- | found, because Error() uses an EasyRequester (available in v37
- | only); in that case IntuiInit displays an AutoRequester on the
- | Workbench window (we need intuition.library, that is opened at
- | every available revision) and exits calling Cleanup().
- **/
-
- WORD xOffset;
- WORD yOffset;
- short i;
- struct Gadget *pG;
- struct DosList *pDL;
- ULONG flags;
- char drive[] = "DF0";
-
- static struct TextAttr t80 = { /* Text used for button gadgets */
- "topaz.font", TOPAZ_EIGHTY, /* (plain topaz-80) */
- FS_NORMAL, FPF_ROMFONT
- };
-
- struct NewGadget ng = {
- GAD_LEFT, GAD_TOP,
- GAD_WIDTH, GAD_HEIGHT,
- NULL, /* Gadget text */
- &t80,
- 0, /* Gadget ID */
- PLACETEXT_IN,
- NULL, /* GetVisualInfo() ret. val. */
- NULL
- };
-
- char *gadText[NUM_BUT] = { /* Button gadgets text */
- "DF0", "DF1", "DF2",
- "DF3", "Quit"
- };
-
- UWORD gadID[NUM_BUT] = { /* Button gadgets ID */
- BUT_1, BUT_2, BUT_3,
- BUT_4, BUT_QUIT
- };
-
- BOOL disabled[NUM_BUT] = { /* Button gadgets state */
- FALSE, FALSE, FALSE,
- FALSE, FALSE
- };
-
- WORD zoomBox[4] = {
- ZB_LEFT, ZB_TOP, ZB_WIDTH, 0
- };
-
- /**
- | Libraries
- **/
-
- IntuitionBase = OpenLibrary("intuition.library", OS_MINREV);
- GfxBase = OpenLibrary("graphics.library", OS_MINREV);
- LayersBase = OpenLibrary("layers.library", OS_MINREV);
- GadToolsBase = OpenLibrary("gadtools.library", OS_MINREV);
- WorkbenchBase = OpenLibrary("workbench.library", OS_MINREV);
-
- if (IntuitionBase == NULL || LayersBase == NULL || GfxBase == NULL ||
- GadToolsBase == NULL || WorkbenchBase == NULL) {
- struct IntuiText it = {
- 0, 1, JAM2, AR_ITX, AR_ITY, NULL,
- "DiskTest requires OS 2.04 (v37)", NULL
- };
- struct IntuiText ok = {
- 0, 1, JAM2, AR_OKX, AR_OKY, NULL, "OK", NULL
- };
-
- if (IntuitionBase != NULL ||
- (IntuitionBase = OpenLibrary("intuition.library", 0)) != NULL) {
- (void) AutoRequest(NULL, &it, NULL, &ok, 0, 0, AR_WX, AR_WY);
- }
- Cleanup();
- }
-
- /**
- | Connected floppies
- **/
-
- flags = LDF_DEVICES | LDF_READ;
- pDL = LockDosList(flags);
- for (i=0; i<4; i++, (drive[2])++) {
- disabled[i] = (BOOL)(FindDosEntry(pDL, drive, flags) == NULL);
- }
- UnLockDosList(flags);
-
- /**
- | Gadtools preamble: font, screen, VisualInfo, offsets.
- **/
-
- if ((GadFont = OpenFont(&t80)) == NULL ||
- (pScr = LockPubScreen("Workbench")) == NULL)
- Error("Couldn't lock Workbench screen");
-
- if ((pVI = GetVisualInfo(pScr, TAG_END)) == NULL ||
- (pG = CreateContext(&pGlist)) == NULL)
- Error("Error from CreateContext");
-
- xOffset = pScr->WBorLeft;
- yOffset = pScr->WBorTop + (WORD) pScr->RastPort.TxHeight + 1;
-
- /**
- | Gadtools button gadgets
- **/
-
- ng.ng_LeftEdge += xOffset;
- ng.ng_TopEdge += yOffset;
- ng.ng_VisualInfo = pVI;
-
- for (i=0; i<NUM_BUT; i++) {
- ng.ng_GadgetText = gadText[i];
- ng.ng_GadgetID = gadID[i];
- pG = CreateGadget(BUTTON_KIND, pG, &ng,
- GA_Disabled, disabled[i],
- TAG_DONE);
- ng.ng_LeftEdge += (i == 3 ? GAD_DX2 : GAD_DX1);
- }
-
- /**
- | Gadtools checkbutton gadget
- **/
-
- ng.ng_LeftEdge = xOffset + CBG_LEFT;
- ng.ng_TopEdge = yOffset + CBG_TOP;
- ng.ng_Width = CBG_WIDTH;
- ng.ng_Height = CBG_HEIGHT;
- ng.ng_GadgetText = "List file names";
- ng.ng_GadgetID = BUT_FN;
- ng.ng_Flags = PLACETEXT_RIGHT;
- pG = CreateGadget(CHECKBOX_KIND, pG, &ng,
- GTCB_Checked, TRUE,
- TAG_DONE);
-
- /**
- | Gadtools scroller gadget
- **/
-
- ng.ng_LeftEdge = xOffset + SCR_LEFT;
- ng.ng_TopEdge = yOffset + SCR_TOP;
- ng.ng_Width = SCR_WIDTH;
- ng.ng_Height = SCR_HEIGHT;
- ng.ng_GadgetText = NULL;
- ng.ng_GadgetID = SCROLLER;
- ng.ng_Flags = 0;
-
- if ((pScroller = pG = CreateGadget(SCROLLER_KIND, pG, &ng,
- GTSC_Arrows, ARROW_HEIGHT,
- PGA_Freedom, LORIENT_VERT,
- GA_Immediate, TRUE,
- GA_RelVerify, TRUE,
- TAG_DONE)) == NULL) {
- Error("Error from CreateGadget");
- }
-
- /**
- | Break window gadgets
- **/
-
- if ((pG = CreateContext(&pBGad)) == NULL) {
- Error("Error from CreateContext (2)");
- }
-
- ng.ng_LeftEdge = xOffset + BKG_LEFT;
- ng.ng_TopEdge = yOffset + BKG_TOP;
- ng.ng_Width = BKG_WIDTH;
- ng.ng_Height = BKG_HEIGHT;
- ng.ng_GadgetText = "Click here to Break";
- ng.ng_GadgetID = BUT_BREAK;
- ng.ng_Flags = PLACETEXT_IN;
-
- if ((pG = CreateGadget(BUTTON_KIND, pG, &ng,
- TAG_DONE)) == NULL) {
- Error("Error from Creategadget (2)");
- }
-
- /**
- | Output window
- **/
-
- zoomBox[0] += xOffset;
- zoomBox[1] += yOffset;
- zoomBox[3] = yOffset;
-
- if ((pWind = OpenWindowTags(NULL,
- WA_Left, WIN_LEFT,
- WA_Top, WIN_TOP,
- WA_Width, WIN_WIDTH + xOffset,
- WA_Height, WIN_HEIGHT + yOffset,
- WA_Activate, TRUE,
- WA_DragBar, TRUE,
- WA_DepthGadget, TRUE,
- WA_Zoom, zoomBox,
- WA_SmartRefresh, TRUE,
- WA_IDCMP, WIN_IDCMP,
- WA_Title, PROG_NAME " v" REVISION,
- WA_Gadgets, pGlist,
- WA_PubScreen, pScr,
- TAG_DONE)) == NULL) {
- Error("Error from OpenWindow");
- }
-
- GT_RefreshWindow(pWind, NULL);
- }
-