]> git.mxchange.org Git - flightgear.git/blobdiff - src/Joystick/joystick.cxx
David Megginson writes:
[flightgear.git] / src / Joystick / joystick.cxx
index 76f2f636a991f2c1d17ae251fa92c544bc4fd951..1aa232627726c0c7dbc549d7925821cb8b145fe1 100644 (file)
@@ -21,6 +21,7 @@
 //
 // $Id$
 
+#include <simgear/compiler.h>
 
 #ifdef HAVE_CONFIG_H
 #  include <config.h>
 
 #include <string>
 
-#include <simgear/misc/props.hxx>
+#include <Main/fg_props.hxx>
+
 #include <simgear/debug/logstream.hxx>
 #include <plib/js.h>
+
 #include "joystick.hxx"
 
-using std::string;
+FG_USING_STD(string);
+FG_USING_STD(cout);
 
 #ifdef WIN32
 static const int MAX_JOYSTICKS = 2;
@@ -111,8 +115,13 @@ struct button {
 struct joystick {
     virtual ~joystick () {
        delete js;
+#ifndef macintosh
        delete axes;
        delete buttons;
+#else
+       delete[] axes;
+       delete[] buttons;
+#endif
     }
     int naxes;
     int nbuttons;
@@ -128,101 +137,6 @@ struct joystick {
 static joystick joysticks[MAX_JOYSTICKS];
 
 
-/**
- * Set up default values if properties are not specified.
- */
-static void
-setupDefaults ()
-{
-    SGPropertyList &props = current_properties;
-
-    // Default axis 0 to aileron
-    if (!props.getValue("/input/js0/axis0/control")) {
-       props.setStringValue("/input/js0/axis0/control", "/controls/aileron");
-       props.setDoubleValue("/input/js0/axis0/dead-band", 0.1);
-    }
-
-    // Default axis 1 to elevator
-    if (!props.getValue("/input/js0/axis1/control")) {
-       props.setStringValue("/input/js0/axis1/control", "/controls/elevator");
-       props.setDoubleValue("/input/js0/axis1/dead-band", 0.1);
-       props.setDoubleValue("/input/js0/axis1/factor", -1.0);
-    }
-
-    // Default axis 2 to rudder
-    if (!props.getValue("/input/js0/axis2/control")) {
-       props.setStringValue("/input/js0/axis2/control", "/controls/rudder");
-       props.setDoubleValue("/input/js0/axis2/dead-band", 0.1);
-    }
-
-    // Default axis 3 to throttle
-    // We need to fiddle with the offset
-    // and factor to make it work
-    if (!props.getValue("/input/js0/axis3/control")) {
-       props.setStringValue("/input/js0/axis3/control", "/controls/throttle");
-       props.setDoubleValue("/input/js0/axis3/dead-band", 0.0);
-       props.setDoubleValue("/input/js0/axis3/offset", -1.0);
-       props.setDoubleValue("/input/js0/axis3/factor", -0.5);
-    }
-
-    // Default button 0 to all brakes
-    if (!props.getValue("/input/js0/button0/control")) {
-       props.setStringValue("/input/js0/button0/action", "switch");
-       props.setStringValue("/input/js0/button0/control", "/controls/brakes/all");
-       props.setDoubleValue("/input/js0/button0/step", 1.0);
-       props.setDoubleValue("/input/js0/button0/repeatable", false);
-    }
-
-    // Default button 1 to left brake.
-    if (!props.getValue("/input/js0/button1/control")) {
-       props.setStringValue("/input/js0/button1/action", "switch");
-       props.setStringValue("/input/js0/button1/control",
-                            "/controls/brakes/left");
-       props.setDoubleValue("/input/js0/button1/step", 1.0);
-       props.setDoubleValue("/input/js0/button1/repeatable", false);
-    }
-
-    // Default button 2 to right brake.
-    if (!props.getValue("/input/js0/button2/control")) {
-       props.setStringValue("/input/js0/button2/action", "switch");
-       props.setStringValue("/input/js0/button2/control",
-                            "/controls/brakes/right");
-       props.setDoubleValue("/input/js0/button2/step", 1.0);
-       props.setDoubleValue("/input/js0/button2/repeatable", false);
-    }
-
-    // Default buttons 3 and 4 to elevator trim
-    if (!props.getValue("/input/js0/button3/control")) {
-       props.setStringValue("/input/js0/button3/action", "adjust");
-       props.setStringValue("/input/js0/button3/control",
-                            "/controls/elevator-trim");
-       props.setDoubleValue("/input/js0/button3/step", 0.001);
-       props.setBoolValue("/input/js0/button3/repeatable", true);
-    }
-    if (!props.getValue("/input/js0/button4/control")) {
-       props.setStringValue("/input/js0/button4/action", "adjust");
-       props.setStringValue("/input/js0/button4/control",
-                            "/controls/elevator-trim");
-       props.setDoubleValue("/input/js0/button4/step", -0.001);
-       props.setBoolValue("/input/js0/button4/repeatable", true);
-    }
-
-    // Default buttons 5 and 6 to flaps
-    if (!props.getValue("/input/js0/button5/control")) {
-       props.setStringValue("/input/js0/button5/action", "adjust");
-       props.setStringValue("/input/js0/button5/control", "/controls/flaps");
-       props.setDoubleValue("/input/js0/button5/step", -0.34);
-       props.setBoolValue("/input/js0/button5/repeatable", false);
-    }
-    if (!props.getValue("/input/js0/button6/control")) {
-       props.setStringValue("/input/js0/button6/action", "adjust");
-       props.setStringValue("/input/js0/button6/control", "/controls/flaps");
-       props.setDoubleValue("/input/js0/button6/step", 0.34);
-       props.setBoolValue("/input/js0/button6/repeatable", false);
-    }
-}
-
-
 /**
  * Initialize any joysticks found.
  */
@@ -233,8 +147,6 @@ fgJoystickInit()
 
     FG_LOG(FG_INPUT, FG_INFO, "Initializing joysticks");
 
-    setupDefaults();
-
     for (int i = 0; i < MAX_JOYSTICKS; i++) {
        jsJoystick * js = new jsJoystick(i);
        joysticks[i].js = js;
@@ -288,19 +200,19 @@ fgJoystickInit()
            // Control property
            string name = base;
            name += "/control";
-           SGValue * value = current_properties.getValue(name);
+           SGValue * value = fgGetValue(name);
            if (value == 0) {
                FG_LOG(FG_INPUT, FG_INFO, "    no control defined");
                continue;
            }
            const string &control = value->getStringValue();
-           a.value = current_properties.getValue(control, true);
+           a.value = fgGetValue(control, true);
            FG_LOG(FG_INPUT, FG_INFO, "    using control " << control);
 
            // Dead band
            name = base;
            name += "/dead-band";
-           value = current_properties.getValue(name);
+           value = fgGetValue(name);
            if (value != 0)
                js->setDeadBand(j, value->getDoubleValue());
            FG_LOG(FG_INPUT, FG_INFO, "    dead-band is " << js->getDeadBand(j));
@@ -308,7 +220,7 @@ fgJoystickInit()
            // Offset
            name = base;
            name += "/offset";
-           value = current_properties.getValue(name);
+           value = fgGetValue(name);
            if (value != 0)
                a.offset = value->getDoubleValue();
            FG_LOG(FG_INPUT, FG_INFO, "    offset is " << a.offset);
@@ -317,7 +229,7 @@ fgJoystickInit()
            // Factor
            name = base;
            name += "/factor";
-           value = current_properties.getValue(name);
+           value = fgGetValue(name);
            if (value != 0)
                a.factor = value->getDoubleValue();
            FG_LOG(FG_INPUT, FG_INFO, "    factor is " << a.factor);
@@ -326,7 +238,7 @@ fgJoystickInit()
            // Tolerance
            name = base;
            name += "/tolerance";
-           value = current_properties.getValue(name);
+           value = fgGetValue(name);
            if (value != 0)
                a.tolerance = value->getDoubleValue();
            FG_LOG(FG_INPUT, FG_INFO, "    tolerance is " << a.tolerance);
@@ -335,7 +247,7 @@ fgJoystickInit()
            // Saturation
            name = base;
            name += "/saturation";
-           value = current_properties.getValue(name);
+           value = fgGetValue(name);
            if (value != 0)
                js->setSaturation(j, value->getDoubleValue());
            FG_LOG(FG_INPUT, FG_INFO, "    saturation is " << js->getSaturation(j));
@@ -343,7 +255,7 @@ fgJoystickInit()
            // Minimum range
            name = base;
            name += "/min-range";
-           value = current_properties.getValue(name);
+           value = fgGetValue(name);
            if (value != 0)
                minRange[j] = value->getDoubleValue();
            FG_LOG(FG_INPUT, FG_INFO, "    min-range is " << minRange[j]);
@@ -351,7 +263,7 @@ fgJoystickInit()
            // Maximum range
            name = base;
            name += "/max-range";
-           value = current_properties.getValue(name);
+           value = fgGetValue(name);
            if (value != 0)
                maxRange[j] = value->getDoubleValue();
            FG_LOG(FG_INPUT, FG_INFO, "    max-range is " << maxRange[j]);
@@ -359,7 +271,7 @@ fgJoystickInit()
            // Center
            name = base;
            name += "/center";
-           value = current_properties.getValue(name);
+           value = fgGetValue(name);
            if (value != 0)
                center[j] = value->getDoubleValue();
            FG_LOG(FG_INPUT, FG_INFO, "    center is " << center[j]);
@@ -382,19 +294,19 @@ fgJoystickInit()
            string name = base;
            name += "/control";
            cout << "Trying name " << name << endl;
-           SGValue * value = current_properties.getValue(name);
+           SGValue * value = fgGetValue(name);
            if (value == 0) {
                FG_LOG(FG_INPUT, FG_INFO, "    no control defined");
                continue;
            }
            const string &control = value->getStringValue();
-           b.value = current_properties.getValue(control, true);
+           b.value = fgGetValue(control, true);
            FG_LOG(FG_INPUT, FG_INFO, "    using control " << control);
 
            // Step
            name = base;
            name += "/step";
-           value = current_properties.getValue(name);
+           value = fgGetValue(name);
            if (value != 0)
                b.step = value->getDoubleValue();
            FG_LOG(FG_INPUT, FG_INFO, "    step is " << b.step);
@@ -402,7 +314,7 @@ fgJoystickInit()
            // Type
            name = base;
            name += "/action";
-           value = current_properties.getValue(name);
+           value = fgGetValue(name);
            string action = "adjust";
            if (value != 0)
                action = value->getStringValue();
@@ -426,7 +338,7 @@ fgJoystickInit()
            // Repeatability.
            name = base;
            name += "/repeatable";
-           value = current_properties.getValue(name);
+           value = fgGetValue(name);
            if (value != 0)
                b.isRepeatable = value->getBoolValue();
            FG_LOG(FG_INPUT, FG_INFO, (b.isRepeatable ?
@@ -497,7 +409,7 @@ fgJoystickRead()
        // Buttons
        //
        for (j = 0; j < joysticks[i].nbuttons; j++) {
-           bool flag;
+           bool flag = false;
            button &b = joysticks[i].buttons[j];
            if (b.value == 0)
                continue;
@@ -543,15 +455,14 @@ fgJoystickRead()
                    continue;
 
                switch (b.action) {
+               case button::ADJUST:
                case button::TOGGLE:
                    // no op
+                   flag = true;
                    break;
                case button::SWITCH:
                    flag = b.value->setDoubleValue(0.0);
                    break;
-               case button::ADJUST:
-                   // no op
-                   break;
                default:
                    flag = false;
                    break;