4 #include <simgear/misc/sg_path.hxx>
5 #include <simgear/structure/subsystem_mgr.hxx>
6 #include <simgear/misc/sg_dir.hxx>
7 #include <simgear/nasal/nasal.h>
8 #include <simgear/scene/model/modellib.hxx>
9 #include <simgear/xml/easyxml.hxx>
10 #include <simgear/threads/SGQueue.hxx>
16 class FGNasalListener;
19 /** Nasal model data container.
20 * load and unload methods must be run in main thread (not thread-safe). */
21 class FGNasalModelData : public SGReferenced
24 /** Constructor to be run in an arbitrary thread. */
25 FGNasalModelData(SGPropertyNode *root, const string& path, SGPropertyNode *prop,
26 SGPropertyNode* load, SGPropertyNode* unload) :
28 _root(root), _prop(prop),
29 _load(load), _unload(unload)
33 /** Load hook. Always call from inside the main loop. */
36 /** Unload hook. Always call from inside the main loop. */
40 static unsigned int _module_id;
42 string _module, _path;
43 SGPropertyNode_ptr _root, _prop;
44 SGConstPropertyNode_ptr _load, _unload;
47 /** Thread-safe proxy for FGNasalModelData.
48 * modelLoaded/destroy methods only register the requested
49 * operation. Actual (un)loading of Nasal module is deferred
50 * and done in the main loop. */
51 class FGNasalModelDataProxy : public simgear::SGModelData
54 FGNasalModelDataProxy(SGPropertyNode *root = 0) :
59 ~FGNasalModelDataProxy();
61 void modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *);
64 SGPropertyNode_ptr _root;
65 SGSharedPtr<FGNasalModelData> _data;
68 class FGNasalSys : public SGSubsystem
72 virtual ~FGNasalSys();
74 virtual void update(double dt);
76 // Loads a nasal script from an external file and inserts it as a
77 // global module of the specified name.
78 bool loadModule(SGPath file, const char* moduleName);
80 // Simple hook to run arbitrary source code. Returns a bool to
81 // indicate successful execution. Does *not* return any Nasal
82 // values, because handling garbage-collected objects from C space
83 // is deep voodoo and violates the "simple hook" idea.
84 bool parseAndRun(const char* sourceCode);
86 // Slightly more complicated hook to get a handle to a precompiled
87 // Nasal script that can be invoked via a call() method. The
88 // caller is expected to delete the FGNasalScript returned from
89 // this function. The "name" argument specifies the "file name"
90 // for the source code that will be printed in Nasal stack traces
92 FGNasalScript* parseScript(const char* src, const char* name=0);
94 // Implementation of the settimer extension function
95 void setTimer(naContext c, int argc, naRef* args);
97 // Implementation of the setlistener extension function
98 naRef setListener(naContext c, int argc, naRef* args);
99 naRef removeListener(naContext c, int argc, naRef* args);
101 // Returns a ghost wrapper for the current _cmdArg
104 // Callbacks for command and timer bindings
105 virtual bool handleCommand(const SGPropertyNode* arg);
107 bool createModule(const char* moduleName, const char* fileName,
108 const char* src, int len, const SGPropertyNode* cmdarg=0,
109 int argc=0, naRef*args=0);
111 void deleteModule(const char* moduleName);
113 naRef call(naRef code, int argc, naRef* args, naRef locals);
114 naRef propNodeGhost(SGPropertyNode* handle);
116 void registerToLoad(FGNasalModelData* data) { _loadList.push(data);}
117 void registerToUnload(FGNasalModelData* data) { _unloadList.push(data);}
120 friend class FGNasalScript;
121 friend class FGNasalListener;
122 friend class FGNasalModuleListener;
124 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _loadList;
125 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _unloadList;
128 // FGTimer subclass for handling Nasal timer callbacks.
129 // See the implementation of the settimer() extension function for
133 virtual void timerExpired();
134 virtual ~NasalTimer() {}
141 std::map<int, FGNasalListener *> _listener;
142 vector<FGNasalListener *> _dead_listener;
143 static int _listenerId;
145 void loadPropertyScripts();
146 void loadPropertyScripts(SGPropertyNode* n);
147 void loadScriptDirectory(simgear::Dir nasalDir);
148 void addModule(string moduleName, simgear::PathList scripts);
149 void hashset(naRef hash, const char* key, naRef val);
150 void logError(naContext);
151 naRef parse(const char* filename, const char* buf, int len);
152 naRef genPropsModule();
154 // This mechanism is here to allow naRefs to be passed to
155 // locations "outside" the interpreter. Normally, such a
156 // reference would be garbage collected unexpectedly. By passing
157 // it to gcSave and getting a key/handle, it can be cached in a
158 // globals.__gcsave hash. Be sure to release it with gcRelease
161 void gcRelease(int key);
166 SGPropertyNode_ptr _cmdArg;
172 public: void handleTimer(NasalTimer* t);
176 class FGNasalScript {
178 ~FGNasalScript() { _nas->gcRelease(_gcKey); }
182 naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
183 return naGetError(_nas->_context) == 0;
187 friend class FGNasalSys;
194 class FGNasalListener : public SGPropertyChangeListener {
196 FGNasalListener(SGPropertyNode* node, naRef code, FGNasalSys* nasal,
197 int key, int id, int init, int type);
199 virtual ~FGNasalListener();
200 virtual void valueChanged(SGPropertyNode* node);
201 virtual void childAdded(SGPropertyNode* parent, SGPropertyNode* child);
202 virtual void childRemoved(SGPropertyNode* parent, SGPropertyNode* child);
205 bool changed(SGPropertyNode* node);
206 void call(SGPropertyNode* which, naRef mode);
208 friend class FGNasalSys;
209 SGPropertyNode_ptr _node;
216 unsigned int _active;
224 class NasalXMLVisitor : public XMLVisitor {
226 NasalXMLVisitor(naContext c, int argc, naRef* args);
227 virtual ~NasalXMLVisitor() { naFreeContext(_c); }
229 virtual void startElement(const char* tag, const XMLAttributes& a);
230 virtual void endElement(const char* tag);
231 virtual void data(const char* str, int len);
232 virtual void pi(const char* target, const char* data);
235 void call(naRef func, int num, naRef a = naNil(), naRef b = naNil());
236 naRef make_string(const char* s, int n = -1);
239 naRef _start_element, _end_element, _data, _pi;
242 #endif // __NASALSYS_HXX