Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / effects / runtime / FUiEffects_RuntimeLuaProcessing.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://floralicense.org/license/
10 //
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.
16 //
17 /**
18  * @file        FUiEffects_RuntimeLuaProcessing.cpp
19  * @brief       This file contains an implementation of LuaProcessing class methods
20  *
21  */
22 #include <map>
23 #include <FBaseSysLog.h>
24 #include <FBaseErrors.h>
25 #include <FUiEffects_EffectImpl.h>
26 #include <FUiEffects_EffectManagerImpl.h>
27 #include <FUiEffects_LoggingProfiler.h>
28 #include <tolua++.h>
29 #include <../runtime/lua-cpp-binding/tolua.h>
30 #include "FUiEffects_RuntimeLuaProcessing.h"
31 #include "../FUiEffects_EffectErrorMessages.h"
32
33 using namespace std;
34
35 namespace Tizen { namespace Ui { namespace Effects { namespace _Runtime
36 {
37
38 const char* LuaProcessing::__pStringLua = " function _MyFunction510693810(scene_) \n"
39                                                                                         "               scene = scene_  \n"
40                                                                                         " end";
41
42 LuaProcessing::LuaProcessing(void)
43         : __pLua(null)
44         , __pEMScript(null)
45 {
46 }
47
48 LuaProcessing::LuaProcessing(const char* pPathToScript, IEffectModelScript* pEModel)
49         : __pLua(null)
50         , __pEMScript(null)
51 {
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);
54
55         luaL_openlibs(pLua.get());
56         tolua__open(pLua.get());
57
58         static const char* errorString;
59         switch (luaL_loadfile(pLua.get(), pPathToScript))
60         {
61         case 0:
62                 SysLog(NID_UI_EFFECT, "LuaProcessing: File %s was opened successfully", pPathToScript);
63                 break;
64         case LUA_ERRFILE:
65                 SysLogException(NID_UI_EFFECT, E_FILE_NOT_FOUND, "LuaProcessing: Can not open/read the file %s", pPathToScript);
66                 break;
67         case LUA_ERRSYNTAX:
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);
71                 break;
72         case LUA_ERRMEM:
73                 SysLogException(NID_UI_EFFECT, E_OUT_OF_MEMORY, "LuaProcessing: There is memory allocation error while opening the file %s", pPathToScript);
74                 break;
75         default:
76                 SysLogException(NID_UI_EFFECT, E_FILE_NOT_FOUND, "LuaProcessing: There is unknown error while opening the file %s", pPathToScript);
77                 break;
78         }
79
80         result r = GetLastResult();
81         SysTryReturnVoidResult(NID_UI_EFFECT, r == E_SUCCESS, r, GetErrorMessage(r));
82
83         int res = CallFunction("LuaProcessing", 0, LUA_MULTRET, pLua.get());
84         SysTryReturnVoidResult(NID_UI_EFFECT, res == 0, GetLastResult(), GetErrorMessage(GetLastResult()));
85
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);
88
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);
96
97         __pLua = std::move(pLua);
98         __pEMScript = std::move(pEMScript);
99 }
100
101 LuaProcessing::~LuaProcessing(void)
102 {
103
104 }
105
106 inline bool
107 LuaProcessing::OnTouchEvent(const char* pFunName, const TouchEventScript& position)
108 {
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);
112 }
113
114 bool
115 LuaProcessing::OnEffectStart(const EffectsVector<float>& effectStartInfo)
116 {
117         lua_getfield(__pLua.get(), LUA_GLOBALSINDEX, "OnEffectStart");
118
119         bool isVectorEmpty = false;
120         EffectsVector<float> effectStartInfoTemp(effectStartInfo);
121         if (effectStartInfo.empty())
122         {
123                 isVectorEmpty = true;
124                 effectStartInfoTemp.push_back(0.f);
125         }
126
127         tolua_pushusertype(__pLua.get(), (void*)&effectStartInfoTemp, "EffectsVector<float>");
128
129         if (isVectorEmpty)
130         {
131                 effectStartInfoTemp.clear();
132         }
133
134         return (CallFunction("OnEffectStart", 1, 0, __pLua.get()) == 0);
135 }
136
137 bool
138 LuaProcessing::OnEffectCalculate(float dt)
139 {
140         lua_getfield(__pLua.get(), LUA_GLOBALSINDEX, "OnEffectCalculate");
141         lua_pushnumber(__pLua.get(), dt);
142         return (CallFunction("OnEffectCalculate", 1, 0, __pLua.get()) == 0);
143 }
144
145 bool
146 LuaProcessing::OnTouchPressed(const TouchEventScript &position)
147 {
148         return OnTouchEvent("OnTouchPressed", position);
149 }
150
151 bool
152 LuaProcessing::OnTouchDoublePressed(const TouchEventScript &position)
153 {
154         return OnTouchEvent("OnTouchDoublePressed", position);
155 }
156
157 bool
158 LuaProcessing::OnTouchMoved(const TouchEventScript& position)
159 {
160         return OnTouchEvent("OnTouchMoved", position);
161 }
162
163 bool
164 LuaProcessing::OnTouchReleased(const TouchEventScript& position)
165 {
166         return OnTouchEvent("OnTouchReleased", position);
167 }
168
169 EffectResultS
170 LuaProcessing::IsEffectFinished(void)
171 {
172         EffectResultS effectRes;
173
174         lua_getfield(__pLua.get(), LUA_GLOBALSINDEX, "IsEffectFinished");
175         if (CallFunction("IsEffectFinished", 0, LUA_MULTRET, __pLua.get()) != 0)
176         {
177                 effectRes.effectResult = EFFECT_RESULT_ERROR;
178                 return effectRes;
179         }
180
181         int returnsCount = lua_gettop(__pLua.get());
182         effectRes.lastShownBitmapsId.clear();
183         if (returnsCount == 0)
184         {
185                 effectRes.effectResult = EFFECT_RESULT_ERROR;
186                 return effectRes;
187         }
188         LUA_NUMBER res = lua_tonumber(__pLua.get(), 1);
189         for (int i = 2; i <= returnsCount; ++i)
190         {
191                 effectRes.lastShownBitmapsId.push_back(lua_tonumber(__pLua.get(), i));
192         }
193         lua_pop(__pLua.get(), returnsCount);
194
195         _EffectResult tempRes = static_cast<_EffectResult>(res);
196         switch (tempRes)
197         {
198         case EFFECT_FINISHED:
199                 effectRes.effectResult = EFFECT_RESULT_FINISHED;
200                 break;
201         case EFFECT_CONTINUED:
202                 effectRes.effectResult = static_cast<EffectResult>(0);
203                 break;
204         default:
205                 effectRes.effectResult = EFFECT_RESULT_ERROR;
206                 break;
207         }
208         return effectRes;
209 }
210
211 bool
212 LuaProcessing::Initialize(void)
213 {
214         lua_getfield(__pLua.get(), LUA_GLOBALSINDEX, "Initialize");
215         return (CallFunction("Initialize", 0, 0, __pLua.get()) == 0);
216 }
217
218 int
219 LuaProcessing::CallFunction(const char* pFunctionName, int argsCount, int resultsCount, lua_State* pLua)
220 {
221         static const char* pStringError;
222         int res = lua_pcall(pLua, argsCount, resultsCount, 0);
223         switch (res)
224         {
225         case 0:
226                 //SysLog(NID_UI_EFFECT, "LuaProcessing, %s: Script function was performed successfully", pFunctionName);
227                 break;
228         case LUA_ERRRUN:
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);
232                 break;
233         case LUA_ERRMEM:
234                 SysLogException(NID_UI_EFFECT, E_OUT_OF_MEMORY, "LuaProcessing; %s: There is memory allocation error while performing script", pFunctionName);
235                 break;
236         case LUA_ERRERR:
237                 SysLogException(NID_UI_EFFECT, E_SERVER, "LuaProcessing; %s: There is an error while running the error handler function for script", pFunctionName);
238                 break;
239         default:
240                 SysLogException(NID_UI_EFFECT, E_SERVER, "LuaProcessing; %s > There is an unknown error while performing script", pFunctionName);
241                 break;
242         }
243         return res;
244 }
245
246 } } } } // Tizen::Ui::Effects::_Runtime