File: cocoa_wrapper.mm

package info (click to toggle)
warzone2100 4.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,320 kB
  • sloc: cpp: 676,209; ansic: 391,201; javascript: 78,238; python: 16,632; php: 4,294; sh: 4,094; makefile: 2,629; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (179 lines) | stat: -rw-r--r-- 5,564 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 This file is part of Warzone 2100.
 Copyright (C) 1999-2004  Eidos Interactive
 Copyright (C) 2005-2020  Warzone 2100 Project

 Warzone 2100 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; either version 2 of the License, or
 (at your option) any later version.

 Warzone 2100 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. See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Warzone 2100; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#import "cocoa_wrapper.h"

#ifdef WZ_OS_MAC

#if ! __has_feature(objc_arc)
#error "Objective-C ARC (Automatic Reference Counting) is off"
#endif

#import <AppKit/AppKit.h>
#import <ApplicationServices/ApplicationServices.h>
#import <TargetConditionals.h>

static inline NSString * _Nonnull nsstringify(const char *str)
{
    NSString * nsString = [NSString stringWithUTF8String:str];
    if (nsString == nil)
    {
        return @"stringWithUTF8String failed";
    }
	return nsString;
}

bool cocoaSelectFileInFinder(const char *filename)
{
    if (filename == nullptr) return false;
    BOOL success = NO;
	@autoreleasepool {
        success = [[NSWorkspace sharedWorkspace] selectFile:nsstringify(filename) inFileViewerRootedAtPath:@""];
    }
    return success;
}

bool cocoaSelectFolderInFinder(const char* path)
{
	if (path == nullptr) return false;
	BOOL success = NO;
	@autoreleasepool {
		NSURL *pathURL = [NSURL fileURLWithPath:nsstringify(path) isDirectory:YES];
		if (pathURL == nil) return false;
		success = [[NSWorkspace sharedWorkspace] openURL:pathURL];
	}
	return success;
}

bool cocoaOpenURL(const char *url)
{
    assert(url != nullptr);
    BOOL success = NO;
	@autoreleasepool {
        success = [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:nsstringify(url)]];
    }
    return success;
}

bool cocoaOpenUserCrashReportFolder()
{
    BOOL success = NO;
    @autoreleasepool {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString *libraryPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
        if (libraryPath == nil) return false;
        NSURL *libraryURL = [NSURL fileURLWithPath:libraryPath isDirectory:YES];
        NSURL *crashReportsURL = [NSURL URLWithString:@"Logs/DiagnosticReports" relativeToURL:libraryURL];
        success = [[NSWorkspace sharedWorkspace] openURL:crashReportsURL];
    }
    return success;
}

bool cocoaGetApplicationSupportDir(char *const tmpstr, size_t const size)
{
	@autoreleasepool {
		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, TRUE);
		NSString *path = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
		if (path == nil) return false;
		BOOL success = [path getCString:tmpstr maxLength:size encoding:NSUTF8StringEncoding];
		return success;
	}
}

bool cocoaSetFileQuarantineAttribute(const char *path)
{
	@autoreleasepool {
		//	kLSQuarantineTypeKey -> kLSQuarantineTypeOtherDownload
		NSDictionary * quarantineProperties = @{
			(NSString *)kLSQuarantineTypeKey : (NSString *)kLSQuarantineTypeOtherDownload
		};

//		if (@available(macOS 10.10, *)) {	// "@available" is only available on Xcode 9+
		if (floor(NSAppKitVersionNumber) >= NSAppKitVersionNumber10_10) { // alternative to @available
			NSURL *fileURL = [NSURL fileURLWithFileSystemRepresentation:path isDirectory:NO relativeToURL:NULL];
			if (fileURL == nil) return false;

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability" // only required until we can use @available
			NSError *error = nil;
			BOOL success = [fileURL setResourceValue:quarantineProperties forKey:NSURLQuarantinePropertiesKey error:&error];
#pragma clang diagnostic pop
			if (!success)
			{
				// Failed to set resource value
				NSLog(@"Failed to set resource file: %@", error);
				return false;
			}
		} else {
			// macOS 10.9 and earlier require now-deprecated APIs
			return false;
		}
		return true;
	}
}

bool TransformProcessState(ProcessApplicationTransformState newState)
{
	@autoreleasepool {
		ProcessSerialNumber psn = {0, kCurrentProcess};
		OSStatus result = TransformProcessType(&psn, newState);

		if (result != 0)
		{
			NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:result userInfo:nil];
			NSLog(@"TransformProcessType failed with error - %@", error);
		}
		return (result == 0);
	}
}

bool cocoaTransformToBackgroundApplication()
{
    return TransformProcessState(kProcessTransformToBackgroundApplication);
}

bool cocoaIsRunningOnMacOSAtLeastVersion(unsigned major, unsigned minor)
{
# if TARGET_OS_MAC
	@autoreleasepool {
		NSOperatingSystemVersion targetMin = {
			.majorVersion = major,
			.minorVersion = minor,
			.patchVersion = 0
		};

//		if (@available(macOS 10.10, *)) {	// "@available" is only available on Xcode 9+
		if (floor(NSAppKitVersionNumber) >= NSAppKitVersionNumber10_10) { // alternative to @available
			if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:targetMin]) {
				return true;
			}
		} else {
			// macOS 10.9 and earlier require now-deprecated APIs
			return false;
		}
		return false;
	}
#else
	// not macOS
	return false;
#endif
}

#endif // WZ_OS_MAC