From: mfranz Date: Sun, 19 Jun 2005 17:23:41 +0000 (+0000) Subject: - fix bug that let bindings be executed before the nasal init X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=8027023862fc9ec6e1b2a12488ecdbec79ca0ec8;p=flightgear.git - fix bug that let bindings be executed before the nasal init block (this required to move most of init() into postinit()) - undo the "which" pseudo-namespace workaround, and do it proper: - let all nasal code in a joystick config file be executed in its own namespace. This allows to define functions in the init block that can be used throughout the driver file, and to write state variables in one binding, while reading them in other bindings, all without having to make sure that the values aren't lost in between, or collide with another joystick driver's code. For this the input subsystem creates a namespace for each joystick: "__js0" for joystick 0, etc. The two leading underscores are there to avoid collisions with user defined modules (similar to internal compiler variables). The namespace shouldn't be used explicitly, at least not in releases, and not from outside code, except for testing purposes, for which it can be useful. (If using the Cyborg joystick as js[0], you can read the modifier level as "__js0.modifier" from any nasal context.) --- diff --git a/src/Input/input.cxx b/src/Input/input.cxx index 29be98dc3..5bd2d0681 100644 --- a/src/Input/input.cxx +++ b/src/Input/input.cxx @@ -377,6 +377,7 @@ void FGInput::_init_keyboard () { SG_LOG(SG_INPUT, SG_DEBUG, "Initializing key bindings"); + _module[0] = 0; SGPropertyNode * key_nodes = fgGetNode("/input/keyboard"); if (key_nodes == 0) { SG_LOG(SG_INPUT, SG_WARN, "No key bindings (/input/keyboard)!!"); @@ -427,7 +428,6 @@ FGInput::_init_joystick () // TODO: zero the old bindings first. SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings"); SGPropertyNode * js_nodes = fgGetNode("/input/joysticks", true); - _which_joystick = js_nodes->getNode("which", true); // read all joystick xml files into /input/joysticks/js_named[1000++] SGPath path(globals->get_fg_root()); @@ -447,9 +447,11 @@ FGInput::_init_joystick () jsmap[names[j]->getStringValue()] = n; } + // set up js[] nodes for (int i = 0; i < MAX_JOYSTICKS; i++) { jsJoystick * js = new jsJoystick(i); _joystick_bindings[i].js = js; + if (js->notWorking()) { SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found"); continue; @@ -483,7 +485,25 @@ FGInput::_init_joystick () copyProperties(named, js_node); js_node->setStringValue("id", name); } + } + + // get rid of unused config nodes + for (unsigned int m = 0; m < js_named.size(); m++) + js_nodes->removeChild("js-named", js_named[m]->getIndex(), false); +} + + +void +FGInput::_postinit_joystick() +{ + FGNasalSys *nasalsys = (FGNasalSys *)globals->get_subsystem("nasal"); + SGPropertyNode *js_nodes = fgGetNode("/input/joysticks"); + for (int i = 0; i < MAX_JOYSTICKS; i++) { + SGPropertyNode_ptr js_node = js_nodes->getChild("js", i); + jsJoystick *js = _joystick_bindings[i].js; + if (!js_node || js->notWorking()) + continue; #ifdef WIN32 JOYCAPS jsCaps ; @@ -515,6 +535,19 @@ FGInput::_init_joystick () _joystick_bindings[i].axes = new axis[naxes]; _joystick_bindings[i].buttons = new button[nbuttons]; + // + // Initialize nasal groups. + // + string init; + init = "this=\"" + string(js_node->getPath()) + "\""; + sprintf(_module, "__js%d", i); + nasalsys->createModule(_module, _module, init.c_str(), init.size()); + + vector nasal = js_node->getChildren("nasal"); + for (unsigned int j = 0; j < nasal.size(); j++) { + nasal[j]->setStringValue("module", _module); + nasalsys->handleCommand(nasal[j]); + } // // Initialize the axes. @@ -596,24 +629,6 @@ FGInput::_init_joystick () js->setMaxRange(maxRange); js->setCenter(center); } - - for (unsigned int m = 0; m < js_named.size(); m++) - js_nodes->removeChild("js-named", js_named[m]->getIndex(), false); -} - - -void -FGInput::_postinit_joystick() -{ - SGPropertyNode *js_nodes = fgGetNode("/input/joysticks"); - vector js = js_nodes->getChildren("js"); - for (unsigned int i = 0; i < js.size(); i++) { - _which_joystick->setIntValue(i); - - vector nasal = js[i]->getChildren("nasal"); - for (unsigned int j = 0; j < nasal.size(); j++) - ((FGNasalSys*)globals->get_subsystem("nasal"))->handleCommand(nasal[j]); - } } @@ -642,6 +657,7 @@ void FGInput::_init_mouse () { SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings"); + _module[0] = 0; SGPropertyNode * mouse_nodes = fgGetNode("/input/mice"); if (mouse_nodes == 0) { @@ -754,7 +770,6 @@ FGInput::_update_joystick (double dt) if (js == 0 || js->notWorking()) continue; - _which_joystick->setIntValue(i); js->read(&buttons, axis_values); // Fire bindings for the axes. @@ -873,8 +888,11 @@ FGInput::_read_bindings (const SGPropertyNode * node, SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings"); vector bindings = node->getChildren("binding"); for (unsigned int i = 0; i < bindings.size(); i++) { - SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding " - << bindings[i]->getStringValue("command")); + const char *cmd = bindings[i]->getStringValue("command"); + SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding " << cmd); + + if (!strcmp(cmd, "nasal") && _module[0]) + bindings[i]->setStringValue("module", _module); binding_list[modifiers].push_back(new FGBinding(bindings[i])); } diff --git a/src/Input/input.hxx b/src/Input/input.hxx index 9b8b278dc..5a41a57a4 100644 --- a/src/Input/input.hxx +++ b/src/Input/input.hxx @@ -429,7 +429,10 @@ private: joystick _joystick_bindings[MAX_JOYSTICKS]; mouse _mouse_bindings[MAX_MICE]; - SGPropertyNode *_which_joystick; + /** + * Nasal module name/namespace. + */ + char _module[32]; }; #endif // _INPUT_HXX