Bläddra i källkod

Replaced Gestalt with NSProcessInfo.

woollybah 6 år sedan
förälder
incheckning
22fa82fba5

+ 83 - 0
macos.mod/NSProcessInfo_PECocoaBackports.h

@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2014 Petroules Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import <Foundation/Foundation.h>
+#import <TargetConditionals.h>
+#import <AvailabilityMacros.h>
+
+#if (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) || \
+(defined(MAC_OS_X_VERSION_MAX_ALLOWED) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
+#import <Availability.h>
+#endif
+
+#import "PECocoaBackportsGlobal.h"
+
+#if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
+#if __IPHONE_OS_VERSION_MAX_ALLOWED < 80000
+#ifndef DECLARE_OPERATING_SYSTEM_VERSION
+#define DECLARE_OPERATING_SYSTEM_VERSION 1
+#endif
+#endif
+#if __IPHONE_OS_VERSION_MIN_REQUIRED < 80000
+#ifndef LOAD_OPERATING_SYSTEM_VERSION
+#define LOAD_OPERATING_SYSTEM_VERSION 1
+#endif
+#endif
+#elif defined(TARGET_OS_MAC) && TARGET_OS_MAC
+#if __MAC_OS_X_VERSION_MAX_ALLOWED < 101000
+#ifndef DECLARE_OPERATING_SYSTEM_VERSION
+#define DECLARE_OPERATING_SYSTEM_VERSION 1
+#endif
+#endif
+#if __MAC_OS_X_VERSION_MIN_REQUIRED < 101000
+#ifndef LOAD_OPERATING_SYSTEM_VERSION
+#define LOAD_OPERATING_SYSTEM_VERSION 1
+#endif
+#endif
+#endif
+
+#ifndef DECLARE_OPERATING_SYSTEM_VERSION
+#define DECLARE_OPERATING_SYSTEM_VERSION 0
+#endif
+
+#ifndef LOAD_OPERATING_SYSTEM_VERSION
+#define LOAD_OPERATING_SYSTEM_VERSION 0
+#endif
+
+#if DECLARE_OPERATING_SYSTEM_VERSION
+typedef struct {
+    NSInteger majorVersion;
+    NSInteger minorVersion;
+    NSInteger patchVersion;
+} NSOperatingSystemVersion;
+#endif
+
+@interface NSProcessInfo (PECocoaBackports)
+
+#if DECLARE_OPERATING_SYSTEM_VERSION
+- (NSOperatingSystemVersion)operatingSystemVersion NS_AVAILABLE(10_5, 2_0);
+- (BOOL)isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version NS_AVAILABLE(10_5, 2_0);
+#endif
+
+@end

+ 102 - 0
macos.mod/NSProcessInfo_PECocoaBackports.m

@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2014 Petroules Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "NSProcessInfo_PECocoaBackports.h"
+
+#if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
+#import <UIKit/UIKit.h>
+#elif defined(TARGET_OS_MAC) && TARGET_OS_MAC
+#import <CoreServices/CoreServices.h>
+#endif
+
+@interface NSProcessInfo (PECocoaBackportsPrivate)
+
+#if LOAD_OPERATING_SYSTEM_VERSION
+- (NSOperatingSystemVersion)PECocoaBackports_operatingSystemVersion;
+- (BOOL)PECocoaBackports_isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version;
+#endif
+
+@end
+
+@implementation NSProcessInfo (PECocoaBackportsPrivate)
+
++ (void)load
+{
+#if LOAD_OPERATING_SYSTEM_VERSION
+    // Public API since OS X 10.10 (present since 10.9) and iOS 8.0
+    class_addInstanceMethodIfNecessary([self class],
+                                       NSSelectorFromString(@"operatingSystemVersion"),
+                                       @selector(PECocoaBackports_operatingSystemVersion));
+    
+    // Public API since OS X 10.10 (present since 10.9) and iOS 8.0
+    class_addInstanceMethodIfNecessary([self class],
+                                       NSSelectorFromString(@"isOperatingSystemAtLeastVersion:"),
+                                       @selector(PECocoaBackports_isOperatingSystemAtLeastVersion:));
+#endif
+}
+
+#if LOAD_OPERATING_SYSTEM_VERSION
+- (NSOperatingSystemVersion)PECocoaBackports_operatingSystemVersion
+{
+    NSOperatingSystemVersion v = {0, 0, 0};
+    SInt32 major = 0, minor = 0, patch = 0;
+#if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
+#if !defined(__has_feature) || !__has_feature(objc_arc)
+    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+#endif
+    NSArray *parts = [[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."];
+    major = parts.count > 0 ? [[parts objectAtIndex:0] intValue] : 0;
+    minor = parts.count > 1 ? [[parts objectAtIndex:1] intValue] : 0;
+    patch = parts.count > 2 ? [[parts objectAtIndex:2] intValue] : 0;
+#if !defined(__has_feature) || !__has_feature(objc_arc)
+    [pool release];
+#endif
+#elif defined(TARGET_OS_MAC) && TARGET_OS_MAC
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+    if (Gestalt(gestaltSystemVersionMajor, &major) != noErr) return v;
+    if (Gestalt(gestaltSystemVersionMinor, &minor) != noErr) return v;
+    if (Gestalt(gestaltSystemVersionBugFix, &patch) != noErr) return v;
+#pragma clang diagnostic pop
+#endif
+    v.majorVersion = major;
+    v.minorVersion = minor;
+    v.patchVersion = patch;
+    return v;
+}
+
+- (BOOL)PECocoaBackports_isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version
+{
+    const NSOperatingSystemVersion systemVersion = [self operatingSystemVersion];
+    if (systemVersion.majorVersion == version.majorVersion) {
+        if (systemVersion.minorVersion == version.minorVersion) {
+            return systemVersion.patchVersion >= version.patchVersion;
+        }
+        return systemVersion.minorVersion >= version.minorVersion;
+    }
+    return systemVersion.majorVersion >= version.majorVersion;
+}
+#endif
+
+@end

+ 56 - 0
macos.mod/PECocoaBackportsGlobal.h

@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2014 Petroules Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __PECocoaBackportsGlobal__
+#define __PECocoaBackportsGlobal__
+
+#import <objc/runtime.h>
+
+CF_INLINE void class_addInstanceMethodIfNecessary(Class clazz, SEL realSelector, SEL fakeSelector) {
+    if (!class_getInstanceMethod(clazz, realSelector)) {
+        Method method = class_getInstanceMethod(clazz, fakeSelector);
+        if (!class_addMethod(clazz, realSelector, method_getImplementation(method),
+                             method_getTypeEncoding(method))) {
+#if defined(DEBUG) && DEBUG
+            NSLog(@"PECocoaBackports: error adding -[%@ %@] (-[%@ %@])",
+                  NSStringFromClass(clazz), NSStringFromSelector(fakeSelector),
+                  NSStringFromClass(clazz), NSStringFromSelector(realSelector));
+#endif
+        } else {
+#if defined(DEBUG) && DEBUG
+            NSLog(@"PECocoaBackports: added -[%@ %@] (-[%@ %@])",
+                  NSStringFromClass(clazz), NSStringFromSelector(fakeSelector),
+                  NSStringFromClass(clazz), NSStringFromSelector(realSelector));
+#endif
+        }
+    } else {
+#if defined(DEBUG) && DEBUG
+        NSLog(@"PECocoaBackports: skipped adding -[%@ %@] (-[%@ %@]); already exists",
+              NSStringFromClass(clazz), NSStringFromSelector(fakeSelector),
+              NSStringFromClass(clazz), NSStringFromSelector(realSelector));
+#endif
+    }
+}
+
+#endif // __PECocoaBackportsGlobal__

+ 4 - 1
macos.mod/macos.bmx

@@ -13,14 +13,17 @@ ModuleInfo "History: 1.01 Release"
 
 ?MacOS
 Import "macos.m"
+Import "NSProcessInfo_PECocoaBackports.m"
 
 Extern
 
 Function is_pid_native( pid )
-Function Gestalt( tag,result Var )
 Function bbStringFromNSString$( ns_string:Byte Ptr )
 Function NSStringFromBBString:Byte Ptr( bb_string$ )
 
+Function NSOSVersion(major:Int Var, minor:Int Var, patch:Int Var)
+
+
 End Extern
 ?
 

+ 4 - 0
macos.mod/macos.h

@@ -10,10 +10,14 @@
 #include <sys/types.h>
 #include <sys/sysctl.h>
 
+#include "NSProcessInfo_PECocoaBackports.h"
+
 int is_pid_native( pid_t pid );
 
 BBString *bbStringFromNSString( NSString *s );
 
 NSString *NSStringFromBBString( BBString *s );
 
+void NSOSVersion(int * major, int * minor, int * patch);
+
 #endif

+ 13 - 1
macos.mod/macos.m

@@ -52,5 +52,17 @@ BBString *bbStringFromNSString( NSString *s ) {
 }
 
 NSString *NSStringFromBBString( BBString *s ) {
-	return [NSString stringWithCharacters:s->buf length:s->length];
+	if (!s->length) {
+		return @"";
+	} else {
+		return [NSString stringWithCharacters:s->buf length:s->length];
+	}
+}
+
+
+void NSOSVersion(int * major, int * minor, int * patch) {
+	NSOperatingSystemVersion version = [[NSProcessInfo processInfo] operatingSystemVersion];
+	*major = version.majorVersion;
+	*minor = version.minorVersion;
+	*patch = version.patchVersion;
 }