-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
ini.cpp
217 lines (191 loc) · 6.41 KB
/
ini.cpp
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
214
215
216
217
/* OnePAD - author: arcum42(@gmail.com)
* Copyright (C) 2009
*
* Based on ZeroPAD, author zerofrog@gmail.com
* Copyright (C) 2006-2007
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <string.h>
#include "GamePad.h"
#include "keyboard.h"
#include "onepad.h"
#include "linux.h"
extern std::string s_strIniPath;
std::string KeyName(int pad, int key, int keysym)
{
std::string tmp;
tmp.resize(28);
if (keysym) {
if (keysym < 10) {
// mouse
switch (keysym) {
case 1:
sprintf(&tmp[0], "Mouse Left");
break;
case 2:
sprintf(&tmp[0], "Mouse Middle");
break;
case 3:
sprintf(&tmp[0], "Mouse Right");
break;
default: // Use only number for extra button
sprintf(&tmp[0], "Mouse %d", keysym);
}
} else {
// keyboard
char *pstr = XKeysymToString(keysym);
if (pstr != NULL)
tmp = pstr;
}
} else {
// joystick
KeyType k = type_of_joykey(pad, key);
switch (k) {
case PAD_JOYBUTTONS: {
int button = key_to_button(pad, key);
sprintf(&tmp[0], "JBut %d", button);
break;
}
case PAD_AXIS: {
if (key_to_axis_type(pad, key))
sprintf(&tmp[0], "JAxis %d Full", key_to_axis(pad, key));
else
sprintf(&tmp[0], "JAxis %d Half%s", key_to_axis(pad, key), key_to_axis_sign(pad, key) ? "-" : "+");
break;
}
case PAD_HAT: {
int axis = key_to_axis(pad, key);
switch (key_to_hat_dir(pad, key)) {
case HAT_UP:
sprintf(&tmp[0], "JPOVU-%d", axis);
break;
case HAT_RIGHT:
sprintf(&tmp[0], "JPOVR-%d", axis);
break;
case HAT_DOWN:
sprintf(&tmp[0], "JPOVD-%d", axis);
break;
case HAT_LEFT:
sprintf(&tmp[0], "JPOVL-%d", axis);
break;
}
break;
}
default:
break;
}
}
return tmp;
}
void DefaultKeyboardValues()
{
set_keyboad_key(0, XK_a, PAD_L2);
set_keyboad_key(0, XK_semicolon, PAD_R2);
set_keyboad_key(0, XK_w, PAD_L1);
set_keyboad_key(0, XK_p, PAD_R1);
set_keyboad_key(0, XK_i, PAD_TRIANGLE);
set_keyboad_key(0, XK_l, PAD_CIRCLE);
set_keyboad_key(0, XK_k, PAD_CROSS);
set_keyboad_key(0, XK_j, PAD_SQUARE);
set_keyboad_key(0, XK_v, PAD_SELECT);
set_keyboad_key(0, XK_n, PAD_START);
set_keyboad_key(0, XK_e, PAD_UP);
set_keyboad_key(0, XK_f, PAD_RIGHT);
set_keyboad_key(0, XK_d, PAD_DOWN);
set_keyboad_key(0, XK_s, PAD_LEFT);
}
void SaveConfig()
{
FILE *f;
const std::string iniFile(s_strIniPath + "OnePAD.ini");
f = fopen(iniFile.c_str(), "w");
if (f == NULL) {
printf("OnePAD: failed to save ini %s\n", iniFile.c_str());
return;
}
fprintf(f, "log = %d\n", conf->log);
fprintf(f, "options = %d\n", conf->packed_options);
fprintf(f, "mouse_sensibility = %d\n", conf->get_sensibility());
fprintf(f, "joy_pad_map = %d\n", conf->joyid_map);
fprintf(f, "ff_intensity = %d\n", conf->get_ff_intensity());
for (int pad = 0; pad < GAMEPAD_NUMBER; pad++) {
for (int key = 0; key < MAX_KEYS; key++) {
fprintf(f, "[%d][%d] = 0x%x\n", pad, key, get_key(pad, key));
}
}
std::map<u32, u32>::iterator it;
for (int pad = 0; pad < GAMEPAD_NUMBER; pad++)
for (it = conf->keysym_map[pad].begin(); it != conf->keysym_map[pad].end(); ++it)
fprintf(f, "PAD %d:KEYSYM 0x%x = %d\n", pad, it->first, it->second);
fclose(f);
}
void LoadConfig()
{
FILE *f;
char str[256];
bool have_user_setting = false;
if (!conf)
conf = new PADconf;
conf->init();
const std::string iniFile(s_strIniPath + "OnePAD.ini");
f = fopen(iniFile.c_str(), "r");
if (f == NULL) {
printf("OnePAD: failed to load ini %s\n", iniFile.c_str());
SaveConfig(); //save and return
return;
}
u32 value;
if (fscanf(f, "log = %u\n", &value) == 0)
goto error;
conf->log = value;
if (fscanf(f, "options = %u\n", &value) == 0)
goto error;
conf->packed_options = value;
if (fscanf(f, "mouse_sensibility = %u\n", &value) == 0)
goto error;
conf->set_sensibility(value);
if (fscanf(f, "joy_pad_map = %u\n", &value) == 0)
goto error;
// Value is now hardcoded in controller.h avoid to reload a bad
// value from an old ini file.
//conf->joyid_map = value;
if (fscanf(f, "ff_intensity = %u\n", &value) == 0)
goto error;
conf->set_ff_intensity(value);
for (int pad = 0; pad < GAMEPAD_NUMBER; pad++) {
for (int key = 0; key < MAX_KEYS; key++) {
sprintf(str, "[%d][%d] = 0x%%x\n", pad, key);
u32 temp = 0;
if (fscanf(f, str, &temp) == 0)
temp = 0;
set_key(pad, key, temp);
if (temp && pad == 0)
have_user_setting = true;
}
}
u32 pad;
u32 keysym;
u32 index;
while (fscanf(f, "PAD %u:KEYSYM 0x%x = %u\n", &pad, &keysym, &index) != EOF) {
set_keyboad_key(pad & 1, keysym, index);
if (pad == 0)
have_user_setting = true;
}
if (!have_user_setting)
DefaultKeyboardValues();
error:
fclose(f);
}