1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus-input-sdl - config.c *
3 * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4 * Copyright (C) 2009-2013 Richard Goedeken *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24 #define M64P_PLUGIN_PROTOTYPES 1
25 #include "m64p_types.h"
26 #include "m64p_plugin.h"
27 #include "m64p_config.h"
29 #include "osal_preproc.h"
30 #include "autoconfig.h"
35 #define HAT_POS_NAME( hat ) \
36 ((hat == SDL_HAT_UP) ? "Up" : \
37 ((hat == SDL_HAT_DOWN) ? "Down" : \
38 ((hat == SDL_HAT_LEFT) ? "Left" : \
39 ((hat == SDL_HAT_RIGHT) ? "Right" : \
48 static const char *button_names[] = {
53 "Start", // START_BUTTON
55 "B Button", // B_BUTTON
56 "A Button", // A_BUTTON
57 "C Button R", // R_CBUTTON
58 "C Button L", // L_CBUTTON
59 "C Button D", // D_CBUTTON
60 "C Button U", // U_CBUTTON
65 "C Button2 R", // R_CBUTTON alternate
66 "C Button2 L", // L_CBUTTON alternate
67 "C Button2 D", // D_CBUTTON alternate
68 "C Button2 U", // U_CBUTTON alternate
73 /* static functions */
74 static int get_hat_pos_by_name( const char *name )
76 if( !strcasecmp( name, "up" ) )
78 if( !strcasecmp( name, "down" ) )
80 if( !strcasecmp( name, "left" ) )
82 if( !strcasecmp( name, "right" ) )
84 DebugMessage(M64MSG_WARNING, "get_hat_pos_by_name(): direction '%s' unknown", name);
88 static void clear_controller(int iCtrlIdx)
92 controller[iCtrlIdx].device = DEVICE_NO_JOYSTICK;
93 controller[iCtrlIdx].control->Present = 0;
94 controller[iCtrlIdx].control->RawData = 0;
95 controller[iCtrlIdx].control->Plugin = PLUGIN_NONE;
96 for( b = 0; b < NUM_BUTTONS; b++ )
98 controller[iCtrlIdx].button[b].button = -1;
99 controller[iCtrlIdx].button[b].key = SDL_SCANCODE_UNKNOWN;
100 controller[iCtrlIdx].button[b].axis = -1;
101 controller[iCtrlIdx].button[b].axis_deadzone = -1;
102 controller[iCtrlIdx].button[b].hat = -1;
103 controller[iCtrlIdx].button[b].hat_pos = -1;
104 controller[iCtrlIdx].button[b].mouse = -1;
105 controller[iCtrlIdx].button[b].mouse_up = 0;
106 controller[iCtrlIdx].button[b].mouse_down = 0;
107 controller[iCtrlIdx].button[b].mouse_left = 0;
108 controller[iCtrlIdx].button[b].mouse_right = 0;
110 for( b = 0; b < 2; b++ )
112 controller[iCtrlIdx].mouse_sens[b] = 2.0;
113 controller[iCtrlIdx].axis_deadzone[b] = 4096;
114 controller[iCtrlIdx].axis_peak[b] = 32768;
115 controller[iCtrlIdx].axis[b].button_a = controller[iCtrlIdx].axis[b].button_b = -1;
116 controller[iCtrlIdx].axis[b].key_a = controller[iCtrlIdx].axis[b].key_b = SDL_SCANCODE_UNKNOWN;
117 controller[iCtrlIdx].axis[b].axis_a = -1;
118 controller[iCtrlIdx].axis[b].axis_dir_a = 1;
119 controller[iCtrlIdx].axis[b].axis_b = -1;
120 controller[iCtrlIdx].axis[b].axis_dir_b = 1;
121 controller[iCtrlIdx].axis[b].hat = -1;
122 controller[iCtrlIdx].axis[b].hat_pos_a = -1;
123 controller[iCtrlIdx].axis[b].hat_pos_b = -1;
127 static const char * get_sdl_joystick_name(int iCtrlIdx)
129 static char JoyName[256];
130 const char *joySDLName;
131 int joyWasInit = SDL_WasInit(SDL_INIT_JOYSTICK);
133 /* initialize the joystick subsystem if necessary */
135 if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1)
137 DebugMessage(M64MSG_ERROR, "Couldn't init SDL joystick subsystem: %s", SDL_GetError() );
141 /* get the name of the corresponding joystick */
142 joySDLName = SDL_JoystickName(iCtrlIdx);
144 /* copy the name to our local string */
145 if (joySDLName != NULL)
147 strncpy(JoyName, joySDLName, 255);
151 /* quit the joystick subsystem if necessary */
153 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
155 /* if the SDL function had an error, then return NULL, otherwise return local copy of joystick name */
156 if (joySDLName == NULL)
162 static int get_sdl_num_joysticks(void)
164 int numJoysticks = 0;
165 int joyWasInit = SDL_WasInit(SDL_INIT_JOYSTICK);
167 /* initialize the joystick subsystem if necessary */
169 if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1)
171 DebugMessage(M64MSG_ERROR, "Couldn't init SDL joystick subsystem: %s", SDL_GetError() );
175 /* get thenumber of joysticks */
176 numJoysticks = SDL_NumJoysticks();
178 /* quit the joystick subsystem if necessary */
180 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
185 /////////////////////////////////////
186 // load_controller_config()
187 // return value: 1 = OK
188 // 0 = fail: couldn't open config section
190 static int load_controller_config(const char *SectionName, int i, int sdlDeviceIdx)
193 char input_str[256], value1_str[NUM_BUTTONS], value2_str[NUM_BUTTONS];
194 const char *config_ptr;
197 /* Open the configuration section for this controller */
198 if (ConfigOpenSection(SectionName, &pConfig) != M64ERR_SUCCESS)
200 DebugMessage(M64MSG_ERROR, "Couldn't open config section '%s'", SectionName);
204 /* set SDL device number */
205 controller[i].device = sdlDeviceIdx;
207 /* throw warnings if 'plugged' or 'plugin' are missing */
208 if (ConfigGetParameter(pConfig, "plugged", M64TYPE_BOOL, &controller[i].control->Present, sizeof(int)) != M64ERR_SUCCESS)
210 DebugMessage(M64MSG_WARNING, "missing 'plugged' parameter from config section %s. Setting to 1 (true).", SectionName);
211 controller[i].control->Present = 1;
213 if (ConfigGetParameter(pConfig, "plugin", M64TYPE_INT, &controller[i].control->Plugin, sizeof(int)) != M64ERR_SUCCESS)
215 DebugMessage(M64MSG_WARNING, "missing 'plugin' parameter from config section %s. Setting to 1 (none).", SectionName);
216 controller[i].control->Plugin = PLUGIN_NONE;
218 /* load optional parameters */
219 ConfigGetParameter(pConfig, "mouse", M64TYPE_BOOL, &controller[i].mouse, sizeof(int));
220 if (ConfigGetParameter(pConfig, "MouseSensitivity", M64TYPE_STRING, input_str, 256) == M64ERR_SUCCESS)
222 if (sscanf(input_str, "%f,%f", &controller[i].mouse_sens[0], &controller[i].mouse_sens[1]) != 2)
223 DebugMessage(M64MSG_WARNING, "parsing error in MouseSensitivity parameter for controller %i", i + 1);
225 if (ConfigGetParameter(pConfig, "AnalogDeadzone", M64TYPE_STRING, input_str, 256) == M64ERR_SUCCESS)
227 if (sscanf(input_str, "%i,%i", &controller[i].axis_deadzone[0], &controller[i].axis_deadzone[1]) != 2)
228 DebugMessage(M64MSG_WARNING, "parsing error in AnalogDeadzone parameter for controller %i", i + 1);
230 if (ConfigGetParameter(pConfig, "AnalogPeak", M64TYPE_STRING, input_str, 256) == M64ERR_SUCCESS)
232 if (sscanf(input_str, "%i,%i", &controller[i].axis_peak[0], &controller[i].axis_peak[1]) != 2)
233 DebugMessage(M64MSG_WARNING, "parsing error in AnalogPeak parameter for controller %i", i + 1);
235 /* load configuration for all the digital buttons */
236 for (j = 0; j < X_AXIS; j++)
238 if (ConfigGetParameter(pConfig, button_names[j], M64TYPE_STRING, input_str, 256) != M64ERR_SUCCESS)
240 DebugMessage(M64MSG_WARNING, "missing config key '%s' for controller %i button %i", button_names[j], i+1, j);
243 if ((config_ptr = strstr(input_str, "key")) != NULL)
244 if (sscanf(config_ptr, "key(%i)", (int *) &controller[i].button[j].key) != 1)
245 DebugMessage(M64MSG_WARNING, "parsing error in key() parameter of button '%s' for controller %i", button_names[j], i + 1);
246 if ((config_ptr = strstr(input_str, "button")) != NULL)
247 if (sscanf(config_ptr, "button(%i)", &controller[i].button[j].button) != 1)
248 DebugMessage(M64MSG_WARNING, "parsing error in button() parameter of button '%s' for controller %i", button_names[j], i + 1);
249 if ((config_ptr = strstr(input_str, "axis")) != NULL)
252 if (sscanf(config_ptr, "axis(%d%c,%d", &controller[i].button[j].axis, &chAxisDir, &controller[i].button[j].axis_deadzone) != 3 &&
253 sscanf(config_ptr, "axis(%i%c", &controller[i].button[j].axis, &chAxisDir) != 2)
254 DebugMessage(M64MSG_WARNING, "parsing error in axis() parameter of button '%s' for controller %i", button_names[j], i + 1);
255 controller[i].button[j].axis_dir = (chAxisDir == '+' ? 1 : (chAxisDir == '-' ? -1 : 0));
257 if ((config_ptr = strstr(input_str, "hat")) != NULL)
259 char *lastchar = NULL;
260 if (sscanf(config_ptr, "hat(%i %15s", &controller[i].button[j].hat, value1_str) != 2)
261 DebugMessage(M64MSG_WARNING, "parsing error in hat() parameter of button '%s' for controller %i", button_names[j], i + 1);
263 /* chop off the last character of value1_str if it is the closing parenthesis */
264 lastchar = &value1_str[strlen(value1_str) - 1];
265 if (lastchar > value1_str && *lastchar == ')') *lastchar = 0;
266 controller[i].button[j].hat_pos = get_hat_pos_by_name(value1_str);
268 if ((config_ptr = strstr(input_str, "mouse")) != NULL) {
269 // check for mouse_pseudo button first
270 if ((config_ptr = strstr(input_str, "mouse_up")) != NULL) {controller[i].button[j].mouse_up=1; controller[i].mouse_up=1;}
271 else if ((config_ptr = strstr(input_str, "mouse_down")) != NULL) {controller[i].button[j].mouse_down=1; controller[i].mouse_down=1;}
272 else if ((config_ptr = strstr(input_str, "mouse_left")) != NULL) {controller[i].button[j].mouse_left=1; controller[i].mouse_left=1;}
273 else if ((config_ptr = strstr(input_str, "mouse_right")) != NULL) {controller[i].button[j].mouse_right=1; controller[i].mouse_right=1;}
274 else if (sscanf(config_ptr, "mouse(%i)", &controller[i].button[j].mouse) != 1)
275 DebugMessage(M64MSG_WARNING, "parsing error in mouse() parameter of button '%s' for controller %i", button_names[j], i + 1);
278 /* load configuration for the 2 analog joystick axes */
279 for (j = X_AXIS; j <= Y_AXIS; j++)
281 int axis_idx = j - X_AXIS;
282 if (ConfigGetParameter(pConfig, button_names[j], M64TYPE_STRING, input_str, 256) != M64ERR_SUCCESS)
284 DebugMessage(M64MSG_WARNING, "missing config key '%s' for controller %i axis %i", button_names[j], i+1, axis_idx);
287 if ((config_ptr = strstr(input_str, "key")) != NULL)
288 if (sscanf(config_ptr, "key(%i,%i)", (int *) &controller[i].axis[axis_idx].key_a, (int *) &controller[i].axis[axis_idx].key_b) != 2)
289 DebugMessage(M64MSG_WARNING, "parsing error in key() parameter of axis '%s' for controller %i", button_names[j], i + 1);
290 if ((config_ptr = strstr(input_str, "button")) != NULL)
291 if (sscanf(config_ptr, "button(%i,%i)", &controller[i].axis[axis_idx].button_a, &controller[i].axis[axis_idx].button_b) != 2)
292 DebugMessage(M64MSG_WARNING, "parsing error in button() parameter of axis '%s' for controller %i", button_names[j], i + 1);
293 if ((config_ptr = strstr(input_str, "axis")) != NULL)
295 char chAxisDir1, chAxisDir2;
296 if (sscanf(config_ptr, "axis(%i%c,%i%c)", &controller[i].axis[axis_idx].axis_a, &chAxisDir1,
297 &controller[i].axis[axis_idx].axis_b, &chAxisDir2) != 4)
298 DebugMessage(M64MSG_WARNING, "parsing error in axis() parameter of axis '%s' for controller %i", button_names[j], i + 1);
299 controller[i].axis[axis_idx].axis_dir_a = (chAxisDir1 == '+' ? 1 : (chAxisDir1 == '-' ? -1 : 0));
300 controller[i].axis[axis_idx].axis_dir_b = (chAxisDir2 == '+' ? 1 : (chAxisDir2 == '-' ? -1 : 0));
302 if ((config_ptr = strstr(input_str, "hat")) != NULL)
304 char *lastchar = NULL;
305 if (sscanf(config_ptr, "hat(%i %15s %15s", &controller[i].axis[axis_idx].hat, value1_str, value2_str) != 3)
306 DebugMessage(M64MSG_WARNING, "parsing error in hat() parameter of axis '%s' for controller %i", button_names[j], i + 1);
307 value1_str[15] = value2_str[15] = 0;
308 /* chop off the last character of value2_str if it is the closing parenthesis */
309 lastchar = &value2_str[strlen(value2_str) - 1];
310 if (lastchar > value2_str && *lastchar == ')') *lastchar = 0;
311 controller[i].axis[axis_idx].hat_pos_a = get_hat_pos_by_name(value1_str);
312 controller[i].axis[axis_idx].hat_pos_b = get_hat_pos_by_name(value2_str);
319 static void init_controller_config(int iCtrlIdx, const char *pccDeviceName, eModeType mode)
322 char SectionName[32], Param[32], ParamString[128];
325 /* Delete the configuration section for this controller, so we can use SetDefaults and save the help comments also */
326 sprintf(SectionName, "Input-SDL-Control%i", iCtrlIdx + 1);
327 ConfigDeleteSection(SectionName);
328 /* Open the configuration section for this controller (create a new one) */
329 if (ConfigOpenSection(SectionName, &pConfig) != M64ERR_SUCCESS)
331 DebugMessage(M64MSG_ERROR, "Couldn't open config section '%s'", SectionName);
335 /* save the general controller parameters */
336 ConfigSetDefaultFloat(pConfig, "version", CONFIG_VERSION, "Mupen64Plus SDL Input Plugin config parameter version number. Please don't change this version number.");
337 ConfigSetDefaultInt(pConfig, "mode", (int) mode, "Controller configuration mode: 0=Fully Manual, 1=Auto with named SDL Device, 2=Fully automatic");
338 ConfigSetDefaultInt(pConfig, "device", controller[iCtrlIdx].device, "Specifies which joystick is bound to this controller: -1=No joystick, 0 or more= SDL Joystick number");
339 ConfigSetDefaultString(pConfig, "name", pccDeviceName, "SDL joystick name (or Keyboard)");
340 ConfigSetDefaultBool(pConfig, "plugged", controller[iCtrlIdx].control->Present, "Specifies whether this controller is 'plugged in' to the simulated N64");
341 ConfigSetDefaultInt(pConfig, "plugin", controller[iCtrlIdx].control->Plugin, "Specifies which type of expansion pak is in the controller: 1=None, 2=Mem pak, 5=Rumble pak");
342 ConfigSetDefaultBool(pConfig, "mouse", controller[iCtrlIdx].mouse, "If True, then mouse buttons may be used with this controller");
344 sprintf(Param, "%.2f,%.2f", controller[iCtrlIdx].mouse_sens[0], controller[iCtrlIdx].mouse_sens[1]);
345 ConfigSetDefaultString(pConfig, "MouseSensitivity", Param, "Scaling factor for mouse movements. For X, Y axes.");
346 sprintf(Param, "%i,%i", controller[iCtrlIdx].axis_deadzone[0], controller[iCtrlIdx].axis_deadzone[1]);
347 ConfigSetDefaultString(pConfig, "AnalogDeadzone", Param, "The minimum absolute value of the SDL analog joystick axis to move the N64 controller axis value from 0. For X, Y axes.");
348 sprintf(Param, "%i,%i", controller[iCtrlIdx].axis_peak[0], controller[iCtrlIdx].axis_peak[1]);
349 ConfigSetDefaultString(pConfig, "AnalogPeak", Param, "An absolute value of the SDL joystick axis >= AnalogPeak will saturate the N64 controller axis value (at 80). For X, Y axes. For each axis, this must be greater than the corresponding AnalogDeadzone value");
351 /* save configuration for all the digital buttons */
352 for (j = 0; j < X_AXIS; j++ )
357 if (controller[iCtrlIdx].button[j].key > 0)
359 sprintf(Param, "key(%i) ", controller[iCtrlIdx].button[j].key);
360 strcat(ParamString, Param);
362 if (controller[iCtrlIdx].button[j].button >= 0)
364 sprintf(Param, "button(%i) ", controller[iCtrlIdx].button[j].button);
365 strcat(ParamString, Param);
367 if (controller[iCtrlIdx].button[j].axis >= 0)
369 if (controller[iCtrlIdx].button[j].axis_deadzone >= 0)
370 sprintf(Param, "axis(%i%c,%i) ", controller[iCtrlIdx].button[j].axis, (controller[iCtrlIdx].button[j].axis_dir == -1) ? '-' : '+',
371 controller[iCtrlIdx].button[j].axis_deadzone);
373 sprintf(Param, "axis(%i%c) ", controller[iCtrlIdx].button[j].axis, (controller[iCtrlIdx].button[j].axis_dir == -1) ? '-' : '+');
374 strcat(ParamString, Param);
376 if (controller[iCtrlIdx].button[j].hat >= 0)
378 sprintf(Param, "hat(%i %s) ", controller[iCtrlIdx].button[j].hat, HAT_POS_NAME(controller[iCtrlIdx].button[j].hat_pos));
379 strcat(ParamString, Param);
381 if (controller[iCtrlIdx].button[j].mouse >= 0)
383 sprintf(Param, "mouse(%i) ", controller[iCtrlIdx].button[j].mouse);
384 strcat(ParamString, Param);
386 if (controller[iCtrlIdx].button[j].mouse_up > 0)
388 sprintf(Param, "mouse_up ");
389 strcat(ParamString, Param);
391 if (controller[iCtrlIdx].button[j].mouse_down > 0)
393 sprintf(Param, "mouse_down ");
394 strcat(ParamString, Param);
396 if (controller[iCtrlIdx].button[j].mouse_right > 0)
398 sprintf(Param, "mouse_right ");
399 strcat(ParamString, Param);
401 if (controller[iCtrlIdx].button[j].mouse_left > 0)
403 sprintf(Param, "mouse_left ");
404 strcat(ParamString, Param);
407 Help = "Digital button configuration mappings";
410 /* if last character is a space, chop it off */
411 len = strlen(ParamString);
412 if (len > 0 && ParamString[len-1] == ' ')
413 ParamString[len-1] = 0;
414 ConfigSetDefaultString(pConfig, button_names[j], ParamString, Help);
417 /* save configuration for the 2 analog axes */
418 for (j = 0; j < 2; j++ )
423 if (controller[iCtrlIdx].axis[j].key_a > 0 && controller[iCtrlIdx].axis[j].key_b > 0)
425 sprintf(Param, "key(%i,%i) ", controller[iCtrlIdx].axis[j].key_a, controller[iCtrlIdx].axis[j].key_b);
426 strcat(ParamString, Param);
428 if (controller[iCtrlIdx].axis[j].button_a >= 0 && controller[iCtrlIdx].axis[j].button_b >= 0)
430 sprintf(Param, "button(%i,%i) ", controller[iCtrlIdx].axis[j].button_a, controller[iCtrlIdx].axis[j].button_b);
431 strcat(ParamString, Param);
433 if (controller[iCtrlIdx].axis[j].axis_a >= 0 && controller[iCtrlIdx].axis[j].axis_b >= 0)
435 sprintf(Param, "axis(%i%c,%i%c) ", controller[iCtrlIdx].axis[j].axis_a, (controller[iCtrlIdx].axis[j].axis_dir_a <= 0) ? '-' : '+',
436 controller[iCtrlIdx].axis[j].axis_b, (controller[iCtrlIdx].axis[j].axis_dir_b <= 0) ? '-' : '+' );
437 strcat(ParamString, Param);
439 if (controller[iCtrlIdx].axis[j].hat >= 0)
441 sprintf(Param, "hat(%i %s %s) ", controller[iCtrlIdx].axis[j].hat,
442 HAT_POS_NAME(controller[iCtrlIdx].axis[j].hat_pos_a),
443 HAT_POS_NAME(controller[iCtrlIdx].axis[j].hat_pos_b));
444 strcat(ParamString, Param);
447 Help = "Analog axis configuration mappings";
450 /* if last character is a space, chop it off */
451 len = strlen(ParamString);
452 if (len > 0 && ParamString[len-1] == ' ')
453 ParamString[len-1] = 0;
454 ConfigSetDefaultString(pConfig, button_names[X_AXIS + j], ParamString, Help);
459 static int setup_auto_controllers(int bPreConfig, int n64CtrlStart, int sdlCtrlIdx, const char *sdlJoyName, eModeType ControlMode[], eModeType OrigControlMode[], char DeviceName[][256])
461 char SectionName[32];
462 int ActiveControllers = 0;
465 /* create auto-config section(s) for this joystick (if this joystick is in the InputAutoConfig.ini) */
466 int ControllersFound = auto_set_defaults(sdlCtrlIdx, sdlJoyName);
467 if (ControllersFound == 0)
470 /* copy the auto-config settings to the controller config section, and load our plugin joystick config from this */
471 sprintf(SectionName, "Input-SDL-Control%i", n64CtrlStart + 1);
472 if (OrigControlMode[n64CtrlStart] == E_MODE_FULL_AUTO)
473 auto_copy_inputconfig("AutoConfig0", SectionName, sdlJoyName);
475 auto_copy_inputconfig("AutoConfig0", SectionName, NULL); // don't overwrite 'name' parameter if original mode was "named auto"
476 if (load_controller_config("AutoConfig0", n64CtrlStart, sdlCtrlIdx) > 0)
479 DebugMessage(M64MSG_INFO, "N64 Controller #%i: Using auto-config with SDL joystick %i ('%s')", n64CtrlStart+1, sdlCtrlIdx, sdlJoyName);
481 ConfigSaveSection(SectionName);
486 DebugMessage(M64MSG_ERROR, "Autoconfig data invalid for SDL joystick '%s'", sdlJoyName);
488 ConfigDeleteSection("AutoConfig0");
490 /* we have to handle the unfortunate case of a USB device mapping more than > 1 controller */
491 if (ControllersFound > 1)
493 for (j = 1; j < ControllersFound; j++)
495 char AutoSectionName[32];
496 sprintf(AutoSectionName, "AutoConfig%i", j);
497 /* if this would be > 4th controller, then just delete the auto-config */
498 if (n64CtrlStart + j >= 4)
500 ConfigDeleteSection(AutoSectionName);
503 /* look for another N64 controller that is in AUTO mode */
504 if (ControlMode[n64CtrlStart+j] == E_MODE_FULL_AUTO ||
505 (ControlMode[n64CtrlStart+j] == E_MODE_NAMED_AUTO && strncmp(DeviceName[n64CtrlStart+j], sdlJoyName, 255) == 0))
507 sprintf(SectionName, "Input-SDL-Control%i", n64CtrlStart + j + 1);
508 /* load our plugin joystick settings from the autoconfig */
509 if (load_controller_config(AutoSectionName, n64CtrlStart+j, sdlCtrlIdx) > 0)
511 /* copy the auto-config settings to the controller config section */
512 if (OrigControlMode[n64CtrlStart+j] == E_MODE_FULL_AUTO)
513 auto_copy_inputconfig(AutoSectionName, SectionName, sdlJoyName);
515 auto_copy_inputconfig(AutoSectionName, SectionName, NULL); // don't overwrite 'name' parameter if original mode was "named auto"
517 DebugMessage(M64MSG_INFO, "N64 Controller #%i: Using auto-config with SDL joystick %i ('%s')", n64CtrlStart+j+1, sdlCtrlIdx, sdlJoyName);
519 ConfigSaveSection(SectionName);
520 /* set the local controller mode to Manual so that we won't re-configure this controller in the next loop */
521 ControlMode[n64CtrlStart+j] = E_MODE_MANUAL;
526 DebugMessage(M64MSG_ERROR, "Autoconfig data invalid for SDL device '%s'", sdlJoyName);
528 /* delete the autoconfig section */
529 ConfigDeleteSection(AutoSectionName);
534 return ActiveControllers;
537 /* global functions */
539 /* There are 4 special section parameters: version, mode, device, and name. There are also 24 regular
540 * parameters: plugged, plugin, mouse, MouseSensitivity, DPad R/L/D/U, Start, Z/L/R Trigger, A/B button,
541 * C Button R/L/D/U, Mempak/Rumblepak switch, X/Y Axis, AnalogDeadzone, AnalogPeak.
543 * The N64 controller configuration behavior is regulated by the 'mode' parameter. If this parameter is
544 * 0 (Fully Manual), then all configuration data is loaded and parsed without changes or autoconfig from
545 * the configuration section. Any errors or warnings are printed. If the mode parameter is 1 (Auto with
546 * named SDL Device), then the code below searches through the available SDL joysticks for one which
547 * matches with the 'name' parameter in the config section. If a match is found, then the corresponding
548 * autoconfig is loaded and the regular parameters from the config section are updated with the
549 * autoconfig'ed settings. If the mode parameter is 2 (Fully Auto), then the code below searches through
550 * all available detected SDL joysticks to try and find an autoconfig for each. If an autoconfig match is
551 * found, then it is loaded and all config parameters (including device, name, and the regular parameters)
554 * This function is called with bPreConfig=true from the PluginStartup() function. The purpose of this
555 * call is to load the 4 configuration sections with autoconfig data if necessary for a GUI front-end.
556 * When we are called with bPreConfig=true, we should only print warning/error messages and not info/status.
557 * This function is also called right before running the ROM game from InitiateControllers() with
558 * bPreConfig=false. We should only print informational messages here, because we do not want to duplicate
559 * console output with the console-ui front-end (since the PluginStart() and InitiateControllers()
560 * functions are called in quick sequence from the console-ui).
563 void load_configuration(int bPreConfig)
565 char SectionName[32];
567 int n64CtrlIdx, sdlCtrlIdx, j;
568 int sdlNumDevUsed = 0;
569 int sdlDevicesUsed[4];
570 eModeType OrigControlMode[4], ControlMode[4];
571 int ControlDevice[4];
572 char DeviceName[4][256];
573 int ActiveControllers = 0;
574 int sdlNumJoysticks = get_sdl_num_joysticks();
575 float fVersion = 0.0f;
576 const char *sdl_name;
577 int ControllersFound = 0;
579 /* tell user how many SDL joysticks are available */
581 DebugMessage(M64MSG_INFO, "%i SDL joysticks were found.", sdlNumJoysticks);
583 /* loop through 4 N64 controllers, initializing and validating special section parameters */
584 for (n64CtrlIdx=0; n64CtrlIdx < 4; n64CtrlIdx++)
587 /* reset the controller configuration */
588 clear_controller(n64CtrlIdx);
590 /* Open the configuration section for this controller */
591 sprintf(SectionName, "Input-SDL-Control%i", n64CtrlIdx + 1);
592 if (ConfigOpenSection(SectionName, &pConfig) != M64ERR_SUCCESS)
594 // this should never happen
595 DebugMessage(M64MSG_ERROR, "Couldn't open config section '%s'. Aborting...", SectionName);
598 /* Check version number, and if it doesn't match: delete the config section */
600 if (ConfigGetParameter(pConfig, "version", M64TYPE_FLOAT, &fVersion, sizeof(float)) != M64ERR_SUCCESS || ((int) fVersion) != ((int) CONFIG_VERSION))
602 DebugMessage(M64MSG_WARNING, "Missing or incompatible config section '%s'. Clearing.", SectionName);
603 ConfigDeleteSection(SectionName);
604 // set local controller default parameters
605 OrigControlMode[n64CtrlIdx] = ControlMode[n64CtrlIdx] = E_MODE_FULL_AUTO;
606 ControlDevice[n64CtrlIdx] = DEVICE_NO_JOYSTICK;
607 DeviceName[n64CtrlIdx][0] = 0;
608 // write blank config for GUI front-ends
609 init_controller_config(n64CtrlIdx, "", E_MODE_FULL_AUTO);
610 // save it to the file too
611 ConfigSaveSection(SectionName);
615 if (ConfigGetParameter(pConfig, "mode", M64TYPE_INT, &OrigControlMode[n64CtrlIdx], sizeof(int)) != M64ERR_SUCCESS ||
616 (int) OrigControlMode[n64CtrlIdx] < 0 || (int) OrigControlMode[n64CtrlIdx] > 2)
619 DebugMessage(M64MSG_WARNING, "Missing or invalid 'mode' parameter in config section '%s'. Setting to 2 (Fully Auto)", SectionName);
620 OrigControlMode[n64CtrlIdx] = E_MODE_FULL_AUTO;
622 ControlMode[n64CtrlIdx] = OrigControlMode[n64CtrlIdx];
623 if (ConfigGetParameter(pConfig, "device", M64TYPE_INT, &ControlDevice[n64CtrlIdx], sizeof(int)) != M64ERR_SUCCESS)
626 DebugMessage(M64MSG_WARNING, "Missing 'device' parameter in config section '%s'. Setting to -1 (No joystick)", SectionName);
627 ControlDevice[n64CtrlIdx] = DEVICE_NO_JOYSTICK;
629 if (ConfigGetParameter(pConfig, "name", M64TYPE_STRING, DeviceName[n64CtrlIdx], 256) != M64ERR_SUCCESS)
631 DeviceName[n64CtrlIdx][0] = 0;
636 /* loop through 4 N64 controllers and set up those in Fully Manual mode */
637 for (n64CtrlIdx=0; n64CtrlIdx < 4; n64CtrlIdx++)
639 if (ControlMode[n64CtrlIdx] != E_MODE_MANUAL)
641 /* load the stored configuration (disregard any errors) */
642 sprintf(SectionName, "Input-SDL-Control%i", n64CtrlIdx + 1);
643 load_controller_config(SectionName, n64CtrlIdx, ControlDevice[n64CtrlIdx]);
644 /* if this config uses an SDL joystick, mark it as used */
645 if (ControlDevice[n64CtrlIdx] == DEVICE_NO_JOYSTICK)
648 DebugMessage(M64MSG_INFO, "N64 Controller #%i: Using manual config with no SDL joystick (keyboard/mouse only)", n64CtrlIdx+1);
652 sdlDevicesUsed[sdlNumDevUsed++] = ControlDevice[n64CtrlIdx];
654 DebugMessage(M64MSG_INFO, "N64 Controller #%i: Using manual config for SDL joystick %i", n64CtrlIdx+1, ControlDevice[n64CtrlIdx]);
659 /* now loop through again, setting up those in Named Auto mode */
660 for (n64CtrlIdx=0; n64CtrlIdx < 4; n64CtrlIdx++)
662 if (ControlMode[n64CtrlIdx] != E_MODE_NAMED_AUTO)
664 /* if name is empty, then use full auto mode instead */
665 if (DeviceName[n64CtrlIdx][0] == 0)
667 ControlMode[n64CtrlIdx] = E_MODE_FULL_AUTO;
670 sprintf(SectionName, "Input-SDL-Control%i", n64CtrlIdx + 1);
671 /* if user is looking for a keyboard, set that up */
672 if (strcasecmp(DeviceName[n64CtrlIdx], "Keyboard") == 0)
674 auto_set_defaults(DEVICE_NO_JOYSTICK, "Keyboard");
675 if (load_controller_config("AutoConfig0", n64CtrlIdx, DEVICE_NO_JOYSTICK) > 0)
678 DebugMessage(M64MSG_INFO, "N64 Controller #%i: Using auto-config for keyboard", n64CtrlIdx+1);
679 /* copy the auto-config settings to the controller config section */
680 auto_copy_inputconfig("AutoConfig0", SectionName, "Keyboard");
682 ConfigSaveSection(SectionName);
686 DebugMessage(M64MSG_ERROR, "Autoconfig keyboard setup invalid");
688 ConfigDeleteSection("AutoConfig0");
691 /* search for an unused SDL device with the matching name */
692 for (sdlCtrlIdx=0; sdlCtrlIdx < sdlNumJoysticks; sdlCtrlIdx++)
694 /* check if this one is in use */
695 int deviceAlreadyUsed = 0;
696 for (j = 0; j < sdlNumDevUsed; j++)
698 if (sdlDevicesUsed[j] == sdlCtrlIdx)
699 deviceAlreadyUsed = 1;
701 if (deviceAlreadyUsed)
703 /* check if the name matches */
704 sdl_name = get_sdl_joystick_name(sdlCtrlIdx);
705 if (sdl_name != NULL && strncmp(DeviceName[n64CtrlIdx], sdl_name, 255) == 0)
707 /* set up one or more controllers for this SDL device, if present in InputAutoConfig.ini */
708 int ControllersFound = setup_auto_controllers(bPreConfig, n64CtrlIdx, sdlCtrlIdx, sdl_name, ControlMode, OrigControlMode, DeviceName);
709 if (ControllersFound == 0)
711 // error: no auto-config found for this SDL device
712 DebugMessage(M64MSG_ERROR, "No auto-config found for joystick named '%s' in InputAutoConfig.ini", sdl_name);
713 // mark this device as being used just so we don't complain about it again
714 sdlDevicesUsed[sdlNumDevUsed++] = sdlCtrlIdx;
715 // quit looking for SDL joysticks which match the name, because there's no valid autoconfig for that name.
716 // this controller will be unused; skip to the next one
719 /* mark this sdl device as used */
720 sdlDevicesUsed[sdlNumDevUsed++] = sdlCtrlIdx;
721 ActiveControllers += ControllersFound;
725 /* if we didn't find a match for this joystick name, then set the controller to fully auto */
726 if (sdlCtrlIdx == sdlNumJoysticks)
729 DebugMessage(M64MSG_WARNING, "N64 Controller #%i: No SDL joystick found matching name '%s'. Using full auto mode.", n64CtrlIdx+1, DeviceName[n64CtrlIdx]);
730 ControlMode[n64CtrlIdx] = E_MODE_FULL_AUTO;
734 /* Final loop through N64 controllers, setting up those in Full Auto mode */
735 for (n64CtrlIdx=0; n64CtrlIdx < 4; n64CtrlIdx++)
737 if (ControlMode[n64CtrlIdx] != E_MODE_FULL_AUTO)
739 sprintf(SectionName, "Input-SDL-Control%i", n64CtrlIdx + 1);
740 /* search for an unused SDL device */
741 for (sdlCtrlIdx=0; sdlCtrlIdx < sdlNumJoysticks; sdlCtrlIdx++)
743 /* check if this one is in use */
744 int deviceAlreadyUsed = 0;
745 for (j = 0; j < sdlNumDevUsed; j++)
747 if (sdlDevicesUsed[j] == sdlCtrlIdx)
748 deviceAlreadyUsed = 1;
750 if (deviceAlreadyUsed)
752 /* set up one or more controllers for this SDL device, if present in InputAutoConfig.ini */
753 sdl_name = get_sdl_joystick_name(sdlCtrlIdx);
754 ControllersFound = setup_auto_controllers(bPreConfig, n64CtrlIdx, sdlCtrlIdx, sdl_name, ControlMode, OrigControlMode, DeviceName);
755 if (!bPreConfig && ControllersFound == 0)
757 // error: no auto-config found for this SDL device
758 DebugMessage(M64MSG_ERROR, "No auto-config found for joystick named '%s' in InputAutoConfig.ini", sdl_name);
759 // mark this device as being used just so we don't complain about it again
760 sdlDevicesUsed[sdlNumDevUsed++] = sdlCtrlIdx;
761 // keep trying more SDL devices to see if we can auto-config one for this N64 controller
764 /* mark this sdl device as used */
765 sdlDevicesUsed[sdlNumDevUsed++] = sdlCtrlIdx;
766 ActiveControllers += ControllersFound;
769 /* if this N64 controller was not activated, set device to -1 */
770 if (sdlCtrlIdx == sdlNumJoysticks)
773 if (ConfigOpenSection(SectionName, §ion) == M64ERR_SUCCESS)
775 const int iNoDevice = -1;
776 ConfigSetParameter(section, "device", M64TYPE_INT, &iNoDevice);
777 if (OrigControlMode[n64CtrlIdx] == E_MODE_FULL_AUTO)
778 ConfigSetParameter(section, "name", M64TYPE_STRING, "");
779 ConfigSaveSection(SectionName);
784 /* fallback to keyboard if no controllers were configured */
785 if (ActiveControllers == 0)
788 DebugMessage(M64MSG_INFO, "N64 Controller #1: Forcing default keyboard configuration");
789 auto_set_defaults(DEVICE_NO_JOYSTICK, "Keyboard");
790 if (load_controller_config("AutoConfig0", 0, DEVICE_NO_JOYSTICK) > 0)
792 /* copy the auto-config settings to the controller config section */
793 if (OrigControlMode[0] == E_MODE_FULL_AUTO)
794 auto_copy_inputconfig("AutoConfig0", "Input-SDL-Control1", "Keyboard");
796 auto_copy_inputconfig("AutoConfig0", "Input-SDL-Control1", NULL); // don't overwrite 'name' parameter
798 ConfigSaveSection("Input-SDL-Control1");
802 DebugMessage(M64MSG_ERROR, "Autoconfig keyboard setup invalid");
804 ConfigDeleteSection("AutoConfig0");
807 /* see how many joysticks are plugged in */
809 for (j = 0; j < 4; j++)
811 if (controller[j].control->Present)
815 /* print out summary info message */
820 DebugMessage(M64MSG_INFO, "%i controller(s) found, %i plugged in and usable in the emulator", ActiveControllers, joy_plugged);
824 if (ActiveControllers == 0)
825 DebugMessage(M64MSG_WARNING, "No joysticks/controllers found");
827 DebugMessage(M64MSG_WARNING, "%i controllers found, but none were 'plugged in'", ActiveControllers);