Merge "Fixed crash issues" into devel/master
[platform/core/uifw/dali-adaptor.git] / adaptors / common / feedback / feedback-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 <feedback/feedback-plugin-proxy.h>
20
21 // EXTERNAL INCLUDES
22 #include <dlfcn.h>
23 #include <dali/integration-api/debug.h>
24
25 namespace Dali
26 {
27
28 namespace Internal
29 {
30
31 namespace Adaptor
32 {
33
34 #if _GLIBCXX_USE_CXX11_ABI
35 const char * const FeedbackPluginProxy::DEFAULT_OBJECT_NAME( "libdali-feedback-plugin.so" );
36 #else
37 const char * const FeedbackPluginProxy::DEFAULT_OBJECT_NAME( "libdali-feedback-plugin-cxx03.so" );
38 #endif
39
40 FeedbackPluginProxy::FeedbackPluginProxy( const std::string& sharedObjectName )
41 : mInitializeAttempted( false ),
42   mLibHandle( NULL ),
43   mSharedObjectName( sharedObjectName ),
44   mCreatePluginFunctionPtr( NULL ),
45   mFeedbackPlugin( NULL )
46 {
47   // Lazily initialize when sound/haptic is first played
48 }
49
50 FeedbackPluginProxy::~FeedbackPluginProxy()
51 {
52   if( mFeedbackPlugin )
53   {
54     delete mFeedbackPlugin;
55     mFeedbackPlugin = NULL;
56
57     if( mLibHandle && dlclose( mLibHandle ) )
58     {
59       DALI_LOG_ERROR( "Error closing dali feedback plugin library: %s\n", dlerror() );
60     }
61   }
62 }
63
64 void FeedbackPluginProxy::PlayHaptic( const std::string& filePath )
65 {
66   // Lazy initialization
67   Initialize();
68
69   if( mFeedbackPlugin )
70   {
71     mFeedbackPlugin->PlayHaptic( filePath );
72   }
73 }
74
75 void FeedbackPluginProxy::PlayHapticMonotone( unsigned int duration )
76 {
77   // Lazy initialization
78   Initialize();
79
80   if( mFeedbackPlugin )
81   {
82     mFeedbackPlugin->PlayHapticMonotone( duration );
83   }
84 }
85
86 void FeedbackPluginProxy::StopHaptic()
87 {
88   // Must already have been initialized to play haptic
89   if( mFeedbackPlugin )
90   {
91     mFeedbackPlugin->StopHaptic();
92   }
93 }
94
95 int FeedbackPluginProxy::PlaySound( const std::string& fileName )
96 {
97   // Lazy initialization
98   Initialize();
99
100   if( mFeedbackPlugin )
101   {
102     return mFeedbackPlugin->PlaySound( fileName );
103   }
104
105   return 0;
106 }
107
108 void FeedbackPluginProxy::StopSound( int handle )
109 {
110   // Must already have been initialized to play sound
111   if ( mFeedbackPlugin )
112   {
113     mFeedbackPlugin->StopSound( handle );
114   }
115 }
116
117 void FeedbackPluginProxy::PlayFeedbackPattern( int type, int pattern )
118 {
119   // Lazy initialization
120   Initialize();
121
122   if( mFeedbackPlugin )
123   {
124     mFeedbackPlugin->PlayFeedbackPattern( type, pattern );
125   }
126 }
127
128 void FeedbackPluginProxy::Initialize()
129 {
130   // Only attempt to load dll once
131   if ( !mInitializeAttempted )
132   {
133     mInitializeAttempted = true;
134
135     mLibHandle = dlopen( mSharedObjectName.c_str(), RTLD_NOW | RTLD_GLOBAL );
136     if( !mLibHandle )
137     {
138       DALI_LOG_ERROR( "Cannot load dali feedback plugin library error: %s\n", dlerror() );
139       return;
140     }
141
142     // reset errors
143     dlerror();
144
145     // load plugin
146     mCreatePluginFunctionPtr = reinterpret_cast<CreateFeedbackPlugin*>(dlsym(mLibHandle, "CreateFeedbackPlugin"));
147     if(!mCreatePluginFunctionPtr)
148     {
149       DALI_LOG_ERROR("Cannot load symbol CreateFeedbackPlugin(): %s\n", dlerror());
150       return;
151     }
152
153     // reset errors
154     dlerror();
155
156     mFeedbackPlugin = mCreatePluginFunctionPtr();
157
158     if(!mFeedbackPlugin)
159     {
160       DALI_LOG_ERROR("Call to function CreateFeedbackPlugin() failed\n");
161     }
162   }
163 }
164
165 } // namespace Adaptor
166
167 } // namespace Internal
168
169 } // namespace Dali