home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of A1200
/
World_Of_A1200.iso
/
programs
/
disk
/
misc
/
disktest
/
source.lha
/
source
/
beginend.c
next >
Wrap
C/C++ Source or Header
|
1993-04-04
|
14KB
|
439 lines
/*---------------------------------------------------------*
| 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/asl_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 the ASL file/directory requester;
| - releases every resource needed for the gadtools gadgets, and
| unlocks the Workbench screen;
| - closes all the opened libraries:
| - restore the process' pr_WindowPtr.
**/
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);
}
ClearMenuStrip(pWind);
CloseWindow(pWind);
}
if (pFR != NULL) FreeAslRequest(pFR);
if (pGlist != NULL) FreeGadgets(pGlist);
if (pMenu != NULL) FreeMenus(pMenu);
if (pVI != NULL) FreeVisualInfo(pVI);
if (pScr != NULL) UnlockPubScreen(NULL, pScr);
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);
if (mySelf != NULL) mySelf->pr_WindowPtr = save_pr_WindowPtr;
exit(EXIT_OK);
}
void Init(void)
{
/**
| Main initialisation routine:
| - disable system requesters caused from this process;
| - initialise the Intuition stuff;
| - fill and initialise the text scroller.
**/
mySelf = (struct Process *) FindTask(NULL);
save_pr_WindowPtr = mySelf->pr_WindowPtr;
mySelf->pr_WindowPtr = (APTR) -1L;
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_.",
"",
"A third method are the menus, or their keyboard shortcuts."
};
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().
**/
short i;
struct Gadget *pG;
struct DosList *pDL;
ULONG flags;
char drive[] = "DF0";
static char bgText[] = "Click here to Break";
struct IntuiText bgIt = {
0, 0, 0, 0, 0, NULL, bgText, NULL
};
static struct NewMenu nm[] = {
{NM_TITLE, "Project", NULL, 0, 0, NULL},
{NM_ITEM, "About", NULL, 0, 0, (APTR) M_ABOUT},
{NM_ITEM, "Quit", "Q", 0, 0, (APTR) M_QUIT},
{NM_TITLE, "Disk Test", NULL, 0, 0, NULL},
{NM_ITEM, "DF0", "0", 0, 0, (APTR) M_DF0},
{NM_ITEM, "DF1", "1", 0, 0, (APTR) M_DF1},
{NM_ITEM, "DF2", "2", 0, 0, (APTR) M_DF2},
{NM_ITEM, "DF3", "3", 0, 0, (APTR) M_DF3},
{NM_TITLE, "Integrity Test", NULL, 0, 0, NULL},
{NM_ITEM, "Device/Directory", "D", 0, 0, (APTR) M_DEVDIR},
{NM_ITEM, "File", "F", 0, 0, (APTR) M_FILE},
{NM_END, NULL, NULL, 0, 0, NULL}
};
#define MENU_DF0 4 /* nm[4] is the DF0 item */
struct NewGadget ng = {
GAD_LEFT, GAD_TOP,
GAD_WIDTH, GAD_HEIGHT,
NULL, /* Gadget text */
NULL, /* Gadget font */
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);
for (i=0; i<4; i++) {
if (disabled[i]) {
nm[MENU_DF0+i].nm_Flags = NM_ITEMDISABLED;
}
}
/**
| Allocates an ASL file/directory requester
**/
if ((pFR = AllocAslRequestTags(ASL_FileRequest,
ASL_FuncFlags, FILF_MULTISELECT | FILF_PATGAD,
ASL_Pattern, (ULONG) "~(#?.info)",
ASL_Dir, "SYS:",
TAG_END)) == NULL) {
Error("Error from AllocAslrequest");
}
/**
| Gadtools preamble: screen, font, VisualInfo, offsets.
**/
if ((pScr = LockPubScreen("Workbench")) == NULL)
Error("Couldn't lock Workbench screen");
ng.ng_TextAttr = bgIt.ITextFont = pScr->Font;
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;
/**
| Menus
**/
if ((pMenu = CreateMenus(nm, TAG_DONE)) == NULL ||
LayoutMenus(pMenu, pVI, TAG_DONE) == NULL) {
Error("Error from Create/LayoutMenus");
}
/**
| 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");
}
/**
| The "break window" and its button gadget: computes their location
| and dimensions using the width and height of the gadget text.
**/
if ((pG = CreateContext(&pBGad)) == NULL) {
Error("Error from CreateContext (2)");
}
ng.ng_Width = (WORD) (IntuiTextLength(&bgIt) + 2 * BKT_XOFFSET);
ng.ng_Height = (WORD) (pScr->Font->ta_YSize + 2 * BKT_YOFFSET);
ng.ng_LeftEdge = xOffset + BKT_XOFFSET;
ng.ng_TopEdge = yOffset + BKT_YOFFSET;
ng.ng_GadgetText = bgText;
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)");
}
bkwWidth = ng.ng_Width + 2 * BKT_XOFFSET + xOffset + pScr->WBorRight;
bkwHeight = ng.ng_Height + 2 * BKT_YOFFSET + yOffset + pScr->WBorBottom;
bkwLeft = BEVEL_LEFT + BEVEL_WIDTH - bkwWidth + xOffset;
bkwTop = BEVEL_TOP + BEVEL_HEIGHT - bkwHeight + yOffset;
/**
| 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");
}
(void) SetMenuStrip(pWind, pMenu);
GT_RefreshWindow(pWind, NULL);
}