00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <iostream>
00012
00013 #include "jshandler.h"
00014 #include "jsscripting.h"
00015 #include "jsenvironment.h"
00016
00017 using namespace VRUT;
00018 using namespace std;
00019
00020 JSHandler::JSHandler(JSScriptingModule *_module)
00021 {
00022 module = _module;
00023 jsEnv = new JSEnvironment(this);
00024 }
00025
00026 JSHandler::~JSHandler()
00027 {
00028 delete jsEnv;
00029 }
00030
00031 bool JSHandler::Sleep(unsigned long milis)
00032 {
00033 unsigned long t;
00034 if (milis > TIMEOUT_SLEEP)
00035 {
00036 t = milis % TIMEOUT_SLEEP;
00037 int steps = milis / TIMEOUT_SLEEP;
00038 for (int i = 0; i < steps; i++)
00039 {
00040 wxThread::Sleep(TIMEOUT_SLEEP);
00041 if (IsEnd())
00042 return false;
00043 }
00044 }
00045 else
00046 t = milis;
00047
00048 if (t)
00049 wxThread::Sleep(t);
00050
00051 return true;
00052 }
00053
00054 wxCommandEvent *JSHandler::WaitForEvent(Event::EVENT_TYPE type, unsigned long milis, bool waitFromNow)
00055 {
00056 wxCommandEvent *pevt = NULL;
00057 MessageQueueError err;
00058 size_t size;
00059
00060 if (waitFromNow)
00061 {
00063 size = module->evtQueue.GetSize();
00064 for (unsigned i = 0; i < size; i++)
00065 {
00066 wxCommandEvent evt1 = module->evtQueue.ReceiveTimeout(0, err);
00067 if (err)
00068 return NULL;
00069 ProcessEvent(evt1);
00070 }
00071 }
00072
00073 bool unlimited = (milis == 0);
00074 while (unlimited || milis > 0)
00075 {
00076 size = module->evtQueue.GetSize();
00077 for (unsigned i = 0; i < size; i++)
00078 {
00079 wxCommandEvent evt2 = module->evtQueue.ReceiveTimeout(0, err);
00080
00081 if (err)
00082 return NULL;
00083 if (evt2.GetEventType() == type)
00084 return new wxCommandEvent(evt2);
00085 ProcessEvent(evt2);
00086 }
00087
00088 wxThread::Sleep(TIMEOUT_WAITING);
00089 if (IsEnd())
00090 return NULL;
00091
00092 if (!unlimited)
00093 milis -= TIMEOUT_WAITING;
00094 }
00095
00096 return NULL;
00097 }
00098
00099 void JSHandler::PostEvent(wxCommandEvent & evt)
00100 {
00101 module->PostEvent(evt);
00102 }
00103
00104 void JSHandler::PostToKernel(wxCommandEvent & evt)
00105 {
00106 module->PostToKernel(evt);
00107 }
00108
00109 void JSHandler::ProcessEvent(wxCommandEvent & evt)
00110 {
00111 if (evt.GetEventType() == Event::EVT_PARAM_SET)
00112 {
00113 if (IS_PARAM(evt, module->sceneParamID))
00114 {
00115 long val;
00116 if (((wxString *) evt.GetClientData())->AfterFirst('=').ToLong(&val) && val >= 0)
00117 module->sceneID = (SCENE_ID) val;
00118 else
00119 module->sceneID = SCENE_ID_NONE;
00120 }
00121 else if (IS_PARAM(evt, module->runParamID))
00122 executions.push(RunData(module->scriptPath, module->sceneID));
00123 else
00124 UPDATE_PARAM_FROM_EVENT_STRING(module->pathParamID, module->scriptPath, evt);
00125 }
00126 }
00127
00128 bool JSHandler::RunScript()
00129 {
00130 if (executions.empty())
00131 return false;
00132 RunData data = executions.front();
00133 executions.pop();
00134 jsEnv->RunScript(module->kernel, data.scriptPath.ToAscii(), data.sceneID);
00135 return true;
00136 }
00137
00139 bool JSHandler::IsEnd()
00140 {
00141 return module->TestExit();
00142 }