1//
2//  cfgHelper.m
3//  Pcsxr
4//
5//  Created by C.W. Betts on 8/28/13.
6//
7//
8
9#import <Foundation/Foundation.h>
10#import "PadController.h"
11#include "cfg.h"
12
13#define padType @"Pad Type"
14#define deviceNumber @"Device Number"
15#define dfKey @"Key Value"
16#define joyType @"Joystick Type"
17#define joyVal @"Joystick Value"
18
19#define dSelect @"DKEY_SELECT"
20#define dStart @"DKEY_START"
21#define dUp @"DKEY_UP"
22#define dRight @"DKEY_RIGHT"
23#define dDown @"DKEY_DOWN"
24#define dLeft @"DKEY_LEFT"
25#define dL1 @"DKEY_L1"
26#define dL2 @"DKEY_L2"
27#define dL3 @"DKEY_L3"
28#define dR1 @"DKEY_R1"
29#define dR2 @"DKEY_R2"
30#define dR3 @"DKEY_R3"
31#define dTriangle @"DKEY_TRIANGLE"
32#define dCircle @"DKEY_CIRCLE"
33#define dCross @"DKEY_CROSS"
34#define dSquare @"DKEY_SQUARE"
35#define dAnalog @"DKEY_ANALOG"
36#define dLeftAnalogXP @"LeftAnalogXP"
37#define dLeftAnalogXM @"LeftAnalogXM"
38#define dLeftAnalogYP @"LeftAnalogYP"
39#define dLeftAnalogYM @"LeftAnalogYM"
40#define dRightAnalogXP @"RightAnalogXP"
41#define dRightAnalogXM @"RightAnalogXM"
42#define dRightAnalogYP @"RightAnalogYP"
43#define dRightAnalogYM @"RightAnalogYM"
44
45#define VibrateOn @"Visual Vibration"
46#define UseSDL2Mapping @"SDL2 Mapping"
47
48NSDictionary *DefaultPadArray(int padnum)
49{
50	NSMutableDictionary *mutArray =
51	[NSMutableDictionary dictionaryWithDictionary:@{VibrateOn: @NO,
52									 deviceNumber: @(padnum),
53										  padType: @(PSE_PAD_TYPE_STANDARD),
54										  dSelect: [NSMutableDictionary dictionaryWithObjectsAndKeys:@8, joyVal, @(BUTTON), joyType, nil],
55										   dStart: [NSMutableDictionary dictionaryWithObjectsAndKeys:@9, joyVal, @(BUTTON), joyType, nil],
56											  dUp: [NSMutableDictionary dictionaryWithObjectsAndKeys:@(-2), joyVal, @(AXIS), joyType, nil],
57										   dRight: [NSMutableDictionary dictionaryWithObjectsAndKeys:@1, joyVal, @(AXIS), joyType, nil],
58											dDown: [NSMutableDictionary dictionaryWithObjectsAndKeys:@2, joyVal, @(AXIS), joyType, nil],
59											dLeft: [NSMutableDictionary dictionaryWithObjectsAndKeys:@(-1), joyVal, @(AXIS), joyType, nil],
60											  dL2: [NSMutableDictionary dictionaryWithObjectsAndKeys:@4, joyVal, @(BUTTON), joyType, nil],
61											  dL1: [NSMutableDictionary dictionaryWithObjectsAndKeys:@6, joyVal, @(BUTTON), joyType, nil],
62											  dR2: [NSMutableDictionary dictionaryWithObjectsAndKeys:@5, joyVal, @(BUTTON), joyType, nil],
63											  dR1: [NSMutableDictionary dictionaryWithObjectsAndKeys:@7, joyVal, @(BUTTON), joyType, nil],
64										dTriangle: [NSMutableDictionary dictionaryWithObjectsAndKeys:@0, joyVal, @(BUTTON), joyType, nil],
65										  dCircle: [NSMutableDictionary dictionaryWithObjectsAndKeys:@1, joyVal, @(BUTTON), joyType, nil],
66										   dCross: [NSMutableDictionary dictionaryWithObjectsAndKeys:@2, joyVal, @(BUTTON), joyType, nil],
67										  dSquare: [NSMutableDictionary dictionaryWithObjectsAndKeys:@3, joyVal, @(BUTTON), joyType, nil],
68													UseSDL2Mapping: @YES}];
69	if (padnum == 0) {
70		mutArray[dSelect][dfKey] = @9;
71		mutArray[dStart][dfKey] = @10;
72		mutArray[dUp][dfKey] = @127;
73		mutArray[dRight][dfKey] = @125;
74		mutArray[dDown][dfKey] = @126;
75		mutArray[dLeft][dfKey] = @124;
76		mutArray[dL2][dfKey] = @16;
77		mutArray[dR2][dfKey] = @18;
78		mutArray[dL1][dfKey] = @14;
79		mutArray[dR1][dfKey] = @15;
80		mutArray[dTriangle][dfKey] = @3;
81		mutArray[dCircle][dfKey] = @8;
82		mutArray[dCross][dfKey] = @7;
83		mutArray[dSquare][dfKey] = @2;
84		mutArray[dAnalog] = @{dfKey: @12};
85	}
86	return [NSDictionary dictionaryWithDictionary:mutArray];
87}
88
89static NSDictionary *DictionaryFromButtonDef(KEYDEF theKey)
90{
91	NSMutableDictionary *mutDict = [NSMutableDictionary dictionaryWithCapacity:3];
92	if (theKey.Key) {
93		mutDict[dfKey] = @(theKey.Key);
94	}
95	if (theKey.JoyEvType != NONE) {
96		mutDict[joyType] = @(theKey.JoyEvType);
97		switch (theKey.JoyEvType) {
98			case BUTTON:
99				mutDict[joyVal] = @(theKey.J.Button);
100				break;
101
102			case HAT:
103				mutDict[joyVal] = @(theKey.J.Hat);
104				break;
105
106			case AXIS:
107				mutDict[joyVal] = @(theKey.J.Axis);
108				break;
109
110			case NONE:
111			default:
112				//[mutDict setObject:@(theKey.J.d) forKey:joyVal];
113				[mutDict removeObjectForKey:joyType];
114				break;
115		}
116	}
117	return [NSDictionary dictionaryWithDictionary:mutDict];
118}
119
120static void SetKeyFromDictionary(NSDictionary *inDict, KEYDEF *outDef)
121{
122	assert(outDef != NULL);
123	if (!inDict) {
124		return;
125	}
126	NSNumber *theJoyType = inDict[joyType];
127	if (theJoyType) {
128		NSNumber *theJoyVal = inDict[joyVal];
129		outDef->JoyEvType = [theJoyType unsignedCharValue];
130		switch (outDef->JoyEvType) {
131			case BUTTON:
132				outDef->J.Button = [theJoyVal unsignedShortValue];
133				break;
134
135			case HAT:
136				outDef->J.Hat = [theJoyVal unsignedShortValue];
137				break;
138
139			case AXIS:
140				outDef->J.Axis = [theJoyVal shortValue];
141				break;
142
143			default:
144				break;
145		}
146	}
147	NSNumber *keyVal = inDict[dfKey];
148	if (keyVal) {
149		outDef->Key = [keyVal unsignedShortValue];
150	}
151}
152
153void LoadPadArray(int padnum, NSDictionary *nsPrefs)
154{
155	PADDEF *curDef = &g.cfg.PadDef[padnum];
156	curDef->DevNum = [nsPrefs[deviceNumber] charValue];
157	curDef->Type = [nsPrefs[padType] unsignedShortValue];
158	curDef->VisualVibration = [nsPrefs[VibrateOn] boolValue]; //Not implemented on OS X right now.
159	curDef->UseSDL2 = [nsPrefs[UseSDL2Mapping] boolValue];
160
161	//Analog buttons
162	SetKeyFromDictionary(nsPrefs[dL3], &curDef->KeyDef[DKEY_L3]);
163	SetKeyFromDictionary(nsPrefs[dR3], &curDef->KeyDef[DKEY_R3]);
164	SetKeyFromDictionary(nsPrefs[dAnalog], &curDef->KeyDef[DKEY_ANALOG]);
165
166	//Analog sticks
167	SetKeyFromDictionary(nsPrefs[dLeftAnalogXP], &curDef->AnalogDef[ANALOG_LEFT][ANALOG_XP]);
168	SetKeyFromDictionary(nsPrefs[dLeftAnalogXM], &curDef->AnalogDef[ANALOG_LEFT][ANALOG_XM]);
169	SetKeyFromDictionary(nsPrefs[dLeftAnalogYP], &curDef->AnalogDef[ANALOG_LEFT][ANALOG_YP]);
170	SetKeyFromDictionary(nsPrefs[dLeftAnalogYM], &curDef->AnalogDef[ANALOG_LEFT][ANALOG_YM]);
171
172	SetKeyFromDictionary(nsPrefs[dRightAnalogXP], &curDef->AnalogDef[ANALOG_RIGHT][ANALOG_XP]);
173	SetKeyFromDictionary(nsPrefs[dRightAnalogXM], &curDef->AnalogDef[ANALOG_RIGHT][ANALOG_XM]);
174	SetKeyFromDictionary(nsPrefs[dRightAnalogYP], &curDef->AnalogDef[ANALOG_RIGHT][ANALOG_YP]);
175	SetKeyFromDictionary(nsPrefs[dRightAnalogYM], &curDef->AnalogDef[ANALOG_RIGHT][ANALOG_YM]);
176
177	//Digital shouldurs
178	SetKeyFromDictionary(nsPrefs[dL1], &curDef->KeyDef[DKEY_L1]);
179	SetKeyFromDictionary(nsPrefs[dL2], &curDef->KeyDef[DKEY_L2]);
180	SetKeyFromDictionary(nsPrefs[dR1], &curDef->KeyDef[DKEY_R1]);
181	SetKeyFromDictionary(nsPrefs[dR2], &curDef->KeyDef[DKEY_R2]);
182
183	//Digital buttons
184	SetKeyFromDictionary(nsPrefs[dSelect], &curDef->KeyDef[DKEY_SELECT]);
185	SetKeyFromDictionary(nsPrefs[dStart], &curDef->KeyDef[DKEY_START]);
186	SetKeyFromDictionary(nsPrefs[dUp], &curDef->KeyDef[DKEY_UP]);
187	SetKeyFromDictionary(nsPrefs[dRight], &curDef->KeyDef[DKEY_RIGHT]);
188	SetKeyFromDictionary(nsPrefs[dDown], &curDef->KeyDef[DKEY_DOWN]);
189	SetKeyFromDictionary(nsPrefs[dLeft], &curDef->KeyDef[DKEY_LEFT]);
190	SetKeyFromDictionary(nsPrefs[dTriangle], &curDef->KeyDef[DKEY_TRIANGLE]);
191	SetKeyFromDictionary(nsPrefs[dCircle], &curDef->KeyDef[DKEY_CIRCLE]);
192	SetKeyFromDictionary(nsPrefs[dCross], &curDef->KeyDef[DKEY_CROSS]);
193	SetKeyFromDictionary(nsPrefs[dSquare], &curDef->KeyDef[DKEY_SQUARE]);
194}
195
196NSDictionary *SavePadArray(int padnum)
197{
198	NSMutableDictionary *mutArray = [[NSMutableDictionary alloc] init];
199	PADDEF *curDef = &g.cfg.PadDef[padnum];
200	mutArray[deviceNumber] = @(curDef->DevNum);
201	mutArray[padType] = @(curDef->Type);
202	mutArray[VibrateOn] = curDef->VisualVibration ? @YES : @NO;
203
204	switch (curDef->Type) {
205		case PSE_PAD_TYPE_ANALOGPAD:
206			mutArray[dL3] = DictionaryFromButtonDef(curDef->KeyDef[DKEY_L3]);
207			mutArray[dR3] = DictionaryFromButtonDef(curDef->KeyDef[DKEY_R3]);
208			mutArray[dAnalog] = DictionaryFromButtonDef(curDef->KeyDef[DKEY_ANALOG]);
209
210			mutArray[dLeftAnalogXP] = DictionaryFromButtonDef(curDef->AnalogDef[ANALOG_LEFT][ANALOG_XP]);
211			mutArray[dLeftAnalogXM] = DictionaryFromButtonDef(curDef->AnalogDef[ANALOG_LEFT][ANALOG_XM]);
212			mutArray[dLeftAnalogYP] = DictionaryFromButtonDef(curDef->AnalogDef[ANALOG_LEFT][ANALOG_YP]);
213			mutArray[dLeftAnalogYM] = DictionaryFromButtonDef(curDef->AnalogDef[ANALOG_LEFT][ANALOG_YM]);
214
215			mutArray[dRightAnalogXP] = DictionaryFromButtonDef(curDef->AnalogDef[ANALOG_RIGHT][ANALOG_XP]);
216			mutArray[dRightAnalogXM] = DictionaryFromButtonDef(curDef->AnalogDef[ANALOG_RIGHT][ANALOG_XM]);
217			mutArray[dRightAnalogYP] = DictionaryFromButtonDef(curDef->AnalogDef[ANALOG_RIGHT][ANALOG_YP]);
218			mutArray[dRightAnalogYM] = DictionaryFromButtonDef(curDef->AnalogDef[ANALOG_RIGHT][ANALOG_YM]);
219			//Fall through
220
221		case PSE_PAD_TYPE_STANDARD:
222			mutArray[dL1] = DictionaryFromButtonDef(curDef->KeyDef[DKEY_L1]);
223			mutArray[dL2] = DictionaryFromButtonDef(curDef->KeyDef[DKEY_L2]);
224			mutArray[dR1] = DictionaryFromButtonDef(curDef->KeyDef[DKEY_R1]);
225			mutArray[dR2] = DictionaryFromButtonDef(curDef->KeyDef[DKEY_R2]);
226
227			mutArray[dSelect] = DictionaryFromButtonDef(curDef->KeyDef[DKEY_SELECT]);
228			mutArray[dStart] = DictionaryFromButtonDef(curDef->KeyDef[DKEY_START]);
229			mutArray[dUp] = DictionaryFromButtonDef(curDef->KeyDef[DKEY_UP]);
230			mutArray[dRight] = DictionaryFromButtonDef(curDef->KeyDef[DKEY_RIGHT]);
231			mutArray[dDown] = DictionaryFromButtonDef(curDef->KeyDef[DKEY_DOWN]);
232			mutArray[dLeft] = DictionaryFromButtonDef(curDef->KeyDef[DKEY_LEFT]);
233			mutArray[dTriangle] = DictionaryFromButtonDef(curDef->KeyDef[DKEY_TRIANGLE]);
234			mutArray[dCircle] = DictionaryFromButtonDef(curDef->KeyDef[DKEY_CIRCLE]);
235			mutArray[dCross] = DictionaryFromButtonDef(curDef->KeyDef[DKEY_CROSS]);
236			mutArray[dSquare] = DictionaryFromButtonDef(curDef->KeyDef[DKEY_SQUARE]);
237			mutArray[UseSDL2Mapping] = @((BOOL)curDef->UseSDL2);
238			break;
239
240		default:
241			break;
242	}
243
244	return [NSDictionary dictionaryWithDictionary:mutArray];
245}
246