Debian Codegraph @

src/GUI/CocoaHelpers.mm

raw
1 // CocoaHelpers.mm - C++ implementation of Cocoa/AppKit helpers
2
3 // Copyright (C) 2013 James Turner <[email protected]>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 //
19
20 #include <config.h>
21
22 #include "CocoaHelpers.h"
23 #include "CocoaHelpers_private.h"
24
25 // standard
26 #include <string>
27 #include <dlfcn.h>
28
29 // OS
30
31 #include <Foundation/NSAutoreleasePool.h>
32 #include <Foundation/NSURL.h>
33 #include <Foundation/NSFileManager.h>
34 #include <Foundation/NSBundle.h>
35 #include <Foundation/NSLocale.h>
36 #include <AppKit/NSAlert.h>
37
38 // simgear
39 #include <simgear/misc/sg_path.hxx>
40 #include <simgear/debug/logstream.hxx>
41 #include <simgear/structure/commands.hxx>
42
43 // flightgear
44 #include <GUI/MessageBox.hxx>
45 #include <Main/options.hxx>
46 #include <Main/locale.hxx>
47 #include <Main/globals.hxx>
48
49 #if defined(HAVE_QT)
50 # include <GUI/QtLauncher.hxx>
51 #endif
52
53 NSString* stdStringToCocoa(const std::string& s)
54 {
55 return [NSString stringWithUTF8String:s.c_str()];
56 }
57
58 std::string stdStringFromCocoa(NSString* s)
59 {
60 return std::string([s UTF8String]);
61 }
62
63 NSURL* pathToNSURL(const SGPath& aPath)
64 {
65 return [NSURL fileURLWithPath:stdStringToCocoa(aPath.utf8Str())];
66 }
67
68 SGPath URLToPath(NSURL* url)
69 {
70 if (!url) {
71 return SGPath();
72 }
73
74 return SGPath([[url path] UTF8String]);
75 }
76
77 flightgear::MessageBoxResult cocoaMessageBox(const std::string& msg,
78 const std::string& text)
79 {
80 CocoaAutoreleasePool pool;
81 NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
82 defaultButton:nil /* localized 'ok' */
83 alternateButton:nil
84 otherButton:nil
85 informativeTextWithFormat:@" %@",stdStringToCocoa(text)];
86 [[alert retain] autorelease];
87 [alert runModal];
88 return flightgear::MSG_BOX_OK;
89 }
90
91
92
93 flightgear::MessageBoxResult cocoaFatalMessage(const std::string& msg,
94 const std::string& text)
95 {
96 CocoaAutoreleasePool pool;
97 NSAlert* alert = [NSAlert alertWithMessageText:stdStringToCocoa(msg)
98 defaultButton:@"Quit FlightGear"
99 alternateButton:nil
100 otherButton:nil
101 informativeTextWithFormat:@"%@", stdStringToCocoa(text)];
102 [[alert retain] autorelease];
103 [alert runModal];
104 return flightgear::MSG_BOX_OK;
105 }
106
107 void cocoaOpenUrl(const std::string& url)
108 {
109 CocoaAutoreleasePool pool;
110 NSURL* nsu = [NSURL URLWithString:stdStringToCocoa(url)];
111 [[NSWorkspace sharedWorkspace] openURL:nsu];
112 }
113
114 CocoaAutoreleasePool::CocoaAutoreleasePool()
115 {
116 pool = [[NSAutoreleasePool alloc] init];
117 }
118
119 CocoaAutoreleasePool::~CocoaAutoreleasePool()
120 {
121 [pool release];
122 }
123
124 SGPath platformDefaultDataPath()
125 {
126 CocoaAutoreleasePool ap;
127 NSFileManager* fm = [NSFileManager defaultManager];
128
129 NSURL* appSupportUrl = [fm URLForDirectory:NSApplicationSupportDirectory
130 inDomain:NSUserDomainMask
131 appropriateForURL:Nil
132 create:YES
133 error:nil];
134 if (!appSupportUrl) {
135 return SGPath();
136 }
137
138 SGPath appData(URLToPath(appSupportUrl));
139 appData.append("FlightGear");
140 return appData;
141 }
142
143 string_list FGLocale::getUserLanguages()
144 {
145 CocoaAutoreleasePool ap;
146 string_list result;
147
148 for (NSString* lang in [NSLocale preferredLanguages]) {
149 result.push_back(stdStringFromCocoa(lang));
150 }
151
152 return result;
153 }
154
155 void transformToForegroundApp()
156 {
157 ProcessSerialNumber sn = { 0, kCurrentProcess };
158 TransformProcessType(&sn,kProcessTransformToForegroundApplication);
159
160
161 [[NSApplication sharedApplication] activateIgnoringOtherApps: YES];
162 }
163
164 @interface FlightGearNSAppDelegate : NSObject <NSApplicationDelegate>
165 @end
166
167 @implementation FlightGearNSAppDelegate
168
169
170 // it would be lovely to do the following, but the return code doesn't
171 // seem to work as documented - maybe it's ignored due to newer process
172 // management
173 #if 0
174 - (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication *)sender
175 {
176 SGCommandMgr* mgr = SGCommandMgr::instance();
177 SGPropertyNode_ptr propArgs(new SGPropertyNode);
178 propArgs->setStringValue("dialog-name", "exit");
179 mgr->execute("dialog-show", propArgs, globals->get_props());
180
181 return NSTerminateCancel;
182 }
183 #endif
184
185 - (void) applicationWillTerminate:(NSNotification *)notification
186 {
187 SG_LOG(SG_GENERAL, SG_INFO, "macOS quit occuring");
188 #if defined(HAVE_QT)
189 flightgear::shutdownQtApp();
190 #endif
191 }
192
193 @end
194
195 void cocoaRegisterTerminateHandler()
196 {
197 FlightGearNSAppDelegate* delegate = [[FlightGearNSAppDelegate alloc] init];
198
199 auto *app = [NSApplication sharedApplication];
200 [[NSNotificationCenter defaultCenter]
201 addObserver:delegate
202 selector:@selector(applicationWillTerminate:)
203 name:NSApplicationWillTerminateNotification object:app];
204 #if 9
205 [[NSNotificationCenter defaultCenter]
206 addObserver:delegate
207 selector:@selector(applicationShouldTerminate:)
208 name:NSApplicationWillTerminateNotification object:app];
209 #endif
210 }
211
212
213
214 bool cocoaIsRunningTranslocated()
215 {
216 // #define NSAppKitVersionNumber10_11 1404
217 if (floor(NSAppKitVersionNumber) <= 1404) {
218 return false;
219 }
220
221 void *handle = dlopen("/System/Library/Frameworks/Security.framework/Security", RTLD_LAZY);
222 if (handle == NULL) {
223 return false;
224 }
225
226 bool isTranslocated = false;
227 using SecIsTranslocatedFunc = Boolean (*)(CFURLRef path, bool *isTranslocated, CFErrorRef * __nullable error);
228
229 SecIsTranslocatedFunc f = reinterpret_cast<SecIsTranslocatedFunc>(dlsym(handle, "SecTranslocateIsTranslocatedURL"));
230 if (f != NULL) {
231 auto url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
232 f(url, &isTranslocated, NULL);
233
234 CFRelease(url);
235 }
236
237 dlclose(handle);
238 return isTranslocated;
239 }