]> git.mxchange.org Git - flightgear.git/blobdiff - src/Input/FGJoystickInput.cxx
toggle fullscreen: also adapt GUI plane when resizing
[flightgear.git] / src / Input / FGJoystickInput.cxx
index 516679f25136565358ccbfbde3d1ac1c0b542f8a..049ae7c2f1c6f84a8aa76eddcdcb34838cc0a55e 100644 (file)
 //
 // $Id$
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
 #include "FGJoystickInput.hxx"
 
 #include <simgear/props/props_io.hxx>
 #include "FGDeviceConfigurationMap.hxx"
 #include <Main/fg_props.hxx>
 #include <Scripting/NasalSys.hxx>
+#include <boost/foreach.hpp>
 
 using simgear::PropertyList;
 
@@ -86,7 +91,7 @@ void FGJoystickInput::_remove(bool all)
     for (int i = 0; i < MAX_JOYSTICKS; i++)
     {
         // do not remove predefined joysticks info on reinit
-        if ((all)||(!bindings[i].predefined))
+        if (all || (!bindings[i].predefined))
             js_nodes->removeChild("js", i, false);
         if (bindings[i].js)
             delete bindings[i].js;
@@ -99,8 +104,9 @@ void FGJoystickInput::init()
   jsInit();
   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
   SGPropertyNode_ptr js_nodes = fgGetNode("/input/joysticks", true);
+  status_node = fgGetNode("/devices/status/joysticks", true);
 
-  FGDeviceConfigurationMap configMap("Input/Joysticks", js_nodes, "js-named");
+  FGDeviceConfigurationMap configMap("Input/Joysticks",js_nodes, "js-named");
 
   for (int i = 0; i < MAX_JOYSTICKS; i++) {
     jsJoystick * js = new jsJoystick(i);
@@ -122,11 +128,12 @@ void FGJoystickInput::init()
       SG_LOG(SG_INPUT, SG_INFO, "Looking for bindings for joystick \"" << name << '"');
       SGPropertyNode_ptr named;
 
-      if ((named = configMap[name])) {
+      if (configMap.hasConfiguration(name)) {
+        named = configMap.configurationForDeviceName(name);
         string source = named->getStringValue("source", "user defined");
         SG_LOG(SG_INPUT, SG_INFO, "... found joystick: " << source);
 
-      } else if ((named = configMap["default"])) {
+      } else if ((named = configMap.configurationForDeviceName("default"))) {
         string source = named->getStringValue("source", "user defined");
         SG_LOG(SG_INPUT, SG_INFO, "No config found for joystick \"" << name
             << "\"\nUsing default: \"" << source << '"');
@@ -193,7 +200,7 @@ void FGJoystickInput::postinit()
     //
     // Initialize nasal groups.
     //
-    ostringstream str;
+    std::ostringstream str;
     str << "__js" << i;
     string module = str.str();
     nasalsys->createModule(module.c_str(), module.c_str(), "", 0);
@@ -261,30 +268,27 @@ void FGJoystickInput::postinit()
     // Initialize the buttons.
     //
     PropertyList buttons = js_node->getChildren("button");
-    char buf[32];
-    for (j = 0; j < buttons.size() && j < nbuttons; j++) {
-      const SGPropertyNode * button_node = buttons[j];
-      const SGPropertyNode * num_node = button_node->getChild("number");
+    BOOST_FOREACH( SGPropertyNode * button_node, buttons ) {
       size_t n_but = button_node->getIndex();
-      if (num_node != 0) {
+
+      const SGPropertyNode * num_node = button_node->getChild("number");
+      if (NULL != num_node)
           n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
-      }
 
       if (n_but >= nbuttons) {
           SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but);
           continue;
       }
 
-      sprintf(buf, "%u", (unsigned)n_but);
-      SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
-      bindings[i].buttons[n_but].init(button_node, buf, module );
+      std::ostringstream buf;
+      buf << (unsigned)n_but;
 
-      // get interval-sec property
+      SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << buf.str());
       FGButton &b = bindings[i].buttons[n_but];
-      if (button_node != 0) {
-        b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
-        b.last_dt = 0.0;
-      }
+      b.init(button_node, buf.str(), module );
+      // get interval-sec property
+      b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
+      b.last_dt = 0.0;
     }
 
     js->setMinRange(minRange);
@@ -308,6 +312,16 @@ void FGJoystickInput::update( double dt )
     js->read(&buttons, axis_values);
     if (js->notWorking()) // If js is disconnected
       continue;
+            
+    // Update device status    
+    SGPropertyNode_ptr status = status_node->getChild("joystick", i, true);
+    for (int j = 0; j < MAX_JOYSTICK_AXES; j++) {
+      status->getChild("axis", j, true)->setFloatValue(axis_values[j]);
+    }
+    
+    for (int j = 0; j < MAX_JOYSTICK_BUTTONS; j++) {
+      status->getChild("button", j, true)->setBoolValue((buttons & (1u << j)) > 0 );
+    }
 
                                 // Fire bindings for the axes.
     for (int j = 0; j < bindings[i].naxes; j++) {