15#include "../../stdafx.h"
16#include "../../core/bitmath_func.hpp"
19#include "../../string_func.h"
20#include "../../fileio_func.h"
25static NSAutoreleasePool *_ottd_autorelease_pool;
34 NSOperatingSystemVersion ver = [ [ NSProcessInfo processInfo ] operatingSystemVersion ];
35 return {
static_cast<int>(ver.majorVersion),
static_cast<int>(ver.minorVersion),
static_cast<int>(ver.patchVersion) };
39extern void CocoaDialog(std::string_view title, std::string_view message, std::string_view buttonLabel);
48void ShowMacDialog(std::string_view title, std::string_view message, std::string_view buttonLabel)
53 fmt::print(stderr,
"{}: {}\n", title, message);
66 ShowMacDialog(
"OpenTTD has encountered an error", buf,
"Quit");
78 [ [ NSWorkspace sharedWorkspace ] openURL:[ NSURL URLWithString:[ NSString stringWithUTF8String:url.c_str() ] ] ];
87 NSUserDefaults *defs = [ NSUserDefaults standardUserDefaults ];
88 NSArray *languages = [ defs objectForKey:
@"AppleLanguages" ];
89 NSString *preferredLang = [ languages objectAtIndex:0 ];
92 std::string retbuf{32,
'\0'};
93 [ preferredLang getCString:retbuf.data() maxLength:retbuf.size() encoding:NSASCIIStringEncoding ];
94 auto end = retbuf.find(
'\0');
95 if (end == 0)
return std::nullopt;
96 if (end != std::string::npos) retbuf.erase(end);
109 NSPasteboard *pb = [ NSPasteboard generalPasteboard ];
110 NSArray *types = [ NSArray arrayWithObject:NSPasteboardTypeString ];
111 NSString *bestType = [ pb availableTypeFromArray:types ];
114 if (bestType == nil)
return std::nullopt;
116 NSString *
string = [ pb stringForType:NSPasteboardTypeString ];
117 if (
string == nil || [
string length ] == 0)
return std::nullopt;
119 return [
string UTF8String ];
127void CocoaSetApplicationBundleDir()
129 extern std::array<std::string, NUM_SEARCHPATHS>
_searchpaths;
131 char tmp[MAXPATHLEN];
133 if (CFURLGetFileSystemRepresentation(url.get(),
true, (
unsigned char *)tmp, MAXPATHLEN)) {
147void CocoaSetupAutoreleasePool()
149 _ottd_autorelease_pool = [ [ NSAutoreleasePool alloc ] init ];
155void CocoaReleaseAutoreleasePool()
157 [ _ottd_autorelease_pool release ];
169 NSFont *font = [ NSFont fontWithName:(__bridge NSString *)name size:0.0f ];
171 return font != nil ? [ font isFixedPitch ] :
false;
180 pthread_setname_np(name.c_str());
182 NSThread *cur = [ NSThread currentThread ];
183 if (cur != nil && [ cur respondsToSelector:
@selector(setName:) ]) {
184 [ cur performSelector:
@selector(setName:) withObject:[ NSString stringWithUTF8String:name.c_str() ] ];
194 return [ [ NSProcessInfo processInfo ] physicalMemory ];
void CocoaDialog(std::string_view title, std::string_view message, std::string_view buttonLabel)
Catch asserts prior to initialization of the videodriver.
void AppendPathSeparator(std::string &buf)
Appends, if necessary, the path separator character to the end of the string.
std::array< std::string, NUM_SEARCHPATHS > _searchpaths
The search paths OpenTTD could search through.
@ SP_APPLICATION_BUNDLE_DIR
Search within the application bundle.
Functions related to MacOS support.
std::unique_ptr< typename std::remove_pointer< T >::type, CFDeleter< typename std::remove_pointer< T >::type > > CFAutoRelease
Specialisation of std::unique_ptr for CoreFoundation objects.
bool IsMonospaceFont(CFStringRef name)
Check if a font is a monospace font.
std::tuple< int, int, int > GetMacOSVersion()
Get the version of the MacOS we are running under.
uint64_t MacOSGetPhysicalMemory()
Ask OS how much RAM it has physically attached.
void MacOSSetThreadName(const std::string &name)
Set the name of the current thread for the debugger.
void ShowOSErrorBox(std::string_view buf, bool system)
Show an error message.
void ShowMacDialog(std::string_view title, std::string_view message, std::string_view buttonLabel)
Show the system dialogue message, uses Cocoa if available and console otherwise.
void OSOpenBrowser(const std::string &url)
Opens browser on MacOS.
std::optional< std::string > GetCurrentLocale(const char *)
Determine and return the current user's charset.
Includes of mac os specific headers wich contain objective c.
std::optional< std::string > GetClipboardContents()
Try to retrieve the current clipboard contents.