| 1 |
/* |
| 2 |
* Copyright (c) 2010, Wei Mingzhi <[email protected]>. |
| 3 |
* All Rights Reserved. |
| 4 |
* |
| 5 |
* Based on: Cdrom for Psemu Pro like Emulators |
| 6 |
* By: linuzappz <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 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 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program; if not, see <http://www.gnu.org/licenses>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#import "PluginConfigController.h" |
| 23 |
#include "typedefs.h" |
| 24 |
#include "sio1.h" |
| 25 |
|
| 26 |
#define APP_ID @"net.pcsxr.Bladesio1" |
| 27 |
#define PrefsKey APP_ID @" Settings" |
| 28 |
|
| 29 |
static PluginConfigController *windowController = nil; |
| 30 |
|
| 31 |
#define kSioEnabled @"SIO Enabled" |
| 32 |
#define kSioPort @"Port" |
| 33 |
#define kSioPlayer @"Player" |
| 34 |
#define kSioIPAddress @"IP address" |
| 35 |
|
| 36 |
void AboutDlgProc() |
| 37 |
{ |
| 38 |
// Get parent application instance |
| 39 |
NSBundle *bundle = [NSBundle bundleWithIdentifier:APP_ID]; |
| 40 |
|
| 41 |
// Get Credits.rtf |
| 42 |
NSString *path = [bundle pathForResource:@"Credits" ofType:@"rtf"]; |
| 43 |
NSAttributedString *credits; |
| 44 |
if (path) { |
| 45 |
credits = [[NSAttributedString alloc] initWithPath: path |
| 46 |
documentAttributes:NULL]; |
| 47 |
|
| 48 |
} else { |
| 49 |
credits = [[NSAttributedString alloc] initWithString:@""]; |
| 50 |
} |
| 51 |
|
| 52 |
// Get Application Icon |
| 53 |
NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile:[bundle bundlePath]]; |
| 54 |
NSSize size = NSMakeSize(64, 64); |
| 55 |
[icon setSize:size]; |
| 56 |
|
| 57 |
NSDictionary *infoPaneDict = |
| 58 |
@{@"ApplicationName": [bundle objectForInfoDictionaryKey:@"CFBundleName"], |
| 59 |
@"ApplicationIcon": icon, |
| 60 |
@"ApplicationVersion": [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"], |
| 61 |
@"Version": [bundle objectForInfoDictionaryKey:@"CFBundleVersion"], |
| 62 |
@"Copyright": [bundle objectForInfoDictionaryKey:@"NSHumanReadableCopyright"], |
| 63 |
@"Credits": credits}; |
| 64 |
dispatch_async(dispatch_get_main_queue(), ^{ |
| 65 |
[NSApp orderFrontStandardAboutPanelWithOptions:infoPaneDict]; |
| 66 |
}); |
| 67 |
} |
| 68 |
|
| 69 |
static inline void RunOnMainThreadSync(dispatch_block_t block) |
| 70 |
{ |
| 71 |
if ([NSThread isMainThread]) { |
| 72 |
block(); |
| 73 |
} else { |
| 74 |
dispatch_sync(dispatch_get_main_queue(), block); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
void ConfDlgProc() |
| 79 |
{ |
| 80 |
//We need this block due to the xib's use of auto layout |
| 81 |
RunOnMainThreadSync(^{ |
| 82 |
NSWindow *window; |
| 83 |
if (windowController == nil) { |
| 84 |
windowController = [[PluginConfigController alloc] initWithWindowNibName:@"Bladesio1PluginConfig"]; |
| 85 |
} |
| 86 |
window = [windowController window]; |
| 87 |
|
| 88 |
[windowController loadValues]; |
| 89 |
|
| 90 |
[window center]; |
| 91 |
[window makeKeyAndOrderFront:nil]; |
| 92 |
}); |
| 93 |
} |
| 94 |
|
| 95 |
void ReadConfig() |
| 96 |
{ |
| 97 |
NSDictionary *keyValues; |
| 98 |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
| 99 |
[defaults registerDefaults:@{PrefsKey: @{kSioEnabled: @NO, |
| 100 |
kSioPort: @33307, |
| 101 |
kSioIPAddress: @"127.0.0.1", |
| 102 |
kSioPlayer: @(PLAYER_DISABLED)}}]; |
| 103 |
|
| 104 |
keyValues = [defaults dictionaryForKey:PrefsKey]; |
| 105 |
|
| 106 |
settings.enabled = [keyValues[kSioEnabled] boolValue]; |
| 107 |
settings.port = [keyValues[kSioPort] unsignedShortValue]; |
| 108 |
settings.player = [keyValues[kSioPlayer] intValue]; |
| 109 |
strlcpy(settings.ip, [keyValues[kSioIPAddress] cStringUsingEncoding:NSASCIIStringEncoding], sizeof(settings.ip)); |
| 110 |
} |
| 111 |
|
| 112 |
@implementation Bladesio1PluginConfigController |
| 113 |
|
| 114 |
- (IBAction)cancel:(id)sender |
| 115 |
{ |
| 116 |
[self close]; |
| 117 |
} |
| 118 |
|
| 119 |
- (IBAction)ok:(id)sender |
| 120 |
{ |
| 121 |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
| 122 |
|
| 123 |
NSMutableDictionary *writeDic = [NSMutableDictionary dictionaryWithDictionary:keyValues]; |
| 124 |
|
| 125 |
NSString *theAddress = [ipAddressField stringValue]; |
| 126 |
NSInteger asciiLen = [theAddress lengthOfBytesUsingEncoding:NSASCIIStringEncoding]; |
| 127 |
if (asciiLen > (sizeof(settings.ip) - 1)) { |
| 128 |
NSBeginAlertSheet(@"Address too long", nil, nil, nil, [self window], nil, NULL, NULL, NULL, @"The address is too long.\n\nTry to use only the IP address and not a host name."); |
| 129 |
return; |
| 130 |
} else if (asciiLen == 0) { |
| 131 |
NSBeginAlertSheet(@"Blank address", nil, nil, nil, [self window], nil, NULL, NULL, NULL, @"The address specified is either blank, or can't be converted to ASCII.\n\nTry connecting directly using the IP address using latin numerals."); |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
writeDic[kSioEnabled] = (([enabledButton state] == NSOnState) ? @YES : @NO); |
| 136 |
writeDic[kSioIPAddress] = theAddress; |
| 137 |
writeDic[kSioPort] = @((u16)[portField intValue]); |
| 138 |
|
| 139 |
{ |
| 140 |
int player; |
| 141 |
switch ([playerMenu indexOfSelectedItem]) { |
| 142 |
default: |
| 143 |
case 0: player = PLAYER_DISABLED; break; |
| 144 |
case 1: player = PLAYER_MASTER; break; |
| 145 |
case 2: player = PLAYER_SLAVE; break; |
| 146 |
} |
| 147 |
writeDic[kSioPlayer] = @(player); |
| 148 |
} |
| 149 |
|
| 150 |
// write to defaults |
| 151 |
[defaults setObject:writeDic forKey:PrefsKey]; |
| 152 |
[defaults synchronize]; |
| 153 |
|
| 154 |
// and set global values accordingly |
| 155 |
ReadConfig(); |
| 156 |
|
| 157 |
[self close]; |
| 158 |
} |
| 159 |
|
| 160 |
- (IBAction)toggleEnabled:(id)sender |
| 161 |
{ |
| 162 |
BOOL isEnabled = [enabledButton state] == NSOnState ? YES : NO; |
| 163 |
|
| 164 |
for (NSView *subView in [[configBox subviews][0] subviews]) { |
| 165 |
if ([subView isKindOfClass:[NSTextField class]] && ![(NSTextField*)subView isEditable]) { |
| 166 |
[(NSTextField*)subView setTextColor:isEnabled ? [NSColor controlTextColor] : [NSColor disabledControlTextColor]]; |
| 167 |
} else { |
| 168 |
if ([subView respondsToSelector:@selector(setEnabled:)]) { |
| 169 |
[(NSControl*)subView setEnabled:isEnabled]; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
- (IBAction)resetPreferences:(id)sender |
| 176 |
{ |
| 177 |
[[NSUserDefaults standardUserDefaults] removeObjectForKey:PrefsKey]; |
| 178 |
[self loadValues]; |
| 179 |
} |
| 180 |
|
| 181 |
- (void)loadValues |
| 182 |
{ |
| 183 |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
| 184 |
|
| 185 |
ReadConfig(); |
| 186 |
|
| 187 |
// load from preferences |
| 188 |
keyValues = [[defaults dictionaryForKey:PrefsKey] mutableCopy]; |
| 189 |
|
| 190 |
[enabledButton setState: [keyValues[kSioEnabled] boolValue] ? NSOnState : NSOffState]; |
| 191 |
[ipAddressField setTitleWithMnemonic:keyValues[kSioIPAddress]]; |
| 192 |
[portField setIntValue:[keyValues[kSioPort] intValue]]; |
| 193 |
|
| 194 |
switch ([keyValues[kSioPlayer] integerValue]) { |
| 195 |
default: |
| 196 |
case PLAYER_DISABLED: [playerMenu selectItemAtIndex:0]; break; |
| 197 |
case PLAYER_MASTER: [playerMenu selectItemAtIndex:1]; break; |
| 198 |
case PLAYER_SLAVE: [playerMenu selectItemAtIndex:2]; break; |
| 199 |
} |
| 200 |
|
| 201 |
[self toggleEnabled:nil]; |
| 202 |
} |
| 203 |
|
| 204 |
- (void)awakeFromNib |
| 205 |
{ |
| 206 |
} |
| 207 |
|
| 208 |
@end |
| 209 |
|
| 210 |
#import "OSXPlugLocalization.h" |
| 211 |
PLUGLOCIMP([PluginConfigController class]); |