Index: code/branches/gui/src/core/input/InputManager.cc
===================================================================
--- code/branches/gui/src/core/input/InputManager.cc	(revision 1638)
+++ code/branches/gui/src/core/input/InputManager.cc	(revision 1641)
@@ -91,5 +91,16 @@
     /**
     @brief
+        Destructor itself only called at the end of the program, after main.
+        Instance gets registered for destruction with atexit(.).
+    */
+    InputManager::~InputManager()
+    {
+        _destroy();
+    }
+
+    /**
+    @brief
         The one instance of the InputManager is stored in this function.
+        Only for internal use. Public Interface ist static.
     @return
         A reference to the only instance of the InputManager
@@ -99,13 +110,4 @@
         static InputManager theOnlyInstance;
         return theOnlyInstance;
-    }
-
-    /**
-    @brief
-        Destructor only called at the end of the program, after main.
-    */
-    InputManager::~InputManager()
-    {
-        _destroy();
     }
 
@@ -133,5 +135,5 @@
         {
             CCOUT(3) << "Initialising Input System..." << std::endl;
-            CCOUT(ORX_DEBUG) << "Initialising OIS components..." << std::endl;
+            CCOUT(4) << "Initialising OIS components..." << std::endl;
 
             OIS::ParamList paramList;
@@ -180,9 +182,4 @@
             CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl;
 
-            // InputManager holds the input buffer --> create one and add it.
-            //buffer_ = new InputBuffer();
-            //addKeyHandler(buffer_, "buffer");
-            //Shell::getInstance().setInputBuffer(buffer_);
-
             setConfigValues();
 
@@ -204,9 +201,9 @@
             _updateActiveStates();
 
-            CCOUT(ORX_DEBUG) << "Initialising complete." << std::endl;
+            CCOUT(3) << "Initialising complete." << std::endl;
         }
         else
         {
-            CCOUT(ORX_WARNING) << "Warning: OIS compoments already initialised, skipping" << std::endl;
+            CCOUT(2) << "Warning: OIS compoments already initialised, skipping" << std::endl;
         }
         return true;
@@ -333,4 +330,12 @@
     }
 
+    /**
+    @brief
+        Sets the size of all the different lists that are dependent on the number
+        of joy stick devices created.
+    @remarks
+        No matter whether there are a mouse and/or keyboard, they will always
+        occupy 2 places in the device number dependent lists.
+    */
     void InputManager::_redimensionLists()
     {
@@ -356,5 +361,5 @@
         activeStatesTop_.resize(devicesNum_);
 
-        // inform all registered states
+        // inform all states
         for (std::map<int, InputState*>::const_iterator it = inputStatesByPriority_.begin();
             it != inputStatesByPriority_.end(); ++it)
@@ -364,9 +369,10 @@
     /**
     @brief
-        Sets the configurable values. Use keybindings.ini as file..
+        Sets the configurable values.
+        This mainly concerns joy stick calibrations.
     */
     void InputManager::setConfigValues()
     {
-        if (joySticksSize_)
+        if (joySticksSize_ > 0)
         {
             std::vector<MultiTypeMath> coeffPos;
@@ -386,5 +392,5 @@
             if (!cont)
             {
-                cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), "CoeffPos", coeffPos);
+                cont = new ConfigValueContainer(CFT_Settings, getIdentifier(), "CoeffPos", coeffPos);
                 getIdentifier()->addConfigValueContainer("CoeffPos", cont);
             }
@@ -394,5 +400,5 @@
             if (!cont)
             {
-                cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), "CoeffNeg", coeffNeg);
+                cont = new ConfigValueContainer(CFT_Settings, getIdentifier(), "CoeffNeg", coeffNeg);
                 getIdentifier()->addConfigValueContainer("CoeffNeg", cont);
             }
@@ -402,5 +408,5 @@
             if (!cont)
             {
-                cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), "Zero", zero);
+                cont = new ConfigValueContainer(CFT_Settings, getIdentifier(), "Zero", zero);
                 getIdentifier()->addConfigValueContainer("Zero", cont);
             }
@@ -425,5 +431,6 @@
     /**
     @brief
-        Destroys all the created input devices and sets the InputManager to construction state.
+        Destroys all the created input devices. InputManager will be ready for a
+        new initialisation.
     */
     void InputManager::_destroy()
