X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fcontrol%2Fjoystickkeyboardcontroller.cpp;h=e3bba1be2aa78173528da2c3456d0dd48d2ee256;hb=38105c22495d9439b30221732dd5d7b89f328a0c;hp=300f1d896867dbfaa5033de78b0ec60070c481c0;hpb=45b706a8a4ab73a8095a7f75f3646272b9d962e7;p=supertux.git diff --git a/src/control/joystickkeyboardcontroller.cpp b/src/control/joystickkeyboardcontroller.cpp index 300f1d896..e3bba1be2 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 @@ -80,9 +82,9 @@ JoystickKeyboardController::JoystickKeyboardController() keymap[SDLK_KP_ENTER] = MENU_SELECT; keymap[SDLK_CARET] = CONSOLE; keymap[SDLK_DELETE] = PEEK_LEFT; - keymap[SDLK_END] = PEEK_RIGHT; - keymap[SDLK_PAGEUP] = PEEK_UP; - keymap[SDLK_PAGEDOWN] = PEEK_DOWN; + keymap[SDLK_PAGEDOWN] = PEEK_RIGHT; + keymap[SDLK_HOME] = PEEK_UP; + keymap[SDLK_END] = PEEK_DOWN; jump_with_up_joy = false; jump_with_up_kbd = false; @@ -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 @@ -403,7 +410,7 @@ JoystickKeyboardController::process_axis_event(const SDL_JoyAxisEvent& jaxis) else { // Split the axis into left and right, so that both can be - // mapped seperatly (needed for jump/down vs up/down) + // mapped separately (needed for jump/down vs up/down) int axis = jaxis.axis + 1; AxisMap::iterator left = joy_axis_map.find(-axis); @@ -507,7 +514,7 @@ JoystickKeyboardController::process_key_event(const SDL_Event& event) process_menu_key_event(event); } else if(key_mapping == keymap.end()) { // default action: update controls - log_debug << "Key " << event.key.keysym.sym << " is unbound" << std::endl; + //log_debug << "Key " << event.key.keysym.sym << " is unbound" << std::endl; } else { Control control = key_mapping->second; controls[control] = (event.type == SDL_KEYDOWN); @@ -560,7 +567,7 @@ JoystickKeyboardController::process_console_key_event(const SDL_Event& event) default: int c = event.key.keysym.unicode; if ((c >= 32) && (c <= 126)) { - Console::instance->input((char)c); + Console::instance->input((char)c); } break; } @@ -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) { @@ -920,18 +937,23 @@ JoystickKeyboardController::JoystickMenu::JoystickMenu( add_toggle(Controller::CONTROLCOUNT, _("Jump with Up"), controller->jump_with_up_joy); } else { - add_deactive(-1, _("No Joysticks found")); + add_inactive(-1, _("No Joysticks found")); } - add_deactive(-1,""); + add_inactive(-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_inactive(-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(); } }