From: Wolfgang Becker Date: Tue, 29 Apr 2008 20:37:09 +0000 (+0000) Subject: Add option to scan for joysticks to the joystick setup. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=45b706a8a4ab73a8095a7f75f3646272b9d962e7;p=supertux.git Add option to scan for joysticks to the joystick setup. SVN-Revision: 5390 --- diff --git a/src/control/joystickkeyboardcontroller.cpp b/src/control/joystickkeyboardcontroller.cpp index 305ce1ebd..300f1d896 100644 --- a/src/control/joystickkeyboardcontroller.cpp +++ b/src/control/joystickkeyboardcontroller.cpp @@ -31,6 +31,10 @@ #include "console.hpp" #include "gameconfig.hpp" +namespace{ + const int SCAN_JOYSTICKS = Controller::CONTROLCOUNT + 1; +} + class JoystickKeyboardController::JoystickMenu : public Menu { public: @@ -83,6 +87,60 @@ JoystickKeyboardController::JoystickKeyboardController() jump_with_up_joy = false; jump_with_up_kbd = false; + updateAvailableJoysticks(); + + dead_zone = 1000; + + // Default joystick button configuration + joy_button_map[0] = JUMP; + joy_button_map[1] = ACTION; + // 6 or more Buttons + if( min_joybuttons > 5 ){ + joy_button_map[4] = PEEK_LEFT; + joy_button_map[5] = PEEK_RIGHT; + // 8 or more + if(min_joybuttons > 7) + joy_button_map[min_joybuttons-1] = PAUSE_MENU; + } else { + // map the last 2 buttons to menu and pause + if(min_joybuttons > 2) + joy_button_map[min_joybuttons-1] = PAUSE_MENU; + // map all remaining joystick buttons to MENU_SELECT + for(int i = 2; i < max_joybuttons; ++i) { + if(i != min_joybuttons-1) + joy_button_map[i] = MENU_SELECT; + } + } + + // Default joystick axis configuration + joy_axis_map[-1] = LEFT; + joy_axis_map[ 1] = RIGHT; + joy_axis_map[-2] = UP; + joy_axis_map[ 2] = DOWN; +} + +JoystickKeyboardController::~JoystickKeyboardController() +{ + for(std::vector::iterator i = joysticks.begin(); + i != joysticks.end(); ++i) { + if(*i != 0) + SDL_JoystickClose(*i); + } + + delete key_options_menu; + delete joystick_options_menu; +} + +void +JoystickKeyboardController::updateAvailableJoysticks() +{ + for(std::vector::iterator i = joysticks.begin(); + i != joysticks.end(); ++i) { + if(*i != 0) + SDL_JoystickClose(*i); + } + joysticks.clear(); + int joystick_count = SDL_NumJoysticks(); min_joybuttons = -1; max_joybuttons = -1; @@ -93,12 +151,12 @@ JoystickKeyboardController::JoystickKeyboardController() SDL_Joystick* joystick = SDL_JoystickOpen(i); bool good = true; if(SDL_JoystickNumButtons(joystick) < 2) { - log_info << "Joystick " << i << " has less than 2 buttons" << std::endl; + log_info << "Joystick " << i << ": " << SDL_JoystickName(i) << " has less than 2 buttons" << std::endl; good = false; } if(SDL_JoystickNumAxes(joystick) < 2 && SDL_JoystickNumHats(joystick) == 0) { - log_info << "Joystick " << i << " has less than 2 axes and no hat" << std::endl; + log_info << "Joystick " << i << ": " << SDL_JoystickName(i) << " has less than 2 axes and no hat" << std::endl; good = false; } if(!good) { @@ -121,35 +179,6 @@ JoystickKeyboardController::JoystickKeyboardController() joysticks.push_back(joystick); } - dead_zone = 1000; - - // Default joystick button configuration - joy_button_map[0] = JUMP; - joy_button_map[1] = ACTION; - // 6 or more Buttons - if( min_joybuttons > 5 ){ - joy_button_map[4] = PEEK_LEFT; - joy_button_map[5] = PEEK_RIGHT; - // 8 or more - if(min_joybuttons > 7) - joy_button_map[min_joybuttons-1] = PAUSE_MENU; - } else { - // map the last 2 buttons to menu and pause - if(min_joybuttons > 2) - joy_button_map[min_joybuttons-1] = PAUSE_MENU; - // map all remaining joystick buttons to MENU_SELECT - for(int i = 2; i < max_joybuttons; ++i) { - if(i != min_joybuttons-1) - joy_button_map[i] = MENU_SELECT; - } - } - - // Default joystick axis configuration - joy_axis_map[-1] = LEFT; - joy_axis_map[ 1] = RIGHT; - joy_axis_map[-2] = UP; - joy_axis_map[ 2] = DOWN; - // some joysticks or SDL seem to produce some bogus events after being opened Uint32 ticks = SDL_GetTicks(); while(SDL_GetTicks() - ticks < 200) { @@ -158,18 +187,6 @@ JoystickKeyboardController::JoystickKeyboardController() } } -JoystickKeyboardController::~JoystickKeyboardController() -{ - for(std::vector::iterator i = joysticks.begin(); - i != joysticks.end(); ++i) { - if(*i != 0) - SDL_JoystickClose(*i); - } - - delete key_options_menu; - delete joystick_options_menu; -} - void JoystickKeyboardController::read(const lisp::Lisp& lisp) { @@ -846,7 +863,7 @@ JoystickKeyboardController::KeyboardMenu::menu_action(MenuItem* item) controller->wait_for_key = item->id; } else if( item->id == Controller::CONTROLCOUNT) { controller->jump_with_up_kbd = item->toggled; - } + } } void @@ -905,6 +922,8 @@ JoystickKeyboardController::JoystickMenu::JoystickMenu( } else { add_deactive(-1, _("No Joysticks found")); } + add_deactive(-1,""); + add_entry(SCAN_JOYSTICKS, _("Scan for Joysticks")); add_hl(); add_back(_("Back")); update(); @@ -932,6 +951,8 @@ JoystickKeyboardController::JoystickMenu::menu_action(MenuItem* item) controller->wait_for_joystick = item->id; } else if (item->id == Controller::CONTROLCOUNT) { controller->jump_with_up_joy = item->toggled; + } else if( item->id == SCAN_JOYSTICKS) { + controller->updateAvailableJoysticks(); } } diff --git a/src/control/joystickkeyboardcontroller.hpp b/src/control/joystickkeyboardcontroller.hpp index 9b6a39fe7..cd85533af 100644 --- a/src/control/joystickkeyboardcontroller.hpp +++ b/src/control/joystickkeyboardcontroller.hpp @@ -45,6 +45,7 @@ public: Menu* get_key_options_menu(); Menu* get_joystick_options_menu(); + void updateAvailableJoysticks(); private: void process_key_event(const SDL_Event& event);