@@ -431,39 +438,49 @@
         if (inputSystem_)
         {
-            CCOUT(ORX_DEBUG) << "Destroying ..." << std::endl;
-
-            // kick all active states 'nicely'
-            for (std::map<int, InputState*>::reverse_iterator rit = activeStates_.rbegin();
-                rit != activeStates_.rend(); ++rit)
-                (*rit).second->onLeave();
-            activeStates_.clear();
-
-            // destroy our own states
-            stateEmpty_->removeAndDestroyAllHandlers();
-            stateCalibrator_->removeAndDestroyAllHandlers();
-            stateDetector_->removeAndDestroyAllHandlers();
-            _destroyState(stateEmpty_);
-            _destroyState(stateCalibrator_);
-            _destroyState(stateDetector_);
-            stateEmpty_ = 0;
-            stateCalibrator_ = 0;
-            stateDetector_ = 0;
-
-            // we don't remove the other states yet because the singleton might still exist.
-            // So people can still removeAndDestroy their states
-            //inputStatesByName_.clear();
-            //inputStatesByPriority_.clear();
-
-            // destroy the devices
-            _destroyKeyboard();
-            _destroyMouse();
-            _destroyJoySticks();
-
-            _redimensionLists();
-
-            OIS::InputManager::destroyInputSystem(inputSystem_);
-            inputSystem_ = 0;
-
-            CCOUT(ORX_DEBUG) << "Destroying done." << std::endl;
+            try
+            {
+                CCOUT(3) << "Destroying ..." << std::endl;
+
+                // clear our own states
+                stateEmpty_->removeAndDestroyAllHandlers();
+                stateCalibrator_->removeAndDestroyAllHandlers();
+                stateDetector_->removeAndDestroyAllHandlers();
+
+                // kick all active states 'nicely'
+                for (std::map<int, InputState*>::reverse_iterator rit = activeStates_.rbegin();
+                    rit != activeStates_.rend(); ++rit)
+                {
+                    (*rit).second->onLeave();
+                }
+                activeStates_.clear();
+                _updateActiveStates();
+
+                // destroy all input states
+                while (inputStatesByPriority_.size() > 0)
+                {
+                    _destroyState((*inputStatesByPriority_.rbegin()).second);
+                }
+
+                stateEmpty_ = 0;
+                stateCalibrator_ = 0;
+                stateDetector_ = 0;
+
+                // destroy the devices
+                _destroyKeyboard();
+                _destroyMouse();
+                _destroyJoySticks();
+
+                // 0 joy sticks now
+                _redimensionLists();
+
+                OIS::InputManager::destroyInputSystem(inputSystem_);
+                inputSystem_ = 0;
+
+                CCOUT(3) << "Destroying done." << std::endl;
+            }
+            catch (OIS::Exception& ex)
+            {
+                CCOUT(1) << "An exception has occured while destroying:\n" << ex.what() << std::endl;
+            }
         }
     }
@@ -479,5 +496,5 @@
         keyboard_ = 0;
         keysDown_.clear();
-        CCOUT(ORX_DEBUG) << "Keyboard destroyed." << std::endl;
+        CCOUT(4) << "Keyboard destroyed." << std::endl;
     }
 
@@ -492,5 +509,5 @@
         mouse_ = 0;
         mouseButtonsDown_.clear();
-        CCOUT(ORX_DEBUG) << "Mouse destroyed." << std::endl;
+        CCOUT(4) << "Mouse destroyed." << std::endl;
     }
 
@@ -510,5 +527,5 @@
             joySticks_.clear();
         }
-        CCOUT(ORX_DEBUG) << "Joy sticks destroyed." << std::endl;
+        CCOUT(4) << "Joy sticks destroyed." << std::endl;
     }
 
@@ -597,4 +614,9 @@
     }
 
+    /**
+    @brief
+        Updates the currently active states (according to activeStates_) for each device.
+        Also, a list of all active states (no duplicates!) is compiled for the general tick.
+    */
     void InputManager::_updateActiveStates()
     {
@@ -616,4 +638,8 @@
     }
 
+    /**
+    @brief
+        Processes the accumultated data for the joy stick calibration.
+    */
     void InputManager::_completeCalibration()
     {
@@ -676,4 +702,5 @@
         // restore old input state
         requestLeaveState("calibrator");
+        bCalibrating_ = false;
     }
 
@@ -822,4 +849,8 @@
     // ###### Joy Stick Events ######
 
+    /**
+    @brief
+        Returns the joy stick ID (orxonox) according to a OIS::JoyStickEvent
+    */
     inline unsigned int InputManager::_getJoystick(const OIS::JoyStickEvent& arg)
     {
@@ -839,12 +870,12 @@
 
         // check whether the button already is in the list (can happen when focus was lost)
-        std::vector<int>& buttonsDown = joyStickButtonsDown_[iJoyStick];
+        std::vector<JoyStickButton::Enum>& buttonsDown = joyStickButtonsDown_[iJoyStick];
         unsigned int iButton = 0;
         while (iButton < buttonsDown.size() && buttonsDown[iButton] != button)
             iButton++;
         if (iButton == buttonsDown.size())
-            buttonsDown.push_back(button);
-
-        activeStatesTop_[2 + iJoyStick]->joyStickButtonPressed(iJoyStick, button);
+            buttonsDown.push_back((JoyStickButton::Enum)button);
+
+        activeStatesTop_[2 + iJoyStick]->joyStickButtonPressed(iJoyStick, (JoyStickButton::Enum)button);
 
         return true;
@@ -856,5 +887,5 @@
 
         // remove the button from the joyStickButtonsDown_ list
-        std::vector<int>& buttonsDown = joyStickButtonsDown_[iJoyStick];
+        std::vector<JoyStickButton::Enum>& buttonsDown = joyStickButtonsDown_[iJoyStick];
         for (unsigned int iButton = 0; iButton < buttonsDown.size(); iButton++)
         {
@@ -866,9 +897,14 @@
         }
 
-        activeStatesTop_[2 + iJoyStick]->joyStickButtonReleased(iJoyStick, button);
+        activeStatesTop_[2 + iJoyStick]->joyStickButtonReleased(iJoyStick, (JoyStickButton::Enum)button);
 
         return true;
     }
 
