home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 20 / AACD20.BIN / AACD / Programming / AmIDE / developer / example / LibInit.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-04  |  6.0 KB  |  181 lines

  1. /*
  2. **      $VER: LibInit.c 37.32 (2.3.99)
  3. **
  4. **      Library initializers and functions to be called by StartUp.c
  5. **
  6. **      (C) Copyright 1996-99 Andreas R. Kleinert
  7. **      All Rights Reserved.
  8. **
  9. **      Modifications for the AmIDE_API v1.0
  10. **      (C) Copyright 2001 Jens Langner
  11. **      All Rights Reserved.
  12. */
  13.  
  14. #define __USE_SYSBASE        // perhaps only recognized by SAS/C
  15.  
  16. #include <exec/types.h>
  17. #include <exec/memory.h>
  18. #include <exec/libraries.h>
  19. #include <exec/execbase.h>
  20. #include <exec/resident.h>
  21. #include <exec/initializers.h>
  22.  
  23. #ifdef __MAXON__
  24. #include <clib/exec_protos.h>
  25. #else
  26. #include <proto/exec.h>
  27. #endif
  28. #include "compiler.h"
  29. #include "sasc.module.h"
  30.  
  31. #include <amide/amide_api_base.h>
  32.  
  33. ULONG __saveds __stdargs L_OpenLibs(struct AmIDE_API_Base *API_Base);
  34. void  __saveds __stdargs L_CloseLibs(void);
  35.  
  36. struct ExecBase      *SysBase       = NULL;
  37. struct IntuitionBase *IntuitionBase = NULL;
  38. struct GfxBase       *GfxBase       = NULL;
  39. struct UtilityBase   *UtilityBase   = NULL;
  40. struct DOSBase       *DOSBase       = NULL;
  41.  
  42. #define VERSION  0
  43. #define REVISION 1
  44.  
  45. char __aligned LibName [] = LIBNAME;
  46. char __aligned LibID   [] = LIBNAME LIBVER;
  47. char __aligned Copyright [] = "(C)opyright 2001 by Jens Langner. All rights reserved.";
  48.  
  49. char __aligned VERSTRING [] = "\0$VER: " LIBNAME LIBVER;
  50.  
  51. /* ----------------------------------------------------------------------------------------
  52.    ! ROMTag and Library inilitalization structure:
  53.    !
  54.    ! Below you find the ROMTag, which is the most important "magic" part of a library
  55.    ! (as for any other resident module). You should not need to modify any of the
  56.    ! structures directly, since all the data is referenced from constants from somewhere else.
  57.    !
  58.    ! You may place the ROMTag directly after the LibStart (-> StartUp.c) function as well.
  59.    !
  60.    ! Note, that the data initialization structure may be somewhat redundant - it's
  61.    ! for demonstration purposes.
  62.    !
  63.    ! EndResident can be placed somewhere else - but it must follow the ROMTag and
  64.    ! it must not be placed in a different SECTION.
  65.    ---------------------------------------------------------------------------------------- */
  66.  
  67. extern ULONG InitTab[];
  68. extern APTR EndResident; /* below */
  69.  
  70. struct Resident __aligned ROMTag =     /* do not change */
  71. {
  72.  RTC_MATCHWORD,
  73.  &ROMTag,
  74.  &EndResident,
  75.  RTF_AUTOINIT,
  76.  VERSION,
  77.  NT_LIBRARY,
  78.  0,
  79.  &LibName[0],
  80.  &LibID[0],
  81.  &InitTab[0]
  82. };
  83.  
  84. APTR EndResident;
  85.  
  86. struct MyDataInit                      /* do not change */
  87. {
  88.  UWORD ln_Type_Init;      UWORD ln_Type_Offset;      UWORD ln_Type_Content;
  89.  UBYTE ln_Name_Init;      UBYTE ln_Name_Offset;      ULONG ln_Name_Content;
  90.  UWORD lib_Flags_Init;    UWORD lib_Flags_Offset;    UWORD lib_Flags_Content;
  91.  UWORD lib_Version_Init;  UWORD lib_Version_Offset;  UWORD lib_Version_Content;
  92.  UWORD lib_Revision_Init; UWORD lib_Revision_Offset; UWORD lib_Revision_Content;
  93.  UBYTE lib_IdString_Init; UBYTE lib_IdString_Offset; ULONG lib_IdString_Content;
  94.  ULONG ENDMARK;
  95. } DataTab =
  96. #ifdef VBCC
  97. {
  98.         0xe000,8,NT_LIBRARY,
  99.         0x0080,10,(ULONG) &LibName[0],
  100.         0xe000,LIBF_SUMUSED|LIBF_CHANGED,
  101.         0xd000,20,VERSION,
  102.         0xd000,22,REVISION,
  103.         0x80,24,(ULONG) &LibID[0],
  104.         (ULONG) 0
  105. };
  106. #else
  107. {
  108.  INITBYTE(OFFSET(Node,         ln_Type),      NT_LIBRARY),
  109.  0x80, (UBYTE) OFFSET(Node,    ln_Name),      (ULONG) &LibName[0],
  110.  INITBYTE(OFFSET(Library,      lib_Flags),    LIBF_SUMUSED|LIBF_CHANGED),
  111.  INITWORD(OFFSET(Library,      lib_Version),  VERSION),
  112.  INITWORD(OFFSET(Library,      lib_Revision), REVISION),
  113.  0x80, (UBYTE) OFFSET(Library, lib_IdString), (ULONG) &LibID[0],
  114.  (ULONG) 0
  115. };
  116. #endif
  117.  
  118.  
  119. /* ----------------------------------------------------------------------------------------
  120.    ! L_OpenLibs:
  121.    !
  122.    ! Since this one is called by InitLib, libraries not shareable between Processes or
  123.    ! libraries messing with RamLib (deadlock and crash) may not be opened here.
  124.    !
  125.    ! You may bypass this by calling this function fromout LibOpen, but then you will
  126.    ! have to a) protect it by a semaphore and b) make sure, that libraries are only
  127.    ! opened once (when using global library bases).
  128.    ---------------------------------------------------------------------------------------- */
  129.  
  130. ULONG __saveds __stdargs L_OpenLibs(struct AmIDE_API_Base *API_Base)
  131. {
  132.  SysBase = (*((struct ExecBase **) 4));
  133.  
  134.  IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37);
  135.  if(!IntuitionBase) return(FALSE);
  136.  
  137.  GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 37);
  138.  if(!GfxBase) return(FALSE);
  139.  
  140.  UtilityBase = (struct UtilityBase *) OpenLibrary("utility.library", 37);
  141.  if(!UtilityBase) return(FALSE);
  142.  
  143.  DOSBase = (struct DOSBase *) OpenLibrary("dos.library", 37);
  144.  if(!DOSBase) return(FALSE);
  145.  
  146.  // Creation of the Class
  147.  if( cl = MakeClass( NULL,  ROOTCLASS, NULL, sizeof(struct MODS_InstData), 0))
  148.  {
  149.     /* Fill in the callback hook */
  150.     cl->cl_Dispatcher.h_Entry = (ULONG (*) ())MODULE_Dispatcher;
  151.     cl->cl_UserData = NULL;
  152.  }
  153.  
  154.  API_Base->exb_IntuitionBase = IntuitionBase;
  155.  
  156.  API_Base->exb_GfxBase       = GfxBase;
  157.  API_Base->exb_SysBase       = SysBase;
  158.  
  159.  return(TRUE);
  160. }
  161.  
  162. /* ----------------------------------------------------------------------------------------
  163.    ! L_CloseLibs:
  164.    !
  165.    ! This one by default is called by ExpungeLib, which only can take place once
  166.    ! and thus per definition is single-threaded.
  167.    !
  168.    ! When calling this fromout LibClose instead, you will have to protect it by a
  169.    ! semaphore, since you don't know whether a given CloseLibrary(foobase) may cause a Wait().
  170.    ! Additionally, there should be protection, that a library won't be closed twice.
  171.    ---------------------------------------------------------------------------------------- */
  172.  
  173. void __saveds __stdargs L_CloseLibs(void)
  174. {
  175.  if( cl )          FreeClass(cl);
  176.  if(DOSBase)       CloseLibrary((struct Library *) DOSBase);
  177.  if(UtilityBase)   CloseLibrary((struct Library *) UtilityBase);
  178.  if(GfxBase)       CloseLibrary((struct Library *) GfxBase);
  179.  if(IntuitionBase) CloseLibrary((struct Library *) IntuitionBase);
  180. }
  181.