2009-08-21 22:21:05 +02:00
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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 <http://www.gnu.org/licenses/>.
*/
2011-07-21 18:13:34 +02:00
/** @file macos.mm Code related to MacOSX. */
2009-07-14 18:43:45 +02:00
#include "../../stdafx.h"
#include "../../core/bitmath_func.hpp"
#include "../../rev.h"
2009-10-04 23:08:30 +02:00
#include "macos.h"
2009-10-04 23:08:38 +02:00
#include "../../string_func.h"
2018-01-28 23:45:02 +01:00
#include <pthread.h>
2007-11-25 15:43:16 +01:00
2009-07-14 18:43:45 +02:00
#define Rect OTTDRect
#define Point OTTDPoint
2005-07-24 22:47:42 +02:00
#include <AppKit/AppKit.h>
2009-07-14 18:43:45 +02:00
#undef Rect
#undef Point
2005-07-24 22:47:42 +02:00
2018-01-28 23:01:10 +01:00
#ifndef __clang__
#define __bridge
#endif
2005-07-24 22:47:42 +02:00
/*
* This file contains objective C
* Apple uses objective C instead of plain C to interact with OS specific/native functions
*/
2009-10-04 22:53:30 +02:00
2018-01-28 23:02:22 +01:00
#if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_10)
typedef struct {
NSInteger majorVersion;
NSInteger minorVersion;
NSInteger patchVersion;
} OTTDOperatingSystemVersion;
#define NSOperatingSystemVersion OTTDOperatingSystemVersion
#endif
2009-10-04 22:53:30 +02:00
/**
* Get the version of the MacOS we are running under. Code adopted
* from http://www.cocoadev.com/index.pl?DeterminingOSVersion
* @param return_major major version of the os. This would be 10 in the case of 10.4.11
* @param return_minor minor version of the os. This would be 4 in the case of 10.4.11
* @param return_bugfix bugfix version of the os. This would be 11 in the case of 10.4.11
* A return value of -1 indicates that something went wrong and we don't know.
*/
void GetMacOSVersion(int *return_major, int *return_minor, int *return_bugfix)
{
*return_major = -1;
*return_minor = -1;
*return_bugfix = -1;
2018-01-28 23:02:22 +01:00
if ([[ NSProcessInfo processInfo] respondsToSelector:@selector(operatingSystemVersion) ]) {
IMP sel = [ [ NSProcessInfo processInfo] methodForSelector:@selector(operatingSystemVersion) ];
NSOperatingSystemVersion ver = ((NSOperatingSystemVersion (*)(id, SEL))sel)([ NSProcessInfo processInfo], @selector(operatingSystemVersion));
*return_major = (int)ver.majorVersion;
*return_minor = (int)ver.minorVersion;
*return_bugfix = (int)ver.patchVersion;
return;
}
#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_10)
2009-10-04 22:53:30 +02:00
SInt32 systemVersion, version_major, version_minor, version_bugfix;
if (Gestalt(gestaltSystemVersion, &systemVersion) == noErr) {
if (systemVersion >= 0x1040) {
if (Gestalt(gestaltSystemVersionMajor, &version_major) == noErr) *return_major = (int)version_major;
if (Gestalt(gestaltSystemVersionMinor, &version_minor) == noErr) *return_minor = (int)version_minor;
if (Gestalt(gestaltSystemVersionBugFix, &version_bugfix) == noErr) *return_bugfix = (int)version_bugfix;
} else {
*return_major = (int)(GB(systemVersion, 12, 4) * 10 + GB(systemVersion, 8, 4));
*return_minor = (int)GB(systemVersion, 4, 4);
*return_bugfix = (int)GB(systemVersion, 0, 4);
}
}
2018-01-28 23:02:22 +01:00
#endif
2009-10-04 22:53:30 +02:00
}
2006-01-06 22:27:44 +01:00
#ifdef WITH_SDL
2011-07-21 18:13:34 +02:00
/**
* Show the system dialogue message (SDL on MacOSX).
*
* @param title Window title.
* @param message Message text.
* @param buttonLabel Button text.
*/
2009-01-10 01:31:47 +01:00
void ShowMacDialog(const char *title, const char *message, const char *buttonLabel)
2005-07-24 22:47:42 +02:00
{
2009-10-04 23:33:31 +02:00
NSRunAlertPanel([ NSString stringWithUTF8String:title ], [ NSString stringWithUTF8String:message ], [ NSString stringWithUTF8String:buttonLabel ], nil, nil);
2005-07-24 22:47:42 +02:00
}
2006-01-06 22:27:44 +01:00
#elif defined WITH_COCOA
2009-10-04 23:08:30 +02:00
extern void CocoaDialog(const char *title, const char *message, const char *buttonLabel);
2006-01-06 22:27:44 +01:00
2011-07-21 18:13:34 +02:00
/**
* Show the system dialogue message (Cocoa on MacOSX).
*
* @param title Window title.
* @param message Message text.
* @param buttonLabel Button text.
*/
2009-01-10 01:31:47 +01:00
void ShowMacDialog(const char *title, const char *message, const char *buttonLabel)
2006-01-06 22:27:44 +01:00
{
CocoaDialog(title, message, buttonLabel);
}
#else
2011-07-21 18:13:34 +02:00
/**
* Show the system dialogue message (console on MacOSX).
*
* @param title Window title.
* @param message Message text.
* @param buttonLabel Button text.
*/
2009-01-10 01:31:47 +01:00
void ShowMacDialog(const char *title, const char *message, const char *buttonLabel)
2006-01-06 22:27:44 +01:00
{
fprintf(stderr, "%s: %s\n", title, message);
}
#endif
2006-03-02 22:43:09 +01:00
2011-07-21 18:13:34 +02:00
/**
* Show an error message.
*
* @param buf error message text.
* @param system message text originates from OS.
*/
2009-10-04 23:08:30 +02:00
void ShowOSErrorBox(const char *buf, bool system)
2006-03-02 22:43:09 +01:00
{
2009-10-04 23:08:30 +02:00
/* Display the error in the best way possible. */
if (system) {
ShowMacDialog("OpenTTD has encountered an error", buf, "Quit");
} else {
2010-09-30 20:58:56 +02:00
ShowMacDialog(buf, "See the readme for more info.", "Quit");
2009-10-04 23:08:30 +02:00
}
2006-03-02 22:43:09 +01:00
}
2007-08-11 17:52:34 +02:00
2011-12-11 12:47:08 +01:00
void OSOpenBrowser(const char *url)
{
[ [ NSWorkspace sharedWorkspace ] openURL:[ NSURL URLWithString:[ NSString stringWithUTF8String:url ] ] ];
}
2007-08-11 17:52:34 +02:00
2011-07-21 18:13:34 +02:00
/**
* Determine and return the current user's locale.
*/
2007-08-11 17:52:34 +02:00
const char *GetCurrentLocale(const char *)
{
static char retbuf[32] = { '\0' };
2009-10-04 23:36:17 +02:00
NSUserDefaults *defs = [ NSUserDefaults standardUserDefaults ];
NSArray *languages = [ defs objectForKey:@"AppleLanguages" ];
NSString *preferredLang = [ languages objectAtIndex:0 ];
2007-08-11 17:52:34 +02:00
/* preferredLang is either 2 or 5 characters long ("xx" or "xx_YY"). */
2007-11-30 14:26:24 +01:00
2008-07-31 16:00:01 +02:00
/* Since Apple introduced encoding to CString in OSX 10.4 we have to make a few conditions
* to get the right code for the used version of OSX. */
2009-10-04 22:53:22 +02:00
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
2007-11-25 15:43:16 +01:00
if (MacOSVersionIsAtLeast(10, 4, 0)) {
[ preferredLang getCString:retbuf maxLength:32 encoding:NSASCIIStringEncoding ];
2008-07-31 16:00:01 +02:00
} else
#endif
2007-11-30 14:26:24 +01:00
{
2009-10-04 22:53:22 +02:00
#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4)
/* maxLength does not include the \0 char in contrast to the call above. */
[ preferredLang getCString:retbuf maxLength:31 ];
2007-11-30 14:26:24 +01:00
#endif
2007-11-25 15:43:16 +01:00
}
2007-08-11 17:52:34 +02:00
return retbuf;
}
2007-11-25 15:43:16 +01:00
2009-11-14 16:26:43 +01:00
#ifdef WITH_COCOA
2011-07-21 18:13:34 +02:00
/**
* Return the contents of the clipboard (COCOA).
*
2014-04-23 22:44:42 +02:00
* @param buffer Clipboard content.
* @param last The pointer to the last element of the destination buffer
2011-07-21 18:13:34 +02:00
* @return Whether clipboard is empty or not.
*/
2014-04-23 22:44:42 +02:00
bool GetClipboardContents(char *buffer, const char *last)
2009-10-04 23:08:38 +02:00
{
NSPasteboard *pb = [ NSPasteboard generalPasteboard ];
NSArray *types = [ NSArray arrayWithObject:NSStringPboardType ];
NSString *bestType = [ pb availableTypeFromArray:types ];
/* Clipboard has no text data available. */
if (bestType == nil) return false;
NSString *string = [ pb stringForType:NSStringPboardType ];
if (string == nil || [ string length ] == 0) return false;
2014-04-23 22:44:42 +02:00
strecpy(buffer, [ string UTF8String ], last);
2009-10-04 23:08:38 +02:00
return true;
}
2009-11-14 16:26:43 +01:00
#endif
2011-12-10 17:54:41 +01:00
2013-08-05 22:36:06 +02:00
/**
* Check if a font is a monospace font.
* @param name Name of the font.
* @return True if the font is a monospace font.
*/
bool IsMonospaceFont(CFStringRef name)
{
2018-01-28 23:01:10 +01:00
NSFont *font = [ NSFont fontWithName:(__bridge NSString *)name size:0.0f ];
2013-08-05 22:36:06 +02:00
return font != NULL ? [ font isFixedPitch ] : false;
}
2018-01-28 23:45:02 +01:00
/**
* Set the name of the current thread for the debugger.
* @param name The new name of the current thread.
*/
void MacOSSetThreadName(const char *name)
{
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
if (MacOSVersionIsAtLeast(10, 6, 0)) {
pthread_setname_np(name);
}
#endif
NSThread *cur = [ NSThread currentThread ];
if (cur != NULL && [ cur respondsToSelector:@selector(setName:) ]) {
[ cur performSelector:@selector(setName:) withObject:[ NSString stringWithUTF8String:name ] ];
}
}