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