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