+ BlitBitmapMasked(marker, drawto, 0, 0,
+ marker->width, marker->height,
+ marker_x, marker_y);
+
+ if (!screen_initialized)
+ FadeIn(REDRAW_FIELD);
+ else
+ BackToFront();
+
+ screen_initialized = TRUE;
+
+ while (NextValidEvent(&event))
+ {
+ switch (event.type)
+ {
+ case SDL_JOYAXISMOTION:
+ if (event.jaxis.value > 20000 ||
+ event.jaxis.value < -20000)
+ {
+ for (j = 0; j < i; j++)
+ if (steps[j].axis == event.jaxis.axis)
+ break;
+
+ if (j == i)
+ {
+ if (step->marker != MARKER_AXIS_X &&
+ step->marker != MARKER_AXIS_Y)
+ break;
+
+ step->axis = event.jaxis.axis;
+ strcat(mapping, step->field);
+ snprintf(temp, sizeof(temp), ":a%u,", event.jaxis.axis);
+ strcat(mapping, temp);
+ i++;
+ next = TRUE;
+ }
+ }
+
+ break;
+
+ case SDL_JOYHATMOTION:
+ /* ignore centering; we're probably just coming back
+ to the center from the previous item we set */
+ if (event.jhat.value == SDL_HAT_CENTERED)
+ break;
+
+ for (j = 0; j < i; j++)
+ if (steps[j].hat == event.jhat.hat &&
+ steps[j].hat_value == event.jhat.value)
+ break;
+
+ if (j == i)
+ {
+ step->hat = event.jhat.hat;
+ step->hat_value = event.jhat.value;
+ strcat(mapping, step->field);
+ snprintf(temp, sizeof(temp), ":h%u.%u,",
+ event.jhat.hat, event.jhat.value );
+ strcat(mapping, temp);
+ i++;
+ next = TRUE;
+ }
+
+ break;
+
+ case SDL_JOYBALLMOTION:
+ break;
+
+ case SDL_JOYBUTTONUP:
+ for (j = 0; j < i; j++)
+ if (steps[j].button == event.jbutton.button)
+ break;
+
+ if (j == i)
+ {
+ step->button = event.jbutton.button;
+ strcat(mapping, step->field);
+ snprintf(temp, sizeof(temp), ":b%u,", event.jbutton.button);
+ strcat(mapping, temp);
+ i++;
+ next = TRUE;
+ }
+
+ break;
+
+ case SDL_FINGERDOWN:
+ case SDL_MOUSEBUTTONDOWN:
+ /* skip this step */
+ i++;
+ next = TRUE;
+
+ break;
+
+ case SDL_KEYDOWN:
+ if (event.key.keysym.sym == KSYM_BackSpace ||
+ event.key.keysym.sym == KSYM_Back)
+ {
+ if (i == 0)
+ {
+ /* leave screen */
+ success = FALSE;
+ done = TRUE;
+
+ break;
+ }
+
+ /* undo this step */
+ prev_step = &steps[i - 1];
+ strcpy(mapping, prev_step->mapping);
+ i--;
+ next = TRUE;
+
+ break;
+ }
+
+ if (event.key.keysym.sym == KSYM_space ||
+ event.key.keysym.sym == KSYM_Return ||
+ event.key.keysym.sym == KSYM_Menu)
+ {
+ /* skip this step */
+ i++;
+ next = TRUE;
+
+ break;
+ }
+
+ if (event.key.keysym.sym == KSYM_Escape)
+ {
+ /* leave screen */
+ success = FALSE;
+ done = TRUE;
+ }
+
+ break;
+
+ case SDL_QUIT:
+ program.exit_function(0);
+ break;
+
+ default:
+ break;
+ }
+
+ // do not handle events for longer than standard frame delay period
+ if (DelayReached(&event_frame_delay, event_frame_delay_value))
+ break;
+ }
+ }
+ }
+
+ if (success)
+ {
+#if DEBUG_JOYSTICKS
+ Error(ERR_DEBUG, "New game controller mapping:\n\n%s\n\n", mapping);
+#endif
+
+ // activate mapping for this game
+ SDL_GameControllerAddMapping(mapping);
+
+ // save mapping to personal mappings
+ SaveSetup_AddGameControllerMapping(mapping);
+ }
+
+ /* wait until the last pending event was removed from event queue */
+ while (NextValidEvent(&event));
+
+ return success;
+#else
+ return TRUE;
+#endif
+}
+
+static int ConfigureJoystickMain(int player_nr)
+{
+ char *device_name = setup.input[player_nr].joy.device_name;
+ int joystick_nr = getJoystickNrFromDeviceName(device_name);
+ boolean joystick_active = CheckJoystickOpened(joystick_nr);
+ int success = FALSE;
+ int i;
+
+ if (joystick.status == JOYSTICK_NOT_AVAILABLE)
+ return JOYSTICK_NOT_AVAILABLE;
+
+ if (!joystick_active || !setup.input[player_nr].use_joystick)
+ return JOYSTICK_NOT_AVAILABLE;
+
+ FadeSetEnterMenu();
+ FadeOut(REDRAW_FIELD);
+
+ // close all joystick devices (potentially opened as game controllers)
+ for (i = 0; i < SDL_NumJoysticks(); i++)
+ SDLCloseJoystick(i);
+
+ // open joystick device as plain joystick to configure as game controller
+ SDL_Joystick *joystick = SDL_JoystickOpen(joystick_nr);
+
+ // as the joystick was successfully opened before, this should not happen
+ if (joystick == NULL)
+ return FALSE;
+
+ // create new game controller mapping (buttons and axes) for joystick device
+ success = ConfigureJoystickMapButtonsAndAxes(joystick);
+
+ // close joystick (and maybe re-open as configured game controller later)
+ SDL_JoystickClose(joystick);
+
+ // re-open all joystick devices (potentially as game controllers)
+ for (i = 0; i < SDL_NumJoysticks(); i++)
+ SDLOpenJoystick(i);
+
+ // clear all joystick input actions for all joystick devices
+ SDLClearJoystickState();
+
+ return (success ? JOYSTICK_CONFIGURED : JOYSTICK_NOT_CONFIGURED);
+}
+
+void ConfigureJoystick(int player_nr)
+{
+ boolean state = ConfigureJoystickMain(player_nr);
+
+ if (state != JOYSTICK_NOT_CONFIGURED)
+ {
+ boolean success = (state == JOYSTICK_CONFIGURED);
+ char message1[MAX_OUTPUT_LINESIZE + 1];
+ char *message2 = (success ? "configured!" : "not available!");
+ char *device_name = setup.input[player_nr].joy.device_name;
+ int nr = getJoystickNrFromDeviceName(device_name) + 1;
+ int font_nr = FONT_TITLE_1;
+ int font_height = getFontHeight(font_nr);
+ int ypos1 = SYSIZE / 2 - font_height * 2;
+ int ypos2 = SYSIZE / 2 - font_height * 1;
+ unsigned int wait_frame_delay = 0;
+ unsigned int wait_frame_delay_value = 2000;
+
+ ResetDelayCounter(&wait_frame_delay);