OpenTTD Source 20260129-master-g2bb01bd0e4
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
15#include "../../stdafx.h"
16#include "../../core/bitmath_func.hpp"
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{
129 extern std::array<std::string, NUM_SEARCHPATHS> _searchpaths;
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
147void CocoaSetupAutoreleasePool()
148{
149 _ottd_autorelease_pool = [ [ NSAutoreleasePool alloc ] init ];
150}
151
155void CocoaReleaseAutoreleasePool()
156{
157 [ _ottd_autorelease_pool release ];
158}
159
160#endif
161
167bool IsMonospaceFont(CFStringRef name)
168{
169 NSFont *font = [ NSFont fontWithName:(__bridge NSString *)name size:0.0f ];
170
171 return font != nil ? [ font isFixedPitch ] : false;
172}
173
178void MacOSSetThreadName(const std::string &name)
179{
180 pthread_setname_np(name.c_str());
181
182 NSThread *cur = [ NSThread currentThread ];
183 if (cur != nil && [ cur respondsToSelector:@selector(setName:) ]) {
184 [ cur performSelector:@selector(setName:) withObject:[ NSString stringWithUTF8String:name.c_str() ] ];
185 }
186}
187
193{
194 return [ [ NSProcessInfo processInfo ] physicalMemory ];
195}
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:379
void AppendPathSeparator(std::string &buf)
Appends, if necessary, the path separator character to the end of the string.
Definition fileio.cpp:348
std::array< std::string, NUM_SEARCHPATHS > _searchpaths
The search paths OpenTTD could search through.
Definition fileio.cpp:65
@ 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.
Definition macos.h:35
bool IsMonospaceFont(CFStringRef name)
Check if a font is a monospace font.
Definition macos.mm:167
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:192
void MacOSSetThreadName(const std::string &name)
Set the name of the current thread for the debugger.
Definition macos.mm:178
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 wich contain objective c.
std::optional< std::string > GetClipboardContents()
Try to retrieve the current clipboard contents.
Definition unix.cpp:205