[dali_1.9.1] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / color-controller-impl.cpp
1 /*
2  * Copyright (c) 2019 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/system/common/color-controller-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dlfcn.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/object/type-registry.h>
25 #include <dali/devel-api/common/singleton-service.h>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 namespace Adaptor
34 {
35
36 namespace // unnamed namespace
37 {
38 const char* COLOR_CONTROLLER_PLUGIN_SO( "libdali-color-controller-plugin.so" );
39 }
40
41 Dali::ColorController ColorController::Get()
42 {
43   Dali::ColorController colorController;
44
45   Dali::SingletonService service( SingletonService::Get() );
46   if ( service )
47   {
48     // Check whether the singleton is already created
49     Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::ColorController ) );
50     if(handle)
51     {
52       // If so, downcast the handle
53       colorController = Dali::ColorController( dynamic_cast< ColorController* >( handle.GetObjectPtr() ) );
54     }
55     else
56     {
57       colorController = Dali::ColorController( new ColorController( ) );
58       service.Register( typeid( colorController ), colorController );
59     }
60   }
61
62   return colorController;
63 }
64
65 ColorController::ColorController()
66 : mLibHandle( NULL ),
67   mPlugin( NULL ),
68   mCreateColorControllerPtr( NULL )
69 {
70   Initialize();
71 }
72
73 ColorController::~ColorController()
74 {
75   if( mPlugin )
76   {
77     delete mPlugin;
78     mPlugin = NULL;
79
80     if( mLibHandle && dlclose( mLibHandle ) )
81     {
82       DALI_LOG_ERROR( "Error closing color controller plugin library: %s\n", dlerror() );
83     }
84   }
85 }
86
87 void ColorController::Initialize()
88 {
89   mLibHandle = dlopen( COLOR_CONTROLLER_PLUGIN_SO, RTLD_LAZY );
90
91   char* error = dlerror();
92   if( mLibHandle == NULL || error != NULL )
93   {
94     DALI_LOG_ERROR( "ColorController::Initialize: dlopen error [%s]\n", error );
95     return;
96   }
97
98   // load plugin
99   mCreateColorControllerPtr = reinterpret_cast< CreateColorControllerFunction >( dlsym( mLibHandle, "CreateColorControllerPlugin" ) );
100
101   error = dlerror();
102   if( mCreateColorControllerPtr == NULL || error != NULL )
103   {
104     DALI_LOG_ERROR( "ColorController::Initialize: Cannot load symbol CreateColorControllerPlugin(): %s\n", error );
105     return;
106   }
107
108   mPlugin = mCreateColorControllerPtr();
109   if( !mPlugin )
110   {
111     DALI_LOG_ERROR("ColorController::Initialize: Plugin creation failed\n");
112     return;
113   }
114 }
115
116 bool ColorController::RetrieveColor( const std::string& colorCode, Vector4& colorValue )
117 {
118   if( mPlugin )
119   {
120     return mPlugin->RetrieveColor( colorCode, colorValue );
121   }
122   return false;
123 }
124
125 bool ColorController::RetrieveColor( const std::string& colorCode , Vector4& textColor, Vector4& textOutlineColor, Vector4& textShadowColor)
126 {
127   if( mPlugin )
128   {
129     return mPlugin->RetrieveColor( colorCode, textColor, textOutlineColor, textShadowColor );
130   }
131   return false;
132 }
133
134 } // namespace Adaptor
135
136 } // namespace Internal
137
138 } // namespace Dali