2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 * @file FUiEffects_RuntimeLuaProcessing.cpp
19 * @brief This file contains an implementation of LuaProcessing class methods
23 #include <FBaseSysLog.h>
24 #include <FBaseErrors.h>
25 #include <FUiEffects_EffectImpl.h>
26 #include <FUiEffects_EffectManagerImpl.h>
27 #include <FUiEffects_LoggingProfiler.h>
29 #include <../runtime/lua-cpp-binding/tolua.h>
30 #include "FUiEffects_RuntimeLuaProcessing.h"
31 #include "../FUiEffects_EffectErrorMessages.h"
35 namespace Tizen { namespace Ui { namespace Effects { namespace _Runtime
38 const char* LuaProcessing::__pStringLua = " function _MyFunction510693810(scene_) \n"
42 LuaProcessing::LuaProcessing(void)
48 LuaProcessing::LuaProcessing(const char* pPathToScript, IEffectModelScript* pEModel)
52 std::unique_ptr<lua_State, LuaClose> pLua(luaL_newstate(), LuaClose());
53 SysTryReturnVoidResult(NID_UI_EFFECT, pLua.get() != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
55 luaL_openlibs(pLua.get());
56 tolua__open(pLua.get());
58 static const char* errorString;
59 switch (luaL_loadfile(pLua.get(), pPathToScript))
62 SysLog(NID_UI_EFFECT, "LuaProcessing: File %s was opened successfully", pPathToScript);
65 SysLogException(NID_UI_EFFECT, E_FILE_NOT_FOUND, "LuaProcessing: Can not open/read the file %s", pPathToScript);
68 errorString = lua_tostring(pLua.get(), -1);
69 errorString = (errorString == null ? "error null" : errorString);
70 SysLogException(NID_UI_EFFECT, E_PARSING_FAILED, "LuaProcessing: There is syntax error during pre-compilation of file %s: %s", pPathToScript, errorString);
73 SysLogException(NID_UI_EFFECT, E_OUT_OF_MEMORY, "LuaProcessing: There is memory allocation error while opening the file %s", pPathToScript);
76 SysLogException(NID_UI_EFFECT, E_FILE_NOT_FOUND, "LuaProcessing: There is unknown error while opening the file %s", pPathToScript);
80 result r = GetLastResult();
81 SysTryReturnVoidResult(NID_UI_EFFECT, r == E_SUCCESS, r, GetErrorMessage(r));
83 int res = CallFunction("LuaProcessing", 0, LUA_MULTRET, pLua.get());
84 SysTryReturnVoidResult(NID_UI_EFFECT, res == 0, GetLastResult(), GetErrorMessage(GetLastResult()));
86 std::unique_ptr<EffectModelScript> pEMScript(new (std::nothrow) EffectModelScript(pEModel));
87 SysTryReturnVoidResult(NID_UI_EFFECT, pEMScript.get() != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
89 //loading and performing the string __pStringLua
90 int resLoadString = luaL_dostring(pLua.get(), __pStringLua);
91 SysAssertf(resLoadString == 0, _UiEffectError::INTERNAL_ERROR);
92 lua_getfield(pLua.get(), LUA_GLOBALSINDEX, "_MyFunction510693810");
93 tolua_pushusertype(pLua.get(), (void*)pEMScript.get(), "EffectModelScript");
94 int resCallMyFun = lua_pcall(pLua.get(), 1, 0, 0);
95 SysAssertf(resCallMyFun == 0, _UiEffectError::INTERNAL_ERROR);
97 __pLua = std::move(pLua);
98 __pEMScript = std::move(pEMScript);
101 LuaProcessing::~LuaProcessing(void)
107 LuaProcessing::OnTouchEvent(const char* pFunName, const TouchEventScript& position)
109 lua_getfield(__pLua.get(), LUA_GLOBALSINDEX, pFunName);
110 tolua_pushusertype(__pLua.get(), (void*)&position, "TouchEventScript");
111 return (CallFunction(pFunName, 1, 0, __pLua.get()) == 0);
115 LuaProcessing::OnEffectStart(const EffectsVector<float>& effectStartInfo)
117 lua_getfield(__pLua.get(), LUA_GLOBALSINDEX, "OnEffectStart");
119 bool isVectorEmpty = false;
120 EffectsVector<float> effectStartInfoTemp(effectStartInfo);
121 if (effectStartInfo.empty())
123 isVectorEmpty = true;
124 effectStartInfoTemp.push_back(0.f);
127 tolua_pushusertype(__pLua.get(), (void*)&effectStartInfoTemp, "EffectsVector<float>");
131 effectStartInfoTemp.clear();
134 return (CallFunction("OnEffectStart", 1, 0, __pLua.get()) == 0);
138 LuaProcessing::OnEffectCalculate(float dt)
140 lua_getfield(__pLua.get(), LUA_GLOBALSINDEX, "OnEffectCalculate");
141 lua_pushnumber(__pLua.get(), dt);
142 return (CallFunction("OnEffectCalculate", 1, 0, __pLua.get()) == 0);
146 LuaProcessing::OnTouchPressed(const TouchEventScript &position)
148 return OnTouchEvent("OnTouchPressed", position);
152 LuaProcessing::OnTouchDoublePressed(const TouchEventScript &position)
154 return OnTouchEvent("OnTouchDoublePressed", position);
158 LuaProcessing::OnTouchMoved(const TouchEventScript& position)
160 return OnTouchEvent("OnTouchMoved", position);
164 LuaProcessing::OnTouchReleased(const TouchEventScript& position)
166 return OnTouchEvent("OnTouchReleased", position);
170 LuaProcessing::IsEffectFinished(void)
172 EffectResultS effectRes;
174 lua_getfield(__pLua.get(), LUA_GLOBALSINDEX, "IsEffectFinished");
175 if (CallFunction("IsEffectFinished", 0, LUA_MULTRET, __pLua.get()) != 0)
177 effectRes.effectResult = EFFECT_RESULT_ERROR;
181 int returnsCount = lua_gettop(__pLua.get());
182 effectRes.lastShownBitmapsId.clear();
183 if (returnsCount == 0)
185 effectRes.effectResult = EFFECT_RESULT_ERROR;
188 LUA_NUMBER res = lua_tonumber(__pLua.get(), 1);
189 for (int i = 2; i <= returnsCount; ++i)
191 effectRes.lastShownBitmapsId.push_back(lua_tonumber(__pLua.get(), i));
193 lua_pop(__pLua.get(), returnsCount);
195 _EffectResult tempRes = static_cast<_EffectResult>(res);
198 case EFFECT_FINISHED:
199 effectRes.effectResult = EFFECT_RESULT_FINISHED;
201 case EFFECT_CONTINUED:
202 effectRes.effectResult = static_cast<EffectResult>(0);
205 effectRes.effectResult = EFFECT_RESULT_ERROR;
212 LuaProcessing::Initialize(void)
214 lua_getfield(__pLua.get(), LUA_GLOBALSINDEX, "Initialize");
215 return (CallFunction("Initialize", 0, 0, __pLua.get()) == 0);
219 LuaProcessing::CallFunction(const char* pFunctionName, int argsCount, int resultsCount, lua_State* pLua)
221 static const char* pStringError;
222 int res = lua_pcall(pLua, argsCount, resultsCount, 0);
226 //SysLog(NID_UI_EFFECT, "LuaProcessing, %s: Script function was performed successfully", pFunctionName);
229 pStringError = lua_tostring(pLua, -1);
230 pStringError = (pStringError == null ? "error null" : pStringError);
231 SysLogException(NID_UI_EFFECT, E_SERVER, "LuaProcessing; %s: There is a runtime error in script: %s", pFunctionName, pStringError);
234 SysLogException(NID_UI_EFFECT, E_OUT_OF_MEMORY, "LuaProcessing; %s: There is memory allocation error while performing script", pFunctionName);
237 SysLogException(NID_UI_EFFECT, E_SERVER, "LuaProcessing; %s: There is an error while running the error handler function for script", pFunctionName);
240 SysLogException(NID_UI_EFFECT, E_SERVER, "LuaProcessing; %s > There is an unknown error while performing script", pFunctionName);
246 } } } } // Tizen::Ui::Effects::_Runtime