From: Wolfgang Becker Date: Wed, 30 Apr 2008 22:26:51 +0000 (+0000) Subject: Reinitialize SDL's joystick subsystem when scanning for sticks, so X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=519979665a5ce1b5aed256cefc1eacbf845953cd;p=supertux.git Reinitialize SDL's joystick subsystem when scanning for sticks, so hotpluging also works when no joystick was present on startup. SVN-Revision: 5392 --- diff --git a/src/control/joystickkeyboardcontroller.cpp b/src/control/joystickkeyboardcontroller.cpp index 300f1d896..4b475a792 100644 --- a/src/control/joystickkeyboardcontroller.cpp +++ b/src/control/joystickkeyboardcontroller.cpp @@ -46,6 +46,8 @@ public: void update_menu_item(Control id); virtual void menu_action(MenuItem* item); JoystickKeyboardController* controller; +private: + void recreateMenu(); }; class JoystickKeyboardController::KeyboardMenu : public Menu @@ -140,6 +142,9 @@ JoystickKeyboardController::updateAvailableJoysticks() SDL_JoystickClose(*i); } joysticks.clear(); + + SDL_QuitSubSystem(SDL_INIT_JOYSTICK); + SDL_InitSubSystem(SDL_INIT_JOYSTICK); int joystick_count = SDL_NumJoysticks(); min_joybuttons = -1; @@ -147,36 +152,38 @@ JoystickKeyboardController::updateAvailableJoysticks() max_joyaxis = -1; max_joyhats = -1; - for(int i = 0; i < joystick_count; ++i) { - SDL_Joystick* joystick = SDL_JoystickOpen(i); - bool good = true; - if(SDL_JoystickNumButtons(joystick) < 2) { - 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 << ": " << SDL_JoystickName(i) << " has less than 2 axes and no hat" << std::endl; - good = false; - } - if(!good) { - SDL_JoystickClose(joystick); - continue; - } + if( joystick_count > 0 ){ + for(int i = 0; i < joystick_count; ++i) { + SDL_Joystick* joystick = SDL_JoystickOpen(i); + bool good = true; + if(SDL_JoystickNumButtons(joystick) < 2) { + 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 << ": " << SDL_JoystickName(i) << " has less than 2 axes and no hat" << std::endl; + good = false; + } + if(!good) { + SDL_JoystickClose(joystick); + continue; + } - if(min_joybuttons < 0 || SDL_JoystickNumButtons(joystick) < min_joybuttons) - min_joybuttons = SDL_JoystickNumButtons(joystick); + if(min_joybuttons < 0 || SDL_JoystickNumButtons(joystick) < min_joybuttons) + min_joybuttons = SDL_JoystickNumButtons(joystick); - if(SDL_JoystickNumButtons(joystick) > max_joybuttons) - max_joybuttons = SDL_JoystickNumButtons(joystick); + if(SDL_JoystickNumButtons(joystick) > max_joybuttons) + max_joybuttons = SDL_JoystickNumButtons(joystick); - if(SDL_JoystickNumAxes(joystick) > max_joyaxis) - max_joyaxis = SDL_JoystickNumAxes(joystick); + if(SDL_JoystickNumAxes(joystick) > max_joyaxis) + max_joyaxis = SDL_JoystickNumAxes(joystick); - if(SDL_JoystickNumHats(joystick) > max_joyhats) - max_joyhats = SDL_JoystickNumHats(joystick); + if(SDL_JoystickNumHats(joystick) > max_joyhats) + max_joyhats = SDL_JoystickNumHats(joystick); - joysticks.push_back(joystick); + joysticks.push_back(joystick); + } } // some joysticks or SDL seem to produce some bogus events after being opened @@ -903,6 +910,16 @@ JoystickKeyboardController::JoystickMenu::JoystickMenu( JoystickKeyboardController* _controller) : controller(_controller) { + recreateMenu(); +} + +JoystickKeyboardController::JoystickMenu::~JoystickMenu() +{} + +void +JoystickKeyboardController::JoystickMenu::recreateMenu() +{ + clear(); add_label(_("Setup Joystick")); add_hl(); if(controller->joysticks.size() > 0) { @@ -924,14 +941,19 @@ JoystickKeyboardController::JoystickMenu::JoystickMenu( } add_deactive(-1,""); add_entry(SCAN_JOYSTICKS, _("Scan for Joysticks")); + + //Show Joysticks currently activated: + for(std::vector::iterator i = controller->joysticks.begin(); + i != controller->joysticks.end(); ++i) { + if(*i != 0) + add_deactive(-1, SDL_JoystickName(SDL_JoystickIndex(*i)) ); + } + add_hl(); add_back(_("Back")); update(); } -JoystickKeyboardController::JoystickMenu::~JoystickMenu() -{} - std::string JoystickKeyboardController::JoystickMenu::get_button_name(int button) { @@ -953,6 +975,7 @@ JoystickKeyboardController::JoystickMenu::menu_action(MenuItem* item) controller->jump_with_up_joy = item->toggled; } else if( item->id == SCAN_JOYSTICKS) { controller->updateAvailableJoysticks(); + recreateMenu(); } }