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