2016-07-28 00:20:33 +01:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
2021-12-26 18:47:58 +01:00
|
|
|
* This program 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2016-07-28 00:20:33 +01:00
|
|
|
*
|
|
|
|
* This program 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
|
2021-12-26 18:47:58 +01:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-07-28 00:20:33 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Disable symbol overrides so that we can use system headers.
|
|
|
|
#define FORBIDDEN_SYMBOL_ALLOW_ALL
|
|
|
|
|
|
|
|
#include "backends/platform/sdl/macosx/macosx_wrapper.h"
|
2020-08-30 19:42:19 +01:00
|
|
|
#include "backends/platform/sdl/macosx/macosx-compat.h"
|
2016-07-28 00:20:33 +01:00
|
|
|
|
|
|
|
#include <AppKit/NSPasteboard.h>
|
|
|
|
#include <Foundation/NSArray.h>
|
2022-05-18 16:32:29 +02:00
|
|
|
#include <Foundation/NSBundle.h>
|
2017-04-24 00:30:28 +01:00
|
|
|
#include <Foundation/NSPathUtilities.h>
|
|
|
|
#include <AvailabilityMacros.h>
|
2018-04-11 00:04:48 +01:00
|
|
|
#include <CoreFoundation/CFString.h>
|
2016-07-28 00:20:33 +01:00
|
|
|
|
2021-08-04 09:12:26 +02:00
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6
|
|
|
|
#define NSPasteboardTypeString NSStringPboardType
|
|
|
|
#endif
|
|
|
|
|
2020-08-30 19:42:19 +01:00
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
|
|
|
|
typedef unsigned long NSUInteger;
|
|
|
|
|
2021-08-03 17:23:27 +02:00
|
|
|
// Those are not defined in the 10.4 SDK, but they are defined when targeting
|
|
|
|
// Mac OS X 10.4 or above in the 10.5 SDK. So hopefully that means it works with 10.4 as well.
|
2020-08-30 19:42:19 +01:00
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
|
|
|
|
enum {
|
|
|
|
NSUTF32StringEncoding = 0x8c000100,
|
|
|
|
NSUTF32BigEndianStringEncoding = 0x98000100,
|
|
|
|
NSUTF32LittleEndianStringEncoding = 0x9c000100
|
2020-08-30 23:56:10 +01:00
|
|
|
};
|
2020-08-30 19:42:19 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2016-07-28 00:20:33 +01:00
|
|
|
bool hasTextInClipboardMacOSX() {
|
2021-08-04 09:12:26 +02:00
|
|
|
return [[NSPasteboard generalPasteboard] availableTypeFromArray:[NSArray arrayWithObject:NSPasteboardTypeString]] != nil;
|
2016-07-28 00:20:33 +01:00
|
|
|
}
|
|
|
|
|
2020-06-22 23:25:43 +05:30
|
|
|
Common::U32String getTextFromClipboardMacOSX() {
|
2016-07-28 00:20:33 +01:00
|
|
|
if (!hasTextInClipboardMacOSX())
|
2020-06-22 23:25:43 +05:30
|
|
|
return Common::U32String();
|
2021-08-04 09:12:26 +02:00
|
|
|
|
2016-07-28 00:20:33 +01:00
|
|
|
NSPasteboard *pb = [NSPasteboard generalPasteboard];
|
2021-08-04 09:12:26 +02:00
|
|
|
NSString *str = [pb stringForType:NSPasteboardTypeString];
|
2021-10-11 18:16:40 -04:00
|
|
|
if (![str respondsToSelector:@selector(getBytes:maxLength:usedLength:encoding:options:range:remainingRange:)])
|
2020-06-22 23:25:43 +05:30
|
|
|
return Common::U32String();
|
2018-04-11 00:04:48 +01:00
|
|
|
|
|
|
|
// If translations are supported, use the current TranslationManager charset and otherwise
|
|
|
|
// use ASCII. If the string cannot be represented using the requested encoding we get a null
|
|
|
|
// pointer below, which is fine as ScummVM would not know what to do with the string anyway.
|
2020-06-22 23:25:43 +05:30
|
|
|
#ifdef SCUMM_LITTLE_ENDIAN
|
|
|
|
NSStringEncoding stringEncoding = NSUTF32LittleEndianStringEncoding;
|
2018-04-11 00:04:48 +01:00
|
|
|
#else
|
2020-06-22 23:25:43 +05:30
|
|
|
NSStringEncoding stringEncoding = NSUTF32BigEndianStringEncoding;
|
2018-04-11 00:04:48 +01:00
|
|
|
#endif
|
2020-06-22 23:25:43 +05:30
|
|
|
NSUInteger textLength = [str length];
|
|
|
|
uint32 *text = new uint32[textLength];
|
2020-08-18 00:35:52 +01:00
|
|
|
if (![str getBytes:text maxLength:4*textLength usedLength:NULL encoding: stringEncoding options:0 range:NSMakeRange(0, textLength) remainingRange:NULL]) {
|
2020-06-22 23:25:43 +05:30
|
|
|
delete[] text;
|
|
|
|
return Common::U32String();
|
|
|
|
}
|
|
|
|
Common::U32String u32String(text, textLength);
|
|
|
|
delete[] text;
|
|
|
|
|
|
|
|
return u32String;
|
2016-07-28 00:20:33 +01:00
|
|
|
}
|
2017-04-24 00:30:28 +01:00
|
|
|
|
2020-06-22 23:25:43 +05:30
|
|
|
bool setTextInClipboardMacOSX(const Common::U32String &text) {
|
2018-04-10 23:41:10 +01:00
|
|
|
NSPasteboard *pb = [NSPasteboard generalPasteboard];
|
2021-08-04 09:12:26 +02:00
|
|
|
[pb declareTypes:[NSArray arrayWithObject:NSPasteboardTypeString] owner:nil];
|
2018-04-11 00:04:48 +01:00
|
|
|
|
2020-06-22 23:25:43 +05:30
|
|
|
#ifdef SCUMM_LITTLE_ENDIAN
|
|
|
|
NSStringEncoding stringEncoding = NSUTF32LittleEndianStringEncoding;
|
2018-04-11 00:04:48 +01:00
|
|
|
#else
|
2020-06-22 23:25:43 +05:30
|
|
|
NSStringEncoding stringEncoding = NSUTF32BigEndianStringEncoding;
|
2018-04-11 00:04:48 +01:00
|
|
|
#endif
|
2020-08-19 20:05:45 +01:00
|
|
|
NSString *nsstring = [[NSString alloc] initWithBytes:text.c_str() length:4*text.size() encoding: stringEncoding];
|
2021-08-04 09:12:26 +02:00
|
|
|
bool status = [pb setString:nsstring forType:NSPasteboardTypeString];
|
2020-08-19 20:05:45 +01:00
|
|
|
[nsstring release];
|
|
|
|
return status;
|
2018-04-10 23:41:10 +01:00
|
|
|
}
|
|
|
|
|
2017-04-24 00:30:28 +01:00
|
|
|
Common::String getDesktopPathMacOSX() {
|
2021-08-03 17:23:27 +02:00
|
|
|
// The recommended method is to use NSFileManager.
|
2017-04-24 00:30:28 +01:00
|
|
|
// NSUrl *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDesktopDirectory inDomains:NSUserDomainMask] firstObject];
|
|
|
|
// However it is only available in OS X 10.6+. So use NSSearchPathForDirectoriesInDomains instead (available since OS X 10.0)
|
|
|
|
// [NSArray firstObject] is also only available in OS X 10.6+. So we need to use [NSArray count] and [NSArray objectAtIndex:]
|
2017-04-26 08:38:55 +01:00
|
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
|
2017-04-24 00:30:28 +01:00
|
|
|
if ([paths count] == 0)
|
|
|
|
return Common::String();
|
|
|
|
NSString *path = [paths objectAtIndex:0];
|
|
|
|
if (path == nil)
|
|
|
|
return Common::String();
|
2022-02-05 01:45:27 -07:00
|
|
|
return Common::String([path fileSystemRepresentation]);
|
2017-04-26 08:38:55 +01:00
|
|
|
}
|
2022-05-06 18:59:38 +02:00
|
|
|
|
|
|
|
Common::String getResourceAppBundlePathMacOSX() {
|
2022-05-18 16:32:29 +02:00
|
|
|
NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
|
|
|
|
if (bundlePath == nil)
|
|
|
|
return Common::String();
|
|
|
|
return Common::String([bundlePath fileSystemRepresentation]);
|
2022-05-06 18:59:38 +02:00
|
|
|
}
|
2022-06-30 13:21:42 -05:00
|
|
|
|
|
|
|
Common::String getAppSupportPathMacOSX() {
|
|
|
|
// See comments in getDesktopPathMacOSX() as we use the same methods
|
|
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
|
|
|
|
if ([paths count] == 0)
|
|
|
|
return Common::String();
|
|
|
|
NSString *path = [paths objectAtIndex:0];
|
|
|
|
if (path == nil)
|
|
|
|
return Common::String();
|
|
|
|
return Common::String([path fileSystemRepresentation]) + "/ScummVM";
|
|
|
|
}
|