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