| 1 |
// |
| 2 |
// AudioSettingsPrefPanelController.m |
| 3 |
// Alchemusica |
| 4 |
// |
| 5 |
// Created by Toshi Nagata on 2019/07/21. |
| 6 |
// Copyright 2010-2019 Toshi Nagata. All rights reserved. |
| 7 |
// |
| 8 |
/* |
| 9 |
This program is free software; you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation version 2 of the License. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
*/ |
| 18 |
|
| 19 |
#import "AudioSettingsPrefPanelController.h" |
| 20 |
#import "MyAppController.h" |
| 21 |
#import "MDHeaders.h" |
| 22 |
|
| 23 |
@implementation AudioSettingsPrefPanelController |
| 24 |
|
| 25 |
static AudioSettingsPrefPanelController *sharedAudioSettingsPrefPanelController; |
| 26 |
static NSString *sPrefKey = @"audioSettingsExportPref"; |
| 27 |
static NSString *sTypeKey = @"deviceType"; |
| 28 |
static NSString *sInternalKey = @"saveInternal"; |
| 29 |
static NSString *sApplicationKey = @"callApplication"; |
| 30 |
static NSString *sAppPathKey = @"applicationPath"; |
| 31 |
|
| 32 |
+ (void)openAudioSettingsPrefPanel |
| 33 |
{ |
| 34 |
if (sharedAudioSettingsPrefPanelController == nil) { |
| 35 |
sharedAudioSettingsPrefPanelController = [[AudioSettingsPrefPanelController alloc] initWithWindowNibName: @"AudioSettingsPrefPanel"]; |
| 36 |
} |
| 37 |
[[sharedAudioSettingsPrefPanelController window] makeKeyAndOrderFront: nil]; |
| 38 |
[sharedAudioSettingsPrefPanelController updateDisplay]; |
| 39 |
} |
| 40 |
|
| 41 |
+ (AudioSettingsPrefPanelController *)sharedAudioSettingsPrefPanelController |
| 42 |
{ |
| 43 |
if (sharedAudioSettingsPrefPanelController == nil) |
| 44 |
[AudioSettingsPrefPanelController openAudioSettingsPrefPanel]; |
| 45 |
return sharedAudioSettingsPrefPanelController; |
| 46 |
} |
| 47 |
|
| 48 |
+ (id)lookupDeviceName:(const char *)devname create:(BOOL)flag type:(int)type |
| 49 |
{ |
| 50 |
int i, j, n; |
| 51 |
id set; |
| 52 |
if (sharedAudioSettingsPrefPanelController != nil && sharedAudioSettingsPrefPanelController->settings != nil) { |
| 53 |
set = sharedAudioSettingsPrefPanelController->settings; |
| 54 |
} else { |
| 55 |
set = MyAppCallback_getObjectGlobalSettings(sPrefKey); |
| 56 |
flag = NO; |
| 57 |
} |
| 58 |
n = (int)[set count]; |
| 59 |
for (i = n - 1; i >= 0; i--) { |
| 60 |
NSDictionary *dic = [set objectAtIndex:i]; |
| 61 |
NSString *nstr = [dic valueForKey:@"name"]; |
| 62 |
if (nstr != nil && strcmp([nstr UTF8String], devname) == 0) |
| 63 |
break; |
| 64 |
} |
| 65 |
if (i < 0 && flag) { |
| 66 |
NSMutableDictionary *mdic = [NSMutableDictionary dictionary]; |
| 67 |
[mdic setValue:[NSString stringWithUTF8String:devname] forKey:@"name"]; |
| 68 |
[mdic setValue:[NSNumber numberWithInt:type] forKey:sTypeKey]; |
| 69 |
[mdic setValue:[NSNumber numberWithInt:0] forKey:sInternalKey]; |
| 70 |
// Look up where to insert |
| 71 |
for (j = 0; j < n; j++) { |
| 72 |
int type0 = [[[set objectAtIndex:j] objectForKey:@"name"] intValue]; |
| 73 |
if (type0 > type) |
| 74 |
break; |
| 75 |
} |
| 76 |
[set insertObject:mdic atIndex:j]; |
| 77 |
i = j; |
| 78 |
} |
| 79 |
if (i < 0) |
| 80 |
return nil; |
| 81 |
else return [set objectAtIndex: i]; |
| 82 |
} |
| 83 |
|
| 84 |
+ (BOOL)shouldSaveInternalForDeviceName:(const char *)name |
| 85 |
{ |
| 86 |
NSDictionary *dic = [AudioSettingsPrefPanelController lookupDeviceName:name create:NO type:0]; |
| 87 |
if (dic != nil) { |
| 88 |
return [[dic valueForKey:sInternalKey] intValue] != 0; |
| 89 |
} else return NO; |
| 90 |
} |
| 91 |
|
| 92 |
+ (NSString *)shouldCallApplicationForDeviceName:(const char *)name |
| 93 |
{ |
| 94 |
NSDictionary *dic = [AudioSettingsPrefPanelController lookupDeviceName:name create:NO type:0]; |
| 95 |
if (dic != nil) { |
| 96 |
if ([[dic valueForKey:sApplicationKey] intValue] != 0) { |
| 97 |
NSString *s = [dic valueForKey:sAppPathKey]; |
| 98 |
return s; |
| 99 |
} |
| 100 |
} |
| 101 |
return nil; |
| 102 |
} |
| 103 |
|
| 104 |
- (void)updateDisplay |
| 105 |
{ |
| 106 |
int i, n, type; |
| 107 |
MDAudioDeviceInfo *ap; |
| 108 |
MDAudioMusicDeviceInfo *mp; |
| 109 |
if (settings == nil) { |
| 110 |
// Rebuild the settings from the global preference |
| 111 |
NSArray *set = MyAppCallback_getObjectGlobalSettings(sPrefKey); |
| 112 |
settings = [[NSMutableArray array] retain]; |
| 113 |
if (set != nil) { |
| 114 |
n = (int)[set count]; |
| 115 |
for (i = 0; i < n; i++) { |
| 116 |
NSDictionary *dic = [set objectAtIndex:i]; |
| 117 |
[settings addObject:[NSMutableDictionary dictionaryWithDictionary:dic]]; |
| 118 |
} |
| 119 |
} |
| 120 |
// Add entries if the currently active devices are not listed in the settings |
| 121 |
for (i = 0; (ap = MDAudioDeviceInfoAtIndex(i, 0)) != NULL; i++) { |
| 122 |
[[self class] lookupDeviceName:ap->name create:YES type:0]; |
| 123 |
} |
| 124 |
for (i = 0; (ap = MDAudioDeviceInfoAtIndex(i, 1)) != NULL; i++) { |
| 125 |
[[self class] lookupDeviceName:ap->name create:YES type:1]; |
| 126 |
} |
| 127 |
for (i = 0; (mp = MDAudioMusicDeviceInfoAtIndex(i)) != NULL; i++) { |
| 128 |
[[self class] lookupDeviceName:mp->name create:YES type:2]; |
| 129 |
} |
| 130 |
for (i = 0; (mp = MDAudioEffectDeviceInfoAtIndex(i)) != NULL; i++) { |
| 131 |
[[self class] lookupDeviceName:mp->name create:YES type:3]; |
| 132 |
} |
| 133 |
|
| 134 |
n = (int)[settings count]; |
| 135 |
[devicePopUp removeAllItems]; |
| 136 |
type = 0; |
| 137 |
for (i = 0; i < n; i++) { |
| 138 |
int type0; |
| 139 |
NSDictionary *dic = [settings objectAtIndex:i]; |
| 140 |
type0 = [[dic objectForKey:sTypeKey] intValue]; |
| 141 |
if (type != type0) { |
| 142 |
type = type0; |
| 143 |
[[devicePopUp menu] addItem:[NSMenuItem separatorItem]]; |
| 144 |
} |
| 145 |
[devicePopUp addItemWithTitle:[dic valueForKey:@"name"]]; |
| 146 |
[[devicePopUp lastItem] setTag:i]; |
| 147 |
} |
| 148 |
} |
| 149 |
i = (int)[devicePopUp selectedTag]; |
| 150 |
if (i < 0) { |
| 151 |
[internalCheck setEnabled:NO]; |
| 152 |
[applicationCheck setEnabled:NO]; |
| 153 |
[selectButton setEnabled:NO]; |
| 154 |
[applicationPath setEnabled:NO]; |
| 155 |
} else { |
| 156 |
NSDictionary *dic = [settings objectAtIndex:i]; |
| 157 |
NSString *s; |
| 158 |
[internalCheck setEnabled:YES]; |
| 159 |
if ([[dic valueForKey:sInternalKey] intValue] == 0) |
| 160 |
[internalCheck setState:NSOffState]; |
| 161 |
else |
| 162 |
[internalCheck setState:NSOnState]; |
| 163 |
[applicationCheck setEnabled:YES]; |
| 164 |
s = [dic valueForKey:sAppPathKey]; |
| 165 |
[applicationPath setStringValue:(s == nil ? @"" : s)]; |
| 166 |
if ([[dic valueForKey:sApplicationKey] intValue] == 0) { |
| 167 |
[applicationCheck setState:NSOffState]; |
| 168 |
[selectButton setEnabled:NO]; |
| 169 |
[applicationPath setEnabled:NO]; |
| 170 |
} else { |
| 171 |
[applicationCheck setState:NSOnState]; |
| 172 |
[selectButton setEnabled:YES]; |
| 173 |
[applicationPath setEnabled:YES]; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
- (IBAction)devicePopUpSelected:(id)sender |
| 179 |
{ |
| 180 |
[self updateDisplay]; |
| 181 |
} |
| 182 |
|
| 183 |
- (IBAction)selectApplication:(id)sender |
| 184 |
{ |
| 185 |
NSOpenPanel *panel = [NSOpenPanel openPanel]; |
| 186 |
int result; |
| 187 |
int n = (int)[devicePopUp selectedTag]; |
| 188 |
NSMutableDictionary *dic; |
| 189 |
if (n < 0) |
| 190 |
return; |
| 191 |
[panel setAllowsMultipleSelection:NO]; |
| 192 |
[panel setCanChooseDirectories:NO]; |
| 193 |
[panel setCanCreateDirectories:YES]; |
| 194 |
[panel setCanChooseFiles:YES]; |
| 195 |
[panel setAllowedFileTypes:[NSArray arrayWithObjects:@"app", nil]]; |
| 196 |
result = (int)[panel runModal]; |
| 197 |
if (result == NSFileHandlingPanelOKButton) { |
| 198 |
NSURL *url = [panel URL]; |
| 199 |
dic = [settings objectAtIndex:n]; |
| 200 |
[dic setValue:[url path] forKey:sAppPathKey]; |
| 201 |
[self updateDisplay]; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
- (IBAction)checkBoxClicked:(id)sender |
| 206 |
{ |
| 207 |
int n = (int)[devicePopUp selectedTag]; |
| 208 |
int state = ([sender state] == NSOnState); |
| 209 |
NSMutableDictionary *dic; |
| 210 |
if (n < 0) |
| 211 |
return; |
| 212 |
dic = [settings objectAtIndex:n]; |
| 213 |
if (sender == internalCheck) { |
| 214 |
[dic setValue:[NSNumber numberWithInt:state] forKey:sInternalKey]; |
| 215 |
} else if (sender == applicationCheck) { |
| 216 |
[dic setValue:[NSNumber numberWithInt:state] forKey:sApplicationKey]; |
| 217 |
} |
| 218 |
[self updateDisplay]; |
| 219 |
} |
| 220 |
|
| 221 |
- (IBAction)cancelClicked:(id)sender |
| 222 |
{ |
| 223 |
[[self window] orderOut:self]; |
| 224 |
[settings release]; |
| 225 |
settings = nil; |
| 226 |
} |
| 227 |
|
| 228 |
- (IBAction)saveClicked:(id)sender |
| 229 |
{ |
| 230 |
MyAppCallback_setObjectGlobalSettings(sPrefKey, settings); |
| 231 |
MyAppCallback_saveGlobalSettings(); |
| 232 |
[[self window] orderOut:self]; |
| 233 |
[settings release]; |
| 234 |
settings = nil; |
| 235 |
} |
| 236 |
|
| 237 |
@end |