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 namespace simgear { class BufferedLogCallback; }
21 /** Nasal model data container.
22 * load and unload methods must be run in main thread (not thread-safe). */
23 class FGNasalModelData : public SGReferenced
26 /** Constructor to be run in an arbitrary thread. */
27 FGNasalModelData(SGPropertyNode *root, const string& path, SGPropertyNode *prop,
28 SGPropertyNode* load, SGPropertyNode* unload) :
30 _root(root), _prop(prop),
31 _load(load), _unload(unload)
35 /** Load hook. Always call from inside the main loop. */
38 /** Unload hook. Always call from inside the main loop. */
42 static unsigned int _module_id;
44 string _module, _path;
45 SGPropertyNode_ptr _root, _prop;
46 SGConstPropertyNode_ptr _load, _unload;
49 /** Thread-safe proxy for FGNasalModelData.
50 * modelLoaded/destroy methods only register the requested
51 * operation. Actual (un)loading of Nasal module is deferred
52 * and done in the main loop. */
53 class FGNasalModelDataProxy : public simgear::SGModelData
56 FGNasalModelDataProxy(SGPropertyNode *root = 0) :
61 ~FGNasalModelDataProxy();
63 void modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *);
66 SGPropertyNode_ptr _root;
67 SGSharedPtr<FGNasalModelData> _data;
70 SGPropertyNode* ghostToPropNode(naRef ref);
71 SGCondition* conditionGhost(naRef r);
73 class FGNasalSys : public SGSubsystem
77 virtual ~FGNasalSys();
79 virtual void update(double dt);
81 // Loads a nasal script from an external file and inserts it as a
82 // global module of the specified name.
83 bool loadModule(SGPath file, const char* moduleName);
85 // Simple hook to run arbitrary source code. Returns a bool to
86 // indicate successful execution. Does *not* return any Nasal
87 // values, because handling garbage-collected objects from C space
88 // is deep voodoo and violates the "simple hook" idea.
89 bool parseAndRun(const char* sourceCode);
91 // Slightly more complicated hook to get a handle to a precompiled
92 // Nasal script that can be invoked via a call() method. The
93 // caller is expected to delete the FGNasalScript returned from
94 // this function. The "name" argument specifies the "file name"
95 // for the source code that will be printed in Nasal stack traces
97 FGNasalScript* parseScript(const char* src, const char* name=0);
99 // Implementation of the settimer extension function
100 void setTimer(naContext c, int argc, naRef* args);
102 // Implementation of the setlistener extension function
103 naRef setListener(naContext c, int argc, naRef* args);
104 naRef removeListener(naContext c, int argc, naRef* args);
106 // Returns a ghost wrapper for the current _cmdArg
109 // Callbacks for command and timer bindings
110 virtual bool handleCommand( const char* moduleName,
111 const char* fileName,
113 const SGPropertyNode* arg = 0 );
114 virtual bool handleCommand(const SGPropertyNode* arg);
116 bool createModule(const char* moduleName, const char* fileName,
117 const char* src, int len, const SGPropertyNode* cmdarg=0,
118 int argc=0, naRef*args=0);
120 void deleteModule(const char* moduleName);
123 * Set member of specified hash to given value
125 void hashset(naRef hash, const char* key, naRef val);
128 * Set member of globals hash to given value
130 void globalsSet(const char* key, naRef val);
132 naRef call(naRef code, int argc, naRef* args, naRef locals);
134 naRef callMethod(naRef code, naRef self, int argc, naRef* args, naRef locals);
136 naRef propNodeGhost(SGPropertyNode* handle);
138 void registerToLoad(FGNasalModelData* data) { _loadList.push(data);}
139 void registerToUnload(FGNasalModelData* data) { _unloadList.push(data);}
141 // can't call this 'globals' due to naming clash
142 naRef nasalGlobals() const
145 naContext context() const
148 // This mechanism is here to allow naRefs to be passed to
149 // locations "outside" the interpreter. Normally, such a
150 // reference would be garbage collected unexpectedly. By passing
151 // it to gcSave and getting a key/handle, it can be cached in a
152 // globals.__gcsave hash. Be sure to release it with gcRelease
155 void gcRelease(int key);
157 /// retrive the associated log object, for displaying log
158 /// output somewhere (a UI, presumably)
159 simgear::BufferedLogCallback* log() const
162 friend class FGNasalScript;
163 friend class FGNasalListener;
164 friend class FGNasalModuleListener;
166 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _loadList;
167 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _unloadList;
170 // FGTimer subclass for handling Nasal timer callbacks.
171 // See the implementation of the settimer() extension function for
175 virtual void timerExpired();
176 virtual ~NasalTimer() {}
183 std::map<int, FGNasalListener *> _listener;
184 vector<FGNasalListener *> _dead_listener;
185 static int _listenerId;
187 void loadPropertyScripts();
188 void loadPropertyScripts(SGPropertyNode* n);
189 void loadScriptDirectory(simgear::Dir nasalDir);
190 void addModule(string moduleName, simgear::PathList scripts);
191 void logError(naContext);
192 naRef parse(const char* filename, const char* buf, int len);
193 naRef genPropsModule();
199 SGPropertyNode_ptr _cmdArg;
205 simgear::BufferedLogCallback* _log;
207 void handleTimer(NasalTimer* t);
211 class FGNasalScript {
213 ~FGNasalScript() { _nas->gcRelease(_gcKey); }
217 naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
218 return naGetError(_nas->_context) == 0;
222 friend class FGNasalSys;
229 class FGNasalListener : public SGPropertyChangeListener {
231 FGNasalListener(SGPropertyNode* node, naRef code, FGNasalSys* nasal,
232 int key, int id, int init, int type);
234 virtual ~FGNasalListener();
235 virtual void valueChanged(SGPropertyNode* node);
236 virtual void childAdded(SGPropertyNode* parent, SGPropertyNode* child);
237 virtual void childRemoved(SGPropertyNode* parent, SGPropertyNode* child);
240 bool changed(SGPropertyNode* node);
241 void call(SGPropertyNode* which, naRef mode);
243 friend class FGNasalSys;
244 SGPropertyNode_ptr _node;
251 unsigned int _active;
259 class NasalXMLVisitor : public XMLVisitor {
261 NasalXMLVisitor(naContext c, int argc, naRef* args);
262 virtual ~NasalXMLVisitor() { naFreeContext(_c); }
264 virtual void startElement(const char* tag, const XMLAttributes& a);
265 virtual void endElement(const char* tag);
266 virtual void data(const char* str, int len);
267 virtual void pi(const char* target, const char* data);
270 void call(naRef func, int num, naRef a = naNil(), naRef b = naNil());
271 naRef make_string(const char* s, int n = -1);
274 naRef _start_element, _end_element, _data, _pi;
277 #endif // __NASALSYS_HXX