use new method in MidiPatchManager to use MIDNAM data when setting a MidiTimeAxisView
[ardour.git] / gtk2_ardour / cocoacarbon.mm
1 /*
2  * Copyright (C) 2008-2016 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2008 David Robillard <d@drobilla.net>
4  * Copyright (C) 2014-2016 Robin Gareus <robin@gareus.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <string>
22 #include <ctype.h>
23 #include <stdlib.h>
24 #include <pbd/error.h>
25 #include <gtkmm2ext/gtkapplication.h>
26 #include <gdk/gdkquartz.h>
27 #undef check
28 #undef YES
29 #undef NO
30 #ifdef verify
31 #undef verify
32 #endif
33
34 #include "ardour_ui.h"
35 #include "actions.h"
36 #include "opts.h"
37
38 #include <CoreFoundation/CFLocale.h>
39 #import <CoreFoundation/CFString.h>
40 #import <Foundation/NSString.h>
41 #import <Foundation/NSAutoreleasePool.h>
42
43 using namespace std;
44 using namespace PBD;
45
46 bool
47 cocoa_open_url (const char* uri)
48 {
49         NSString* struri = [[NSString alloc] initWithUTF8String:uri];
50         NSURL* nsurl = [[NSURL alloc] initWithString:struri];
51
52         bool ret = [[NSWorkspace sharedWorkspace] openURL:nsurl];
53
54         [struri release];
55         [nsurl release];
56
57         return ret;
58 }
59
60 void
61 set_language_preference ()
62 {
63         gtk_disable_setlocale ();
64
65         /* the gettext manual is potentially misleading about the utility of
66            LANGUAGE.  It notes that if LANGUAGE is set to include a dialect/region-free
67            language code, like "it", it will assume that you mean the main
68            dialect (e.g. "it_IT"). But in reality, it doesn't bother looking for
69            locale dirs with the full name, only the short code (it doesn't
70            know any better).
71            
72            Since Apple's preferred language list only consists of short language codes,
73            if we set LANGUAGE then gettext will not look for the relevant long-form
74            variants.
75         */
76
77         /* how to get language preferences with CoreFoundation
78          */
79
80         NSArray* languages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
81         
82         /* push into LANGUAGE */
83
84         if (languages && [languages count] > 0) {
85
86                 int i, count = [languages count];
87                 for (i = 0; i < count; ++i) {
88                         if ([[languages objectAtIndex:i]
89                              isEqualToString:@"en"]) {
90                                 count = i+1;
91                                 break;
92                         }
93                 }
94                 NSRange r = { 0, static_cast<NSUInteger> (count) };
95                 setenv ("LANGUAGE", [[[languages subarrayWithRange:r] componentsJoinedByString:@":"] UTF8String], 0);
96                 cout << "LANGUAGE set to " << getenv ("LANGUAGE") << endl;
97         }
98
99         /* now get AppleLocale value and use that for LANG */
100
101         CFLocaleRef cflocale = CFLocaleCopyCurrent();
102         NSString* nslocale = (NSString*) CFLocaleGetValue (cflocale, kCFLocaleIdentifier);
103
104         /* the full POSIX locale specification allows for lots of things. that could be an issue. Silly Apple.
105          */
106
107         cout << "LANG set to " << [nslocale UTF8String] << endl;
108         setenv ("LANG", [nslocale UTF8String], 0);
109         CFRelease (cflocale);
110 }
111
112         /* Prevent "App Nap" */
113
114 void
115 no_app_nap ()
116 {
117
118 #ifndef NSActivityLatencyCritical
119 #define NSActivityLatencyCritical 0xFF00000000ULL
120 #endif
121
122         if ( [ [ NSProcessInfo processInfo ] respondsToSelector:@selector(beginActivityWithOptions:reason:) ] ) {
123                 cout << "Disabling MacOS AppNap\n";
124                 [ [ NSProcessInfo processInfo] beginActivityWithOptions:NSActivityLatencyCritical reason:@"realtime audio" ];
125         }
126 }