Add option to scan for joysticks to the joystick setup.
authorWolfgang Becker <uafr@gmx.de>
Tue, 29 Apr 2008 20:37:09 +0000 (20:37 +0000)
committerWolfgang Becker <uafr@gmx.de>
Tue, 29 Apr 2008 20:37:09 +0000 (20:37 +0000)
SVN-Revision: 5390

src/control/joystickkeyboardcontroller.cpp
src/control/joystickkeyboardcontroller.hpp

index 305ce1e..300f1d8 100644 (file)
 #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<SDL_Joystick*>::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<SDL_Joystick*>::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<SDL_Joystick*>::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();
   }
 }
 
index 9b6a39f..cd85533 100644 (file)
@@ -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);