pcsxr-1.9.92
[pcsx_rearmed.git] / macosx / plugins / DFInput / macsrc / ControllerList.m
1 /*
2  * Copyright (c) 2010, Wei Mingzhi <whistler@openoffice.org>.
3  * All Rights Reserved.
4  *
5  * Based on HIDInput by Gil Pedersen.
6  * Copyright (c) 2004, Gil Pedersen.
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 "ControllerList.h"
23 #include "pad.h"
24 #include "cfg.h"
25
26 static int currentController;
27
28 @implementation ControllerList
29
30 - (id)initWithConfig
31 {
32         if (!(self = [super init])) return nil;
33         return self;
34 }
35
36 - (void)dealloc
37 {
38         [super dealloc];
39 }
40
41 /* sets current controller data returned by data source */
42 + (void)setCurrentController:(int)which
43 {
44         currentController = which;
45 }
46
47 + (int)currentController
48 {
49         return currentController;
50 }
51
52 /* NSDataSource */
53 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
54 {
55         return DKEY_TOTAL + (g.cfg.PadDef[currentController].Type == PSE_PAD_TYPE_ANALOGPAD ? 8 : -2);
56 }
57
58 static const NSString *LabelText[DKEY_TOTAL + 8] = {
59         @"D-Pad Up",
60         @"D-Pad Down",
61         @"D-Pad Left",
62         @"D-Pad Right",
63         @"Cross",
64         @"Circle",
65         @"Square",
66         @"Triangle",
67         @"L1",
68         @"R1",
69         @"L2",
70         @"R2",
71         @"Select",
72         @"Start",
73         @"L3",
74         @"R3",
75         @"L-Stick Right",
76         @"L-Stick Left",
77         @"L-Stick Down",
78         @"L-Stick Up",
79         @"R-Stick Right",
80         @"R-Stick Left",
81         @"R-Stick Down",
82         @"R-Stick Up"
83 };
84
85 static const int DPad[DKEY_TOTAL] = {
86         DKEY_UP,
87         DKEY_DOWN,
88         DKEY_LEFT,
89         DKEY_RIGHT,
90         DKEY_CROSS,
91         DKEY_CIRCLE,
92         DKEY_SQUARE,
93         DKEY_TRIANGLE,
94         DKEY_L1,
95         DKEY_R1,
96         DKEY_L2,
97         DKEY_R2,
98         DKEY_SELECT,
99         DKEY_START,
100         DKEY_L3,
101         DKEY_R3
102 };
103
104 + (int)getButtonOfRow:(int)row
105 {
106         return DPad[row];
107 }
108
109 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn 
110                 row:(int)rowIndex
111 {
112         char buf[256];
113
114         if ([((NSString *)[aTableColumn identifier]) isEqualToString:@"key"])
115                 return LabelText[rowIndex];
116         else {
117                 // actual keys
118                 if (rowIndex < DKEY_TOTAL) {
119                         GetKeyDescription(buf, currentController, DPad[rowIndex]);
120                 } else {
121                         rowIndex -= DKEY_TOTAL;
122                         GetAnalogDescription(buf, currentController, rowIndex / 4, rowIndex % 4);
123                 }
124
125                 return [NSString stringWithUTF8String:buf];
126         }
127 }
128
129 - (void)deleteRow:(int)which
130 {
131         if (which < DKEY_TOTAL) {
132                 g.cfg.PadDef[currentController].KeyDef[DPad[which]].Key = 0;
133                 g.cfg.PadDef[currentController].KeyDef[DPad[which]].JoyEvType = NONE;
134                 g.cfg.PadDef[currentController].KeyDef[DPad[which]].J.d = 0;
135         } else {
136                 which -= DKEY_TOTAL;
137                 g.cfg.PadDef[currentController].AnalogDef[which / 4][which % 4].Key = 0;
138                 g.cfg.PadDef[currentController].AnalogDef[which / 4][which % 4].JoyEvType = NONE;
139                 g.cfg.PadDef[currentController].AnalogDef[which / 4][which % 4].J.d = 0;
140         }
141 }
142
143 @end