home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Documentation / develop / develop Issue 15 / develop 15 code / Managing Component Registration / Curly.c < prev    next >
Encoding:
Text File  |  1993-06-02  |  6.5 KB  |  283 lines  |  [TEXT/KAHL]

  1. /*
  2.     File:        Curly.c
  3.  
  4.     Contains:    Curly component routines.
  5.  
  6.                 Refer to develop Issue 15, "Managing Component Registration",
  7.                 for details on this code.
  8.  
  9.     Written by:    Gary Woodcock
  10.  
  11.     Copyright:    © 1993 by Apple Computer, Inc.
  12.  
  13.     Change History (most recent first):
  14.  
  15. */
  16.  
  17. //-----------------------------------------------------------------------
  18. // Includes
  19.  
  20. #include "CurlyPrivate.h"
  21.  
  22. #include <Errors.h>
  23. #include <Memory.h>
  24. #include <Packages.h>
  25. #include <SysEqu.h>
  26. #include <OSUtils.h>
  27.  
  28. //-----------------------------------------------------------------------
  29.  
  30. #ifdef BUILD_LINKED
  31.  
  32. // Use this declaration when we're running linked (for debugging)
  33. pascal ComponentResult
  34. CurlyDispatcher (ComponentParameters *params, Handle storage)
  35.                                              
  36. #else
  37.  
  38. // Use this declaration when we're a standalone component
  39. pascal ComponentResult
  40. main (ComponentParameters *params, Handle storage)
  41.  
  42. #endif BUILD_LINKED
  43.  
  44. {
  45.     // This routine is the main dispatcher for the component
  46.     
  47.     ComponentResult        result = noErr;
  48.     ComponentFunction    curlyFunction = nil;
  49.     
  50.     // Did we get a Component Manager request code (< 0)?
  51.     if (params->what < 0)
  52.     {
  53.         switch (params->what)
  54.         {
  55.             case kComponentOpenSelect:            // Open request
  56.             {
  57.                 curlyFunction = _CurlyOpen;
  58.                 break;
  59.             }
  60.             case kComponentCloseSelect:            // Close request
  61.             {
  62.                 curlyFunction = _CurlyClose;
  63.                 break;
  64.             }
  65.             case kComponentCanDoSelect:            // Can Do request
  66.             {
  67.                 result = CallComponentFunction (params, 
  68.                     (ComponentFunction) _CurlyCanDo);
  69.                 break;
  70.             }
  71.             case kComponentVersionSelect:        // Version request
  72.             {
  73.                 result = CallComponentFunction (params,
  74.                     (ComponentFunction) _CurlyVersion);
  75.                 break;
  76.             }
  77.             case kComponentRegisterSelect:        // Register request
  78.             {
  79.                 result = CallComponentFunction (params,
  80.                     (ComponentFunction) _CurlyRegister);
  81.                 break;
  82.             }
  83.             case kComponentTargetSelect:        // Target request not supported
  84.             case kComponentUnregisterSelect:    // Unregister request not supported
  85.             default:                            // Unknown request
  86.             {
  87.                 result = badComponentSelector;
  88.                 break;
  89.             }
  90.         }
  91.     }
  92.     else    // Unknown request
  93.     {
  94.         result = badComponentSelector;
  95.     }
  96.     if (curlyFunction != nil)
  97.     {
  98.         result = CallComponentFunctionWithStorage (storage, params, curlyFunction);
  99.     }
  100.     return (result);
  101. }
  102.                                              
  103. //-----------------------------------------------------------------------
  104.  
  105. pascal ComponentResult
  106. _CurlyOpen (Handle storage, ComponentInstance self)
  107. {
  108.     #pragma    unused (storage)
  109.     
  110.     ComponentResult            result = noErr;
  111.     
  112.     #ifdef THINK_C
  113.     SetComponentInstanceA5 (self, *(long *) CurrentA5);
  114.     #endif THINK_C
  115.     
  116.     // Can we open another instance?
  117.     if (CountComponentInstances ((Component) self) <= kMaxCurlyInstances)
  118.     {
  119.         // Set up our instance storage
  120.         CurlyPrivateGlobalsHdl    globals = (CurlyPrivateGlobalsHdl) NewHandleClear (sizeof (CurlyPrivateGlobals));
  121.  
  122.         // Did we get our storage?
  123.         if (globals != nil)
  124.         {
  125.             // Keep a reference to self
  126.             (**globals).self = (Component) self;
  127.                         
  128.             // Set storage ref
  129.             SetComponentInstanceStorage (self, (Handle) globals);
  130.         }
  131.         else    // NewHandleClear failed
  132.         {
  133.             result = MemError();
  134.         }
  135.     }
  136.     else    // No more instances can be opened
  137.     {
  138.         result = -1L;    // Return anonymous error
  139.     }
  140.     return (result);
  141. }
  142.  
  143. //-----------------------------------------------------------------------
  144.  
  145. pascal ComponentResult
  146. _CurlyClose (Handle storage, ComponentInstance self)
  147. {
  148.     CurlyPrivateGlobalsHdl    globals = (CurlyPrivateGlobalsHdl) storage;
  149.     ComponentResult            result = noErr;
  150.     
  151.     // Do we have any clean up to do?
  152.     if (globals != nil)
  153.     {
  154.         // Dispose globals
  155.         DisposHandle ((Handle) globals);
  156.     }
  157.     return (result);
  158. }
  159.  
  160. //-----------------------------------------------------------------------
  161.  
  162. pascal ComponentResult
  163. _CurlyCanDo (short selector)
  164. {
  165.     // Case on the request code
  166.     switch (selector)
  167.     {
  168.         // Component Manager request codes
  169.         case kComponentOpenSelect:
  170.         case kComponentCloseSelect:
  171.         case kComponentCanDoSelect:
  172.         case kComponentVersionSelect:
  173.         case kComponentRegisterSelect:
  174.         {
  175.             return (true);
  176.         }
  177.         
  178.         // Unsupported or unknown request codes
  179.         case kComponentTargetSelect:        // Target request not supported
  180.         case kComponentUnregisterSelect:    // Unregister request not supported
  181.         default:                            // Not a request code we recognize
  182.         {
  183.             return (false); 
  184.         }
  185.     }
  186. }
  187.  
  188. //-----------------------------------------------------------------------
  189.  
  190. pascal ComponentResult
  191. _CurlyVersion (void)
  192. {
  193.     // Return the version info
  194.     return (curlyInterfaceRev);
  195. }
  196.  
  197. //-----------------------------------------------------------------------
  198.  
  199. pascal ComponentResult
  200. _CurlyRegister (void)
  201. {
  202.     ComponentDescription    theDesc;
  203.     Component                moeCompID;
  204.     Component                larryCompID;
  205.     long                    dummy;
  206.     
  207.     // Set up component description for Moe
  208.     theDesc.componentType = 'moe ';
  209.     theDesc.componentSubType = kAnyComponentSubType;
  210.     theDesc.componentManufacturer = 'appl';
  211.     theDesc.componentFlags = 0L;
  212.     theDesc.componentFlagsMask = kAnyComponentFlagsMask;
  213.     
  214.     // Try to find Moe
  215.     moeCompID = FindNextComponent (nil, &theDesc);
  216.     
  217.     // Set up component description for Larry
  218.     theDesc.componentType = 'lary';
  219.     theDesc.componentSubType = kAnyComponentSubType;
  220.     theDesc.componentManufacturer = 'appl';
  221.     theDesc.componentFlags = 0L;
  222.     theDesc.componentFlagsMask = kAnyComponentFlagsMask;
  223.     
  224.     // Try to find Larry
  225.     larryCompID = FindNextComponent (nil, &theDesc);
  226.     
  227.     // If we found both Moe and Larry, we can register
  228.     if ((moeCompID != 0L) && (larryCompID != 0L))
  229.     {
  230.         // Beep three times to let us know Curly's registered
  231.         SysBeep(5);
  232.         SysBeep(5);
  233.         SysBeep(5);
  234.         
  235.         // Delay for a sec (just to keep all the components' beeps from
  236.         // running together)
  237.         Delay (60, &dummy);
  238.     }
  239.     
  240.     return (((moeCompID != 0L) && (larryCompID != 0L)) ? 0L : 1L);
  241. }
  242.  
  243. //-----------------------------------------------------------------------
  244.  
  245. #ifdef THINK_C
  246. #ifdef BUILD_LINKED
  247.  
  248. Component
  249. RegisterCurly (void)
  250. {
  251.     ComponentDescription    theDesc;
  252.     Handle                    name;
  253.     Component                theComp;
  254.  
  255.     // Set up component description
  256.       theDesc.componentType = 'crly';
  257.       theDesc.componentSubType = kAnyComponentSubType;
  258.     theDesc.componentManufacturer = 'appl';
  259.       theDesc.componentFlags = cmpWantsRegisterMessage;
  260.       theDesc.componentFlagsMask = kAnyComponentFlagsMask;
  261.  
  262.     // Name the component
  263.     PtrToHand ("\pCurly (linked)", &name, 14);
  264.     
  265.     // Register with the component main entry point
  266.     theComp = RegisterComponent (&theDesc, (void *)CurlyDispatcher, 0, name, 0, 0);
  267.     
  268.     // Clean up
  269.     if (name != nil)
  270.     {
  271.         DisposHandle (name);
  272.     }
  273.     return (theComp);
  274. }
  275.  
  276. #endif BUILD_LINKED
  277. #endif THINK_C
  278.  
  279. //-----------------------------------------------------------------------
  280.  
  281.  
  282.  
  283.