bjarni@5941: /* $Id$ */ bjarni@5941: egladil@7965: #include egladil@7965: bjarni@5941: #include bjarni@5941: bjarni@5941: #include bjarni@5941: #include bjarni@5941: #include bjarni@5941: #include bjarni@5941: #include bjarni@5941: #include "../../stdafx.h" bjarni@8117: #include "../../core/bitmath_func.hpp" smatz@9085: #include "../../rev.h" bjarni@5941: bjarni@5941: #ifndef CPU_SUBTYPE_POWERPC_970 bjarni@5941: #define CPU_SUBTYPE_POWERPC_970 ((cpu_subtype_t) 100) bjarni@5941: #endif bjarni@5941: bjarni@5941: /* bjarni@5941: * This file contains objective C bjarni@5941: * Apple uses objective C instead of plain C to interact with OS specific/native functions bjarni@5941: * bjarni@5941: * Note: TrueLight's crosscompiler can handle this, but it likely needs a manual modification for each change in this file. bjarni@5941: * To insure that the crosscompiler still works, let him try any changes before they are committed bjarni@5941: */ bjarni@5941: bjarni@5941: void ToggleFullScreen(bool fs); bjarni@5941: rubidium@6247: static char *GetOSString() bjarni@5941: { bjarni@5941: static char buffer[175]; bjarni@5941: const char* CPU; bjarni@5941: char OS[20]; bjarni@5941: char newgrf[125]; bjarni@5941: long sysVersion; bjarni@5941: bjarni@5941: // get the hardware info bjarni@5941: host_basic_info_data_t hostInfo; bjarni@5941: mach_msg_type_number_t infoCount; bjarni@5941: bjarni@5941: infoCount = HOST_BASIC_INFO_COUNT; bjarni@5941: host_info( bjarni@5941: mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount bjarni@5941: ); bjarni@5941: bjarni@5941: // replace the hardware info with strings, that tells a bit more than just an int bjarni@5941: switch (hostInfo.cpu_subtype) { bjarni@5941: #ifdef __POWERPC__ bjarni@5941: case CPU_SUBTYPE_POWERPC_750: CPU = "G3"; break; bjarni@5941: case CPU_SUBTYPE_POWERPC_7400: bjarni@5941: case CPU_SUBTYPE_POWERPC_7450: CPU = "G4"; break; bjarni@5941: case CPU_SUBTYPE_POWERPC_970: CPU = "G5"; break; bjarni@5941: default: CPU = "Unknown PPC"; break; bjarni@5941: #else bjarni@5941: /* it looks odd to have a switch for two cases, but it leaves room for easy bjarni@5941: * expansion. Odds are that Apple will some day use newer CPUs than i686 bjarni@5941: */ bjarni@5941: case CPU_SUBTYPE_PENTPRO: CPU = "i686"; break; bjarni@5941: default: CPU = "Unknown Intel"; break; bjarni@5941: #endif bjarni@5941: } bjarni@5941: bjarni@5941: // get the version of OSX bjarni@5941: if (Gestalt(gestaltSystemVersion, &sysVersion) != noErr) { bjarni@5941: sprintf(OS, "Undetected"); bjarni@5941: } else { bjarni@5941: int majorHiNib = GB(sysVersion, 12, 4); bjarni@5941: int majorLoNib = GB(sysVersion, 8, 4); bjarni@5941: int minorNib = GB(sysVersion, 4, 4); bjarni@5941: int bugNib = GB(sysVersion, 0, 4); bjarni@5941: bjarni@5941: sprintf(OS, "%d%d.%d.%d", majorHiNib, majorLoNib, minorNib, bugNib); bjarni@5941: } bjarni@5941: bjarni@5941: // make a list of used newgrf files bjarni@5941: /* if (_first_grffile != NULL) { bjarni@5941: char* n = newgrf; bjarni@5941: const GRFFile* file; bjarni@5941: bjarni@5941: for (file = _first_grffile; file != NULL; file = file->next) { bjarni@5941: n = strecpy(n, " ", lastof(newgrf)); bjarni@5941: n = strecpy(n, file->filename, lastof(newgrf)); bjarni@5941: } bjarni@5941: } else {*/ bjarni@5941: sprintf(newgrf, "none"); bjarni@5941: // } bjarni@5941: bjarni@5941: snprintf( bjarni@5941: buffer, lengthof(buffer), bjarni@5941: "Please add this info: (tip: copy-paste works)\n" bjarni@5941: "CPU: %s, OSX: %s, OpenTTD version: %s\n" bjarni@5941: "NewGRF files:%s", bjarni@5941: CPU, OS, _openttd_revision, newgrf bjarni@5941: ); bjarni@5941: return buffer; bjarni@5941: } bjarni@5941: bjarni@5941: bjarni@5941: #ifdef WITH_SDL bjarni@5941: bjarni@5941: void ShowMacDialog(const char* title, const char* message, const char* buttonLabel) bjarni@5941: { bjarni@5941: NSRunAlertPanel([NSString stringWithCString: title], [NSString stringWithCString: message], [NSString stringWithCString: buttonLabel], nil, nil); bjarni@5941: } bjarni@5941: bjarni@5941: #elif defined WITH_COCOA bjarni@5941: bjarni@5941: void CocoaDialog(const char* title, const char* message, const char* buttonLabel); bjarni@5941: bjarni@5941: void ShowMacDialog(const char* title, const char* message, const char* buttonLabel) bjarni@5941: { bjarni@5941: CocoaDialog(title, message, buttonLabel); bjarni@5941: } bjarni@5941: bjarni@5941: bjarni@5941: #else bjarni@5941: bjarni@5941: void ShowMacDialog(const char* title, const char* message, const char* buttonLabel) bjarni@5941: { bjarni@5941: fprintf(stderr, "%s: %s\n", title, message); bjarni@5941: } bjarni@5941: bjarni@5941: #endif bjarni@5941: bjarni@5941: void ShowMacAssertDialog(const char* function, const char* file, const int line, const char* expression) bjarni@5941: { bjarni@5941: const char* buffer = bjarni@5941: [[NSString stringWithFormat:@ bjarni@5941: "An assertion has failed and OpenTTD must quit.\n" bjarni@5941: "%s in %s (line %d)\n" bjarni@5941: "\"%s\"\n" bjarni@5941: "\n" bjarni@5941: "You should report this error the OpenTTD developers if you think you found a bug.\n" bjarni@5941: "\n" bjarni@5941: "%s", bjarni@5941: function, file, line, expression, GetOSString()] cString bjarni@5941: ]; bjarni@5941: NSLog(@"%s", buffer); bjarni@5941: ToggleFullScreen(0); bjarni@5941: ShowMacDialog("Assertion Failed", buffer, "Quit"); bjarni@5941: bjarni@5941: // abort so that a debugger has a chance to notice bjarni@5941: abort(); bjarni@5941: } bjarni@5941: bjarni@5941: bjarni@5941: void ShowMacErrorDialog(const char *error) bjarni@5941: { bjarni@5941: const char* buffer = bjarni@5941: [[NSString stringWithFormat:@ bjarni@5941: "Please update to the newest version of OpenTTD\n" bjarni@5941: "If the problem presists, please report this to\n" bjarni@5941: "http://bugs.openttd.org\n" bjarni@5941: "\n" bjarni@5941: "%s", bjarni@5941: GetOSString()] cString bjarni@5941: ]; bjarni@5941: ToggleFullScreen(0); bjarni@5941: ShowMacDialog(error, buffer, "Quit"); bjarni@5941: abort(); bjarni@5941: } bjarni@7435: bjarni@7435: bjarni@7435: /** Determine the current user's locale. */ bjarni@7435: const char *GetCurrentLocale(const char *) bjarni@7435: { bjarni@7435: static char retbuf[32] = { '\0' }; bjarni@7435: NSUserDefaults* defs = [NSUserDefaults standardUserDefaults]; bjarni@7435: NSArray* languages = [defs objectForKey:@"AppleLanguages"]; bjarni@7435: NSString* preferredLang = [languages objectAtIndex:0]; bjarni@7435: /* preferredLang is either 2 or 5 characters long ("xx" or "xx_YY"). */ bjarni@7984: bjarni@9757: /* Since Apple introduced encoding to CString in OSX 10.4 we have to make a few conditions bjarni@9757: * to get the right code for the used version of OSX. */ bjarni@9757: #if (MAC_OS_X_VERSION_MAX_ALLOWED == MAC_OS_X_VERSION_10_4) bjarni@9757: /* 10.4 can compile both versions just fine and will select the correct version at runtime based bjarni@9757: * on the version of OSX at execution time. */ egladil@7965: if (MacOSVersionIsAtLeast(10, 4, 0)) { egladil@7965: [ preferredLang getCString:retbuf maxLength:32 encoding:NSASCIIStringEncoding ]; bjarni@9757: } else bjarni@9757: #endif bjarni@7984: { bjarni@9757: [ preferredLang getCString:retbuf maxLength:32 bjarni@9757: #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5) bjarni@9757: /* If 10.5+ is used to compile then encoding is needed here. bjarni@9757: * If 10.3 or 10.4 is used for compiling then this line is used by 10.3 and encoding should not be present here. */ bjarni@9757: encoding:NSASCIIStringEncoding bjarni@7984: #endif bjarni@9757: ]; egladil@7965: } bjarni@7435: return retbuf; bjarni@7435: } egladil@7965: egladil@7965: egladil@7965: /* egladil@7965: * This will only give an accurate result for versions before OS X 10.8 since it uses bcd encoding egladil@7965: * for the minor and bugfix version numbers and a scheme of representing all numbers from 9 and up egladil@7965: * with 9. This means we can't tell OS X 10.9 from 10.9 or 10.11. Please use GetMacOSVersionMajor() egladil@7965: * and GetMacOSVersionMinor() instead. egladil@7965: */ egladil@7965: static long GetMacOSVersion() egladil@7965: { egladil@7965: static long sysVersion = -1; egladil@7965: egladil@7965: if (sysVersion != -1) return sysVersion; egladil@7965: egladil@7965: if (Gestalt(gestaltSystemVersion, &sysVersion) != noErr) sysVersion = -1; egladil@7965: return sysVersion; egladil@7965: } egladil@7965: egladil@7965: long GetMacOSVersionMajor() egladil@7965: { egladil@7965: static long sysVersion = -1; egladil@7965: egladil@7965: if (sysVersion != -1) return sysVersion; egladil@7965: egladil@7965: sysVersion = GetMacOSVersion(); egladil@7965: if (sysVersion == -1) return -1; egladil@7965: egladil@7965: if (sysVersion >= 0x1040) { egladil@7965: if (Gestalt(gestaltSystemVersionMajor, &sysVersion) != noErr) sysVersion = -1; egladil@7965: } else { egladil@7965: sysVersion = GB(sysVersion, 12, 4) * 10 + GB(sysVersion, 8, 4); egladil@7965: } egladil@7965: egladil@7965: return sysVersion; egladil@7965: } egladil@7965: egladil@7965: long GetMacOSVersionMinor() egladil@7965: { egladil@7965: static long sysVersion = -1; egladil@7965: egladil@7965: if (sysVersion != -1) return sysVersion; egladil@7965: egladil@7965: sysVersion = GetMacOSVersion(); egladil@7965: if (sysVersion == -1) return -1; egladil@7965: egladil@7965: if (sysVersion >= 0x1040) { egladil@7965: if (Gestalt(gestaltSystemVersionMinor, &sysVersion) != noErr) sysVersion = -1; egladil@7965: } else { egladil@7965: sysVersion = GB(sysVersion, 4, 4); egladil@7965: } egladil@7965: egladil@7965: return sysVersion; egladil@7965: } egladil@7965: egladil@7965: long GetMacOSVersionBugfix() egladil@7965: { egladil@7965: static long sysVersion = -1; egladil@7965: egladil@7965: if (sysVersion != -1) return sysVersion; egladil@7965: egladil@7965: sysVersion = GetMacOSVersion(); egladil@7965: if (sysVersion == -1) return -1; egladil@7965: egladil@7965: if (sysVersion >= 0x1040) { egladil@7965: if (Gestalt(gestaltSystemVersionBugFix, &sysVersion) != noErr) sysVersion = -1; egladil@7965: } else { egladil@7965: sysVersion = GB(sysVersion, 0, 4); egladil@7965: } egladil@7965: egladil@7965: return sysVersion; egladil@7965: }