QISAPlatform.c

/*
    File:       QISAPlatform.c
 
    Contains:   Implementation of the platform-specific dispatcher.
 
    Written by: DTS
 
    Copyright:  Copyright © 2002 by Apple Computer, Inc., All Rights Reserved.
 
    Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
                ("Apple") in consideration of your agreement to the following terms, and your
                use, installation, modification or redistribution of this Apple software
                constitutes acceptance of these terms.  If you do not agree with these terms,
                please do not use, install, modify or redistribute this Apple software.
 
                In consideration of your agreement to abide by the following terms, and subject
                to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
                copyrights in this original Apple software (the "Apple Software"), to use,
                reproduce, modify and redistribute the Apple Software, with or without
                modifications, in source and/or binary forms; provided that if you redistribute
                the Apple Software in its entirety and without modifications, you must retain
                this notice and the following text and disclaimers in all such redistributions of
                the Apple Software.  Neither the name, trademarks, service marks or logos of
                Apple Computer, Inc. may be used to endorse or promote products derived from the
                Apple Software without specific prior written permission from Apple.  Except as
                expressly stated in this notice, no other rights or licenses, express or implied,
                are granted by Apple herein, including but not limited to any patent rights that
                may be infringed by your derivative works or by other works in which the Apple
                Software may be incorporated.
 
                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
                COMBINATION WITH YOUR PRODUCTS.
 
                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
    Change History (most recent first):
 
*/
 
/////////////////////////////////////////////////////////////////
 
#include "QISAPlatform.h"
 
// System interfaces
 
#include <stdio.h>
 
// MoreIsBetter interfaces
 
#include "MoreCFQ.h"
#include "MoreOSUtils.h"
 
// QISA interfaces
 
#include "QISA.h"
 
/////////////////////////////////////////////////////////////////
#pragma mark ***** Global Variables
 
#pragma mark gPlugInBundle
 
// gPlugInBundle holds a reference to the bundle of the platform plug-in that 
// we're talking to.  We store it in a global because we have to hold on to 
// a reference for the life of the application (if we release it, CF will unload 
// our plug-in!).
 
static CFBundleRef gPlugInBundle;
 
/////////////////////////////////////////////////////////////////
#pragma mark ***** Implemented Locally
 
extern pascal OSStatus QISACopyPlatformProperties(CFDictionaryRef *platDict)
    // See comments in header.
    //
    // Note that this is currently implemented locally because the 
    // current implementation does not need dynamically calculated 
    // properties, so I can just read it out of a file.  If things 
    // change in the future, I can make this dispatch to the platform 
    // plug-in.
{
    OSStatus        err;
    CFURLRef        propertiesPlistURL;
    CFDictionaryRef platDictLocal;
    
    assert( platDict != NULL);
    assert(*platDict == NULL);
    
    assert(gPlugInBundle != NULL);
 
    platDictLocal = NULL;
    
    propertiesPlistURL = CFBundleCopyResourceURL(gPlugInBundle, CFSTR("PlatformProperties"), CFSTR("plist"), NULL);
    err = CFQError(propertiesPlistURL);
    if (err == noErr) {
        err = CFQPropertyListCreateFromXMLCFURL(propertiesPlistURL, kCFPropertyListImmutable, (CFPropertyListRef *) &platDictLocal);
    }
    if (err == noErr) {
        if ( CFGetTypeID(platDictLocal) != CFDictionaryGetTypeID() ) {
            err = kCFQDataErr;
        }
    }
    
    // Clean up.
 
    if (err != noErr) {
        CFQRelease(platDictLocal);
        platDictLocal = NULL;
    }   
    *platDict = platDictLocal;
    CFQRelease(propertiesPlistURL);
    
    assert( (err == noErr) == (*platDict != NULL) );
    
    return err;
}
 
/////////////////////////////////////////////////////////////////
#pragma mark ***** Dispatch
 
// Each of these routines just calls directly through to their platform equivalent.
 
typedef pascal OSStatus (*QISACreateCCLArrayProc)(CFArrayRef *result, CFIndex *indexOfDefaultCCL);
 
static QISACreateCCLArrayProc gQISACreateCCLArrayProcPtr;
 
extern pascal OSStatus QISACreateCCLArray(CFArrayRef *result, CFIndex *indexOfDefaultCCL)
    // See comment in header.
{
    assert(gQISACreateCCLArrayProcPtr != NULL);
    return gQISACreateCCLArrayProcPtr(result, indexOfDefaultCCL);
}
 
typedef pascal OSStatus (*QISACreatePortArrayProc)(CFArrayRef *portArray);
 
static QISACreatePortArrayProc gQISACreatePortArrayProcPtr;
 
extern pascal OSStatus QISACreatePortArray(CFArrayRef *portArray)
    // See comment in header.
{
    assert(gQISACreatePortArrayProcPtr != NULL);
    return gQISACreatePortArrayProcPtr(portArray);
}
 