+    /**
+    @brief
+        Calls the states for a particular axis with our enumeration.
+        Used by OIS sliders and OIS axes.
+    */
     void InputManager::_fireAxis(unsigned int iJoyStick, int axis, int value)
     {
@@ -919,4 +955,5 @@
 
         // translate the POV into 8 simple buttons
+
         int lastState = povStates_[iJoyStick][id];
         if (lastState & OIS::Pov::North)
@@ -959,19 +996,4 @@
     }
 
-    bool InputManager::initialiseKeyboard()
-    {
-        return _getInstance()._initialiseKeyboard();
-    }
-
-    bool InputManager::initialiseMouse()
-    {
-        return _getInstance()._initialiseMouse();
-    }
-
-    bool InputManager::initialiseJoySticks()
-    {
-        return _getInstance()._initialiseJoySticks();
-    }
-
     int InputManager::numberOfKeyboards()
     {
@@ -1030,19 +1052,4 @@
     {
         _getInstance()._destroy();
-    }
-
-    void InputManager::destroyKeyboard()
-    {
-        return _getInstance()._destroyKeyboard();
-    }
-
-    void InputManager::destroyMouse()
-    {
-        return _getInstance()._destroyMouse();
-    }
-
-    void InputManager::destroyJoySticks()
-    {
-        return _getInstance()._destroyJoySticks();
     }
 
@@ -1068,4 +1075,14 @@
     }
 
+    /**
+    @brief
+        Method for easily storing a string with the command executor. It is used by the
+        KeyDetector to get assign commands. The KeyDetector simply executes
+        the command 'storeKeyStroke myName' for each button/axis.
+    @remarks
+        This is only a temporary hack until we thourouhgly support multiple KeyBinders.
+    @param name
+        The name of the button/axis.
+    */
     void InputManager::storeKeyStroke(const std::string& name)
     {
@@ -1075,4 +1092,11 @@
     }
 
+    /**
+    @brief
+        Assigns a command string to a key/button/axis. The name is determined via KeyDetector
+        and InputManager::storeKeyStroke(.).
+    @param command
+        Command string that can be executed by the CommandExecutor
+    */
     void InputManager::keyBind(const std::string& command)
     {
@@ -1082,6 +1106,11 @@
     }
 
+    /**
+    @brief
+        Starts joy stick calibration.
+    */
     void InputManager::calibrate()
     {
+        _getInstance().bCalibrating_ = true;
         requestEnterState("calibrator");
     }
@@ -1101,6 +1130,8 @@
     @param name
         Unique name of the handler.
+    @param priority
+        Unique integer number. Higher means more prioritised.
     @return
-        True if added, false if name already existed.
+        True if added, false if name or priority already existed.
     */
     bool InputManager::_configureInputState(InputState* state, const std::string& name, int priority)
@@ -1134,4 +1165,8 @@
     }
 
+    /**
+    @brief
+        Returns a new SimpleInputState and configures it first.
+    */
     SimpleInputState* InputManager::createSimpleInputState(const std::string &name, int priority)
     {
@@ -1146,4 +1181,8 @@
     }
 
+    /**
+    @brief
+        Returns a new ExtendedInputState and configures it first.
+    */
     ExtendedInputState* InputManager::createExtendedInputState(const std::string &name, int priority)
     {
@@ -1160,9 +1199,11 @@
     /**
     @brief
-        Removes a Key handler from the list.
+        Removes an input state internally.
     @param name
-        Unique name of the handler.
+        Name of the handler.
     @return
         True if removal was successful, false if name was not found.
+    @remarks
+        You can't remove the internal states "empty", "calibrator" and "detector".
     */
     bool InputManager::destroyState(const std::string& name)
@@ -1184,7 +1225,7 @@
     /**
     @brief
-        Returns the pointer to a handler.
+        Returns the pointer to the requested InputState.
     @param name
-        Unique name of the handler.
+        Unique name of the state.
     @return
         Pointer to the instance, 0 if name was not found.
@@ -1201,7 +1242,7 @@
     /**
     @brief
-        Returns the current input handling method
+        Returns the current input state (there might be others active too!)
     @return
-        The current input mode.
+        The current highest prioritised active input state.
     */
     InputState* InputManager::getCurrentState()
@@ -1212,7 +1253,8 @@
     /**
     @brief
-        Enables a specific key handler that has already been added.
+        Activates a specific input state.
+        It might not be really activated if the priority is too low!
     @param name
-        Unique name of the handler.
+        Unique name of the state.
     @return
         False if name was not found, true otherwise.
@@ -1230,4 +1272,12 @@
     }
 
+    /**
+    @brief
+        Deactivates a specific input state.
+    @param name
+        Unique name of the state.
+    @return
+        False if name was not found, true otherwise.
+    */
     bool InputManager::requestLeaveState(const std::string& name)
     {
