2008-01-10 16:20:59 -05:00
|
|
|
/*
|
2019-08-02 17:26:43 -04:00
|
|
|
* Copyright (C) 2008-2016 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
* Copyright (C) 2008 David Robillard <d@drobilla.net>
|
|
|
|
* Copyright (C) 2014-2016 Robin Gareus <robin@gareus.org>
|
|
|
|
*
|
|
|
|
* 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 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2008-01-10 16:20:59 -05:00
|
|
|
|
2010-11-13 00:14:48 -05:00
|
|
|
#include <string>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
2020-03-24 09:25:22 -04:00
|
|
|
#include <sys/utsname.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
2010-11-13 00:14:48 -05:00
|
|
|
#include <pbd/error.h>
|
|
|
|
#include <gtkmm2ext/gtkapplication.h>
|
|
|
|
#include <gdk/gdkquartz.h>
|
2020-03-24 09:25:22 -04:00
|
|
|
|
2010-11-13 00:14:48 -05:00
|
|
|
#undef check
|
|
|
|
#undef YES
|
|
|
|
#undef NO
|
2014-10-19 13:54:01 -04:00
|
|
|
#ifdef verify
|
|
|
|
#undef verify
|
|
|
|
#endif
|
2009-08-08 18:36:32 -04:00
|
|
|
|
2008-01-10 16:20:59 -05:00
|
|
|
#include "ardour_ui.h"
|
|
|
|
#include "actions.h"
|
|
|
|
#include "opts.h"
|
|
|
|
|
2010-11-13 00:14:48 -05:00
|
|
|
#include <CoreFoundation/CFLocale.h>
|
|
|
|
#import <CoreFoundation/CFString.h>
|
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
#import <Foundation/NSAutoreleasePool.h>
|
2008-01-10 16:20:59 -05:00
|
|
|
|
2010-11-13 00:14:48 -05:00
|
|
|
using namespace std;
|
|
|
|
using namespace PBD;
|
2008-01-10 16:20:59 -05:00
|
|
|
|
2010-11-13 00:14:48 -05:00
|
|
|
bool
|
|
|
|
cocoa_open_url (const char* uri)
|
2008-03-17 16:54:03 -04:00
|
|
|
{
|
2010-11-13 00:14:48 -05:00
|
|
|
NSString* struri = [[NSString alloc] initWithUTF8String:uri];
|
|
|
|
NSURL* nsurl = [[NSURL alloc] initWithString:struri];
|
2008-03-17 16:54:03 -04:00
|
|
|
|
2010-11-13 00:14:48 -05:00
|
|
|
bool ret = [[NSWorkspace sharedWorkspace] openURL:nsurl];
|
2008-03-17 16:54:03 -04:00
|
|
|
|
2010-11-13 00:14:48 -05:00
|
|
|
[struri release];
|
|
|
|
[nsurl release];
|
2008-01-10 16:20:59 -05:00
|
|
|
|
2010-11-13 00:14:48 -05:00
|
|
|
return ret;
|
2008-01-10 16:20:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-11-13 00:14:48 -05:00
|
|
|
set_language_preference ()
|
2008-01-10 16:20:59 -05:00
|
|
|
{
|
2010-11-13 00:14:48 -05:00
|
|
|
gtk_disable_setlocale ();
|
2008-01-10 16:20:59 -05:00
|
|
|
|
2010-11-13 00:14:48 -05:00
|
|
|
/* the gettext manual is potentially misleading about the utility of
|
|
|
|
LANGUAGE. It notes that if LANGUAGE is set to include a dialect/region-free
|
|
|
|
language code, like "it", it will assume that you mean the main
|
|
|
|
dialect (e.g. "it_IT"). But in reality, it doesn't bother looking for
|
|
|
|
locale dirs with the full name, only the short code (it doesn't
|
|
|
|
know any better).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* how to get language preferences with CoreFoundation
|
|
|
|
*/
|
|
|
|
|
|
|
|
NSArray* languages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
|
2020-05-29 12:23:26 -04:00
|
|
|
|
2010-11-13 00:14:48 -05:00
|
|
|
/* push into LANGUAGE */
|
2008-03-17 16:54:03 -04:00
|
|
|
|
2020-05-29 12:23:26 -04:00
|
|
|
int count;
|
|
|
|
|
|
|
|
if (languages && ((count = [languages count]) > 0)) {
|
2008-01-10 16:20:59 -05:00
|
|
|
|
2020-05-01 14:30:46 -04:00
|
|
|
bool have_translatable_languages = true;
|
|
|
|
|
2020-05-29 12:23:26 -04:00
|
|
|
const char *cstr = [[languages objectAtIndex:0] UTF8String];
|
|
|
|
const size_t len = strlen (cstr);
|
|
|
|
|
|
|
|
if (len > 1 && cstr[0] == 'e' && cstr[1] == 'n') {
|
|
|
|
if (len == 2) {
|
|
|
|
/* primary language is english (no region). Do not set
|
|
|
|
LANGUAGE, gettext should not translate
|
|
|
|
*/
|
|
|
|
have_translatable_languages = false;
|
|
|
|
cout << "User has en as primary language choice. " << PROGRAM_NAME << " will not be translated\n";
|
2020-05-29 20:51:54 -04:00
|
|
|
} else if (len == 5 && cstr[len-2] == 'U' && cstr[len-1] == 'S') {
|
2020-05-01 14:30:46 -04:00
|
|
|
/* primary language choice is english (US). Stop looking, and do not set
|
|
|
|
LANGUAGE. gettext needs to just skip translation entirely.
|
|
|
|
*/
|
|
|
|
have_translatable_languages = false;
|
|
|
|
cout << "User has en_US as primary language choice. " << PROGRAM_NAME << " will not be translated\n";
|
2010-11-13 00:14:48 -05:00
|
|
|
}
|
2020-05-29 12:23:26 -04:00
|
|
|
|
|
|
|
/* else en-<FOO> ... still leave the door open for translation
|
|
|
|
to other version of english (e.g. en_IN, en_GB, etc)
|
|
|
|
*/
|
2010-11-13 00:14:48 -05:00
|
|
|
}
|
2020-05-01 14:31:52 -04:00
|
|
|
|
|
|
|
if (have_translatable_languages) {
|
|
|
|
|
|
|
|
NSRange r = { 0, static_cast<NSUInteger> (count) };
|
|
|
|
|
|
|
|
std::string stupid_apple_string = [[[languages subarrayWithRange:r] componentsJoinedByString:@":"] UTF8String];
|
|
|
|
|
|
|
|
/* Apple's language preference tokens use "-" to separate the two letter ISO language code from the two-letter
|
|
|
|
ISO region code. So for a German speaker in Germany whose macOS system settings reflect these realities the user
|
|
|
|
language preference will be "de-DE".
|
|
|
|
|
|
|
|
Why Apple did this when the standard everywhere else is to use an underscore is unclear. However, we do know that
|
|
|
|
neither gettext not setlocale(2) will work with these hyphen separated tokens, so fix them.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (std::string::iterator s = stupid_apple_string.begin(); s != stupid_apple_string.end(); ++s) {
|
|
|
|
if (*s == '-') {
|
|
|
|
*s = '_';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setenv ("LANGUAGE", stupid_apple_string.c_str(), 0);
|
|
|
|
cout << "LANGUAGE set to " << getenv ("LANGUAGE") << endl;
|
|
|
|
}
|
2010-11-13 00:14:48 -05:00
|
|
|
}
|
2008-03-17 16:54:03 -04:00
|
|
|
|
2020-05-01 14:32:38 -04:00
|
|
|
/* now get AppleLocale value and use that to set LC_ALL because Launchd-started processes (i.e. GUI apps)
|
|
|
|
do not have this environment variable set, and without it, setlocale (LC_ALL, "") will fail.
|
|
|
|
|
|
|
|
Note that it doesn't matter much what we set LC_ALL to for gettext's purposes, but other aspects of the
|
|
|
|
locale system do need a value that mostly/sort-of/kind-of represents the user's own preferences. So, get
|
|
|
|
that from CoreFoundation APIs.
|
|
|
|
*/
|
2008-12-12 09:43:24 -05:00
|
|
|
|
2010-11-13 00:14:48 -05:00
|
|
|
CFLocaleRef cflocale = CFLocaleCopyCurrent();
|
|
|
|
NSString* nslocale = (NSString*) CFLocaleGetValue (cflocale, kCFLocaleIdentifier);
|
2008-12-12 09:43:24 -05:00
|
|
|
|
2020-05-01 14:32:38 -04:00
|
|
|
cout << "Apple's CoreFoundation API says that this user's locale is " << [nslocale UTF8String] << endl;
|
|
|
|
setenv ("LC_ALL", [nslocale UTF8String], 0);
|
2010-11-13 00:14:48 -05:00
|
|
|
CFRelease (cflocale);
|
2008-12-12 09:43:24 -05:00
|
|
|
}
|
2016-08-19 11:12:05 -04:00
|
|
|
|
|
|
|
/* Prevent "App Nap" */
|
|
|
|
|
|
|
|
void
|
|
|
|
no_app_nap ()
|
|
|
|
{
|
2016-08-19 13:19:23 -04:00
|
|
|
|
|
|
|
#ifndef NSActivityLatencyCritical
|
|
|
|
#define NSActivityLatencyCritical 0xFF00000000ULL
|
|
|
|
#endif
|
|
|
|
|
2016-08-19 11:12:05 -04:00
|
|
|
if ( [ [ NSProcessInfo processInfo ] respondsToSelector:@selector(beginActivityWithOptions:reason:) ] ) {
|
|
|
|
cout << "Disabling MacOS AppNap\n";
|
|
|
|
[ [ NSProcessInfo processInfo] beginActivityWithOptions:NSActivityLatencyCritical reason:@"realtime audio" ];
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 09:25:22 -04:00
|
|
|
|
|
|
|
/** Query Darwin kernel version.
|
|
|
|
* @return major kernel version or -1 on failure
|
|
|
|
*
|
|
|
|
* kernel version is 4 ahead of OS X release
|
|
|
|
* 19.x.x - OS 10.15 (Catalina)
|
|
|
|
* 18.x.x - OS 10.14 (Mojave)
|
|
|
|
* 17.x.x - OS 10.13 (High Sierra)
|
|
|
|
* 16.x.x - OS 10.12 (Sierra)
|
|
|
|
* ...
|
|
|
|
* 10.x.x - OS 10.6 (Snow Leopard)
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
query_darwin_version ()
|
|
|
|
{
|
|
|
|
char str[256] = {0};
|
|
|
|
size_t size = sizeof(str);
|
|
|
|
|
|
|
|
if (0 == sysctlbyname ("kern.osrelease", str, &size, NULL, 0)) {
|
|
|
|
short int v[3];
|
|
|
|
if (3 == sscanf (str, "%hd.%hd.%hd", &v[0], &v[1], &v[2])) {
|
|
|
|
return v[0]; // major version only
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
struct utsname name;
|
|
|
|
uname (&name);
|
|
|
|
int v[3];
|
|
|
|
if (3 == sscanf (name.release, "%d.%d.%d", &v[0], &v[1], &v[2])) {
|
|
|
|
return v[0]; // major version only
|
|
|
|
}
|
|
|
|
if (2 == sscanf (name.release, "%d.%d", &v[0], &v[1])) {
|
|
|
|
return v[0]; // major version only
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|