Merge "Size negotiation patch 1: Removed SetPreferred size" into tizen
[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 void ScriptPluginProxy::ExecuteBuffer(const std::string &buffer, const std::string &filename)
61 {
62   if( mIsInitialized )
63   {
64     mScriptingPlugin->ExecuteBuffer( buffer, filename );
65   }
66 }
67
68 void ScriptPluginProxy::ExecuteFile(const std::string &filename)
69 {
70   if( mIsInitialized )
71   {
72     mScriptingPlugin->ExecuteFile( filename );
73   }
74 }
75
76 bool ScriptPluginProxy::IsInitialized() const
77 {
78   return mIsInitialized;
79 };
80
81
82 void ScriptPluginProxy::Initialize()
83 {
84   if( mIsInitialized )
85   {
86     return;
87   }
88
89   // dl library maintains link counts if you call this twice on the same library
90   // (so its okay to do but we should close every handle we get too)
91   mLibHandle = dlopen( mSharedObjectName.c_str(), RTLD_NOW | RTLD_GLOBAL );
92   if( !mLibHandle )
93   {
94     DALI_LOG_ERROR( "Cannot load dali script plugin. %s\n", dlerror() );
95     return;
96   }
97
98   // reset errors
99   dlerror();
100
101   // load plugin
102   mCreatePluginFunctionPtr = reinterpret_cast<ScriptPlugin::Create*>( dlsym( mLibHandle, "CreateScriptPlugin" ) );
103   if( !mCreatePluginFunctionPtr )
104   {
105     DALI_LOG_ERROR( "Cannot load symbol CreateScriptPlugin(). %s\n", dlerror() );
106     return;
107   }
108
109   // reset errors
110   dlerror();
111
112   mDestroyPluginFunctionPtr = reinterpret_cast<ScriptPlugin::Destroy*>( dlsym( mLibHandle, "DestroyScriptPlugin" ) );
113   if( !mDestroyPluginFunctionPtr )
114   {
115     DALI_LOG_ERROR( "Cannot load symbol:DestroyScriptPlugin(). %s\n", dlerror() );
116     return;
117   }
118
119   // reset errors
120   dlerror();
121
122   mScriptingPlugin = mCreatePluginFunctionPtr();
123
124   if( !mScriptingPlugin )
125   {
126     DALI_LOG_ERROR( "Call to function CreateFeedbackPlugin() failed\n" );
127     return;
128   }
129
130   mIsInitialized = true;
131 }
132
133 void ScriptPluginProxy::UnInitialize()
134 {
135   if( mScriptingPlugin )
136   {
137     mDestroyPluginFunctionPtr( mScriptingPlugin );
138   }
139
140   if( mLibHandle )
141   {
142     if( dlclose( mLibHandle ) )
143     {
144       DALI_LOG_ERROR( "Error closing dali plugin library: %s\n", dlerror() );
145     }
146   }
147   mIsInitialized = false;
148 }
149 } // namespace Adaptor
150
151 } // namespace Internal
152
153 } // namespace Dali