Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 317 lines (262 sloc) 9.361 kb
dc5be1c0 » uli
2009-07-04 Initial check-in.
1 //
2 // UKSystemInfo.m
3 // UKSystemInfo
4 //
5 // Created by Uli Kusterer on 23.09.04.
720b0e7b »
2010-03-04 Added licensing information to the source files.
6 // Copyright 2004 M. Uli Kusterer. Uli Kusterer
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
dc5be1c0 » uli
2009-07-04 Initial check-in.
26 //
27
28 #import "UKSystemInfo.h"
29 #include <sys/types.h>
30 #include <sys/sysctl.h>
31
5231e909 »
2013-04-24 Make UKFeedbackProvider build again.
32 unsigned UKPhysicalRAMSize(void)
dc5be1c0 » uli
2009-07-04 Initial check-in.
33 {
aa130dfc »
2013-05-01 Modernized code.
34 return (unsigned) (([NSProcessInfo.processInfo physicalMemory] / 1024ULL) / 1024ULL);
dc5be1c0 » uli
2009-07-04 Initial check-in.
35 }
36
37
5231e909 »
2013-04-24 Make UKFeedbackProvider build again.
38 NSString* UKSystemVersionString(void)
dc5be1c0 » uli
2009-07-04 Initial check-in.
39 {
aa130dfc »
2013-05-01 Modernized code.
40 static NSString* sSysVersionCocoaStr = nil;
41 if( !sSysVersionCocoaStr )
42 {
43 sSysVersionCocoaStr = [[[NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"] objectForKey:@"ProductVersion"] retain];
44 }
45 return sSysVersionCocoaStr;
dc5be1c0 » uli
2009-07-04 Initial check-in.
46 }
47
48
5231e909 »
2013-04-24 Make UKFeedbackProvider build again.
49 void UKGetSystemVersionComponents( SInt32* outMajor, SInt32* outMinor, SInt32* outBugfix )
dc5be1c0 » uli
2009-07-04 Initial check-in.
50 {
aa130dfc »
2013-05-01 Modernized code.
51 NSArray * sysVersionComponents = [UKSystemVersionString() componentsSeparatedByString: @"."];
52
53 if( sysVersionComponents.count > 0 )
54 *outMajor = [[sysVersionComponents objectAtIndex: 0] intValue];
55 if( sysVersionComponents.count > 1 )
56 *outMinor = [[sysVersionComponents objectAtIndex: 1] intValue];
57 if( sysVersionComponents.count > 2 )
58 *outBugfix = [[sysVersionComponents objectAtIndex: 2] intValue];
dc5be1c0 » uli
2009-07-04 Initial check-in.
59 }
60
61
5231e909 »
2013-04-24 Make UKFeedbackProvider build again.
62 long UKSystemVersion(void)
dc5be1c0 » uli
2009-07-04 Initial check-in.
63 {
aa130dfc »
2013-05-01 Modernized code.
64 SInt32 sysVersion, major = 0, minor = 0, bugfix = 0, bcdMajor = 0;
dc5be1c0 » uli
2009-07-04 Initial check-in.
65
aa130dfc »
2013-05-01 Modernized code.
66 UKGetSystemVersionComponents( &major, &minor, &bugfix );
67
68 if( bugfix > 9 )
69 bugfix = 9;
70 if( minor > 9 )
71 minor = 9;
72 bcdMajor = major % 10;
73 while( major >= 10 )
74 {
75 major -= 10;
76 bcdMajor += 16;
77 }
78
79 sysVersion = (bcdMajor << 8) | (minor << 4) | bugfix;
80 printf( "%x\n", sysVersion );
dc5be1c0 » uli
2009-07-04 Initial check-in.
81
82 return sysVersion;
83 }
84
85
5231e909 »
2013-04-24 Make UKFeedbackProvider build again.
86 unsigned UKClockSpeed(void)
dc5be1c0 » uli
2009-07-04 Initial check-in.
87 {
f8e68f3c »
2013-12-22 Use along long here Makes it work for higher CPU speeds than what my …
88 unsigned long long count = 0;
89 size_t size = sizeof(count);
3cfc8d73 »
2013-05-01 Get rid of the last two Gestalt calls.
90
91 if( sysctlbyname( "hw.cpufrequency_max", &count, &size, NULL, 0 ) )
92 return 1;
f8e68f3c »
2013-12-22 Use along long here Makes it work for higher CPU speeds than what my …
93
94 return (unsigned) (count / 1000000ULL);
dc5be1c0 » uli
2009-07-04 Initial check-in.
95 }
96
97
5231e909 »
2013-04-24 Make UKFeedbackProvider build again.
98 unsigned UKCountCores(void)
dc5be1c0 » uli
2009-07-04 Initial check-in.
99 {
100 unsigned count = 0;
101 size_t size = sizeof(count);
102
103 if( sysctlbyname( "hw.ncpu", &count, &size, NULL, 0 ) )
104 return 1;
105
106 return count;
107 }
108
109
5231e909 »
2013-04-24 Make UKFeedbackProvider build again.
110 NSString* UKMachineName(void)
dc5be1c0 » uli
2009-07-04 Initial check-in.
111 {
112 static NSString* cpuName = nil;
113 if( cpuName )
114 return cpuName;
115
aa130dfc »
2013-05-01 Modernized code.
116 char machineName[256] = {};
117 int modelInfo[2] = { CTL_HW, HW_MODEL };
118 size_t modelSize = sizeof(machineName) -1;
119 if( sysctl(modelInfo, 2, machineName, &modelSize, NULL, 0 ) != 0 )
120 return nil;
dc5be1c0 » uli
2009-07-04 Initial check-in.
121
aa130dfc »
2013-05-01 Modernized code.
122 NSString* internalName = [NSString stringWithUTF8String: machineName];
123
124 static NSDictionary* translationDictionary = nil;
125 if( !translationDictionary )
126 translationDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
127 @"PowerMac 8500/8600",@"AAPL,8500",
128 @"PowerMac 9500/9600",@"AAPL,9500",
129 @"PowerMac 7200",@"AAPL,7200",
130 @"PowerMac 7200/7300",@"AAPL,7300",
131 @"PowerMac 7500",@"AAPL,7500",
132 @"Apple Network Server",@"AAPL,ShinerESB",
133 @"Alchemy(Performa 6400 logic-board design)",@"AAPL,e407",
134 @"Gazelle(5500)",@"AAPL,e411",
135 @"PowerBook 3400",@"AAPL,3400/2400",
136 @"PowerBook 3500",@"AAPL,3500",
137 @"PowerMac G3 (Gossamer)",@"AAPL,Gossamer",
138 @"PowerMac G3 (Silk)",@"AAPL,PowerMac G3",
139 @"PowerBook G3 (Wallstreet)",@"AAPL,PowerBook1998",
140 @"Yikes! Old machine",@"AAPL", // generic.
141
142 @"iMac (first generation)",@"iMac,1",
143 @"iMac",@"iMac", // generic.
144
145 @"PowerBook G3 (Lombard)",@"PowerBook1,1",
146 @"iBook (clamshell)",@"PowerBook2,1",
147 @"iBook FireWire (clamshell)",@"PowerBook2,2",
148 @"PowerBook G3 (Pismo)",@"PowerBook3,1",
149 @"PowerBook G4 (Titanium)",@"PowerBook3,2",
150 @"PowerBook G4 (Titanium w/ Gigabit Ethernet)",@"PowerBook3,3",
151 @"PowerBook G4 (Titanium w/ DVI)",@"PowerBook3,4",
152 @"PowerBook G4 (Titanium 1GHZ)",@"PowerBook3,5",
153 @"iBook (12in May 2001)",@"PowerBook4,1",
154 @"iBook (May 2002)",@"PowerBook4,2",
155 @"iBook 2 rev. 2 (w/ or w/o 14in LCD) (Nov 2002)",@"PowerBook4,3",
156 @"iBook 2 (w/ or w/o 14in LDC)",@"PowerBook4,4",
157 @"PowerBook G4 (Aluminum 17in)",@"PowerBook5,1",
158 @"PowerBook G4 (Aluminum 15in)",@"PowerBook5,2",
159 @"PowerBook G4 (Aluminum 17in rev. 2)",@"PowerBook5,3",
160 @"PowerBook G4 (Aluminum 12in)",@"PowerBook6,1",
161 @"PowerBook G4 (Aluminum 12in)",@"PowerBook6,2",
162 @"iBook G4",@"PowerBook6,3",
163 @"PowerBook or iBook",@"PowerBook", // generic.
164
165 @"Blue & White G3",@"PowerMac1,1",
166 @"PowerMac G4 PCI Graphics",@"PowerMac1,2",
167 @"iMac FireWire (CRT)",@"PowerMac2,1",
168 @"iMac FireWire (CRT)",@"PowerMac2,2",
169 @"PowerMac G4 AGP Graphics",@"PowerMac3,1",
170 @"PowerMac G4 AGP Graphics",@"PowerMac3,2",
171 @"PowerMac G4 AGP Graphics",@"PowerMac3,3",
172 @"PowerMac G4 (QuickSilver)",@"PowerMac3,4",
173 @"PowerMac G4 (QuickSilver)",@"PowerMac3,5",
174 @"PowerMac G4 (MDD/Windtunnel)",@"PowerMac3,6",
175 @"iMac (Flower Power)",@"PowerMac4,1",
176 @"iMac (Flat Panel 15in)",@"PowerMac4,2",
177 @"eMac",@"PowerMac4,4",
178 @"iMac (Flat Panel 17in)",@"PowerMac4,5",
179 @"PowerMac G4 Cube",@"PowerMac5,1",
180 @"PowerMac G4 Cube",@"PowerMac5,2",
181 @"iMac (Flat Panel 17in)",@"PowerMac6,1",
182 @"PowerMac G5",@"PowerMac7,2",
183 @"PowerMac G5",@"PowerMac7,3",
184 @"PowerMac",@"PowerMac", // generic.
185
186 @"XServe",@"RackMac1,1",
187 @"XServe rev. 2",@"RackMac1,2",
188 @"XServe G5",@"RackMac3,1",
189 @"XServe",@"RackMac",
190
191 @"Mac Mini Core",@"Macmini1,1", // Core Duo?
192 @"Mac Mini",@"Macmini", // generic
dc5be1c0 » uli
2009-07-04 Initial check-in.
193
aa130dfc »
2013-05-01 Modernized code.
194 @"Mac Pro",@"MacPro", // generic
dc5be1c0 » uli
2009-07-04 Initial check-in.
195
aa130dfc »
2013-05-01 Modernized code.
196 @"MacBook Pro Core 2",@"MacBookPro4,1",
197 @"MacBook Pro",@"MacBookPro", // generic
198
361b0384 »
2013-05-01 Fix copy-and-paste of wrong comment.
199 @"MacBook Air (Late 2010)",@"MacBookAir3,1",
aa130dfc »
2013-05-01 Modernized code.
200 @"MacBook Air",@"MacBookAir", // generic
201 nil];
202
203 NSRange r;
204 NSString* aKey;
205 NSString* foundKey = nil;
206 NSString* humanReadableName = nil;
207
208 // Find the corresponding entry in the NSDictionary
209 // Keys should be sorted to distinguish 'generic' from 'specific' names.
210 // So we can overwrite generic names with the more specific ones as we
211 // progress through the list.
212 NSEnumerator *e=[[[translationDictionary allKeys]
213 sortedArrayUsingSelector:@selector(compare:)]
214 objectEnumerator];
215 while( aKey = [e nextObject] )
216 {
217 r = [internalName rangeOfString: aKey];
218 if( r.location != NSNotFound )
dc5be1c0 » uli
2009-07-04 Initial check-in.
219 {
aa130dfc »
2013-05-01 Modernized code.
220 if( humanReadableName == nil || [foundKey length] != [internalName length] ) // We didn't have an exact match yet?
dc5be1c0 » uli
2009-07-04 Initial check-in.
221 {
aa130dfc »
2013-05-01 Modernized code.
222 humanReadableName = [translationDictionary objectForKey:aKey];
223 foundKey = aKey;
dc5be1c0 » uli
2009-07-04 Initial check-in.
224 }
225 }
226 }
227
aa130dfc »
2013-05-01 Modernized code.
228 // If it was a generic name, include the ugly name so we can add it to the list:
229 if( [foundKey rangeOfString: @","].location == NSNotFound )
230 humanReadableName = [[NSString stringWithFormat: @"%@ (%@)", humanReadableName, foundKey] retain];
231 // If nothing was found, at least show the ugly name so we have some hint:
232 if( humanReadableName == nil )
233 cpuName = [[NSString stringWithFormat: @"Unknown (%@)", internalName] retain];
234 else
235 cpuName = humanReadableName;
236
dc5be1c0 » uli
2009-07-04 Initial check-in.
237 return cpuName;
238 }
239
240
5231e909 »
2013-04-24 Make UKFeedbackProvider build again.
241 NSString* UKCPUName(void)
dc5be1c0 » uli
2009-07-04 Initial check-in.
242 {
243 return UKAutoreleasedCPUName( NO );
244 }
245
246
3cfc8d73 »
2013-05-01 Get rid of the last two Gestalt calls.
247 NSString* UKAutoreleasedCPUName( BOOL dontCache )
dc5be1c0 » uli
2009-07-04 Initial check-in.
248 {
3cfc8d73 »
2013-05-01 Get rid of the last two Gestalt calls.
249 static NSString * sCPUName = nil;
dc5be1c0 » uli
2009-07-04 Initial check-in.
250
3cfc8d73 »
2013-05-01 Get rid of the last two Gestalt calls.
251 if( dontCache || !sCPUName )
dc5be1c0 » uli
2009-07-04 Initial check-in.
252 {
3cfc8d73 »
2013-05-01 Get rid of the last two Gestalt calls.
253 char cpuName[256] = {};
254 size_t size = sizeof(cpuName) -1;
dc5be1c0 » uli
2009-07-04 Initial check-in.
255
3cfc8d73 »
2013-05-01 Get rid of the last two Gestalt calls.
256 if( sysctlbyname( "machdep.cpu.brand_string", cpuName, &size, NULL, 0 ) != 0 )
257 return nil;
258
259 [sCPUName release];
260 sCPUName = [[NSString alloc] initWithUTF8String: cpuName];
dc5be1c0 » uli
2009-07-04 Initial check-in.
261 }
262
3cfc8d73 »
2013-05-01 Get rid of the last two Gestalt calls.
263 return sCPUName;
dc5be1c0 » uli
2009-07-04 Initial check-in.
264 }
265
266
267 /*NSString* UKSystemSerialNumber()
268 {
269 mach_port_t masterPort;
270 kern_return_t kr = noErr;
271 io_registry_entry_t entry;
272 CFTypeRef prop;
273 CFTypeID propID;
274 NSString* str = nil;
275
276 kr = IOMasterPort(MACH_PORT_NULL, &masterPort);
277 if( kr != noErr )
278 goto cleanup;
279 entry = IORegistryGetRootEntry( masterPort );
280 if( entry == MACH_PORT_NULL )
281 goto cleanup;
282 prop = IORegistryEntrySearchCFProperty(entry, kIODeviceTreePlane, CFSTR("serial-number"), nil, kIORegistryIterateRecursively);
283 if( prop == nil )
284 goto cleanup;
285 propID = CFGetTypeID( prop );
286 if( propID != CFDataGetTypeID() )
287 goto cleanup;
288
289 const char* buf = [(NSData*)prop bytes];
290 int len = [(NSData*)prop length],
291 x;
292
293 char secondPart[256];
294 char firstPart[256];
295 char* currStr = secondPart;
296 int y = 0;
297
298 for( x = 0; x < len; x++ )
299 {
300 if( buf[x] > 0 && (y < 255) )
301 currStr[y++] = buf[x];
302 else if( currStr == secondPart )
303 {
304 currStr[y] = 0; // Terminate string.
305 currStr = firstPart;
306 y = 0;
307 }
308 }
309 currStr[y] = 0; // Terminate string.
310
311 str = [NSString stringWithFormat: @"%s%s", firstPart, secondPart];
312
313 cleanup:
314 mach_port_deallocate( mach_task_self(), masterPort );
315
316 return str;
317 }*/
318
Something went wrong with that request. Please try again.