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