typedef pascal OSStatus (*QISADoesNetworkConfigExistProc)(CFStringRef userVisibleName, Boolean *exists);
 
static QISADoesNetworkConfigExistProc gQISADoesNetworkConfigExistProcPtr;
 
extern pascal OSStatus QISADoesNetworkConfigExist(CFStringRef userVisibleName, Boolean *exists)
    // See comment in header.
{
    assert(gQISADoesNetworkConfigExistProcPtr != NULL);
    return gQISADoesNetworkConfigExistProcPtr(userVisibleName, exists);
}
 
typedef pascal OSStatus (*QISAMakeNetworkConfigProc)(CFMutableDictionaryRef configDict);
 
static QISAMakeNetworkConfigProc gQISAMakeNetworkConfigProcPtr;
 
extern pascal OSStatus QISAMakeNetworkConfig(CFMutableDictionaryRef configDict)
    // See comment in header.
{
    assert(gQISAMakeNetworkConfigProcPtr != NULL);
    return gQISAMakeNetworkConfigProcPtr(configDict);
}
 
typedef pascal OSStatus (*QISAPlatformInitProc)(CFBundleRef platformBundle);
 
static QISAPlatformInitProc gQISAPlatformInitProcPtr;
 
/////////////////////////////////////////////////////////////////
#pragma mark ***** Initialisation
 
extern pascal OSStatus QISAPlatformInit(CFBundleRef platformBundle)
    // See comment in header.
{
    OSStatus        err;
    CFURLRef        plugInsFolder;
    CFURLRef        plugInURL;
    CFStringRef     pluginName;
    
    plugInURL = NULL;
    
    // Find the plug-in within the application package.
    
    plugInsFolder = CFBundleCopyBuiltInPlugInsURL(platformBundle);
    err = CFQError(plugInsFolder);
    
    if (err == noErr) {
        if ( MoreRunningOnMacOSX() ) {
            pluginName = CFSTR("QISAPlatformMach-O.bundle");
        } else {
            pluginName = CFSTR("QISAPlatformCFM.bundle");
        }
        plugInURL = CFURLCreateCopyAppendingPathComponent(NULL, plugInsFolder, pluginName, true);
        err = CFQError(plugInURL);
    }
    
    // Create a bundle to load the code, and then get function pointers 
    // from the bundle.
    
    if (err == noErr) {
        gPlugInBundle = CFBundleCreate(NULL, plugInURL);
        err = CFQError(gPlugInBundle);
    }
    if (err == noErr) {
        gQISACreateCCLArrayProcPtr         =        (QISACreateCCLArrayProc) CFBundleGetFunctionPointerForName(gPlugInBundle, CFSTR("QISACreateCCLArray"));
        err = CFQError(gQISACreateCCLArrayProcPtr);
    }
    if (err == noErr) {
        gQISACreatePortArrayProcPtr        =        (QISACreatePortArrayProc) CFBundleGetFunctionPointerForName(gPlugInBundle, CFSTR("QISACreatePortArray"));
        err = CFQError(gQISACreatePortArrayProcPtr);
    }
    if (err == noErr) {
        gQISADoesNetworkConfigExistProcPtr =        (QISADoesNetworkConfigExistProc) CFBundleGetFunctionPointerForName(gPlugInBundle, CFSTR("QISADoesNetworkConfigExist"));
        err = CFQError(gQISADoesNetworkConfigExistProcPtr);
    }
    if (err == noErr) {
        gQISAMakeNetworkConfigProcPtr      =        (QISAMakeNetworkConfigProc) CFBundleGetFunctionPointerForName(gPlugInBundle, CFSTR("QISAMakeNetworkConfig"));
        err = CFQError(gQISAMakeNetworkConfigProcPtr);
    }
    if (err == noErr) {
        gQISAPlatformInitProcPtr           =        (QISAPlatformInitProc) CFBundleGetFunctionPointerForName(gPlugInBundle, CFSTR("QISAPlatformInit"));
        err = CFQError(gQISAPlatformInitProcPtr);
    }
    if (err == noErr) {
        err = gQISAPlatformInitProcPtr(gPlugInBundle);
    }
 
    // Clean up.
        
    if (err != noErr) {
        gQISACreateCCLArrayProcPtr         = NULL;
        gQISACreatePortArrayProcPtr        = NULL;
        gQISADoesNetworkConfigExistProcPtr = NULL;
        gQISAMakeNetworkConfigProcPtr      = NULL;
        gQISAMakeNetworkConfigProcPtr      = NULL;
        gQISAPlatformInitProcPtr           = NULL;
 
        CFQRelease(gPlugInBundle);
        gPlugInBundle = NULL;
    }
    CFQRelease(plugInsFolder);
    CFQRelease(plugInURL);
    
    return err;
}