EXPERIMENTAL! NEEDS TESTING! remove "offset" from almost everything in the process...
[ardour2.git] / libs / appleutility / CAComponent.cpp
blob700d9e2b3ed10c9069114958f16c8749df72ccdf
1 /* Copyright: © Copyright 2005 Apple Computer, Inc. All rights reserved.
3 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
4 ("Apple") in consideration of your agreement to the following terms, and your
5 use, installation, modification or redistribution of this Apple software
6 constitutes acceptance of these terms. If you do not agree with these terms,
7 please do not use, install, modify or redistribute this Apple software.
9 In consideration of your agreement to abide by the following terms, and subject
10 to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
11 copyrights in this original Apple software (the "Apple Software"), to use,
12 reproduce, modify and redistribute the Apple Software, with or without
13 modifications, in source and/or binary forms; provided that if you redistribute
14 the Apple Software in its entirety and without modifications, you must retain
15 this notice and the following text and disclaimers in all such redistributions of
16 the Apple Software. Neither the name, trademarks, service marks or logos of
17 Apple Computer, Inc. may be used to endorse or promote products derived from the
18 Apple Software without specific prior written permission from Apple. Except as
19 expressly stated in this notice, no other rights or licenses, express or implied,
20 are granted by Apple herein, including but not limited to any patent rights that
21 may be infringed by your derivative works or by other works in which the Apple
22 Software may be incorporated.
24 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
25 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
26 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
28 COMBINATION WITH YOUR PRODUCTS.
30 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
32 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
34 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
35 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
36 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 /*=============================================================================
39 CAComponent.cpp
41 =============================================================================*/
43 #include "CAComponent.h"
44 #include "CAComponentDescription.h"
45 #include "CACFDictionary.h"
46 #include <stdlib.h>
48 CAComponent::CAComponent (const ComponentDescription& inDesc, CAComponent* next)
49 : mManuName(0), mAUName(0), mCompName(0), mCompInfo (0)
51 mComp = FindNextComponent ((next ? next->Comp() : NULL), const_cast<ComponentDescription*>(&inDesc));
52 if (mComp)
53 GetComponentInfo (Comp(), &mDesc, NULL, NULL, NULL);
54 else
55 memcpy (&mDesc, &inDesc, sizeof(ComponentDescription));
58 CAComponent::CAComponent (const Component& comp)
59 : mComp (comp),
60 mManuName(0),
61 mAUName(0),
62 mCompName(0),
63 mCompInfo (0)
65 GetComponentInfo (Comp(), &mDesc, NULL, NULL, NULL);
68 CAComponent::CAComponent (const ComponentInstance& compInst)
69 : mComp (Component(compInst)),
70 mManuName(0),
71 mAUName(0),
72 mCompName(0),
73 mCompInfo (0)
75 GetComponentInfo (Comp(), &mDesc, NULL, NULL, NULL);
78 CAComponent::CAComponent (OSType inType, OSType inSubtype, OSType inManu)
79 : mDesc (inType, inSubtype, inManu),
80 mManuName(0), mAUName(0), mCompName(0), mCompInfo (0)
82 mComp = FindNextComponent (NULL, &mDesc);
83 GetComponentInfo (Comp(), &mDesc, NULL, NULL, NULL);
86 CAComponent::~CAComponent ()
88 Clear();
91 OSStatus CAComponent::GetResourceVersion (UInt32 &outVersion) const
93 bool versionFound = false;
94 short componentResFileID = kResFileNotOpened;
95 OSStatus result;
96 short thngResourceCount;
98 short curRes = CurResFile();
99 require_noerr (result = OpenAComponentResFile( mComp, &componentResFileID), home);
100 require_noerr (result = componentResFileID <= 0, home);
102 UseResFile(componentResFileID);
104 thngResourceCount = Count1Resources(kComponentResourceType);
106 require_noerr (result = ResError(), home);
107 // only go on if we successfully found at least 1 thng resource
108 require_noerr (thngResourceCount <= 0 ? -1 : 0, home);
110 // loop through all of the Component thng resources trying to
111 // find one that matches this Component description
112 for (short i = 0; i < thngResourceCount && (!versionFound); i++)
114 // try to get a handle to this code resource
115 Handle thngResourceHandle = Get1IndResource(kComponentResourceType, i+1);
116 if (thngResourceHandle != NULL && ((*thngResourceHandle) != NULL))
118 if (UInt32(GetHandleSize(thngResourceHandle)) >= sizeof(ExtComponentResource))
120 ExtComponentResource * componentThng = (ExtComponentResource*) (*thngResourceHandle);
122 // check to see if this is the thng resource for the particular Component that we are looking at
123 // (there often is more than one Component described in the resource)
124 if ((componentThng->cd.componentType == mDesc.Type())
125 && (componentThng->cd.componentSubType == mDesc.SubType())
126 && (componentThng->cd.componentManufacturer == mDesc.Manu()))
128 outVersion = componentThng->componentVersion;
129 versionFound = true;
132 ReleaseResource(thngResourceHandle);
136 if (!versionFound)
137 result = resNotFound;
139 UseResFile(curRes); // revert
141 if ( componentResFileID != kResFileNotOpened )
142 CloseComponentResFile(componentResFileID);
144 home:
145 return result;
148 void CAComponent::Clear ()
150 if (mManuName) { CFRelease (mManuName); mManuName = 0; }
151 if (mAUName) { CFRelease (mAUName); mAUName = 0; }
152 if (mCompName) { CFRelease (mCompName); mCompName = 0; }
153 if (mCompInfo) { CFRelease (mCompInfo); mCompInfo = 0; }
156 CAComponent& CAComponent::operator= (const CAComponent& y)
158 Clear();
160 mComp = y.mComp;
161 mDesc = y.mDesc;
163 if (y.mManuName) { mManuName = y.mManuName; CFRetain (mManuName); }
164 if (y.mAUName) { mAUName = y.mAUName; CFRetain (mAUName); }
165 if (y.mCompName) { mCompName = y.mCompName; CFRetain (mCompName); }
166 if (y.mCompInfo) { mCompInfo = y.mCompInfo; CFRetain (mCompInfo); }
168 return *this;
171 void CAComponent::SetCompNames () const
173 if (!mCompName) {
174 Handle h1 = NewHandle(4);
175 CAComponentDescription desc;
176 OSStatus err = GetComponentInfo (Comp(), &desc, h1, 0, 0);
178 if (err) { DisposeHandle(h1); return; }
180 HLock(h1);
181 char* ptr1 = *h1;
182 // Get the manufacturer's name... look for the ':' character convention
183 int len = *ptr1++;
184 char* displayStr = 0;
186 const_cast<CAComponent*>(this)->mCompName = CFStringCreateWithPascalString(NULL, (const unsigned char*)*h1, kCFStringEncodingMacRoman);
188 for (int i = 0; i < len; ++i) {
189 if (ptr1[i] == ':') { // found the name
190 ptr1[i] = 0;
191 displayStr = ptr1;
192 break;
196 if (displayStr)
198 const_cast<CAComponent*>(this)->mManuName = CFStringCreateWithCString(NULL, displayStr, kCFStringEncodingMacRoman);
200 //move displayStr ptr past the manu, to the name
201 // we move the characters down a index, because the handle doesn't have any room
202 // at the end for the \0
203 int i = strlen(displayStr), j = 0;
204 while (displayStr[++i] == ' ' && i < len)
206 while (i < len)
207 displayStr[j++] = displayStr[i++];
208 displayStr[j] = 0;
210 const_cast<CAComponent*>(this)->mAUName = CFStringCreateWithCString(NULL, displayStr, kCFStringEncodingMacRoman);
213 DisposeHandle (h1);
217 void CAComponent::SetCompInfo () const
219 if (!mCompInfo) {
220 Handle h1 = NewHandle(4);
221 CAComponentDescription desc;
222 OSStatus err = GetComponentInfo (Comp(), &desc, 0, h1, 0);
223 if (err) return;
224 HLock (h1);
225 const_cast<CAComponent*>(this)->mCompInfo = CFStringCreateWithPascalString(NULL, (const unsigned char*)*h1, kCFStringEncodingMacRoman);
227 DisposeHandle (h1);
231 void _ShowCF (FILE* file, CFStringRef str)
233 if (CFGetTypeID(str) != CFStringGetTypeID()) {
234 CFShow(str);
235 return;
238 UInt32 len = CFStringGetLength(str);
239 char* chars = (char*)malloc (len * 2); // give us plenty of room for unichar chars
240 if (CFStringGetCString (str, chars, len * 2, kCFStringEncodingUTF8))
241 fprintf (file, "%s", chars);
242 else
243 CFShow (str);
245 free (chars);
248 void CAComponent::Print(FILE* file) const
250 fprintf (file, "CAComponent: 0x%X", int(Comp()));
251 if (mManuName) {
252 fprintf (file, ", Manu:"); _ShowCF (file, mManuName);
253 if (mAUName) fprintf (file, ", Name:"); _ShowCF (file, mAUName);
255 fprintf (file, ", ");
256 Desc ().Print(file);