summaryrefslogtreecommitdiff
path: root/macosx/plugins/Bladesio1/macsrc/PluginConfigController.m
blob: 609976eff4700e0b1aab49ab9096b0af5aafb26c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * Copyright (c) 2010, Wei Mingzhi <whistler@openoffice.org>.
 * All Rights Reserved.
 *
 * Based on: Cdrom for Psemu Pro like Emulators
 * By: linuzappz <linuzappz@hotmail.com>
 *
 * 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, see <http://www.gnu.org/licenses>.
 */

#import "PluginConfigController.h"
#include "typedefs.h"
#include "sio1.h"
#import "ARCBridge.h"

#define APP_ID @"net.pcsxr.Bladesio1"
#define PrefsKey APP_ID @" Settings"

static PluginConfigController *windowController = nil;

#define kSioEnabled @"SIO Enabled"
#define kSioPort @"Port"
#define kSioPlayer @"Player"
#define kSioIPAddress @"IP address"

void AboutDlgProc()
{
	// Get parent application instance
	NSApplication *app = [NSApplication sharedApplication];
	NSBundle *bundle = [NSBundle bundleWithIdentifier:APP_ID];

	// Get Credits.rtf
	NSString *path = [bundle pathForResource:@"Credits" ofType:@"rtf"];
	NSAttributedString *credits;
	if (path) {
		credits = [[NSAttributedString alloc] initWithPath: path
				documentAttributes:NULL];
		AUTORELEASEOBJNORETURN(credits);
		
	} else {
		credits = AUTORELEASEOBJ([[NSAttributedString alloc] initWithString:@""]);
	}

	// Get Application Icon
	NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile:[bundle bundlePath]];
	NSSize size = NSMakeSize(64, 64);
	[icon setSize:size];

	NSDictionary *infoPaneDict =
	[[NSDictionary alloc] initWithObjectsAndKeys:
	 [bundle objectForInfoDictionaryKey:@"CFBundleName"], @"ApplicationName",
	 icon, @"ApplicationIcon",
	 [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"], @"ApplicationVersion",
	 [bundle objectForInfoDictionaryKey:@"CFBundleVersion"], @"Version",
	 [bundle objectForInfoDictionaryKey:@"NSHumanReadableCopyright"], @"Copyright",
	 credits, @"Credits",
	 nil];
	dispatch_async(dispatch_get_main_queue(), ^{
		[app orderFrontStandardAboutPanelWithOptions:infoPaneDict];
	});
	RELEASEOBJ(infoPaneDict);
}

void ConfDlgProc()
{
	NSWindow *window;

	if (windowController == nil) {
		windowController = [[PluginConfigController alloc] initWithWindowNibName:@"Bladesio1PluginConfig"];
	}
	window = [windowController window];

	[windowController loadValues];

	[window center];
	[window makeKeyAndOrderFront:nil];
}

void ReadConfig()
{
	NSDictionary *keyValues;
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
								[NSDictionary dictionaryWithObjectsAndKeys:
								 @NO, kSioEnabled,
								 @((u16)33307), kSioPort,
								 @"127.0.0.1", kSioIPAddress,
								 @(PLAYER_DISABLED), kSioPlayer,
								 nil], PrefsKey, nil]];

	keyValues = [defaults dictionaryForKey:PrefsKey];

	settings.enabled = [[keyValues objectForKey:kSioEnabled] boolValue];
	settings.port = [[keyValues objectForKey:kSioPort] unsignedShortValue];
	settings.player = [[keyValues objectForKey:kSioPlayer] intValue];
	strlcpy(settings.ip, [[keyValues objectForKey:kSioIPAddress] cStringUsingEncoding:NSASCIIStringEncoding], sizeof(settings.ip));
}

@implementation Bladesio1PluginConfigController

- (IBAction)cancel:(id)sender
{
	[self close];
}

- (IBAction)ok:(id)sender
{
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

	NSMutableDictionary *writeDic = [NSMutableDictionary dictionaryWithDictionary:keyValues];

	NSString *theAddress = [ipAddressField stringValue];
	if ([theAddress lengthOfBytesUsingEncoding:NSASCIIStringEncoding] > (sizeof(settings.ip) - 1)) {
		NSBeginAlertSheet(@"Address too long", nil, nil, nil, [self window], nil, NULL, NULL, NULL, @"The address is too long. Try to use only the IP address and not a host name.");
		return;
	}
	
	[writeDic setObject:(([enabledButton state]  == NSOnState) ? @YES : @NO) forKey:kSioEnabled];
	[writeDic setObject:theAddress forKey:kSioIPAddress];
	[writeDic setObject:@((unsigned short)[portField intValue]) forKey:kSioPort];
	
	{
		int player;
		switch ([playerMenu indexOfSelectedItem]) {
			default:
			case 0: player = PLAYER_DISABLED; break;
			case 1: player = PLAYER_MASTER; break;
			case 2: player = PLAYER_SLAVE; break;
		}
		[writeDic setObject:@(player) forKey:kSioPlayer];
	}

	// write to defaults
	[defaults setObject:writeDic forKey:PrefsKey];
	[defaults synchronize];

	// and set global values accordingly
	ReadConfig();

	[self close];
}

- (IBAction)toggleEnabled:(id)sender
{
	BOOL isEnabled = [enabledButton state] == NSOnState ? YES : NO;
	
	for (NSView *subView in [[[configBox subviews] objectAtIndex:0] subviews]) {
		if ([subView isKindOfClass:[NSTextField class]] && ![(NSTextField*)subView isEditable]) {
				[(NSTextField*)subView setTextColor:isEnabled ? [NSColor controlTextColor] : [NSColor disabledControlTextColor]];
		} else {
			if ([subView respondsToSelector:@selector(setEnabled:)]) {
				[(NSControl*)subView setEnabled:isEnabled];
			}
		}
	}
}

- (IBAction)resetPreferences:(id)sender
{
	[[NSUserDefaults standardUserDefaults] removeObjectForKey:PrefsKey];
	[self loadValues];
}

- (void)loadValues
{
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

	ReadConfig();

	// load from preferences
	RELEASEOBJ(keyValues);
	keyValues = [[defaults dictionaryForKey:PrefsKey] mutableCopy];

	[enabledButton setState: [[keyValues objectForKey:kSioEnabled] boolValue] ? NSOnState : NSOffState];
	[ipAddressField setTitleWithMnemonic:[keyValues objectForKey:kSioIPAddress]];
	[portField setIntValue:[[keyValues objectForKey:kSioPort] intValue]];
	
	switch ([[keyValues objectForKey:kSioPlayer] integerValue]) {
		default:
		case PLAYER_DISABLED: [playerMenu selectItemAtIndex:0]; break;
		case PLAYER_MASTER: [playerMenu selectItemAtIndex:1]; break;
		case PLAYER_SLAVE: [playerMenu selectItemAtIndex:2]; break;
	}
	
	[self toggleEnabled:nil];
}

- (void)awakeFromNib
{
}

@end

char* PLUGLOC(char *toloc)
{
	NSBundle *mainBundle = [NSBundle bundleForClass:[PluginConfigController class]];
	NSString *origString = nil, *transString = nil;
	origString = @(toloc);
	transString = [mainBundle localizedStringForKey:origString value:nil table:nil];
	return (char*)[transString UTF8String];
}