Merge "Fix the texture bleeding with wrapping in atlas" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / scripting / script-plugin-proxy.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "script-plugin-proxy.h"
20
21 // INTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // EXTERNAL INCLUDES
25 #include <dlfcn.h>
26
27 namespace Dali
28 {
29
30 namespace Toolkit
31 {
32
33 namespace Internal
34 {
35
36 ScriptPluginProxy::ScriptPluginProxy( const std::string& sharedObjectName)
37 : mLibHandle(NULL),
38   mCreatePluginFunctionPtr(NULL),
39   mDestroyPluginFunctionPtr(NULL),
40   mScriptingPlugin(NULL),
41   mSharedObjectName(sharedObjectName),
42   mIsInitialized(false)
43 {
44   Initialize();
45 }
46
47 ScriptPluginProxy::~ScriptPluginProxy()
48 {
49   UnInitialize();
50 }
51
52 void ScriptPluginProxy::SetFlags(const std::string& flags)
53 {
54   if( mIsInitialized )
55   {
56     mScriptingPlugin->SetFlags( flags );
57   }
58 }
59
60 bool ScriptPluginProxy::ExecuteBuffer(const std::string &buffer, const std::string &filename)
61 {
62   if( mIsInitialized )
63   {
64     return mScriptingPlugin->ExecuteBuffer( buffer, filename );
65   }
66   return false;
67 }
68
69 bool ScriptPluginProxy::ExecuteFile(const std::string &filename)
70 {
71   if( mIsInitialized )
72   {
73     return mScriptingPlugin->ExecuteFile( filename );
74   }
75   return false;
76 }
77
78 bool ScriptPluginProxy::IsInitialized() const
79 {
80   return mIsInitialized;
81 };
82
83 void ScriptPluginProxy::Initialize()
84 {
85   if( mIsInitialized )
86   {
87     return;
88   }
89
90   // dl library maintains link counts if you call this twice on the same library
91   // (so its okay to do but we should close every handle we get too)
92   mLibHandle = dlopen( mSharedObjectName.c_str(), RTLD_NOW | RTLD_GLOBAL );
93   if( !mLibHandle )
94   {
95     DALI_LOG_ERROR( "Cannot load dali script plugin. %s\n", dlerror() );
96     return;
97   }
98
99   // reset errors
100   dlerror();
101
102   // load plugin
103   mCreatePluginFunctionPtr = reinterpret_cast<ScriptPlugin::Create*>( dlsym( mLibHandle, "CreateScriptPlugin" ) );
104   if( !mCreatePluginFunctionPtr )
105   {
106     DALI_LOG_ERROR( "Cannot load symbol CreateScriptPlugin(). %s\n", dlerror() );
107     return;
108   }
109
110   // reset errors
111   dlerror();
112
113   mDestroyPluginFunctionPtr = reinterpret_cast<ScriptPlugin::Destroy*>( dlsym( mLibHandle, "DestroyScriptPlugin" ) );
114   if( !mDestroyPluginFunctionPtr )
115   {
116     DALI_LOG_ERROR( "Cannot load symbol:DestroyScriptPlugin(). %s\n", dlerror() );
117     return;
118   }
119
120   // reset errors
121   dlerror();
122
123   mScriptingPlugin = mCreatePluginFunctionPtr();
124
125   if( !mScriptingPlugin )
126   {
127     DALI_LOG_ERROR( "Call to function CreateFeedbackPlugin() failed\n" );
128     return;
129   }
130
131   mIsInitialized = true;
132 }
133
134 void ScriptPluginProxy::UnInitialize()
135 {
136   if( mScriptingPlugin )
137   {
138     mDestroyPluginFunctionPtr( mScriptingPlugin );
139   }
140
141   if( mLibHandle )
142   {
143     if( dlclose( mLibHandle ) )
144     {
145       DALI_LOG_ERROR( "Error closing dali plugin library: %s\n", dlerror() );
146     }
147   }
148   mIsInitialized = false;
149 }
150 } // namespace Adaptor
151
152 } // namespace Internal
153
154 } // namespace Dali