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
69 /* static functions */
70 static int get_hat_pos_by_name( const char *name )
72 if( !strcasecmp( name, "up" ) )
74 if( !strcasecmp( name, "down" ) )
76 if( !strcasecmp( name, "left" ) )
78 if( !strcasecmp( name, "right" ) )
80 DebugMessage(M64MSG_WARNING, "get_hat_pos_by_name(): direction '%s' unknown", name);
84 static void clear_controller(int iCtrlIdx)
88 controller[iCtrlIdx].device = DEVICE_NO_JOYSTICK;
89 controller[iCtrlIdx].control->Present = 0;
90 controller[iCtrlIdx].control->RawData = 0;
91 controller[iCtrlIdx].control->Plugin = PLUGIN_NONE;
92 for( b = 0; b < 16; b++ )
94 controller[iCtrlIdx].button[b].button = -1;
95 controller[iCtrlIdx].button[b].key = SDL_SCANCODE_UNKNOWN;
96 controller[iCtrlIdx].button[b].axis = -1;
97 controller[iCtrlIdx].button[b].axis_deadzone = -1;
98 controller[iCtrlIdx].button[b].hat = -1;
99 controller[iCtrlIdx].button[b].hat_pos = -1;
100 controller[iCtrlIdx].button[b].mouse = -1;
102 for( b = 0; b < 2; b++ )
104 controller[iCtrlIdx].mouse_sens[b] = 2.0;
105 controller[iCtrlIdx].axis_deadzone[b] = 4096;
106 controller[iCtrlIdx].axis_peak[b] = 32768;
107 controller[iCtrlIdx].axis[b].button_a = controller[iCtrlIdx].axis[b].button_b = -1;
108 controller[iCtrlIdx].axis[b].key_a = controller[iCtrlIdx].axis[b].key_b = SDL_SCANCODE_UNKNOWN;
109 controller[iCtrlIdx].axis[b].axis_a = -1;
110 controller[iCtrlIdx].axis[b].axis_dir_a = 1;
111 controller[iCtrlIdx].axis[b].axis_b = -1;
112 controller[iCtrlIdx].axis[b].axis_dir_b = 1;
113 controller[iCtrlIdx].axis[b].hat = -1;
114 controller[iCtrlIdx].axis[b].hat_pos_a = -1;
115 controller[iCtrlIdx].axis[b].hat_pos_b = -1;
119 static const char * get_sdl_joystick_name(int iCtrlIdx)
121 static char JoyName[256];
122 const char *joySDLName;
123 int joyWasInit = SDL_WasInit(SDL_INIT_JOYSTICK);
125 /* initialize the joystick subsystem if necessary */
127 if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1)
129 DebugMessage(M64MSG_ERROR, "Couldn't init SDL joystick subsystem: %s", SDL_GetError() );
133 /* get the name of the corresponding joystick */
134 joySDLName = SDL_JoystickName(iCtrlIdx);
136 /* copy the name to our local string */
137 if (joySDLName != NULL)
139 strncpy(JoyName, joySDLName, 255);
143 /* quit the joystick subsystem if necessary */
145 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
147 /* if the SDL function had an error, then return NULL, otherwise return local copy of joystick name */
148 if (joySDLName == NULL)
154 static int get_sdl_num_joysticks(void)
156 int numJoysticks = 0;
157 int joyWasInit = SDL_WasInit(SDL_INIT_JOYSTICK);
159 /* initialize the joystick subsystem if necessary */
161 if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1)
163 DebugMessage(M64MSG_ERROR, "Couldn't init SDL joystick subsystem: %s", SDL_GetError() );
167 /* get thenumber of joysticks */
168 numJoysticks = SDL_NumJoysticks();
170 /* quit the joystick subsystem if necessary */
172 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
177 /////////////////////////////////////
178 // load_controller_config()
179 // return value: 1 = OK
180 // 0 = fail: couldn't open config section
182 static int load_controller_config(const char *SectionName, int i, int sdlDeviceIdx)
185 char input_str[256], value1_str[16], value2_str[16];
186 const char *config_ptr;
189 /* Open the configuration section for this controller */
190 if (ConfigOpenSection(SectionName, &pConfig) != M64ERR_SUCCESS)
192 DebugMessage(M64MSG_ERROR, "Couldn't open config section '%s'", SectionName);
196 /* set SDL device number */
197 controller[i].device = sdlDeviceIdx;
199 /* throw warnings if 'plugged' or 'plugin' are missing */
200 if (ConfigGetParameter(pConfig, "plugged", M64TYPE_BOOL, &controller[i].control->Present, sizeof(int)) != M64ERR_SUCCESS)
202 DebugMessage(M64MSG_WARNING, "missing 'plugged' parameter from config section %s. Setting to 1 (true).", SectionName);
203 controller[i].control->Present = 1;
205 if (ConfigGetParameter(pConfig, "plugin", M64TYPE_INT, &controller[i].control->Plugin, sizeof(int)) != M64ERR_SUCCESS)
207 DebugMessage(M64MSG_WARNING, "missing 'plugin' parameter from config section %s. Setting to 1 (none).", SectionName);
208 controller[i].control->Plugin = PLUGIN_NONE;
210 /* load optional parameters */
211 ConfigGetParameter(pConfig, "mouse", M64TYPE_BOOL, &controller[i].mouse, sizeof(int));
212 if (ConfigGetParameter(pConfig, "MouseSensitivity", M64TYPE_STRING, input_str, 256) == M64ERR_SUCCESS)
214 if (sscanf(input_str, "%f,%f", &controller[i].mouse_sens[0], &controller[i].mouse_sens[1]) != 2)
215 DebugMessage(M64MSG_WARNING, "parsing error in MouseSensitivity parameter for controller %i", i + 1);
217 if (ConfigGetParameter(pConfig, "AnalogDeadzone", M64TYPE_STRING, input_str, 256) == M64ERR_SUCCESS)
219 if (sscanf(input_str, "%i,%i", &controller[i].axis_deadzone[0], &controller[i].axis_deadzone[1]) != 2)
220 DebugMessage(M64MSG_WARNING, "parsing error in AnalogDeadzone parameter for controller %i", i + 1);
222 if (ConfigGetParameter(pConfig, "AnalogPeak", M64TYPE_STRING, input_str, 256) == M64ERR_SUCCESS)
224 if (sscanf(input_str, "%i,%i", &controller[i].axis_peak[0], &controller[i].axis_peak[1]) != 2)
225 DebugMessage(M64MSG_WARNING, "parsing error in AnalogPeak parameter for controller %i", i + 1);
227 /* load configuration for all the digital buttons */
228 for (j = 0; j < X_AXIS; j++)
230 if (ConfigGetParameter(pConfig, button_names[j], M64TYPE_STRING, input_str, 256) != M64ERR_SUCCESS)
232 DebugMessage(M64MSG_WARNING, "missing config key '%s' for controller %i button %i", button_names[j], i+1, j);
235 if ((config_ptr = strstr(input_str, "key")) != NULL)
236 if (sscanf(config_ptr, "key(%i)", (int *) &controller[i].button[j].key) != 1)
237 DebugMessage(M64MSG_WARNING, "parsing error in key() parameter of button '%s' for controller %i", button_names[j], i + 1);
238 if ((config_ptr = strstr(input_str, "button")) != NULL)
239 if (sscanf(config_ptr, "button(%i)", &controller[i].button[j].button) != 1)
240 DebugMessage(M64MSG_WARNING, "parsing error in button() parameter of button '%s' for controller %i", button_names[j], i + 1);
241 if ((config_ptr = strstr(input_str, "axis")) != NULL)
244 if (sscanf(config_ptr, "axis(%d%c,%d", &controller[i].button[j].axis, &chAxisDir, &controller[i].button[j].axis_deadzone) != 3 &&
245 sscanf(config_ptr, "axis(%i%c", &controller[i].button[j].axis, &chAxisDir) != 2)
246 DebugMessage(M64MSG_WARNING, "parsing error in axis() parameter of button '%s' for controller %i", button_names[j], i + 1);
247 controller[i].button[j].axis_dir = (chAxisDir == '+' ? 1 : (chAxisDir == '-' ? -1 : 0));
249 if ((config_ptr = strstr(input_str, "hat")) != NULL)
251 char *lastchar = NULL;
252 if (sscanf(config_ptr, "hat(%i %15s", &controller[i].button[j].hat, value1_str) != 2)
253 DebugMessage(M64MSG_WARNING, "parsing error in hat() parameter of button '%s' for controller %i", button_names[j], i + 1);
255 /* chop off the last character of value1_str if it is the closing parenthesis */
256 lastchar = &value1_str[strlen(value1_str) - 1];
257 if (lastchar > value1_str && *lastchar == ')') *lastchar = 0;
258 controller[i].button[j].hat_pos = get_hat_pos_by_name(value1_str);
260 if ((config_ptr = strstr(input_str, "mouse")) != NULL)
261 if (sscanf(config_ptr, "mouse(%i)", &controller[i].button[j].mouse) != 1)
262 DebugMessage(M64MSG_WARNING, "parsing error in mouse() parameter of button '%s' for controller %i", button_names[j], i + 1);
264 /* load configuration for the 2 analog joystick axes */
265 for (j = X_AXIS; j <= Y_AXIS; j++)
267 int axis_idx = j - X_AXIS;
268 if (ConfigGetParameter(pConfig, button_names[j], M64TYPE_STRING, input_str, 256) != M64ERR_SUCCESS)
270 DebugMessage(M64MSG_WARNING, "missing config key '%s' for controller %i axis %i", button_names[j], i+1, axis_idx);
273 if ((config_ptr = strstr(input_str, "key")) != NULL)
274 if (sscanf(config_ptr, "key(%i,%i)", (int *) &controller[i].axis[axis_idx].key_a, (int *) &controller[i].axis[axis_idx].key_b) != 2)
275 DebugMessage(M64MSG_WARNING, "parsing error in key() parameter of axis '%s' for controller %i", button_names[j], i + 1);
276 if ((config_ptr = strstr(input_str, "button")) != NULL)
277 if (sscanf(config_ptr, "button(%i,%i)", &controller[i].axis[axis_idx].button_a, &controller[i].axis[axis_idx].button_b) != 2)
278 DebugMessage(M64MSG_WARNING, "parsing error in button() parameter of axis '%s' for controller %i", button_names[j], i + 1);
279 if ((config_ptr = strstr(input_str, "axis")) != NULL)
281 char chAxisDir1, chAxisDir2;
282 if (sscanf(config_ptr, "axis(%i%c,%i%c)", &controller[i].axis[axis_idx].axis_a, &chAxisDir1,
283 &controller[i].axis[axis_idx].axis_b, &chAxisDir2) != 4)
284 DebugMessage(M64MSG_WARNING, "parsing error in axis() parameter of axis '%s' for controller %i", button_names[j], i + 1);
285 controller[i].axis[axis_idx].axis_dir_a = (chAxisDir1 == '+' ? 1 : (chAxisDir1 == '-' ? -1 : 0));
286 controller[i].axis[axis_idx].axis_dir_b = (chAxisDir2 == '+' ? 1 : (chAxisDir2 == '-' ? -1 : 0));
288 if ((config_ptr = strstr(input_str, "hat")) != NULL)
290 char *lastchar = NULL;
291 if (sscanf(config_ptr, "hat(%i %15s %15s", &controller[i].axis[axis_idx].hat, value1_str, value2_str) != 3)
292 DebugMessage(M64MSG_WARNING, "parsing error in hat() parameter of axis '%s' for controller %i", button_names[j], i + 1);
293 value1_str[15] = value2_str[15] = 0;
294 /* chop off the last character of value2_str if it is the closing parenthesis */
295 lastchar = &value2_str[strlen(value2_str) - 1];
296 if (lastchar > value2_str && *lastchar == ')') *lastchar = 0;
297 controller[i].axis[axis_idx].hat_pos_a = get_hat_pos_by_name(value1_str);
298 controller[i].axis[axis_idx].hat_pos_b = get_hat_pos_by_name(value2_str);
305 static void init_controller_config(int iCtrlIdx, const char *pccDeviceName, eModeType mode)
308 char SectionName[32], Param[32], ParamString[128];
311 /* Delete the configuration section for this controller, so we can use SetDefaults and save the help comments also */
312 sprintf(SectionName, "Input-SDL-Control%i", iCtrlIdx + 1);
313 ConfigDeleteSection(SectionName);
314 /* Open the configuration section for this controller (create a new one) */
315 if (ConfigOpenSection(SectionName, &pConfig) != M64ERR_SUCCESS)
317 DebugMessage(M64MSG_ERROR, "Couldn't open config section '%s'", SectionName);
321 /* save the general controller parameters */
322 ConfigSetDefaultFloat(pConfig, "version", CONFIG_VERSION, "Mupen64Plus SDL Input Plugin config parameter version number. Please don't change this version number.");
323 ConfigSetDefaultInt(pConfig, "mode", (int) mode, "Controller configuration mode: 0=Fully Manual, 1=Auto with named SDL Device, 2=Fully automatic");
324 ConfigSetDefaultInt(pConfig, "device", controller[iCtrlIdx].device, "Specifies which joystick is bound to this controller: -1=No joystick, 0 or more= SDL Joystick number");
325 ConfigSetDefaultString(pConfig, "name", pccDeviceName, "SDL joystick name (or Keyboard)");
326 ConfigSetDefaultBool(pConfig, "plugged", controller[iCtrlIdx].control->Present, "Specifies whether this controller is 'plugged in' to the simulated N64");
327 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");
328 ConfigSetDefaultBool(pConfig, "mouse", controller[iCtrlIdx].mouse, "If True, then mouse buttons may be used with this controller");
330 sprintf(Param, "%.2f,%.2f", controller[iCtrlIdx].mouse_sens[0], controller[iCtrlIdx].mouse_sens[1]);
331 ConfigSetDefaultString(pConfig, "MouseSensitivity", Param, "Scaling factor for mouse movements. For X, Y axes.");
332 sprintf(Param, "%i,%i", controller[iCtrlIdx].axis_deadzone[0], controller[iCtrlIdx].axis_deadzone[1]);
333 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.");
334 sprintf(Param, "%i,%i", controller[iCtrlIdx].axis_peak[0], controller[iCtrlIdx].axis_peak[1]);
335 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");
337 /* save configuration for all the digital buttons */
338 for (j = 0; j < X_AXIS; j++ )
343 if (controller[iCtrlIdx].button[j].key > 0)
345 sprintf(Param, "key(%i) ", controller[iCtrlIdx].button[j].key);
346 strcat(ParamString, Param);
348 if (controller[iCtrlIdx].button[j].button >= 0)
350 sprintf(Param, "button(%i) ", controller[iCtrlIdx].button[j].button);
351 strcat(ParamString, Param);
353 if (controller[iCtrlIdx].button[j].axis >= 0)
355 if (controller[iCtrlIdx].button[j].axis_deadzone >= 0)
356 sprintf(Param, "axis(%i%c,%i) ", controller[iCtrlIdx].button[j].axis, (controller[iCtrlIdx].button[j].axis_dir == -1) ? '-' : '+',
357 controller[iCtrlIdx].button[j].axis_deadzone);
359 sprintf(Param, "axis(%i%c) ", controller[iCtrlIdx].button[j].axis, (controller[iCtrlIdx].button[j].axis_dir == -1) ? '-' : '+');
360 strcat(ParamString, Param);
362 if (controller[iCtrlIdx].button[j].hat >= 0)
364 sprintf(Param, "hat(%i %s) ", controller[iCtrlIdx].button[j].hat, HAT_POS_NAME(controller[iCtrlIdx].button[j].hat_pos));
365 strcat(ParamString, Param);
367 if (controller[iCtrlIdx].button[j].mouse >= 0)
369 sprintf(Param, "mouse(%i) ", controller[iCtrlIdx].button[j].mouse);
370 strcat(ParamString, Param);
373 Help = "Digital button configuration mappings";
376 /* if last character is a space, chop it off */
377 len = strlen(ParamString);
378 if (len > 0 && ParamString[len-1] == ' ')
379 ParamString[len-1] = 0;
380 ConfigSetDefaultString(pConfig, button_names[j], ParamString, Help);
383 /* save configuration for the 2 analog axes */
384 for (j = 0; j < 2; j++ )
389 if (controller[iCtrlIdx].axis[j].key_a > 0 && controller[iCtrlIdx].axis[j].key_b > 0)
391 sprintf(Param, "key(%i,%i) ", controller[iCtrlIdx].axis[j].key_a, controller[iCtrlIdx].axis[j].key_b);
392 strcat(ParamString, Param);
394 if (controller[iCtrlIdx].axis[j].button_a >= 0 && controller[iCtrlIdx].axis[j].button_b >= 0)
396 sprintf(Param, "button(%i,%i) ", controller[iCtrlIdx].axis[j].button_a, controller[iCtrlIdx].axis[j].button_b);
397 strcat(ParamString, Param);
399 if (controller[iCtrlIdx].axis[j].axis_a >= 0 && controller[iCtrlIdx].axis[j].axis_b >= 0)
401 sprintf(Param, "axis(%i%c,%i%c) ", controller[iCtrlIdx].axis[j].axis_a, (controller[iCtrlIdx].axis[j].axis_dir_a <= 0) ? '-' : '+',
402 controller[iCtrlIdx].axis[j].axis_b, (controller[iCtrlIdx].axis[j].axis_dir_b <= 0) ? '-' : '+' );
403 strcat(ParamString, Param);
405 if (controller[iCtrlIdx].axis[j].hat >= 0)
407 sprintf(Param, "hat(%i %s %s) ", controller[iCtrlIdx].axis[j].hat,
408 HAT_POS_NAME(controller[iCtrlIdx].axis[j].hat_pos_a),
409 HAT_POS_NAME(controller[iCtrlIdx].axis[j].hat_pos_b));
410 strcat(ParamString, Param);
413 Help = "Analog axis configuration mappings";
416 /* if last character is a space, chop it off */
417 len = strlen(ParamString);
418 if (len > 0 && ParamString[len-1] == ' ')
419 ParamString[len-1] = 0;
420 ConfigSetDefaultString(pConfig, button_names[X_AXIS + j], ParamString, Help);
425 static int setup_auto_controllers(int bPreConfig, int n64CtrlStart, int sdlCtrlIdx, const char *sdlJoyName, eModeType ControlMode[], eModeType OrigControlMode[], char DeviceName[][256])
427 char SectionName[32];
428 int ActiveControllers = 0;
431 /* create auto-config section(s) for this joystick (if this joystick is in the InputAutoConfig.ini) */
432 int ControllersFound = auto_set_defaults(sdlCtrlIdx, sdlJoyName);
433 if (ControllersFound == 0)
436 /* copy the auto-config settings to the controller config section, and load our plugin joystick config from this */
437 sprintf(SectionName, "Input-SDL-Control%i", n64CtrlStart + 1);
438 if (OrigControlMode[n64CtrlStart] == E_MODE_FULL_AUTO)
439 auto_copy_inputconfig("AutoConfig0", SectionName, sdlJoyName);
441 auto_copy_inputconfig("AutoConfig0", SectionName, NULL); // don't overwrite 'name' parameter if original mode was "named auto"
442 if (load_controller_config("AutoConfig0", n64CtrlStart, sdlCtrlIdx) > 0)
445 DebugMessage(M64MSG_INFO, "N64 Controller #%i: Using auto-config with SDL joystick %i ('%s')", n64CtrlStart+1, sdlCtrlIdx, sdlJoyName);
447 ConfigSaveSection(SectionName);
452 DebugMessage(M64MSG_ERROR, "Autoconfig data invalid for SDL joystick '%s'", sdlJoyName);
454 ConfigDeleteSection("AutoConfig0");
456 /* we have to handle the unfortunate case of a USB device mapping more than > 1 controller */
457 if (ControllersFound > 1)
459 for (j = 1; j < ControllersFound; j++)
461 char AutoSectionName[32];
462 sprintf(AutoSectionName, "AutoConfig%i", j);
463 /* if this would be > 4th controller, then just delete the auto-config */
464 if (n64CtrlStart + j >= 4)
466 ConfigDeleteSection(AutoSectionName);
469 /* look for another N64 controller that is in AUTO mode */
470 if (ControlMode[n64CtrlStart+j] == E_MODE_FULL_AUTO ||
471 (ControlMode[n64CtrlStart+j] == E_MODE_NAMED_AUTO && strncmp(DeviceName[n64CtrlStart+j], sdlJoyName, 255) == 0))
473 sprintf(SectionName, "Input-SDL-Control%i", n64CtrlStart + j + 1);
474 /* load our plugin joystick settings from the autoconfig */
475 if (load_controller_config(AutoSectionName, n64CtrlStart+j, sdlCtrlIdx) > 0)
477 /* copy the auto-config settings to the controller config section */
478 if (OrigControlMode[n64CtrlStart+j] == E_MODE_FULL_AUTO)
479 auto_copy_inputconfig(AutoSectionName, SectionName, sdlJoyName);
481 auto_copy_inputconfig(AutoSectionName, SectionName, NULL); // don't overwrite 'name' parameter if original mode was "named auto"
483 DebugMessage(M64MSG_INFO, "N64 Controller #%i: Using auto-config with SDL joystick %i ('%s')", n64CtrlStart+j+1, sdlCtrlIdx, sdlJoyName);
485 ConfigSaveSection(SectionName);
486 /* set the local controller mode to Manual so that we won't re-configure this controller in the next loop */
487 ControlMode[n64CtrlStart+j] = E_MODE_MANUAL;
492 DebugMessage(M64MSG_ERROR, "Autoconfig data invalid for SDL device '%s'", sdlJoyName);
494 /* delete the autoconfig section */
495 ConfigDeleteSection(AutoSectionName);
500 return ActiveControllers;
503 /* global functions */
505 /* There are 4 special section parameters: version, mode, device, and name. There are also 24 regular
506 * parameters: plugged, plugin, mouse, MouseSensitivity, DPad R/L/D/U, Start, Z/L/R Trigger, A/B button,
507 * C Button R/L/D/U, Mempak/Rumblepak switch, X/Y Axis, AnalogDeadzone, AnalogPeak.
509 * The N64 controller configuration behavior is regulated by the 'mode' parameter. If this parameter is
510 * 0 (Fully Manual), then all configuration data is loaded and parsed without changes or autoconfig from
511 * the configuration section. Any errors or warnings are printed. If the mode parameter is 1 (Auto with
512 * named SDL Device), then the code below searches through the available SDL joysticks for one which
513 * matches with the 'name' parameter in the config section. If a match is found, then the corresponding
514 * autoconfig is loaded and the regular parameters from the config section are updated with the
515 * autoconfig'ed settings. If the mode parameter is 2 (Fully Auto), then the code below searches through
516 * all available detected SDL joysticks to try and find an autoconfig for each. If an autoconfig match is
517 * found, then it is loaded and all config parameters (including device, name, and the regular parameters)
520 * This function is called with bPreConfig=true from the PluginStartup() function. The purpose of this
521 * call is to load the 4 configuration sections with autoconfig data if necessary for a GUI front-end.
522 * When we are called with bPreConfig=true, we should only print warning/error messages and not info/status.
523 * This function is also called right before running the ROM game from InitiateControllers() with
524 * bPreConfig=false. We should only print informational messages here, because we do not want to duplicate
525 * console output with the console-ui front-end (since the PluginStart() and InitiateControllers()
526 * functions are called in quick sequence from the console-ui).
529 void load_configuration(int bPreConfig)
531 char SectionName[32];
533 int n64CtrlIdx, sdlCtrlIdx, j;
534 int sdlNumDevUsed = 0;
535 int sdlDevicesUsed[4];
536 eModeType OrigControlMode[4], ControlMode[4];
537 int ControlDevice[4];
538 char DeviceName[4][256];
539 int ActiveControllers = 0;
540 int sdlNumJoysticks = get_sdl_num_joysticks();
541 float fVersion = 0.0f;
542 const char *sdl_name;
543 int ControllersFound = 0;
545 /* tell user how many SDL joysticks are available */
547 DebugMessage(M64MSG_INFO, "%i SDL joysticks were found.", sdlNumJoysticks);
549 /* loop through 4 N64 controllers, initializing and validating special section parameters */
550 for (n64CtrlIdx=0; n64CtrlIdx < 4; n64CtrlIdx++)
553 /* reset the controller configuration */
554 clear_controller(n64CtrlIdx);
556 /* Open the configuration section for this controller */
557 sprintf(SectionName, "Input-SDL-Control%i", n64CtrlIdx + 1);
558 if (ConfigOpenSection(SectionName, &pConfig) != M64ERR_SUCCESS)
560 // this should never happen
561 DebugMessage(M64MSG_ERROR, "Couldn't open config section '%s'. Aborting...", SectionName);
564 /* Check version number, and if it doesn't match: delete the config section */
566 if (ConfigGetParameter(pConfig, "version", M64TYPE_FLOAT, &fVersion, sizeof(float)) != M64ERR_SUCCESS || ((int) fVersion) != ((int) CONFIG_VERSION))
568 DebugMessage(M64MSG_WARNING, "Missing or incompatible config section '%s'. Clearing.", SectionName);
569 ConfigDeleteSection(SectionName);
570 // set local controller default parameters
571 OrigControlMode[n64CtrlIdx] = ControlMode[n64CtrlIdx] = E_MODE_FULL_AUTO;
572 ControlDevice[n64CtrlIdx] = DEVICE_NO_JOYSTICK;
573 DeviceName[n64CtrlIdx][0] = 0;
574 // write blank config for GUI front-ends
575 init_controller_config(n64CtrlIdx, "", E_MODE_FULL_AUTO);
576 // save it to the file too
577 ConfigSaveSection(SectionName);
581 if (ConfigGetParameter(pConfig, "mode", M64TYPE_INT, &OrigControlMode[n64CtrlIdx], sizeof(int)) != M64ERR_SUCCESS ||
582 (int) OrigControlMode[n64CtrlIdx] < 0 || (int) OrigControlMode[n64CtrlIdx] > 2)
585 DebugMessage(M64MSG_WARNING, "Missing or invalid 'mode' parameter in config section '%s'. Setting to 2 (Fully Auto)", SectionName);
586 OrigControlMode[n64CtrlIdx] = E_MODE_FULL_AUTO;
588 ControlMode[n64CtrlIdx] = OrigControlMode[n64CtrlIdx];
589 if (ConfigGetParameter(pConfig, "device", M64TYPE_INT, &ControlDevice[n64CtrlIdx], sizeof(int)) != M64ERR_SUCCESS)
592 DebugMessage(M64MSG_WARNING, "Missing 'device' parameter in config section '%s'. Setting to -1 (No joystick)", SectionName);
593 ControlDevice[n64CtrlIdx] = DEVICE_NO_JOYSTICK;
595 if (ConfigGetParameter(pConfig, "name", M64TYPE_STRING, DeviceName[n64CtrlIdx], 256) != M64ERR_SUCCESS)
597 DeviceName[n64CtrlIdx][0] = 0;
602 /* loop through 4 N64 controllers and set up those in Fully Manual mode */
603 for (n64CtrlIdx=0; n64CtrlIdx < 4; n64CtrlIdx++)
605 if (ControlMode[n64CtrlIdx] != E_MODE_MANUAL)
607 /* load the stored configuration (disregard any errors) */
608 sprintf(SectionName, "Input-SDL-Control%i", n64CtrlIdx + 1);
609 load_controller_config(SectionName, n64CtrlIdx, ControlDevice[n64CtrlIdx]);
610 /* if this config uses an SDL joystick, mark it as used */
611 if (ControlDevice[n64CtrlIdx] == DEVICE_NO_JOYSTICK)
614 DebugMessage(M64MSG_INFO, "N64 Controller #%i: Using manual config with no SDL joystick (keyboard/mouse only)", n64CtrlIdx+1);
618 sdlDevicesUsed[sdlNumDevUsed++] = ControlDevice[n64CtrlIdx];
620 DebugMessage(M64MSG_INFO, "N64 Controller #%i: Using manual config for SDL joystick %i", n64CtrlIdx+1, ControlDevice[n64CtrlIdx]);
625 /* now loop through again, setting up those in Named Auto mode */
626 for (n64CtrlIdx=0; n64CtrlIdx < 4; n64CtrlIdx++)
628 if (ControlMode[n64CtrlIdx] != E_MODE_NAMED_AUTO)
630 /* if name is empty, then use full auto mode instead */
631 if (DeviceName[n64CtrlIdx][0] == 0)
633 ControlMode[n64CtrlIdx] = E_MODE_FULL_AUTO;
636 sprintf(SectionName, "Input-SDL-Control%i", n64CtrlIdx + 1);
637 /* if user is looking for a keyboard, set that up */
638 if (strcasecmp(DeviceName[n64CtrlIdx], "Keyboard") == 0)
640 auto_set_defaults(DEVICE_NO_JOYSTICK, "Keyboard");
641 if (load_controller_config("AutoConfig0", n64CtrlIdx, DEVICE_NO_JOYSTICK) > 0)
644 DebugMessage(M64MSG_INFO, "N64 Controller #%i: Using auto-config for keyboard", n64CtrlIdx+1);
645 /* copy the auto-config settings to the controller config section */
646 auto_copy_inputconfig("AutoConfig0", SectionName, "Keyboard");
648 ConfigSaveSection(SectionName);
652 DebugMessage(M64MSG_ERROR, "Autoconfig keyboard setup invalid");
654 ConfigDeleteSection("AutoConfig0");
657 /* search for an unused SDL device with the matching name */
658 for (sdlCtrlIdx=0; sdlCtrlIdx < sdlNumJoysticks; sdlCtrlIdx++)
660 /* check if this one is in use */
661 int deviceAlreadyUsed = 0;
662 for (j = 0; j < sdlNumDevUsed; j++)
664 if (sdlDevicesUsed[j] == sdlCtrlIdx)
665 deviceAlreadyUsed = 1;
667 if (deviceAlreadyUsed)
669 /* check if the name matches */
670 sdl_name = get_sdl_joystick_name(sdlCtrlIdx);
671 if (sdl_name != NULL && strncmp(DeviceName[n64CtrlIdx], sdl_name, 255) == 0)
673 /* set up one or more controllers for this SDL device, if present in InputAutoConfig.ini */
674 int ControllersFound = setup_auto_controllers(bPreConfig, n64CtrlIdx, sdlCtrlIdx, sdl_name, ControlMode, OrigControlMode, DeviceName);
675 if (ControllersFound == 0)
677 // error: no auto-config found for this SDL device
678 DebugMessage(M64MSG_ERROR, "No auto-config found for joystick named '%s' in InputAutoConfig.ini", sdl_name);
679 // mark this device as being used just so we don't complain about it again
680 sdlDevicesUsed[sdlNumDevUsed++] = sdlCtrlIdx;
681 // quit looking for SDL joysticks which match the name, because there's no valid autoconfig for that name.
682 // this controller will be unused; skip to the next one
685 /* mark this sdl device as used */
686 sdlDevicesUsed[sdlNumDevUsed++] = sdlCtrlIdx;
687 ActiveControllers += ControllersFound;
691 /* if we didn't find a match for this joystick name, then set the controller to fully auto */
692 if (sdlCtrlIdx == sdlNumJoysticks)
695 DebugMessage(M64MSG_WARNING, "N64 Controller #%i: No SDL joystick found matching name '%s'. Using full auto mode.", n64CtrlIdx+1, DeviceName[n64CtrlIdx]);
696 ControlMode[n64CtrlIdx] = E_MODE_FULL_AUTO;
700 /* Final loop through N64 controllers, setting up those in Full Auto mode */
701 for (n64CtrlIdx=0; n64CtrlIdx < 4; n64CtrlIdx++)
703 if (ControlMode[n64CtrlIdx] != E_MODE_FULL_AUTO)
705 sprintf(SectionName, "Input-SDL-Control%i", n64CtrlIdx + 1);
706 /* search for an unused SDL device */
707 for (sdlCtrlIdx=0; sdlCtrlIdx < sdlNumJoysticks; sdlCtrlIdx++)
709 /* check if this one is in use */
710 int deviceAlreadyUsed = 0;
711 for (j = 0; j < sdlNumDevUsed; j++)
713 if (sdlDevicesUsed[j] == sdlCtrlIdx)
714 deviceAlreadyUsed = 1;
716 if (deviceAlreadyUsed)
718 /* set up one or more controllers for this SDL device, if present in InputAutoConfig.ini */
719 sdl_name = get_sdl_joystick_name(sdlCtrlIdx);
720 ControllersFound = setup_auto_controllers(bPreConfig, n64CtrlIdx, sdlCtrlIdx, sdl_name, ControlMode, OrigControlMode, DeviceName);
721 if (!bPreConfig && ControllersFound == 0)
723 // error: no auto-config found for this SDL device
724 DebugMessage(M64MSG_ERROR, "No auto-config found for joystick named '%s' in InputAutoConfig.ini", sdl_name);
725 // mark this device as being used just so we don't complain about it again
726 sdlDevicesUsed[sdlNumDevUsed++] = sdlCtrlIdx;
727 // keep trying more SDL devices to see if we can auto-config one for this N64 controller
730 /* mark this sdl device as used */
731 sdlDevicesUsed[sdlNumDevUsed++] = sdlCtrlIdx;
732 ActiveControllers += ControllersFound;
735 /* if this N64 controller was not activated, set device to -1 */
736 if (sdlCtrlIdx == sdlNumJoysticks)
739 if (ConfigOpenSection(SectionName, §ion) == M64ERR_SUCCESS)
741 const int iNoDevice = -1;
742 ConfigSetParameter(section, "device", M64TYPE_INT, &iNoDevice);
743 if (OrigControlMode[n64CtrlIdx] == E_MODE_FULL_AUTO)
744 ConfigSetParameter(section, "name", M64TYPE_STRING, "");
745 ConfigSaveSection(SectionName);
750 /* fallback to keyboard if no controllers were configured */
751 if (ActiveControllers == 0)
754 DebugMessage(M64MSG_INFO, "N64 Controller #1: Forcing default keyboard configuration");
755 auto_set_defaults(DEVICE_NO_JOYSTICK, "Keyboard");
756 if (load_controller_config("AutoConfig0", 0, DEVICE_NO_JOYSTICK) > 0)
758 /* copy the auto-config settings to the controller config section */
759 if (OrigControlMode[0] == E_MODE_FULL_AUTO)
760 auto_copy_inputconfig("AutoConfig0", "Input-SDL-Control1", "Keyboard");
762 auto_copy_inputconfig("AutoConfig0", "Input-SDL-Control1", NULL); // don't overwrite 'name' parameter
764 ConfigSaveSection("Input-SDL-Control1");
768 DebugMessage(M64MSG_ERROR, "Autoconfig keyboard setup invalid");
770 ConfigDeleteSection("AutoConfig0");
773 /* see how many joysticks are plugged in */
775 for (j = 0; j < 4; j++)
777 if (controller[j].control->Present)
781 /* print out summary info message */
786 DebugMessage(M64MSG_INFO, "%i controller(s) found, %i plugged in and usable in the emulator", ActiveControllers, joy_plugged);
790 if (ActiveControllers == 0)
791 DebugMessage(M64MSG_WARNING, "No joysticks/controllers found");
793 DebugMessage(M64MSG_WARNING, "%i controllers found, but none were 'plugged in'", ActiveControllers);