From 1d5010edf96789140f65c0495495a24c53b5ba1b Mon Sep 17 00:00:00 2001 From: egladil Date: Sun, 25 Nov 2007 14:43:16 +0000 Subject: (svn r11521) -Codechange: [OSX] Check what the running os version is in a cleaner way. --- src/os/macosx/macos.h | 40 +++++++++++++++++++++++++ src/os/macosx/macos.mm | 80 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 119 insertions(+), 1 deletion(-) (limited to 'src/os/macosx') diff --git a/src/os/macosx/macos.h b/src/os/macosx/macos.h index b8a6cd511..dc2637acc 100644 --- a/src/os/macosx/macos.h +++ b/src/os/macosx/macos.h @@ -27,4 +27,44 @@ void ShowMacErrorDialog(const char *error); (__builtin_expect(!(e), 0) ? ShowMacAssertDialog ( __func__, __FILE__, __LINE__, #e ): (void)0 ) #endif + + +/** + * Get the major version of Mac OS we are running under. Useful for things like the cocoa driver. + * @return major version of the os. This would be 10 in the case of 10.4.11. + */ +long GetMacOSVersionMajor(); + +/** + * Get the minor version of Mac OS we are running under. Useful for things like the cocoa driver. + * @return minor version of the os. This would be 4 in the case of 10.4.11. + */ +long GetMacOSVersionMinor(); + +/** + * Get the bugfix version of Mac OS we are running under. Useful for things like the cocoa driver. + * @return bugfix version of the os. This would be 11 in the case of 10.4.11. + */ +long GetMacOSVersionBugfix(); + +/** + * Check if we are at least running on the specified version of Mac OS. + * @param major major version of the os. This would be 10 in the case of 10.4.11. + * @param minor minor version of the os. This would be 4 in the case of 10.4.11. + * @param bugfix bugfix version of the os. This would be 11 in the case of 10.4.11. + * @return true if the running os is at least what we asked, false otherwise. + */ +static inline bool MacOSVersionIsAtLeast(long major, long minor, long bugfix) +{ + long maj = GetMacOSVersionMajor(); + long min = GetMacOSVersionMinor(); + long bf = GetMacOSVersionBugfix(); + + if (maj < major) return false; + if (maj == major && min < minor) return false; + if (maj == major && min == minor && bf < bugfix) return false; + + return true; +} + #endif /* MACOS_H */ diff --git a/src/os/macosx/macos.mm b/src/os/macosx/macos.mm index 22bf8a987..def108059 100644 --- a/src/os/macosx/macos.mm +++ b/src/os/macosx/macos.mm @@ -1,5 +1,8 @@ /* $Id$ */ +#define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_3 +#include + #include #include @@ -168,6 +171,81 @@ const char *GetCurrentLocale(const char *) NSArray* languages = [defs objectForKey:@"AppleLanguages"]; NSString* preferredLang = [languages objectAtIndex:0]; /* preferredLang is either 2 or 5 characters long ("xx" or "xx_YY"). */ - [ preferredLang getCString:retbuf maxLength:32 encoding:NSASCIIStringEncoding ]; + if (MacOSVersionIsAtLeast(10, 4, 0)) { + [ preferredLang getCString:retbuf maxLength:32 encoding:NSASCIIStringEncoding ]; + } else { + [ preferredLang getCString:retbuf maxLength:32 ]; + } return retbuf; } + + +/* + * This will only give an accurate result for versions before OS X 10.8 since it uses bcd encoding + * for the minor and bugfix version numbers and a scheme of representing all numbers from 9 and up + * with 9. This means we can't tell OS X 10.9 from 10.9 or 10.11. Please use GetMacOSVersionMajor() + * and GetMacOSVersionMinor() instead. + */ +static long GetMacOSVersion() +{ + static long sysVersion = -1; + + if (sysVersion != -1) return sysVersion; + + if (Gestalt(gestaltSystemVersion, &sysVersion) != noErr) sysVersion = -1; + return sysVersion; +} + +long GetMacOSVersionMajor() +{ + static long sysVersion = -1; + + if (sysVersion != -1) return sysVersion; + + sysVersion = GetMacOSVersion(); + if (sysVersion == -1) return -1; + + if (sysVersion >= 0x1040) { + if (Gestalt(gestaltSystemVersionMajor, &sysVersion) != noErr) sysVersion = -1; + } else { + sysVersion = GB(sysVersion, 12, 4) * 10 + GB(sysVersion, 8, 4); + } + + return sysVersion; +} + +long GetMacOSVersionMinor() +{ + static long sysVersion = -1; + + if (sysVersion != -1) return sysVersion; + + sysVersion = GetMacOSVersion(); + if (sysVersion == -1) return -1; + + if (sysVersion >= 0x1040) { + if (Gestalt(gestaltSystemVersionMinor, &sysVersion) != noErr) sysVersion = -1; + } else { + sysVersion = GB(sysVersion, 4, 4); + } + + return sysVersion; +} + +long GetMacOSVersionBugfix() +{ + static long sysVersion = -1; + + if (sysVersion != -1) return sysVersion; + + sysVersion = GetMacOSVersion(); + if (sysVersion == -1) return -1; + + if (sysVersion >= 0x1040) { + if (Gestalt(gestaltSystemVersionBugFix, &sysVersion) != noErr) sysVersion = -1; + } else { + sysVersion = GB(sysVersion, 0, 4); + } + + return sysVersion; +} -- cgit v1.2.3-54-g00ecf