OpenRCT2/src/openrct2/platform/macos.mm

183 lines
5.3 KiB
Plaintext
Raw Normal View History

2014-10-06 18:36:58 +02:00
/*****************************************************************************
* Copyright (c) 2014-2018 OpenRCT2 developers
2015-10-20 20:16:30 +02:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2015-10-20 20:16:30 +02:00
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
2014-10-06 18:36:58 +02:00
*****************************************************************************/
2015-11-27 18:01:22 +01:00
#if defined(__APPLE__) && defined(__MACH__)
2014-10-06 18:36:58 +02:00
2018-06-21 12:48:12 +02:00
#include "../config/Config.h"
#include "../localisation/Language.h"
#include "../util/Util.h"
#include "platform.h"
// undefine `interface` and `abstract`, because it's causing conflicts with Objective-C's keywords
#undef interface
#undef abstract
#import <AppKit/AppKit.h>
#import <Foundation/Foundation.h>
2015-12-13 00:22:14 +01:00
#include <mach-o/dyld.h>
#include <pwd.h>
2014-10-06 18:36:58 +02:00
void macos_disallow_automatic_window_tabbing()
{
@autoreleasepool {
2018-06-22 23:04:38 +02:00
if ([NSWindow respondsToSelector:@selector(setAllowsAutomaticWindowTabbing:)])
{
[NSWindow setAllowsAutomaticWindowTabbing:NO];
}
}
}
2018-06-22 23:04:38 +02:00
utf8* macos_str_decomp_to_precomp(utf8* input)
{
2018-06-22 23:04:38 +02:00
@autoreleasepool {
if (input == NULL)
{
return NULL;
}
2018-06-22 23:04:38 +02:00
NSString* inputDecomp = [NSString stringWithUTF8String:input];
return strdup([inputDecomp.precomposedStringWithCanonicalMapping cStringUsingEncoding:NSUTF8StringEncoding]);
}
}
#ifndef NO_TTF
2018-06-22 23:04:38 +02:00
bool platform_get_font_path(TTFFontDescriptor* font, utf8* buffer, size_t size)
{
2018-06-22 23:04:38 +02:00
@autoreleasepool {
CTFontDescriptorRef fontRef = CTFontDescriptorCreateWithNameAndSize(
(CFStringRef)[NSString stringWithUTF8String:font->font_name], 0.0);
CFURLRef url = (CFURLRef)CTFontDescriptorCopyAttribute(fontRef, kCTFontURLAttribute);
2018-06-22 23:04:38 +02:00
if (url)
{
NSString* fontPath = [NSString stringWithString:[(NSURL*)CFBridgingRelease(url) path]];
safe_strcpy(buffer, fontPath.UTF8String, size);
return true;
2018-06-22 23:04:38 +02:00
}
else
{
return false;
}
}
}
#endif // NO_TTF
2018-06-22 23:04:38 +02:00
bool platform_has_matching_language(NSString* preferredLocale, uint16_t* languageIdentifier)
2016-08-21 16:28:24 +02:00
{
2018-06-22 23:04:38 +02:00
@autoreleasepool {
if ([preferredLocale isEqualToString:@"en"] || [preferredLocale isEqualToString:@"en-CA"])
{
*languageIdentifier = LANGUAGE_ENGLISH_US;
return YES;
}
2018-06-22 23:04:38 +02:00
if ([preferredLocale isEqualToString:@"zh-CN"])
{
*languageIdentifier = LANGUAGE_CHINESE_SIMPLIFIED;
return YES;
}
2018-06-22 23:04:38 +02:00
if ([preferredLocale isEqualToString:@"zh-TW"])
{
*languageIdentifier = LANGUAGE_CHINESE_TRADITIONAL;
return YES;
}
// Find an exact match (language and region)
2018-06-22 23:04:38 +02:00
for (int i = 1; i < LANGUAGE_COUNT; i++)
{
if ([preferredLocale isEqualToString:[NSString stringWithUTF8String:LanguagesDescriptors[i].locale]])
{
*languageIdentifier = i;
return YES;
}
}
// Only check for a matching language
2018-06-22 23:04:38 +02:00
NSString* languageCode = [[preferredLocale componentsSeparatedByString:@"-"] firstObject];
for (int i = 1; i < LANGUAGE_COUNT; i++)
{
NSString* optionLanguageCode = [
[[NSString stringWithUTF8String:LanguagesDescriptors[i].locale] componentsSeparatedByString:@"-"] firstObject];
2018-06-22 23:04:38 +02:00
if ([languageCode isEqualToString:optionLanguageCode])
{
*languageIdentifier = i;
return YES;
}
}
return NO;
}
2016-08-21 16:28:24 +02:00
}
uint16_t platform_get_locale_language()
2016-08-21 16:28:24 +02:00
{
2018-06-22 23:04:38 +02:00
@autoreleasepool {
NSArray<NSString*>* preferredLanguages = [NSLocale preferredLanguages];
for (NSString* preferredLanguage in preferredLanguages)
{
uint16_t languageIdentifier;
2018-06-22 23:04:38 +02:00
if (platform_has_matching_language(preferredLanguage, &languageIdentifier))
{
return languageIdentifier;
}
}
// Fallback
return LANGUAGE_ENGLISH_UK;
}
2016-08-21 16:28:24 +02:00
}
uint8_t platform_get_locale_currency()
2016-08-21 15:14:54 +02:00
{
2018-06-22 23:04:38 +02:00
@autoreleasepool {
NSString* currencyCode = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencyCode];
return platform_get_currency_value(currencyCode.UTF8String);
}
2016-08-21 15:14:54 +02:00
}
uint8_t platform_get_locale_measurement_format()
2016-08-21 15:14:54 +02:00
{
2018-06-22 23:04:38 +02:00
@autoreleasepool {
NSNumber* metricSystem = [[NSLocale currentLocale] objectForKey:NSLocaleUsesMetricSystem];
2016-08-21 15:14:54 +02:00
2018-06-22 23:04:38 +02:00
if (metricSystem.boolValue)
{
return MEASUREMENT_FORMAT_METRIC;
}
2016-11-13 20:17:49 +01:00
return MEASUREMENT_FORMAT_IMPERIAL;
}
2016-08-21 15:14:54 +02:00
}
2018-06-22 23:04:38 +02:00
void platform_get_changelog_path(utf8* outPath, size_t outSize)
2017-08-07 14:00:28 +02:00
{
platform_get_openrct_data_path(outPath, outSize);
safe_strcat_path(outPath, "changelog.txt", outSize);
}
2018-06-22 23:04:38 +02:00
bool platform_get_steam_path(utf8* outPath, size_t outSize)
{
char steamPath[1024] = { 0 };
2018-06-22 23:04:38 +02:00
const char* homeDir = getpwuid(getuid())->pw_dir;
if (homeDir != NULL)
{
safe_strcpy(steamPath, homeDir, sizeof(steamPath));
safe_strcat_path(
steamPath, "Library/Application Support/Steam/Steam.AppBundle/Steam/Contents/MacOS/steamapps", sizeof(steamPath));
if (platform_directory_exists(steamPath))
{
safe_strcpy(outPath, steamPath, outSize);
return true;
}
}
return false;
}
2015-11-27 18:01:22 +01:00
#endif