void TXT_ConfigureJoystickAxis(txt_joystick_axis_t *joystick_axis, int using_button, txt_joystick_axis_callback_t callback) { // Open the joystick first. if (SDL_Init(SDL_INIT_JOYSTICK) < 0) { return; } joystick_axis->joystick = SDL_JoystickOpen(joystick_index); if (joystick_axis->joystick == NULL) { TXT_MessageBox(NULL, "Please configure a controller first!"); return; } SDL_JoystickEventState(SDL_ENABLE); // Build the prompt window. joystick_axis->config_window = TXT_NewWindow("Gamepad/Joystick calibration"); TXT_AddWidgets(joystick_axis->config_window, TXT_NewStrut(0, 1), joystick_axis->config_label = TXT_NewLabel(""), TXT_NewStrut(0, 1), NULL); TXT_SetWindowAction(joystick_axis->config_window, TXT_HORIZ_LEFT, NULL); TXT_SetWindowAction(joystick_axis->config_window, TXT_HORIZ_CENTER, TXT_NewWindowAbortAction(joystick_axis->config_window)); TXT_SetWindowAction(joystick_axis->config_window, TXT_HORIZ_RIGHT, NULL); TXT_SetWidgetAlign(joystick_axis->config_window, TXT_HORIZ_CENTER); if (using_button >= 0) { joystick_axis->config_stage = CONFIG_STAGE1; joystick_axis->config_button = using_button; IdentifyBadAxes(joystick_axis); } else { joystick_axis->config_stage = CONFIG_CENTER; } SetCalibrationLabel(joystick_axis); // Close the joystick and shut down joystick subsystem when the window // is closed. TXT_SignalConnect(joystick_axis->config_window, "closed", CalibrateWindowClosed, joystick_axis); TXT_SDL_SetEventCallback(EventCallback, joystick_axis); // When successfully calibrated, invoke this callback: joystick_axis->callback = callback; }
static int EventCallback(SDL_Event *event, TXT_UNCAST_ARG(joystick_axis)) { TXT_CAST_ARG(txt_joystick_axis_t, joystick_axis); boolean advance; if (event->type != SDL_JOYBUTTONDOWN) { return 0; } // At this point, we have a button press. // In the first "center" stage, we're just trying to work out which // joystick is being configured and which button the user is pressing. if (joystick_axis->config_stage == CONFIG_CENTER) { joystick_axis->config_button = event->jbutton.button; IdentifyBadAxes(joystick_axis); // Advance to next stage. joystick_axis->config_stage = CONFIG_STAGE1; SetCalibrationLabel(joystick_axis); return 1; } // In subsequent stages, the user is asked to push in a specific // direction and press the button. They must push the same button // as they did before; this is necessary to support button axes. if (event->jbutton.which == SDL_JoystickInstanceID(joystick_axis->joystick) && event->jbutton.button == joystick_axis->config_button) { switch (joystick_axis->config_stage) { default: case CONFIG_STAGE1: advance = CalibrateAxis(joystick_axis); break; case CONFIG_STAGE2: advance = SetButtonAxisPositive(joystick_axis); break; } // Advance to the next calibration stage? if (advance) { joystick_axis->config_stage = NextCalibrateStage(joystick_axis); SetCalibrationLabel(joystick_axis); // Finished? if (joystick_axis->config_stage == CONFIG_CENTER) { TXT_CloseWindow(joystick_axis->config_window); if (joystick_axis->callback != NULL) { joystick_axis->callback(); } } return 1; } } return 0; }
static int CalibrationEventCallback(SDL_Event *event, void *user_data) { boolean advance; if (event->type != SDL_JOYBUTTONDOWN) { return 0; } // At this point, we have a button press. // In the first "center" stage, we're just trying to work out which // joystick is being configured and which button the user is pressing. if (calibrate_stage == CALIBRATE_CENTER) { joystick_index = event->jbutton.which; calibrate_button = event->jbutton.button; IdentifyBadAxes(); // If the joystick is a known one, auto-load default // config for it. if (IsKnownJoystick(joystick_index)) { LoadKnownConfiguration(); usejoystick = 1; TXT_CloseWindow(calibration_window); } else { // Advance to next stage. calibrate_stage = CALIBRATE_LEFT; SetCalibrationLabel(); } return 1; } // In subsequent stages, the user is asked to push in a specific // direction and press the button. They must push the same button // as they did before; this is necessary to support button axes. if (event->jbutton.which == joystick_index && event->jbutton.button == calibrate_button) { switch (calibrate_stage) { default: case CALIBRATE_LEFT: advance = CalibrateAxis(&joystick_x_axis, &joystick_x_invert); break; case CALIBRATE_RIGHT: advance = SetButtonAxisPositive(&joystick_x_axis); break; case CALIBRATE_UP: advance = CalibrateAxis(&joystick_y_axis, &joystick_y_invert); break; case CALIBRATE_DOWN: advance = SetButtonAxisPositive(&joystick_y_axis); break; } // Advance to the next calibration stage? if (advance) { calibrate_stage = NextCalibrateStage(); SetCalibrationLabel(); // Finished? if (calibrate_stage == CALIBRATE_CENTER) { usejoystick = 1; TXT_CloseWindow(calibration_window); } return 1; } } return 0; }