home *** CD-ROM | disk | FTP | other *** search
- /*
- * Standard Hook Startup Code, to make life easier.
- *
- */
-
- #include <exec/types.h>
- #include <exec/execbase.h>
- #include <intuition/intuition.h>
- #include <clib/dos_protos.h>
- #include <clib/exec_protos.h>
- #include <clib/intuition_protos.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <scan/hooks.h>
-
- /* C startup provides this one... */
- extern struct ExecBase *SysBase;
-
- /* Scan function library base... */
- struct ScanBase *ScanBase = NULL;
-
- /* These are supplied by Scan... */
- struct IntuitionBase *IntuitionBase = NULL;
- struct GfxBase *GfxBase = NULL;
-
- /* To know whether we have called InitHook() or not... */
- static BOOL hooked_in;
-
- /* Our old current directory... */
- static BPTR old_cd;
-
- /* Old task name... */
- static char *old_name;
-
- /* Hook program defines these... */
- extern char *HookName;
- extern char *HookText;
- extern int HookTextCount;
- extern char *HookStrings[];
-
- char **HookTextArray = NULL;
-
- static char **TextArray = NULL;
-
- /* Set this to TRUE if the hook should instruct ImageFX to close */
- /* (we do this by sending a QUIT command to IFX's arexx port) */
- /* WARNING: This can only be done by a hook that is running */
- /* asynchronously from ImageFX. Otherwise, hang city. */
- BOOL HookCloseIFX = FALSE;
-
-
- /*
- * Exit the hook, cleaning up as we go. Should be used in place of
- * the standard exit() call by the main Hook code.
- */
- void hook_exit (int rc)
- {
- struct Task *task;
- char portname[40];
-
- if (hooked_in) {
- if (TextArray) FreeText(TextArray, HookTextCount);
- CurrentDir(old_cd); /* back to our old current directory */
- CleanupHook();
- if (HookCloseIFX) strncpy(portname, ScanBase->sb_HostPort->mp_Node.ln_Name, 40);
- CloseLibrary((struct Library *)ScanBase);
- }
-
- task = FindTask(NULL);
- task->tc_Node.ln_Name = old_name;
-
- if (HookCloseIFX)
- {
- SendRexxMsg(portname, "QUIT FORCE");
- }
-
- exit(0); /* always return success; use SetError() to post
- an error code to imagefx */
-
- // exit(rc);
- }
-
- /*
- * Can be called by the main Hook code, produces an error message and
- * then exits the hook, cleaning up as appropriate.
- */
- void hook_fail (char *msg)
- {
- struct EasyStruct easy;
- BOOL we_opened_intui = FALSE;
-
- if (IntuitionBase == NULL) {
- IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0);
- we_opened_intui = TRUE;
- }
-
- if (ScanBase) {
-
- /* If library open, use a scan call... */
- Errorf(msg);
-
- }
- else {
-
- if (SysBase->LibNode.lib_Version >= 36) {
- /* Under 2.0 do a pleasant EasyRequest */
- easy.es_StructSize = sizeof(struct EasyStruct);
- easy.es_Flags = 0;
- easy.es_Title = HookName;
- easy.es_TextFormat = "%ls: %ls";
- easy.es_GadgetFormat = "Okay";
- EasyRequest(NULL, &easy, NULL, HookName, msg);
- }
- else {
- /* Will use a jarring alert under 1.3 I guess... */
- char buf[80];
- int le;
- buf[0] = 0;
- buf[1] = 10;
- buf[2] = (50-8)/2+6;
- sprintf(&buf[3], "%ls: %ls", HookName, msg);
- le = (640 - (strlen(&buf[3]) * 8)) / 2;
- buf[strlen(&buf[3])+4] = '\0';
- buf[0] = (le >> 8);
- buf[1] = le & 255;
- DisplayAlert(0x00000000L, buf, 50);
- }
-
- }
-
- if (IntuitionBase && we_opened_intui)
- CloseLibrary((struct Library *)IntuitionBase);
-
- hook_exit(0); /* we now do not prompt ifx to show another message */
-
- // hook_exit(20);
- }
-
- /*
- * Hook entry point... this does a little initialization and then calls
- * the main hook entry point, called hook_main().
- *
- * Montage will call us with the name of Montage's function library
- * in argv[1], and arguments to the hook in argv[2] and beyond.
- *
- */
- void main (int argc, char **argv)
- {
- struct Task *task;
- char *libname = "scan1.library";
-
- hooked_in = FALSE;
-
- task = FindTask(NULL);
-
- old_name = task->tc_Node.ln_Name;
- task->tc_Node.ln_Name = "ImageFX Hook";
-
- if (argc > 1) libname = argv[1];
-
- if (ScanBase == NULL) {
- ScanBase = (struct ScanBase *)OpenLibrary(libname, 0);
- if (ScanBase == NULL) {
- /* just in case we're using an old Montage: */
- ScanBase = (struct ScanBase *)OpenLibrary("monty1.library", 0);
- if (ScanBase == NULL)
- hook_fail("Cannot open ImageFX function library.");
- }
- }
-
- if (!InitHook()) hook_fail("Cannot communicate with ImageFX.");
-
- /*
- * Change this hook's current directory to that of Mirage.
- */
- old_cd = CurrentDir(ScanBase->sb_CurrentDir);
-
- hooked_in = TRUE;
-
- IntuitionBase = ScanBase->IntuitionBase;
- GfxBase = ScanBase->GfxBase;
-
- if (HookText) {
- TextArray = ReadText(HookText, HookTextCount);
- HookTextArray = TextArray;
- }
- if (!TextArray) {
- HookTextArray = HookStrings;
- }
-
- argv[1] = argv[0];
-
- SetError(0);
-
- hook_main(argc - 1, &argv[1]);
- hook_exit(0);
- }
-
-
- /*
- * GetStr() - Returns the text associated with a given string index
- * if available, otherwise returns the default text.
- */
- char *GetStr (int index, char *def)
- {
- if (HookTextArray)
- return(HookTextArray[index]);
- else
- return(def);
- }
-
- /*
- * HookGetStr() - Returns the text associated with a given string
- * index.
- *
- */
- char *HookGetStr (int index)
- {
- if (HookTextArray)
- return(HookTextArray[index]);
- else
- return(HookStrings[index]);
- }
-