OpenTTD Source 20260512-master-g20b387b91f
macos.mm
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
14
15#include "../../stdafx.h"
17#include "../../rev.h"
18#include "macos.h"
19#include "../../string_func.h"
20#include "../../fileio_func.h"
21#include <pthread.h>
22#include "macos_objective_c.h"
23
24#ifdef WITH_COCOA
25static NSAutoreleasePool *_ottd_autorelease_pool;
26#endif
27
32std::tuple<int, int, int> GetMacOSVersion()
33{
34 NSOperatingSystemVersion ver = [ [ NSProcessInfo processInfo ] operatingSystemVersion ];
35 return { static_cast<int>(ver.majorVersion), static_cast<int>(ver.minorVersion), static_cast<int>(ver.patchVersion) };
36}
37
38#ifdef WITH_COCOA
39extern void CocoaDialog(std::string_view title, std::string_view message, std::string_view buttonLabel);
40#endif
41
48void ShowMacDialog(std::string_view title, std::string_view message, std::string_view buttonLabel)
49{
50#ifdef WITH_COCOA
51 CocoaDialog(title, message, buttonLabel);
52#else
53 fmt::print(stderr, "{}: {}\n", title, message);
54#endif
55}
56
62void ShowOSErrorBox(std::string_view buf, bool system)
63{
64 /* Display the error in the best way possible. */
65 if (system) {
66 ShowMacDialog("OpenTTD has encountered an error", buf, "Quit");
67 } else {
68 ShowMacDialog(buf, "See the readme for more info.", "Quit");
69 }
70}
71
76void OSOpenBrowser(const std::string &url)
77{
78 [ [ NSWorkspace sharedWorkspace ] openURL:[ NSURL URLWithString:[ NSString stringWithUTF8String:url.c_str() ] ] ];
79}
80
85std::optional<std::string> GetCurrentLocale(const char *)
86{
87 NSUserDefaults *defs = [ NSUserDefaults standardUserDefaults ];
88 NSArray *languages = [ defs objectForKey:@"AppleLanguages" ];
89 NSString *preferredLang = [ languages objectAtIndex:0 ];
90 /* preferredLang is either 2 or 5 characters long ("xx" or "xx_YY"). */
91
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);
97 return retbuf;
98}
99
100
101#ifdef WITH_COCOA
107std::optional<std::string> GetClipboardContents()
108{
109 NSPasteboard *pb = [ NSPasteboard generalPasteboard ];
110 NSArray *types = [ NSArray arrayWithObject:NSPasteboardTypeString ];
111 NSString *bestType = [ pb availableTypeFromArray:types ];
112
113 /* Clipboard has no text data available. */
114 if (bestType == nil) return std::nullopt;
115
116 NSString *string = [ pb stringForType:NSPasteboardTypeString ];
117 if (string == nil || [ string length ] == 0) return std::nullopt;
118
119 return [ string UTF8String ];
120}
121
127void CocoaSetApplicationBundleDir()
128{
130
131 char tmp[MAXPATHLEN];
132 CFAutoRelease<CFURLRef> url(CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle()));
133 if (CFURLGetFileSystemRepresentation(url.get(), true, (unsigned char *)tmp, MAXPATHLEN)) {
136 } else {
138 }
139}
140
144std::string CocoaGetAppSupportDir()
145{
146 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
147 NSString *appSupportPath = [paths firstObject];
148
149 return [appSupportPath UTF8String];
150}
151
158void CocoaSetupAutoreleasePool()
159{
160 _ottd_autorelease_pool = [ [ NSAutoreleasePool alloc ] init ];
161}
162
166void CocoaReleaseAutoreleasePool()
167{
168 [ _ottd_autorelease_pool release ];
169}
170
171#endif
172
178bool IsMonospaceFont(CFStringRef name)
179{
180 NSFont *font = [ NSFont fontWithName:(__bridge NSString *)name size:0.0f ];
181
182 return font != nil ? [ font isFixedPitch ] : false;
183}
184
189void MacOSSetThreadName(const std::string &name)
190{
191 pthread_setname_np(name.c_str());
192
193 NSThread *cur = [ NSThread currentThread ];
194 if (cur != nil && [ cur respondsToSelector:@selector(setName:) ]) {
195 [ cur performSelector:@selector(setName:) withObject:[ NSString stringWithUTF8String:name.c_str() ] ];
196 }
197}
198
204{
205 return [ [ NSProcessInfo processInfo ] physicalMemory ];
206}
Functions related to bit mathematics.
void CocoaDialog(std::string_view title, std::string_view message, std::string_view buttonLabel)
Catch asserts prior to initialization of the videodriver.
Definition cocoa_wnd.mm:385
EnumClassIndexContainer< std::array< T, to_underlying(N)>, Index > EnumIndexArray
A typedef for EnumClassIndexContainer using std::array as the backing container type.
void AppendPathSeparator(std::string &buf)
Appends, if necessary, the path separator character to the end of the string.
Definition fileio.cpp:354
EnumIndexArray< std::string, Searchpath, Searchpath::End > _searchpaths
The search paths OpenTTD could search through.
Definition fileio.cpp:65
Functions for standard in/out file operations.
@ ApplicationBundleDir
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.
Definition macos.h:35
bool IsMonospaceFont(CFStringRef name)
Check if a font is a monospace font.
Definition macos.mm:178
std::tuple< int, int, int > GetMacOSVersion()
Get the version of the MacOS we are running under.
Definition macos.mm:32
uint64_t MacOSGetPhysicalMemory()
Ask OS how much RAM it has physically attached.
Definition macos.mm:203
void MacOSSetThreadName(const std::string &name)
Set the name of the current thread for the debugger.
Definition macos.mm:189
void ShowOSErrorBox(std::string_view buf, bool system)
Show an error message.
Definition macos.mm:62
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.
Definition macos.mm:48
void OSOpenBrowser(const std::string &url)
Opens browser on MacOS.
Definition macos.mm:76
std::optional< std::string > GetCurrentLocale(const char *)
Determine and return the current user's charset.
Definition macos.mm:85
Includes of mac os specific headers which contain objective c.
Declaration of OTTD revision dependent variables.
Definition of base types and functions in a cross-platform compatible way.
Functions related to low-level strings.
std::optional< std::string > GetClipboardContents()
Try to retrieve the current clipboard contents.
Definition unix.cpp:205