home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gfx / imagefx_sdk-2.0.lha / ImageFX_SDK / sas / libsrc / start.c < prev   
Encoding:
C/C++ Source or Header  |  1994-10-18  |  5.5 KB  |  225 lines

  1. /*
  2.  * Standard Hook Startup Code, to make life easier.
  3.  *
  4.  */
  5.  
  6. #include <exec/types.h>
  7. #include <exec/execbase.h>
  8. #include <intuition/intuition.h>
  9. #include <clib/dos_protos.h>
  10. #include <clib/exec_protos.h>
  11. #include <clib/intuition_protos.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <scan/hooks.h>
  16.  
  17. /* C startup provides this one... */
  18. extern struct ExecBase  *SysBase;
  19.  
  20. /* Scan function library base... */
  21. struct ScanBase         *ScanBase = NULL;
  22.  
  23. /* These are supplied by Scan... */
  24. struct IntuitionBase    *IntuitionBase = NULL;
  25. struct GfxBase          *GfxBase = NULL;
  26.  
  27. /* To know whether we have called InitHook() or not... */
  28. static BOOL              hooked_in;
  29.  
  30. /* Our old current directory... */
  31. static BPTR              old_cd;
  32.  
  33. /* Old task name... */
  34. static char             *old_name;
  35.  
  36. /* Hook program defines these... */
  37. extern char             *HookName;
  38. extern char             *HookText;
  39. extern int               HookTextCount;
  40. extern char             *HookStrings[];
  41.  
  42. char                    **HookTextArray = NULL;
  43.  
  44. static char             **TextArray = NULL;
  45.  
  46. /* Set this to TRUE if the hook should instruct ImageFX to close */
  47. /* (we do this by sending a QUIT command to IFX's arexx port) */
  48. /* WARNING:  This can only be done by a hook that is running */
  49. /* asynchronously from ImageFX.  Otherwise, hang city. */
  50. BOOL                    HookCloseIFX = FALSE;
  51.  
  52.  
  53. /*
  54.  * Exit the hook, cleaning up as we go.  Should be used in place of
  55.  * the standard exit() call by the main Hook code.
  56.  */
  57. void hook_exit (int rc)
  58. {
  59.    struct Task *task;
  60.    char portname[40];
  61.  
  62.    if (hooked_in) {
  63.       if (TextArray) FreeText(TextArray, HookTextCount);
  64.       CurrentDir(old_cd);     /* back to our old current directory */
  65.       CleanupHook();
  66.       if (HookCloseIFX) strncpy(portname, ScanBase->sb_HostPort->mp_Node.ln_Name, 40);
  67.       CloseLibrary((struct Library *)ScanBase);
  68.    }
  69.  
  70.    task = FindTask(NULL);
  71.    task->tc_Node.ln_Name = old_name;
  72.  
  73.    if (HookCloseIFX)
  74.    {
  75.       SendRexxMsg(portname, "QUIT FORCE");
  76.    }
  77.  
  78.    exit(0);       /* always return success; use SetError() to post
  79.                      an error code to imagefx */
  80.  
  81. // exit(rc);
  82. }
  83.  
  84. /*
  85.  * Can be called by the main Hook code, produces an error message and
  86.  * then exits the hook, cleaning up as appropriate.
  87.  */
  88. void hook_fail (char *msg)
  89. {
  90.    struct EasyStruct easy;
  91.    BOOL we_opened_intui = FALSE;
  92.  
  93.    if (IntuitionBase == NULL) {
  94.       IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0);
  95.       we_opened_intui = TRUE;
  96.    }
  97.  
  98.    if (ScanBase) {
  99.  
  100.       /* If library open, use a scan call... */
  101.       Errorf(msg);
  102.  
  103.    }
  104.    else {
  105.  
  106.       if (SysBase->LibNode.lib_Version >= 36) {
  107.          /* Under 2.0 do a pleasant EasyRequest */
  108.          easy.es_StructSize = sizeof(struct EasyStruct);
  109.          easy.es_Flags = 0;
  110.          easy.es_Title = HookName;
  111.          easy.es_TextFormat = "%ls: %ls";
  112.          easy.es_GadgetFormat = "Okay";
  113.          EasyRequest(NULL, &easy, NULL, HookName, msg);
  114.       }
  115.       else {
  116.          /* Will use a jarring alert under 1.3 I guess... */
  117.          char buf[80];
  118.          int le;
  119.          buf[0] = 0;
  120.          buf[1] = 10;
  121.          buf[2] = (50-8)/2+6;
  122.          sprintf(&buf[3], "%ls: %ls", HookName, msg);
  123.          le = (640 - (strlen(&buf[3]) * 8)) / 2;
  124.          buf[strlen(&buf[3])+4] = '\0';
  125.          buf[0] = (le >> 8);
  126.          buf[1] = le & 255;
  127.          DisplayAlert(0x00000000L, buf, 50);
  128.       }
  129.  
  130.    }
  131.  
  132.    if (IntuitionBase && we_opened_intui)
  133.       CloseLibrary((struct Library *)IntuitionBase);
  134.  
  135.    hook_exit(0);     /* we now do not prompt ifx to show another message */
  136.  
  137. // hook_exit(20);
  138. }
  139.  
  140. /*
  141.  * Hook entry point... this does a little initialization and then calls
  142.  * the main hook entry point, called hook_main().
  143.  *
  144.  * Montage will call us with the name of Montage's function library
  145.  * in argv[1], and arguments to the hook in argv[2] and beyond.
  146.  *
  147.  */
  148. void main (int argc, char **argv)
  149. {
  150.    struct Task *task;
  151.    char *libname = "scan1.library";
  152.  
  153.    hooked_in = FALSE;
  154.  
  155.    task = FindTask(NULL);
  156.  
  157.    old_name = task->tc_Node.ln_Name;
  158.    task->tc_Node.ln_Name = "ImageFX Hook";
  159.  
  160.    if (argc > 1) libname = argv[1];
  161.  
  162.    if (ScanBase == NULL) {
  163.       ScanBase = (struct ScanBase *)OpenLibrary(libname, 0);
  164.       if (ScanBase == NULL) {
  165.          /* just in case we're using an old Montage: */
  166.          ScanBase = (struct ScanBase *)OpenLibrary("monty1.library", 0);
  167.          if (ScanBase == NULL)
  168.             hook_fail("Cannot open ImageFX function library.");
  169.       }
  170.    }
  171.  
  172.    if (!InitHook()) hook_fail("Cannot communicate with ImageFX.");
  173.  
  174.    /*
  175.     * Change this hook's current directory to that of Mirage.
  176.     */
  177.    old_cd = CurrentDir(ScanBase->sb_CurrentDir);
  178.  
  179.    hooked_in = TRUE;
  180.  
  181.    IntuitionBase = ScanBase->IntuitionBase;
  182.    GfxBase = ScanBase->GfxBase;
  183.  
  184.    if (HookText) {
  185.       TextArray = ReadText(HookText, HookTextCount);
  186.       HookTextArray = TextArray;
  187.    }
  188.    if (!TextArray) {
  189.       HookTextArray = HookStrings;
  190.    }
  191.  
  192.    argv[1] = argv[0];
  193.  
  194.    SetError(0);
  195.  
  196.    hook_main(argc - 1, &argv[1]);
  197.    hook_exit(0);
  198. }
  199.  
  200.  
  201. /*
  202.  * GetStr() - Returns the text associated with a given string index
  203.  *            if available, otherwise returns the default text.
  204.  */
  205. char *GetStr (int index, char *def)
  206. {
  207.    if (HookTextArray)
  208.       return(HookTextArray[index]);
  209.    else
  210.       return(def);
  211. }
  212.  
  213. /*
  214.  * HookGetStr() - Returns the text associated with a given string
  215.  *                index.
  216.  *
  217.  */
  218. char *HookGetStr (int index)
  219. {
  220.    if (HookTextArray)
  221.       return(HookTextArray[index]);
  222.    else
  223.       return(HookStrings[index]);
  224. }
  225.